• Advertisement
Sign in to follow this  

draw quad with VAs-duh !

This topic is 4681 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 can't do it ! How hard can it be - this has got to be the simplest draw command EVER and I can't do it ! Duh ! Feel free to laugh at me ! I'll cut it down and make it as simple as possible:
//the vertices ! ie a quad facing in +ve z direction
Vertices[0][0] = 0.0f; Vertices[0][1]=0.0f; Vertices[0][2]= 0.0f;
Vertices[1][0] = 1.0f; Vertices[1][1]=0.0f; Vertices[1][2]= 0.0f;
Vertices[2][0] = 1.0f; Vertices[2][1]=1.0f; Vertices[2][2]= 0.0f;
Vertices[3][0] = 0.0f; Vertices[3][1]=1.0f; Vertices[3][2]= 0.0f;

//the normal for this face
m_normalArray[0][0]=0.0f;m_normalArray[0][1]=0.0f;m_normalArray[0][2]=1.0f;

//the text coord for this
m_texcoordArray[0][0]=0.0f;	m_texcoordArray[0][1]=0.0f;
m_texcoordArray[1][0]=1.0f;	m_texcoordArray[1][1]=0.0f;
m_texcoordArray[2][0]=1.0f;	m_texcoordArray[2][1]=1.0f;
m_texcoordArray[3][0]=0.0f;	m_texcoordArray[3][1]=1.0f;

//the index pointer for this ie draw vertices 0,1,2,3 !! 
m_indexArray[0]=0; m_indexArray[1]=1;m_indexArray[2]=2;	m_indexArray[3]=3;


//enable client state
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, Vertices);	
glNormalPointer(GL_FLOAT, 0, m_normalArray);
glTexCoordPointer(2, GL_FLOAT, 0, m_texcoordArray);
glIndexPointer(GL_UNSIGNED_BYTE, 0, m_indexArray);




//DRAW !!!! 
glPushMatrix();
	glTranslatef(position.x, position.y, position.z);
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, frontText[0]);
	glPushMatrix();
		glScalef((GLfloat)width, (GLfloat)SHOP_HEIGHT, 0.0f);
                glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, m_indexArray);
	glPopMatrix();
glPopMatrix();

nothing on the screen at all ! I do about 50 of these calls. It works fine if I don't use VAs Humour me - is the above dumbo code correct ?! cheers Ade edited by _the_phantom
Added source tags, please use them in future and check the forum FAQ for details on the tags to use

Share this post


Link to post
Share on other sites
Advertisement
I noticed that you only have one normal vector. You will need a normal for each vertex.

Share this post


Link to post
Share on other sites
hmmm I need a normal for each face,
anyhow I did a normal for each vertex just now and still have nothing on the screen.

This is so annoying, this is the simplest thing ever and I can't do it.


in reply to the other person here's the defs of the arrays:

GLfloat bldgVertices[8][3];
GLubyte m_indexArray[24];
GLfloat m_normalArray[24][3]; //was [6][3]
GLfloat m_colorArray[6][3];
GLfloat m_texcoordArray[24][2];

Share this post


Link to post
Share on other sites
glIndexPointer(GL_UNSIGNED_BYTE, 0, m_indexArray);
look in the spec to see what this does

Share this post


Link to post
Share on other sites
well, I removed the glIndexPointer call, and didn't call the glEnableClientState(GL_INDEX_ARRAY) but still nothing on the screen.

Share this post


Link to post
Share on other sites
well to make sure I wasn't going mad I did the following instead of DrawElements:

glColor3f(0.8f, 0.8f, 0.8f);
glBegin(GL_QUADS);
glVertex3fv(Vertices[0]);
glVertex3fv(Vertices[1]);
glVertex3fv(Vertices[2]);
glVertex3fv(Vertices[3]);
glEnd();


and it worked FINE !!!

So the Vertices are correct but I can't get the DrawElements to work at all.

Any ideas ?

Share this post


Link to post
Share on other sites
ok, refresher:
What I'm tryign to do is (firstly) draw a quad using VAs:

glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, m_indexArray);

That call does not work.

So, in the very same place of the above call I've explicitly created the quad - this time using the indexArray and Vertices array like this:

glBegin(GL_QUADS);
glVertex3fv(Vertices[m_indexArray[quad*4]]);
glVertex3fv(Vertices[m_indexArray[quad*4]+1]);
glVertex3fv(Vertices[m_indexArray[quad*4]+2]);
glVertex3fv(Vertices[m_indexArray[quad*4]+3]);
glEnd();

THIS WORKS !!!

This means that 1)m_indexArray has the correct data 2) Vertices has the correct data !!


So why won't my DrawElements call work at all when it uses precisely the same data as the explicit call.

ppplllllleeeeeaaasee ... anyone ?!

cheers




Share this post


Link to post
Share on other sites
Well, get rid of the inner 'push/popmatrix()' commands...that might be resetting your matrix, give it a try anyway.

