A vertex array and a single array to dereference

Source: Internet
Author: User
Tags server memory

Normal vector
The normal vector of an object defines the direction of his surface in space. Specifically defines his direction relative to the light source. OpenGL uses normal vectors to determine the light that is accepted by each vertex of the object. When defining the geometric shape of an object, it also defines his normal vector. The current normal vector can be set to the value indicated by the function using the glnormal* () function, and the current normal vector is assigned to the specified vertex at a later call to Glvertex* (). Each vertex tasting has a different normal, so you need to call this function alternately.

glBegin(GL_POLYGON);glNormal3fv(n0);glVertex3fv(v0);glNormal3fv(n1);glVertex3fv(v1);glNormal3fv(n2);glVertex3fv(v2);glNormal3fv(n3);glVertex3fv(v3);glEnd();

Because the normal vector represents only the direction, the length is irrelevant. Normals can be specified as arbitrary lengths, but vector is required before performing a light calculation. If the model involves only rotation and movement, the normal vector is guaranteed to be normalized. If an irregular transformation is made, OpenGL automatically normalizes the normal vector after the transformation. In order to enable this feature call glenable (gl_normalize); If you provide a unit-length normal and only scale evenly, you can use glenable (gl_rescale_normal) to scale the normal with a constant factor to restore the unit length.
Vertex Array
OpenGL requires a large number of function calls to complete rendering of the geometry, as well as redundant processing of shared vertices. Using vertex arrays allows you to specify a large number of vertex-related data in only a few arrays, and access the data with a small number of function calls, and putting the data in a vertex array can improve the performance of your application.
Rendering geometric entities using vertex arrays requires three steps:
1. Activation can be up to 8 arrays, each of which is used to store different types of data: vertex coordinates, surface normals, rgba colors, secondary colors, color indexes, fog coordinates, texture coordinates, and polygon boundary coordinates.
2. Put the data into the array. These arrays are accessed through their memory addresses (pointers). In the client server model, these arrays are stored in the client's address space, unless the buffer object is used, where the data is stored in server memory.
3. Use this data to draw the geometry. OpenGL obtains data from all the activated arrays through pointers. In the client-server model, the data is transferred to the server address space. There are three ways to accomplish this:
1. Accessing individual array elements
2. Create a list of individual array elements
3. Linear array elements
Enable arrays:
Invoke glenableclientstate () function, activate the selected array, in practice there are only 6 active arrays, some arrays cannot be activated at the same time, such as display mode can support RGBA or color index, but cannot support both. The
Void glenableclientstate (glenum array ) array parameter is a symbolic constant.
For example, to use lighting, you may need to define a normal vector for each vertex, in which case you need to activate both the surface normal array and the vertex coordinate array when using the vertex array

glEnableClientState(GL_NORMAL_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);

If you need to turn off the light at some point, you need to call the gldisable () function to turn off the light state, and after you turn off the light state, you also need to stop changing the value of the surface normal state, because it is completely wasted
Called gldisableClientState(GL_NORMAL_ARRAY);
Specify array data:
Specifies an array in the customer space with a single command. There are 8 functions that can be used to specify an array, and each function is used to specify a different type of array. Taking Glvertexpointer as an example

voidtype,GLsize stride,const Glvoid* pointer);

Specifies the spatial coordinate data that needs to be accessed. The size is the number of coordinates for each vertex, which must be the byte offset between successive vertices and, if 0, the vertices in the array are tightly connected. 2,3,4.stride Type specifies the data type of each coordinate in the array. Pointer is the memory address of the first coordinate of the first vertex that the array contains.
Use a vertex array to represent the RGBA color and vertex coordinates. RGB floating-point values and their corresponding integer coordinate values are loaded into GL_COLOR_ARRAY and GL_VERTEX_ARRAY arrays.

StaticGlint vertices[]={ -, -, -,325,175, -,175,325, -, -,325,325};StaticGlfloat colors[] ={1.0,0.2,0.2,0.2,0.2,1.0,0.8,1.0,0.2,0.75,0.75,0.75,0.35,0.35,0.35,0.5,0.5,0.5};    Glenableclientstate (Gl_color_array);    Glenableclientstate (Gl_vertex_array); Glcolorpointer (3, Gl_float,0, colors); Glvertexpointer (2, Gl_int,0, vertices);

