Jump to content
  • Advertisement
Sign in to follow this  
Endar

OpenGL glVertexPointer and glDrawElements

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I've been looking at these: glDrawElements and glVertexPointer and I'm still a little in the dark. I get that glVertexPointer defines an array of vertices, and the glDrawElements draws it. That's about where my understanding of these ends. Could someone walk me through an example, or show me where a good example is? I'm actually looking at version 0.1 of the irrlicht engine, but, obviously people don't comment so much in areas where they know what's going on.

Share this post


Link to post
Share on other sites
Advertisement
If you are trying to draw with VERTEX ARRAYS, Here is an example with both vertex arrays and texture coordinate arrays:

glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays

float* vertices= new float[3*numvertices]; //3 coordiantes per vertex
float* textcoord= new float[2*numvertices]; //2 texture coordiantes per vertex

//fill in your array of vertices and texture coordinates with data

glVertexPointer( 3, GL_FLOAT, 0, vertices ); // Set The Vertex Pointer To Vertex Data
glTexCoordPointer( 2, GL_FLOAT, 0, textcoord ); // Set The Vertex Pointer To TexCoord Data

glDrawArrays( GL_TRIANGLES, 0, numvertices ); //Draw the vertices

Share this post


Link to post
Share on other sites
glVertexPointer and glDrawArrays are functionally equivalent to the following (barring errors in the following):
std::map< GLenum, GLuint > buildDataTypeSizeMap()
{
std::map< GLenum, GLuint > dataTypeSizeMap;
dataTypeSizeMap.insert(std::make_pair(GL_SHORT, sizeof(GLshort)));
dataTypeSizeMap.insert(std::make_pair(GL_INT, sizeof(GLint)));
dataTypeSizeMap.insert(std::make_pair(GL_FLOAT, sizeof(GLfloat)));
dataTypeSizeMap.insert(std::make_pair(GL_DOUBLE, sizeof(GLdouble)));
return dataTypeSizeMap;
}

std::map< GLenum, GLuint > sizeOfDataTypes = buildDataTypeSizeMap();

GLint vertexPointerNumberOfComponents = 4;
GLenum vertexPointerDataType = GL_FLOAT;
GLsizei vertexPointerStride = 0;
const GLubyte * vertexPointerData = 0;

void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer)
{
vertexPointerNumberOfComponents = size;
vertexPointerDataType = type;
if (stride == 0)
{
// special case, calculate the stride from the given data
vertexPointerStride = size * sizeOfDataType[type];
}
else
{
vertexPointerStride = stride;
}
vertexPointerData = reinterpret_cast< GLubyte * >(pointer);
}

void glDrawArraysShort(GLint first, GLsizei count);
void glDrawArraysInt(GLint first, GLsizei count);
void glDrawArraysFloat(GLint first, GLsizei count);
void glDrawArraysDouble(GLint first, GLsizei count);

void glDrawArrays(GLenum mode, GLint first, GLsizei count)
{
glBegin(mode);
if (vertexPointerDataType == GL_SHORT)
{
glDrawArraysShort(first, count);
}
else if (vertexPointerDataType == GL_INT)
{
glDrawArraysInt(first, count);
}
else if (vertexPointerDataType == GL_FLOAT)
{
glDrawArraysFloat(first, count);
}
else if (vertexPointerDataType == GL_DOUBLE)
{
glDrawArraysDouble(first, count);
}
glEnd();
}

void glDrawArraysShort(GLint first, GLsizei count)
{
const GLshort * vertexPointer = reinterpret_cast< GLshort * >(vertexPointerData + (first * vertexPointerStride));
for (GLint index = first; index != first + count; ++index)
{
if (vertexPointerNumberOfComponents == 2)
{
glVertex2s(*(vertexPointer + 0), *(vertexPointer + 1));
}
else if (vertexPointerNumberOfComponents == 3)
{
glVertex3s(*(vertexPointer + 0), *(vertexPointer + 1), *(vertexPointer + 2));
}
else if (vertexPointerNumberOfComponents == 4)
{
glVertex4s(*(vertexPointer + 0), *(vertexPointer + 1), *(vertexPointer + 2), *(vertexPointer + 3));
}
reinterpret_cast< GLubyte * >(vertexPointer) += vertexPointerStride;
}
}

void glDrawArraysInt(GLint first, GLsizei count)
{
const GLint * vertexPointer = reinterpret_cast< GLint * >(vertexPointerData + (first * vertexPointerStride));
for (GLint index = first; index != first + count; ++index)
{
if (vertexPointerNumberOfComponents == 2)
{
glVertex2i(*(vertexPointer + 0), *(vertexPointer + 1));
}
else if (vertexPointerNumberOfComponents == 3)
{
glVertex3i(*(vertexPointer + 0), *(vertexPointer + 1), *(vertexPointer + 2));
}
else if (vertexPointerNumberOfComponents == 4)
{
glVertex4i(*(vertexPointer + 0), *(vertexPointer + 1), *(vertexPointer + 2), *(vertexPointer + 3));
}
reinterpret_cast< GLubyte * >(vertexPointer) += vertexPointerStride;
}
}

void glDrawArraysFloat(GLint first, GLsizei count)
{
const GLfloat * vertexPointer = reinterpret_cast< GLfloat * >(vertexPointerData + (first * vertexPointerStride));
for (GLint index = first; index != first + count; ++index)
{
if (vertexPointerNumberOfComponents == 2)
{
glVertex2f(*(vertexPointer + 0), *(vertexPointer + 1));
}
else if (vertexPointerNumberOfComponents == 3)
{
glVertex3f(*(vertexPointer + 0), *(vertexPointer + 1), *(vertexPointer + 2));
}
else if (vertexPointerNumberOfComponents == 4)
{
glVertex4f(*(vertexPointer + 0), *(vertexPointer + 1), *(vertexPointer + 2), *(vertexPointer + 3));
}
reinterpret_cast< GLubyte * >(vertexPointer) += vertexPointerStride;
}
}

void glDrawArraysDouble(GLint first, GLsizei count)
{
const GLdouble * vertexPointer = reinterpret_cast< GLdouble * >(vertexPointerData + (first * vertexPointerStride));
for (GLint index = first; index != first + count; ++index)
{
if (vertexPointerNumberOfComponents == 2)
{
glVertex2d(*(vertexPointer + 0), *(vertexPointer + 1));
}
else if (vertexPointerNumberOfComponents == 3)
{
glVertex3d(*(vertexPointer + 0), *(vertexPointer + 1), *(vertexPointer + 2));
}
else if (vertexPointerNumberOfComponents == 4)
{
glVertex4d(*(vertexPointer + 0), *(vertexPointer + 1), *(vertexPointer + 2), *(vertexPointer + 3));
}
reinterpret_cast< GLubyte * >(vertexPointer) += vertexPointerStride;
}
}


Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!