Also, try using standard VA's ( "glDrawArrays()" ) and see what happends.

-Twixn-

Share this post


Link to post
Share on other sites
This works for me:


//Init stuff
GLfloat Vertices[4][3];
GLfloat m_texcoordArray[4][2];
GLubyte m_indexArray[4];

Vertices[0][0] = 0.0f; Vertices[0][1]=0.0f; Vertices[0][2]= 0.0f;
Vertices[1][0] = 1.0f; Vertices[1][1]=0.0f; Vertices[1][2]= 0.0f;
Vertices[2][0] = 1.0f; Vertices[2][1]=1.0f; Vertices[2][2]= 0.0f;
Vertices[3][0] = 0.0f; Vertices[3][1]=1.0f; Vertices[3][2]= 0.0f;

m_texcoordArray[0][0]=0.0f; m_texcoordArray[0][1]=0.0f;
m_texcoordArray[1][0]=1.0f; m_texcoordArray[1][1]=0.0f;
m_texcoordArray[2][0]=1.0f; m_texcoordArray[2][1]=1.0f;
m_texcoordArray[3][0]=0.0f; m_texcoordArray[3][1]=1.0f;

m_indexArray[0]=0; m_indexArray[1]=1;m_indexArray[2]=2; m_indexArray[3]=3;

//Later on, (in draw loop)

//Set camera, bind texture, blah blah blah, then....
glVertexPointer(3, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, m_texcoordArray);

glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, m_indexArray);




This drew a small quad in the center of my screen with the current texture mapped to it. Note that I didn't even have to call glEnableClientState() on anything, but that may have just been me. I recommend trying it out with minimal data like this (no normals or anything) first, getting that to work, then building on it from there.

Share this post


Link to post
Share on other sites
ah yes, good point, the inital code posted and the code which used glVertex() dont do the same thing!.
The later just draw a quad with no texturing nor normal infomation applied, the former however applies normals and if you've switched lighting on (or havent infact) this could cause your object to appear black.

So, chances are your code does work but something in the lighting is broken and thus it appears as though you dont draw anything.

Share this post


Link to post
Share on other sites
ok, I checked and the light is definitely on, it's never disabled, I can check this because if I revert to drawing the quads not using DrawElements but with normals then I can see the shading etc.
I also tried removing the second push/pop to no avail.


So I now tried this:


glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, Vertices);
glNormalPointer(GL_FLOAT, 0, m_normalArray);
glColorPointer(3, GL_FLOAT, 0, m_colorArray);


glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, m_indexArray);



still nothing.


BTW the color array is set up like this:

for (int i=0; i<4; i++)
{
m_colorArray[0]=m_colorArray[1]=m_colorArray[2] = 0.7f;
}


the normals are set up like (although I think I only need one normal for one quad but this won't hurt.:

for (int i=0; i<4; i++)
{
m_normalArray[0]=0.0f;m_normalArray[1]=0.0f;m_normalArray[2]=1.0f;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by ade-the-heat
the normals are set up like (although I think I only need one normal for one quad but this won't hurt.


No, you need one normal for each vertex in the vertex array, doing anything else will definately break things.

And you arent using the code tags, even after I asked nicely *stern look*

Share this post


Link to post
Share on other sites
even with a normal for each vertex it still didn't work.
sigh....

is there anything else I can do ?... getting depressed...


BTW what's a code tag (not being sarcastic - i don't remember you mentioning it).



cheers


Share this post


Link to post
Share on other sites
Quote:
Original post by ade-the-heat
BTW what's a code tag (not being sarcastic - i don't remember you mentioning it).


I assume you didnt notice your first post in this thread was edited then...

Share this post


Link to post
Share on other sites
Did you say tha you did have lighting turned on? Have you tried switching it off and rendering? If the light or normals are configured wierd it could easily be drawing a black poly on the screen, which would make it effectively invisible.

On a similar note, based on some really horrid lighting bugs in the past I always always ALWAYS clear the screen to a non-black color when I'm doing my testing/developing. This helps immensly, as black or really dark polys are visible. Favorite "debug" setting is: glClearColor(0.1f, 0.1f, 0.2f, 1.0f);

Gives you a nice bluish-grey background that's not very distracting but helps the details show up.

Share this post


Link to post
Share on other sites
thanks, the light is defitely on as I have cleared the screen to light blue each frame, in addition if I create the quad using the glVertex3fv way with normals then I can see lots of these quads (each frame I write about 30 quads to the screen).



Phantom- thanks I'll re-start on the faqs,

cheers

Share this post


Link to post
Share on other sites
ok, you mentioned lights, so here is what i tried and it still didn't work !
Nothing on the screen at all:
(look previously msgs in this thread to see how I set up the arrays - which work when i use them when I did the working version with glVertext3f)


float lightpos[4]={0.0f, 700.0f, 1.0f, 0.0f};
float ambientlight[4]={145.0f/255.0f, 145.0f/255.0f, 145.0f/255.0f, 0.0f};
float diffuselight[4]={145.0f/255.0f, 145.0f/255.0f, 145.0f/255.0f, 0.0f};


glEnable(GL_LIGHTING);

glLightfv(GL_LIGHT0, GL_AMBIENT, ambientlight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuselight);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientlight);

