OpenGL (14) C ++ curved structure grid line

zhaozj2021-02-11  250

Sender: YXY (Tian Mille # Hibaica), the letter area: Game_Designer title: OpenGL (14) Sending Station: BBS Shuimu Tsinghua Station (Tue Feb 17 16:24:53 1998) The structure of the surface can be Grid lines and fill surface forms, in fact, similar to the curve just become two-dimensional. 1. Surface definition void glmap2 {fd} (Glenum Target, Type U1, Type U2, GLINT USTRIDE, GLINT UORDER, TYPE V1, TYPE V2, GLINT VSTRIDE, GLINT VORDER, TYPE POINTS); Target defines the curve introduced in the last introduction The definition of Target. U V is a two-dimensional curved coordinate Uorder, Vorder; USTRIDE, and VSTRIDE definitions are similar to the curve definition. Points is a control point coordinate 2. Calculation of any point in the surface. Void glevalcoord2 {fd} [v] (Type U, TYPE V); the position of the world coordinates in the surface is calculated in the curve coordinate UV 3. Surface drawing control VOID GlmapGrid2 {fd} (Glenum Nu, Type U1, TYPE U2, Glenum NV, Type V1, Type V2); Defines the uniform mesh of the surface parameter space, from U1 to U2 to the equal interval Nu step, from V1 to V2 to the like Interval NV step. The following is given an example of depicting the surface of the grid line: //sample.cpp #include "gles.h" #include

#include

#include "windows.h"

Void Myinit (Void);

Void Callback Display (Void);

Void Callback Reshape (Glsizei W, Glsizei H);

// Control point coordinates

GLFLOAT POINTS [4] [4] [3] = {

{{-1.5, -1.5, 2.0}, {- 0.5, -1.5, 2.0},

{0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}},

{{-1.5, -0.5, 1.0}, {- 0.5, 1.5, 2.0},

{0.5, 0.5, 1.0}, {1.5, -0.5, -1.0}},

{{-1.5, 0.5, 2.0}, {- 0.5, 0.5, 1.0},

{0.5, 0.5, 3.0}, {1.5, -1.5, 1.5}},

{{-1.5, 1.5, -2.0}, {- 0.5, 1.5, -2.0},

{0.5, 0.5, 1.0}, {1.5, 1.5, -1.0}}};

/ / Set a set of colors set in order to display control points

GLFLOAT Color [4] [3] = {

{1.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 1.0, 1.0}};

Void Myinit (Void)

{

AuxinitDisplayMode (aux_single | aux_rgba);

AuxInitPosition (0,0,500,500);

AUXINITWINDOW ("Sample1");

GlclearColor (0.0, 0.0, 0.0, 0.0);

GLCLEAR (GL_COLOR_BUFFER_BIT);

// Use glenable () to enable surface mode

GLMAP2F (GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, & Points [0] [0] [0]);

Glenable (GL_MAP2_VERTEX_3);

GlmapGrid2f (20, 0.0, 1.0, 20, 0.0, 1.0);

Glenable (GL_DEPTH_TEST);

}

Void Callback Reshape (Glsizei W, GLSizei H)

{

GLVIEWPORT (0, 0, W, H);

Glmatrixmode (GL_PROJECTION);

GLLoadIndentity ();

IF (w <= h)

Glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0);

Else

GLORTHO (-5.0 * (GLFLOAT) H / (GLFLOAT) W,

5.0 * (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0);

Glmatrixmode (GL_ModelView);

GLLoadIndentity ();

}

Void Callback Display (Void)

{

GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Glcolor3f (0.0, 0.0, 1.0);

GLPUSHMATRIX ();

GLROTATEF (35.0, 1.0, 1.0, 1.0);

// Draw the surface structure with a line segment

Glbegin (GL_LINE_STRIP);

INT I, J;

// Draw 8 grid lines in the U V direction for constructing a surface structure

For (j = 0; j <= 8; J )

{

/ / Draw a curve in 30 line segments in the U direction

Glbegin (GL_LINE_STRIP);

For (i = 0; i <= 30; i )

Glevalcoord2f ((GLFLOAT) I / 30.0, (GLFLOAT) J / 8.0);

glend;

// Draw a curve in the V direction

Glbegin (GL_LINE_STRIP);

For (i = 0; i <= 30; i )

Glevalcoord2f ((GLFLOAT) J / 8.0, (GLFLOAT) I / 30.0);

glend;

}

// Show the control point in different colors

GLPOINTSIZE (4.0);

Glbegin (GL_POINTS);

For (i = 0; i <4; i )

For (j = 0; j <4; j )

{

Glcolor3FV (Color [I]);

Glvertex3FV (& Points [i] [j] [0]);

}

glend;

GLPOPMAMATRIX ();

GLFlush ();

}

Void main (void)

{

Myinit ();

Auxreshapefunc (reshape);

AuxMainLoop (Display);

}

// end of sample

///

The frame structure of the surface is often not satisfied with the surface of the surface. The method of drawing the surface is described below:

Void GlevAlmesh2 (Glenum Mode, Glint P1, GLINT P2, GLINT Q1, GLINT Q2);

Apply the grid set with GLmapGrid2 {fd} () to the surface calculation that has enabled.

Mode can be GL_POINT GL_LINE GL_FILL. As the name suggests, GL_FILL is generating fill surfaces

Here is an example of a BEZier surface with illumination (this surface is different from the drawing method, mathematics

The form and the previous are the same).

///

//sample.cpp

#include "gles.h"

#include

#include

#include "windows.h"

Void Myinit (Void);

Void Callback Display (Void);

Void Callback Reshape (Glsizei W, Glsizei H);

GLFLOAT POINTS [4] [4] [3] = {

{{-1.5, -1.5, 2.0}, {- 0.5, -1.5, 2.0},

{0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}},

{{-1.5, -0.5, 1.0}, {- 0.5, 1.5, 2.0},

{0.5, 0.5, 1.0}, {1.5, -0.5, -1.0}}, {{- 1.5, 0.5, 2.0}, {- 0.5, 0.5, 1.0},

{0.5, 0.5, 3.0}, {1.5, -1.5, 1.5}},

{{-1.5, 1.5, -2.0}, {- 0.5, 1.5, -2.0},

{0.5, 0.5, 1.0}, {1.5, 1.5, -1.0}}};

GLFLOAT Color [4] [3] = {

{1.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 1.0, 1.0}};

// Initialization of light, material

Void Initlights (Void)

{

GLFLOAT AMBIENT [] = {0.4, 0.6, 0.2, 1.0};

GLFLOAT POSITION [] = {0.0, 1.0, 3.0, 1.0};

GLFLOAT MAT_DIFFUSE [] = {0.2, 0.4, 0.8, 1.0};

GLFLOAT MAT_SPECULAR [] = {1.0, 1.0, 1.0, 1.0};

GLFLOAT MAT_SHININESS [] = {80.0};

Glenable (GL_Lighting);

Glenable (GL_LIGHT0);

GLLightFv (GL_LIGHT0, GL_AMBIENT, AMBIENT);

GLLIGHTFV (GL_LIGHT0, GL_POSITION, POSITION);

GLMATERIALFV (GL_FRONT, GL_DIFFUSE, MAT_DIFFUSE);

Glmaterialfv (GL_FRONT, GL_SPECULAR, MAT_SPECULAR);

glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);

}

