I just want to put some polygons on screen!!

Started by
12 comments, last by creativeminds 21 years, 11 months ago
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]
Advertisement

  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

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
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
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]

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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.
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( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
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

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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]

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement