Easy to get started with OpenGL programming surfaces and curves

Source: Internet
Author: User
Tags constant data structures integer numeric value range set background

We talked about how to draw a planar graph, and we learned how to draw curves and surfaces in this section.

Example 10: Draw a surface, as shown in Figure 12. This program draws a surface using a two-dimensional evaluation device. In this case, there are some special effects in the operation.

#include <windows.h>
#include <GL/GLAUX.h>
#include <GL/glut.h>
#include <math.h>
Glfloat Ctrlpoints[5][5][3] = {{ -2,0,0},{-1,1,0},{0,0,0},{1,-1,0},{2,0,0}},
{{ -2,0,-1},{-1,1,-1},{0,0,-1},{1,-1,-1},{2,0,-1}},
{{ -2,0,-2},{-1,1,-2},{0,0,-2},{1,-1,-2},{2,0,-2}},
{{ -2,0,-3},{-1,1,-3},{0,0,-3},{1,-1,-3},{2,0,-3}},
{{ -2,0,-4},{-1,1,-4},{0,0,-4},{1,-1,-4},{2,0,-4}}};

Glfloat mat_ambient[] = {0.1,0.1,0.1,1.0};
Glfloat mat_diffuse[] = {1.0,0.6,0.0,1.0};
Glfloat mat_specular[] = {1.0,1.0,1.0,1.0};

Glfloat light_ambient[] = {0.1,0.1,0.1,1.0};
Glfloat light_diffuse[] = {1.0,1.0,1.0,0.0};
Glfloat light_specular[] = {1.0,1.0,1.0,0.0};
Glfloat light_position[] = {2.0,23.0,-4.0,1.0};

void Myinit (void)
{
Glclearcolor (0.0,0.0,0.0,0.0);/Set Background color

/* Specify material parameters for the illumination model * *
GLMATERIALFV (gl_front,gl_ambient,mat_ambient);
GLMATERIALFV (Gl_front,gl_diffuse,mat_diffuse);
GLMATERIALFV (Gl_front,gl_specular,mat_specular);
Glmaterialf (gl_front,gl_shininess,60.0);

/* Set the light parameters/*
GLLIGHTFV (gl_light0,gl_ambient,light_ambient);
GLLIGHTFV (Gl_light0,gl_diffuse,light_diffuse);
GLLIGHTFV (Gl_light0,gl_specular,light_specular);
GLLIGHTFV (gl_light0,gl_position,light_position);

Glenable (gl_lighting);
Glenable (GL_LIGHT0);

/*enable depth Comparisons and update the depth buffer*/
Glenable (gl_depth_test);
/* Set SPECIAL effects * *
Glblendfunc (Gl_src_alpha,gl_one_minus_src_alpha);
Glhint (Gl_line_smooth_hint,gl_dont_care);
Glenable (Gl_blend);

Glenable (Gl_auto_normal);
Glenable (gl_normalize);
Glfrontface (GL_CW);
Glshademodel (Gl_smooth);
Glenable (Gl_line_smooth);

}

void Mydisplay (void)
{
Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);
glcolor3f (0.0,0.0,0.0);
Gltranslatef (0.0,-1.0,0.0);
Glrotatef (50.0,1.0,0.0,0.0);
Glpushmatrix ();
/* Draw Surface * *
Glenable (Gl_map2_vertex_3);
GLMAP2F (Gl_map2_vertex_3,0,1,3,5,0,1,15,5,&ctrlpoints[0][0][0]);
GLMAPGRID2F (10.0,0.0,1.0,10.0,0.0,1.0);
GLEVALMESH2 (gl_fill,0,10.0,0,10.0);
Glpopmatrix ();
Glutswapbuffers ();
}

void Myreshape (Glsizei w,glsizei h)
{
Glviewport (0,0,W,H);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (60.0, (glfloat) w/(glfloat) h,1.0,100.0);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
Gltranslatef (0.0,0.0,-5.0);
}

int main (int argc,char * * argv)
{
/* Initialize * *
Glutinit (&AMP;ARGC,ARGV);
Glutinitdisplaymode (glut_double| glut_rgb| Glut_depth);
Glutinitwindowsize (400,400);
Glutinitwindowposition (200,200);

/* Create window * *
Glutcreatewindow ("Lighted bézier surface");

/* Draw and display * *
Myinit ();
Glutreshapefunc (Myreshape);
Glutdisplayfunc (Mydisplay);

/* Enter GLUT event handling cycle * *
Glutmainloop ();
return (0);
}



Figure 12: Surface


Several operations on special effects in Myinit ().

Glblendfunc (glenum sfactor,glenum dfactor) specifies the pixel algorithm. Sfactor specifies how the red, green, blue, and alpha source blending factors are calculated. Dfactor specifies how the red, green, blue, and alpha target blend factors are calculated.

glhint (glenum target,glenum mode) specifies the action thread.

Target is a symbolic constant that needs to be controlled. Mode is the desired behavior symbol constant. In this example gl_line_smooth_hint specifies the sampling quality of the anti-aliasing segment. Gl_dont_care refers to options that are not considered.

Surface operations in Mydisplay ():

void glmap2f (glenum target,glfloat u1,glfloat u2,glint ustride, Glint Uorder, Glfloat v1, GLfloat v2, Glint vstride, Glin T Vorder, const glfloat *points); defines a 2-dimensional evaluation device.

Target specifies the type of numeric value that the evaluation generates. The Gl_map2_vertex_3 in this example indicates that each control point is three floating-point values represented by x, Y, and Z.

U1,U2 specifies a linear mapping.

Ustride Specify the number of single-precision or double-precision floating-point values between the starting point of the rij of the control point and the starting point of the control Point R (i+1) J. Here I and J are the U-and V-control indices, which allow control points to be loaded into arbitrary data structures. The only limitation is that there must be a contiguous memory unit for the value of a particular control point.

The number of dimensions of the Uorder control point array in the direction of the U axis.

V1,V2 specifies a linear map V

VSTRIDE Specifies the number of single-precision or double-precision floating-point values between the starting point of the rij of the control point and the starting point of Ri (j+ 1). Here I and J are the U-and V-control indices, which allow control points to be loaded into arbitrary data structures. The only limitation is that there must be a contiguous memory unit for the value of a particular control point.

The number of dimensions of the Vorder control point array in the direction of the V axis.

Points a pointer to an array of control points.

Glmapgrid defines a one-dimensional or two-dimensional grid. void glmapgrid2f (Glint un, glfloat U1, Glfloat u2,glint, vn glfloat);

The number of fragments of UN in grid [U1,U2].

U1,U2 Specifies the mapping of the integer grid range i= 0;i= un.

VN number of segments in grid [V1,v2].

V1,V2 Specifies the integer grid range j = 0;j= vn mappings.

Glevalmesh computes a one-dimensional or two-dimensional point or line grid. In this case, 2 dimensions. void GlEvalMesh2 (Glenum mode,glint i1,glint i2,glint j1,glint);

mode specifies whether to compute the grid of a two-dimensional point, line, or polygon.

I1,i2 the first and last integer values for the grid-defined field variable i.

J1,j2 is the first and last integer value of the grid-defined field variable J respectively.

Glmapgrid and Glevalmesh are used to generate and obtain a series of equal spacing grid points, Glevalmesh gradually compute one-dimensional or two-dimensional grids, and his definition range is specified by Glmap. mode determines whether the vertex of the final calculation is drawn as a point, a line, or a full polygon. Specific mapping relationships and knowledge of graphics, you can easily find in MSDN, the Internet and related books, this article does not elaborate on this aspect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.