Stride Distance
The stride parameter of the Gl*pointer () function tells OpenGL how to access the data in the pointer array. His value should be the number of bytes between two consecutive pointer elements. If the RGB value and vertex coordinates of the vertex are stored in the same array, as shown below

static GLfloat intertwined[]=    {        1.0,0.2,1.0,100.0,100.0,0.0,        1.0,0.2,0.2,0.0,200.0,0.0,        1.0,1.0,0.2,100.0,300.0,0.0,        0.2,1.0,0.2,200.0,300.0,0.0,        0.2,1.0,1.0,300.0,200.0,0.0,        0.2,0.2,1.0,200.0,100.0,0.0    };

If you want to reference only the color values in the array or the vertex coordinates, you need to skip reading

glColorPointer(3,GL_FLOAT,6*sizeof(GLfloat),&intertwined[0]);glVertexPointer(3,GL_FLOAT,6*sizeof(GLfloat),&intertwined[3]);

If Stride is 0, the data in the array is consistent with only the color values, or vertex coordinates, and so on.
3 Dereference and rendering
Arrays are kept on the client until the contents of the vertex array are dereferenced, and their contents are easily modified. In this step, the data in the array is extracted, then sent to the server, and then sent to the graphics processing pipeline for rendering. You can extract data from a single array element, or from a list of ordered array elements.
Dereference a single array

glArrayElement(Glint ith);

Gets the data for the ith vertex of all currently enabled arrays
For an array of vertex coordinates, the corresponding function is glVertex[size][type]v() where size and type are glvertexPointer() defined by the function. Other enabled arrays are similar.
glArrayElementUsually glBegin() glEnd() called between and, the following program draws a triangle using the 3,4,6 vertex from an enabled vertex array.

void Init () {Glclearcolor (0.0,0.0,0.0,0.0); Glshademodel (Gl_flat);} void Mydisplay () {Static Glint vertices[]={ -, -, -,325,175, -,175,325, -, -,325,325}; Static Glfloat colors[] ={1.0,0.2,0.2,0.2,0.2,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0. *,0. *,0. *,1.0,1.0,1.0};    Glenableclientstate (Gl_color_array);    Glenableclientstate (Gl_vertex_array); Glcolorpointer (3, Gl_float,0, colors); Glvertexpointer (2, Gl_int,0, vertices);    Glbegin (Gl_triangles); Glarrayelement (2); Glarrayelement (3); Glarrayelement (5);    Glend ();    Glflush (); GLCOLOR3FV (colors + (2 *)); Glvertex2iv (vertices + (2 * *));} void Reshape (intWinth) {Glviewport (0,0, (Glsizei) W, (Glsizei) h);    Glmatrixmode (gl_projection);    Glloadidentity (); Gluortho2d (0.0, (gldouble) W,0.0, (gldouble) h);}int_tmain (intARGC, _tchar* argv[]) {Glutinit (&ARGC, (char**) argv); Glutinitdisplaymode (Glut_rgb |    Glut_single); Glutinitwindowposition ( -, -); Glutinitwindowsize ( -, -); Glutcreatewindow ("Draw Triangle");    Init ();    Glutdisplayfunc (&mydisplay);    Glutreshapefunc (reshape); Glutmainloop ();return 0;} The run result generates a white triangle GLCOLOR3FV (colors + (2 *)); Glvertex2iv (vertices + (2 * *)); GLCOLOR3FV (colors + (3 *)); Glvertex2iv (vertices + (3 * *)); GLCOLOR3FV (colors + (5 *)); Glvertex2iv (vertices+ (5 * *));

The three lines of code that are equivalent to the dereference function, because Glarrayelement () is called only once for each vertex, may reduce the number of calls to the function, thereby improving overall program performance. Do not modify the contents of an array element during drawing.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A vertex array and a single array to dereference

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.