Sign in to follow this  
Shamino

Setting up Vertex Arrays in OGL

Recommended Posts

void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
So we have that to set up our vertex pointers, that is all that does... Correct?
Quote:
"The glVertexPointer function specifies the location and data of an array of vertex coordinates to use when rendering. The size parameter specifies the number of coordinates per vertex. The type parameter specifies the data type of each vertex coordinate. The stride parameter determines the byte offset from one vertex to the next, enabling the packing of vertices and attributes in a single array or storage in separate arrays. In some implementations, storing the vertices and attributes in a single array can be more efficient than using separate arrays (see glInterleavedArrays)."
After we do that, we need to do this.
void glEnableClientState(GL_VERTEX_ARRAY; );
void glDrawArray(GLenum mode, GLint first, GLsizei count); 
void glDisableClientState(GL_VERTEX_ARRAY );
Can anyone fill in where I posted pseudocode? Such as GLenum mode, things like that.... Please :)

Share this post


Link to post
Share on other sites
Quote:

The glDrawArrays function specifies multiple primitives to render.

void glDrawArrays(
GLenum mode,
GLint first,
GLsizei count
);

Parameters

mode

The kind of primitives to render. The following constants specify acceptable types of primitives: GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_QUAD_STRIP, GL_QUADS, and GL_POLYGON.

first

The starting index in the enabled arrays.

count

The number of indexes to render.


Remarks

With glDrawArrays, you can specify multiple geometric primitives to render. Instead of calling separate OpenGL functions to pass each individual vertex, normal, or color, you can specify separate arrays of vertices, normals, and colors to define a sequence of primitives (all the same kind) with a single call to glDrawArrays.

When you call glDrawArrays, count sequential elements from each enabled array are used to construct a sequence of geometric primitives, beginning with the first element. The mode parameter specifies what kind of primitive to construct and how to use the array elements to construct the primitives.

After glDrawArrays returns, the values of vertex attributes that are modified by glDrawArrays are undefined. For example, if GL_COLOR_ARRAY is enabled, the value of the current color is undefined after glDrawArrays returns. Attributes not modified by glDrawArrays remain defined. When GL_VERTEX_ARRAY is not enabled, no geometric primitives are generated but the attributes corresponding to enabled arrays are modified.

You can include glDrawArrays in display lists. When you include glDrawArrays in a display list, the necessary array data, determined by the array pointers and the enables, are generated and entered in the display list. The values of array pointers and enables are determined during the creation of display lists.

You can read static array data at any time. If any static array elements are modified and the array is not specified again, the results of any subsequent calls to glDrawArrays are undefined.

Although no error is generated when you specify an array more than once within glBegin and glEnd pairs, the results are undefined.

Error Codes

The following are the error codes generated and their conditions.

Error code Condition

GL_INVALID_VALUE count was negative.
GL_INVALID_ENUM mode was not an accepted value.
GL_INVALID_OPERATION glDrawArrays was called between a call to glBegin and the corresponding call to glEnd.


Requirements

Windows NT/2000: Requires Windows NT 4.0 or later.
Windows 95/98: Requires Windows 95 or later. Available as a redistributable for Windows 95.
Header: Declared in Gl.h.
Library: Use Opengl32.lib.


For example, drawing a triangle using a vertex array:


GLFloat array[9] = {0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.5f, 1.0f, 0.0f};

// 3 coordinates per vertex
// Using GLFloat as the data type
// Assume that there is no padding between coordinate values
// Pointer to the first element in the array
glVertexPointer(3, GLFloat, 0, array);

glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 9);
glDisableClientState(GL_VERTEX_ARRAY);


EDIT: Mistake fixed.

[Edited by - Wavarian on November 5, 2005 8:45:20 PM]

Share this post


Link to post
Share on other sites
When I do this


float* array[9] = {0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.5f, 1.0f, 0.0f};

I get these errors


--------------------Configuration: NeHeGL - Win32 Debug--------------------
Compiling...
Example.cpp
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(70) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(70) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(70) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(71) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(71) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(71) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(72) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(72) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(72) : error C2440: 'initializing' : cannot convert from 'const float' to 'float *'
There is no context in which this conversion is possible
C:\Documents and Settings\Jonathan\My Documents\Scripts and Code\NeHeGL\Example.cpp(79) : error C2065: 'glvertexPointer' : undeclared identifier
Error executing cl.exe.

NeHeGL.exe - 10 error(s), 0 warning(s)

When i do this

float array[9] = {0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.5f, 1.0f, 0.0f};

it runs fine.. even draws a triangle, terrific...

But it's not using a pointer, or is it? do I have to use the new keyword to do this?

Share this post


Link to post
Share on other sites
Yes, it is still using a pointer (pointing to memory allocated on the stack, instead of memory allocated dynamically on the heap).

As for your other question about efficiency, it is hard to say. The OpenGL documentation hints at the possibility of a speed-increase, however it is unlikely that you'll be able to notice it (unless using it to render a huge heightmap). But it definitely is more dynamic than a display list (if you intend on making changes to it every frame).

Share this post


Link to post
Share on other sites
I get about 5-10 fps when I run this algorithm at school, would this be faster if I used a vertex array?


if(w == 3)
{
for(j=1;j<=28;j++)
{
for(k=1;k<=14;k++)
{
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f,0.0f,0.0f);
s3[k][j].x=k*19.0f;
s3[k][j].y=j*19.0f;
glTranslatef(s3[k][j].x,s[2][k][j].y,0.0f);

if(s3{k][j].sold == false)
{
glColor3f(1, 0, 0);
}else
{
glColor4f(0.0f,1.0f,0.0f, 0.7f);
}

glBegin(GL_QUADS); // Draw A Quad
glTexCoord2f(0, 0); glVertex2f( -10.0f, 10.0f); // Top Left
glTexCoord2f(1, 0); glVertex2f( 10.0f, 10.0f); // Top Right
glTexCoord2f(1, 1); glVertex2f( 10.0f, -10.0f); // Bottom Right
glTexCoord2f(0, 1); glVertex2f( -10.0f, -10.0f); // Bottom Left
glEnd();
// glCallList(box);
}
}
}




what is the difference between using the ARB extensions for VBO's and this?


Are VBO's faster? Is it that they allocate all info to high performance graphics memory?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this