glEnable(GL_LIGHT0);


//now draw relevant quad in our vertices
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, bldgVertices);
glNormalPointer(GL_FLOAT, 0, m_normalArray);
glColorPointer(3, GL_FLOAT, 0, m_colorArray);


//glColor3f(0.8f, 0.8f, 0.8f);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, m_indexArray);



Share this post


Link to post
Share on other sites
First off: Thanks for the source tags! Makes things easier to read.

Second: When I mentioned the lights, I asked if they were on, and if so to turn them OFF. Only for a moment, and simply for debug purposes. All you would really have to do is call glDisable(GL_LIGHTING) right before your draw call. I would recommend doing this until you have the quad showing up, THEN enabling lighting again and working from there. Always start with the minimal possible settings and work your way up. The less that's enabled the less that can go wrong.

Also, once again just for debugging purpouses, try commenting out the following two lines:

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, m_normalArray);

Then run the code and see if it makes a difference. Again, this is just starting with a minimal amount of information (Position and TexCoords) and then working your way up after you get it working.

Sorry if you've already tried those, but it's not clear that you have.

Share this post


Link to post
Share on other sites
Why are you enabling these on the client side?

use glEnable(GL_VERTEX_ARRAY).

see if that fixes it.

Share this post


Link to post
Share on other sites
the red book says to use glEnableClientState for VAs

I tried the following and combinations thereof !:

1. enabled/disabled lighting
2. enabled/disabled normals

I now have:


glDisable(GL_LIGHTING); //just to make sure

glEnable(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY); //just to make sure
glVertexPointer(3, GL_FLOAT, 0, Vertices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, m_indexArray);



I mean, how much simpler could it be !!

To recap the below works !! which means at least teh vertex and index arrays are set up properly:


glDisable(GL_LIGHTING); //just to make sure

glEnable(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY); //just to make sure
glVertexPointer(3, GL_FLOAT, 0, Vertices);

glColor3f(0.8f, 0.8f, 0.8f);
glBegin(GL_QUADS);
glVertex3fv(bldgVertices[m_indexArray[0]);
glVertex3fv(bldgVertices[m_indexArray[1]);
glVertex3fv(bldgVertices[m_indexArray[2]);
glVertex3fv(bldgVertices[m_indexArray[3]);
glEnd();



Share this post


Link to post
Share on other sites
Quote:
Original post by ade-the-heat
...
glVertexPointer(3, GL_FLOAT, 0, Vertices);
...
glVertex3fv(bldgVertices[m_indexArray[0]);
...
You're using two different variables... Vertices and bldgVertices. But I'm assuming that since you're missing a closing bracket for the bldgVertices array that this isn't a copy-paste from your code, otherwise you would be getting a compiler error, so I guess they could be the same variables in your real code. As a general guideline when posting code, copy and paste it directly, otherwise it might end up being different and have new errors (such as syntax errors) for people like me to notice and give you the wrong advice on.

Anyway, can you upload your whole project somewhere so we can get a better look at it?

Share this post


Link to post
Share on other sites
(For the record, this is Blue*Omega, I'm just sick of my old user name.)

I noticed that in your most recent code post, you've set glColor3f(0.8f, 0.8f, 0.8f); in the second example, but not the first. You can use the same command in conjunction with glDrawElements(), in case you didn't know.

Sorry if that doesn't help much, but I'm running out of ideas.

Share this post


Link to post
Share on other sites
Alright, but just for the record, I use glEnable(GL_VERTEX_ARRAY) and everything
is peachy-keen.

hhmmm...

Share this post


Link to post
Share on other sites
the Vertices is the same as bldgVertices - you're right I didn't copy n paste exactly.

I set the color using glColor for the VAs and still nothing...

This is getting me now. I've had a problem like this before when doing bit mapped fonts. They were the wrong color always. Everyone did their best to help. Nothing worked. I tried on/off for a month and gave up. Then six months later (this was yesterday !) I solved the problem. It was because I had the lighting on when doing the fonts - if I tunred the light off the font was the correct color ! Now that solution isn't intuitive and it won't be in any red or blue book... I guess I'l have to six months until soemthing stupid turns up..

I'd upload the project but it's too big really...

So my guess is: does anyone have/or can think of any dumb enabling/disabling combination I can do that will make the VAs work - we have to bear in mind that the glVertex3f methods works everytime which suggests that it all seems to be ok... sigh ...

Share this post


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

  • Advertisement