Archived

This topic is now archived and is closed to further replies.

creativeminds

I just want to put some polygons on screen!!

Recommended Posts

creativeminds    122
Heres my problem: I'm using SDL and opengl on my linux box
  
glBeing(GL_QUADS);
glVertex3f( 0.10f,-0.10f, 0.10f);                    
glVertex3f(-0.10f, 0.10f, 0.0f);                          
glVertex3f( 0.10f, 0.10f, 0.10f);                       
glVertex3f(-0.10f,-0.10f, 0.10f);
glEnd();

  
The above code works like its supposed to, it puts a quad on the screen but when I change it to this:
  
glBegin(GL_QUADS);
   // TotalVertex is 3.  The vertices are in clock-wise order

   for (int i = 0; i <= polygon.TotalVertex; i++)
      glVertex3f(polygon.vertex[i].x,
                 polygon.vertex[i].y,
                 polygon.vertex[i].z );
glEnd();
  
I don't get anything on the screen!! Its supposed to put a whole scene on the screen. Help! I can't find whats wrong. I tried to unloop them and just write the 4 glVertex3f without using the for loop. this is the code that calls the function above
  
OBJECT * object;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity( );
 
for ( int i = world->NumObjects; i >= 0; i-- ) {
   object = &world->object[i]; 
   for (int e = 0; e <= object->NumSurface; e++) {
      texture= object->texture;
      polygon = object->surface[e];
      PolygonRend();

   }
}
SDL_GL_SwapBuffers( );
  
[edited by - creativeminds on May 1, 2002 5:15:48 PM]

Share this post


Link to post
Share on other sites
benjamin bunny    838

  
for ( int i = world->NumObjects; i >= 0; i-- ) { object = &world->object[i];


You should set i to world->NumObjects-1, which is the index of the last object in your array. world->NumObjects is the last index plus one (in other words, a random memory location).


____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites
Yeah, to elaborate, when one says:

for( int i = 0; i < NumSomething; i ++ )
// it''s the same as:
for( int i = 0; i <= NumSomething-1; i ++ )

So to loop the reverse direction, you''d start at NumSomething-1 and go as long as i >= 0.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
benjamin bunny    838
quote:

I count 0 as the first number.
When I have 4 objects my NumObjects will be 3 ( 4 -1 )

I tried world->NumObject - 1 but I still have the same problem


Sorry, but that is just plain wrong. If you have 4 objects, numObjects should be 4. What do you set numObjects to if you have no objects? -1?

____________________________________________________________
www.elf-stone.com

[edited by - benjamin bunny on May 2, 2002 5:46:01 AM]

Share this post


Link to post
Share on other sites
creativeminds    122
I always assummed that I will always have at least one object
in the world.
I should change it so that my NumObects is 4 instead of 3
since it just makes it more clear.
I also noticed that if any of the points are greater than
1.0f the polygon just doesn''t show up.

Share this post


Link to post
Share on other sites
In certain situations, that optimization (making numObjects 3 to mean "4") is a good one. But for longs, their maximum value is 4294967295 (4.3 billion) so there is no reason to lower your number; there''s plenty of room to store it

Since the first index of anything is 0, you can say "lastObject = 3" and go from 0 to lastObject. But the number of objects is 4, so if you want to use a variable called numObjects and have your code be readable by humans, then you go from 0 to numObjects-1.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
benjamin bunny    838
quote:
Original post by creativeminds
I also noticed that if any of the points are greater than
1.0f the polygon just doesn''t show up.


That''s down to your perspective transform or your modelview matrix setup. Try moving your camera back, and make sure your near and far clip planes are far enough apart.

____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites
benjamin bunny    838
quote:
Original post by CGameProgrammer
In certain situations, that optimization (making numObjects 3 to mean "4") is a good one. But for longs, their maximum value is 4294967295 (4.3 billion) so there is no reason to lower your number; there's plenty of room to store it

Since the first index of anything is 0, you can say "lastObject = 3" and go from 0 to lastObject. But the number of objects is 4, so if you want to use a variable called numObjects and have your code be readable by humans, then you go from 0 to numObjects-1.

~CGameProgrammer( );




That's one "optimisation" I've never come across. It makes no sense - you _need_ the number 0 when you're measuring the quantity of something, in order to know there's none there.

If, for instance you wanted to draw all the objects in a scene, but none had been allocated - so numObjects is -1 - this code wouldn't work:


    
for (a=0;a<numObjects;++a)
{
}

//You'd have to do this:


for (a=-1;a<numObjects;++a)
{
}

Which means you're forced to use a signed instead of unsigned value, so instead of one number less, you lose a whole bit of precision. That's 2^31 less unsigned numbers in your range. Some optimisation - it actually means you have less numbers.

____________________________________________________________
www.elf-stone.com


[edited by - benjamin bunny on May 2, 2002 5:43:24 PM]

Share this post


Link to post
Share on other sites
I said "in certain situations" not "in all situations". If, for example, you need to use a byte to represent some amount of something, and you know you''ll always have at least one of that thing, you can squeeze more out of the byte by using "0" to represent "1" and "255" to mean "256", in other words the byte is numSomething-1. Then you add 1 to get the real value. There are some cases in which this is useful, not many though.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Sandman    2210
Another possible cause of your problem is that the vertices for each quad are not co-planar.

In order to draw quads you need to be 100% certain that all your points lie on exactly the same plane or the drawing operation will quietly fail. This is one reason why quads are horrible.

Try drawing your object using triangles or triangle lists - with only triangles your points are garaunteed to be co-planar so this problem simply doesn''t arise.

Share this post


Link to post
Share on other sites
creativeminds    122
I finally got the problem sorted out. There was nothing wrong with the points for the quads, but there was something wrong with how I setup opengl. I don''t have the code in front of me to tell how I fixed it but it was a minor bug.
In regards to having my object number ''3'' when the real number is ''4'' is that out of the blue when I was starting my project I decided to have it ''3'' instead of 4 so I wont have to remember to subtract one for each for loop that deals with the NumObects.
It caused more problems that it solved.

Share this post


Link to post
Share on other sites
oeginc    122
quote:
Original post by creativeminds
I finally got the problem sorted out. There was nothing wrong with the points for the quads, but there was something wrong with how I setup opengl. I don''t have the code in front of me to tell how I fixed it but it was a minor bug.
In regards to having my object number ''3'' when the real number is ''4'' is that out of the blue when I was starting my project I decided to have it ''3'' instead of 4 so I wont have to remember to subtract one for each for loop that deals with the NumObects.
It caused more problems that it solved.


So what you do is create a #define like:

#define OBJECT_COUNT (WorldObjects.count() - 1)

and then you:

for (i=0; i
or whatever..

Share this post


Link to post
Share on other sites