Sign in to follow this  

Screwy normals with VA's

This topic is 4660 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

I'm switching over to vertex arrays from display lists. When I draw the item using glDrawElements(), the shape draws fine (geometry wise), but the lighting is wierd. I've output the normals before the creation of the display list and the creation of the vertex array and they are both identical, so I'm thinking I'm doing something wrong with the vertex array's creation. I store each unique vertex with its normal in a struct:
#pragma pack(1)
//other structs

struct vertex
{
   vector norm; //vector is a class with only 3 floats as member variables
   vector vert;
} *vertList;

//other structs
#pragma pack()

First real quick, it doesn't matter that there are other structs between the pragma calls, right? Each struct should be packed using pack(1). Now, when I create my vertex arrays, I do the following:
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glNormalPointer(GL_FLOAT, 6*sizeof(float), &vertList[0].norm);
glVertexPointer(3, GL_FLOAT, 6*sizeof(float), &vertList[0].vert);

Does that look right? Now I make my call to glDrawElements() like this: glDrawElements(GL_TRIANGLES, length, GL_UNSIGNED_INT, myArray); myArray is simply the indexed list of vertices in the order they are to be drawn. Again, the object draws correctly with respect to its shape, it's only the lighting that is wrong, so I think it has something to do with the creation of the VA. Can anybody see anything I've done wrong? Also, a quick question. Are the start and end parameters of glDrawRangeElements the min and max values of the elements stored in the array? Or are they the min and max values of the indices of the array? So let's say we have this simple array: int a[3] = {2, 5, 8}; Would the call to glDrawRangeElements look like this: glDrawRangeElements(GL_TRIANGLES, 2, 8, 3, GL_UNSIGNED_INT, a); or this: glDrawRangeElements(GL_TRIANGLES, 0, 3, 3, GL_UNSIGNED_INT, a); I would think it would be the first one (why would it need the second since it has a length, after all?), but the redbook (v1.2) is a little unclear as to what the parameters should be. Thanks guys!

Share this post


Link to post
Share on other sites
Quote:
First real quick, it doesn't matter that there are other structs between the pragma calls, right? Each struct should be packed using pack(1).

I'm pretty sure that's fine (not 100% sure though).
Quote:
glNormalPointer(GL_FLOAT, 6*sizeof(float), &vertList[0].norm);
glVertexPointer(3, GL_FLOAT, 6*sizeof(float), &vertList[0].vert);

That's fine, but it just makes more sense to use sizeof(Vertex) instead of 6*sizeof(float). It's easier to understand what you're doing and if you ever add anything else to the Vertex struct you won't need to change it at all.
Quote:
Also, a quick question. Are the start and end parameters of glDrawRangeElements the min and max values of the elements stored in the array? Or are they the min and max values of the indices of the array?

They are the range of vertex indices to draw. So if you want to draw the entire array (which is usually the case), you would use start=0 and end=numVertices-1.

As for the wonky normals... did you try looping through the array and rendering in immediate mode? Also render the normals so you can easily see if something's clearly wrong. If it doesn't work in immediate mode (and you are indexing correctly) then there's something wrong with the way you add the data to the vertex array.

Share this post


Link to post
Share on other sites
I found the problem. The indexed array wasn't correct.

Thank you, Kalidor :)

EDIT: When I try using glDrawRangeElements, the compiler says the identifier 'glDrawRangeElements' is not found. Does this suggest I have an outdated version of OpenGL? I don't see how that's really possible as the function is listed in the redbook version 1.2, and my setup isn't even a year old... Where can one find updated OpenGL libraries, anyway? I searched OpenGL.org but couldn't find them.

Share this post


Link to post
Share on other sites
Quote:
Original post by CyberSlag5k
I found the problem. The indexed array wasn't correct.

Thank you, Kalidor :)

EDIT: When I try using glDrawRangeElements, the compiler says the identifier 'glDrawRangeElements' is not found. Does this suggest I have an outdated version of OpenGL? I don't see how that's really possible as the function is listed in the redbook version 1.2, and my setup isn't even a year old... Where can one find updated OpenGL libraries, anyway? I searched OpenGL.org but couldn't find them.


Yeah it's part of the OpenGL 1.2 core. But if you're on windows then you need to use extensions for everything past OpenGL 1.1. Here is the extension for glDrawRangeElements(). It's much easier just using an extension loading library such as GLee or GLEW, though.

Share this post


Link to post
Share on other sites

This topic is 4660 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.

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