Void Myinit (Void)

{

AuxinitDisplayMode (aux_single | aux_rgba);

AuxInitPosition (0,0,500,500);

AUXINITWINDOW ("Sample1");

GlclearColor (0.0, 0.0, 0.0, 0.0);

GLCLEAR (GL_COLOR_BUFFER_BIT);

GLMAP2F (GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, & Points [0] [0] [0]);

Glenable (GL_MAP2_VERTEX_3);

GLENABLE (GL_AUTO_NORMAL);

// glenable (GL_NORMALIZE);

GlmapGrid2f (20, 0.0, 1.0, 20, 0.0, 1.0);

Glenable (GL_DEPTH_TEST);

// Initializing the light, material

INITS ();

// Glshademodel (GL_FLAT);

}

Void Callback Reshape (Glsizei W, GLSizei H)

{

GLVIEWPORT (0, 0, W, H);

Glmatrixmode (GL_PROJECTION);

GLLoadIndentity ();

IF (w <= h)

Glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W,

5.0 * (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0);

Else

GLORTHO (-5.0 * (GLFLOAT) H / (GLFLOAT) W,

5.0 * (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0);

Glmatrixmode (GL_ModelView);

GLLoadIndentity ();

}

Void Callback Display (Void)

{

{

GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Glcolor3f (0.0, 0.0, 1.0);

GLPUSHMATRIX ();

GLROTATEF (35.0, 1.0, 1.0, 1.0); // commented out the drawing part of the original grid, instead of drawing the surface of the surface

GlevAlmesh2 (GL_FILL, 0, 20, 0, 20);

INT I, J;

/ * for (j = 0; j <= 8; J )

{

Glbegin (GL_LINE_STRIP);

For (i = 0; i <= 30; i )

Glevalcoord2f ((GLFLOAT) I / 30.0, (GLFLOAT) J / 8.0);

glend;

Glbegin (GL_LINE_STRIP);

For (i = 0; i <= 30; i )

Glevalcoord2f ((GLFLOAT) J / 8.0, (GLFLOAT) I / 30.0);

glend;

}

* /

GLPOINTSIZE (4.0);

Glbegin (GL_POINTS);

For (i = 0; i <4; i )

For (j = 0; j <4; j )

{

Glcolor3FV (Color [I]);

Glvertex3FV (& Points [i] [j] [0]);

}

glend;

GLPOPMAMATRIX ();

GLFlush ();

}

Void main (void)

{

Myinit ();

Auxreshapefunc (reshape);

AuxMainLoop (Display);

}

// end of sample

A curved surface that truly entity is present. The control point is different, and the shape of the surface can be controlled.

It can be seen that OpenGL's powerful features, only use very little original code to produce real 3D effects.

An example of drawing a NURBS surface (non-uniform B-spline surface) is given before the complex modeling is ended.

Here is a special function provided by the music some OpenGL utility. All new stuff is in the program

Release, annotation given the meaning of the process, and the operation of the specific function can be detailed in detail.

///

//sample.cpp

#include "gles.h"

#include

#include

#include "windows.h"

Void Myinit (Void);

Void Callback Display (Void);

Void Callback Reshape (Glsizei W, Glsizei H);

/ / Define a set of control points storage space

GLFLOAT POINTS [4] [4] [3];

/ / Define a pointer to the NURBS surface object

Glunurbsobj * lifeurb;

// The process used to generate a control point, replacing the original direct assignment method to generate control points

Void Init_Surface (Void)

{

INT U, V;

For (u = 0; u <4; u )

{

FOR (v = 0; V <4; v )

{

Points [U] [V] [0] = 2.0 * ((GLFLOAT) U-1.5);

Points [U] [V] [1] = 2.0 * ((GLFLOAT) V-1.5);

IF ((u == 1 || U == 2) && (v == 1 || V == 2)))

Points [u] [v] [2] = 3.0;

Else

Points [u] [v] [2] = - 3.0;

}

}

}

/ *

GLFLOAT POINTS [4] [4] [3] = {

{{-1.5, -1.5, 2.0}, {- 0.5, -1.5, 4.0},

{0.5, -1.5, 3.0}, {1.5, -1.5, 2.0}},

{{-1.5, -0.5, 3.0}, {- 0.5, 1.5, 4.0},

{0.5, 0.5, 2.0}, {1.5, -0.5, -1.0}},

{{-1.5, 0.5, 2.0}, {- 0.5, 0.5, 4.0},

{0.5, 0.5, 5.0}, {1.5, -1.5, 1.5}},

{{-1.5, 1.5, -2.0}, {- 0.5, 1.5, 3.0},

{0.5, 0.5, 1.0}, {1.5, 1.5, -1.0}}};

* /

GLFLOAT Color [4] [3] = {

{1.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 1.0, 1.0}};

Void Initlights (Void)

{

GLFLOAT AMBIENT [] = {0.4, 0.6, 0.2, 1.0};

GLFLOAT POSITION [] = {0.0, 1.0, 3.0, 1.0};

GLFLOAT MAT_DIFFUSE [] = {0.2, 0.4, 0.8, 1.0};

GLFLOAT MAT_SPECULAR [] = {1.0, 1.0, 1.0, 1.0};

GLFLOAT MAT_SHININESS [] = {80.0};

Glenable (GL_Lighting);

Glenable (GL_LIGHT0);

GLLightFv (GL_LIGHT0, GL_AMBIENT, AMBIENT);

GLLIGHTFV (GL_LIGHT0, GL_POSITION, POSITION);

GLMATERIALFV (GL_FRONT, GL_DIFFUSE, MAT_DIFFUSE);

Glmaterialfv (GL_FRONT, GL_SPECULAR, MAT_SPECULAR);

glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);

// First initialize the control point

INIT_SURFACE ();

// Create an object of a NURBS surface

THENURB = Glunewnurbsrenderer ();

/ / Modify the properties of this surface object

GlunurbsProperty (THENURB, GLU_SAMPLING_TOLERANCE, 25.0);

GLUNURBSPROPERTY (THENURB, GLU_DISPLAY_MODE, GLU_FILL);

}

Void Myinit (Void)

{

AuxinitDisplayMode (aux_single | aux_rgba);

AuxInitPosition (0,0,500,500);

AUXINITWINDOW ("Sample1");

GlclearColor (0.0, 0.0, 0.0, 0.0);

GLCLEAR (GL_COLOR_BUFFER_BIT);

GLMAP2F (GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, & Points [0] [0] [0]);

Glenable (GL_MAP2_VERTEX_3);

GLENABLE (GL_AUTO_NORMAL);

Glenable (GL_NORMALIZE);

GlmapGrid2f (20, 0.0, 1.0, 20, 0.0, 1.0);

Glenable (GL_DEPTH_TEST);

INITS ();

// Glshademodel (GL_FLAT);

}

Void Callback Reshape (Glsizei W, GLSizei H)

{

GLVIEWPORT (0, 0, W, H);

Glmatrixmode (GL_PROJECTION);

GLLoadIndentity ();

IF (w <= h)

Glortho (-5.0, 5.0, -5.0 * (GLFLOAT) H / (GLFLOAT) W,

5.0 * (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0);

Else

GLORTHO (-5.0 * (GLFLOAT) H / (GLFLOAT) W,

5.0 * (GLFLOAT) H / (GLFLOAT) W, -5.0, 5.0, -5.0, 5.0);

Glmatrixmode (GL_MODELVIEW); GLLoadIdentity ();

}

Void Callback Display (Void)

{

// b Spline (NURBS) control vector, please refer to the relevant books of graphics

GLFLOAT KNOTS [8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};

GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Glcolor3f (0.0, 0.0, 1.0);

GLPUSHMATRIX ();

GLROTATEF (35.0, 1.0, 1.0, 1.0);

GLROTATEF (-60.0, 1.0, 0.0, 0.0);

Glscalef (0.5, 0.5, 0.5);

// Note the original surface drawing function, in order to draw a new NURBS surface drawing function

// glevalmesh2 (GL_FILL, 0, 20, 0, 20);

/ / Define the mathematical model of the surface to determine its shape

Glunurbssurface (thenurb,

8, knots,

8, knots,

4 * 3,

3,

& Points [0] [0] [0],

4, 4,

GL_MAP2_VERTEX_3);

/ / End the surface of the surface, this structure is similar to the standard Glbegin () ... glend ()

Gluendsurface (kilb);

INT I, J;

GLPOINTSIZE (4.0);

Glbegin (GL_POINTS);

For (i = 0; i <4; i )

For (j = 0; j <4; j )

{

Glcolor3FV (Color [I]);

Glvertex3FV (& Points [i] [j] [0]);

}

glend;

GLPOPMAMATRIX ();

GLFlush ();

}

Void main (void)

{

Myinit ();

Auxreshapefunc (reshape);

AuxMainLoop (Display);

}

// end of sample

//

At this point, complex construction is in the paragraph. Next introduction special light

-

※ Source: · BBS Shuimu Tsinghua Station bbs.Net.tsinghua.edu.cn · [From: 166.111.74.90]

转载请注明原文地址:https://www.9cbs.com/read-3544.html

New Post(0)