LWJGL Forum

Programming => OpenGL => Topic started by: Hellekin on March 23, 2007, 01:30:10

Title: Cant make 3d objects =/
Post by: Hellekin on March 23, 2007, 01:30:10
I can make 2d objects fine using openGl, but when i try to make 3d objects they simply dont print on the screen, i followed many tutorials like nehe or asteroids and i have the same problem in both, when i add thus to my code:

   GL11.glMatrixMode(GL11.GL_PROJECTION);
   GL11.glLoadIdentity();      
   GLU.gluPerspective(45.0f, ((float) 800) / ((float) 600), 0.1f, 100.0f);
   GL11.glMatrixMode(GL11.GL_MODELVIEW);
   GL11.glLoadIdentity();

my simple 2d figures disapear and i cant draw anything. =/

If someone can help me pls post here...
Title: Re: Cant make 3d objects =/
Post by: bobjob on March 23, 2007, 04:58:32
i used the nehe's tutorial for my foundation. if u follow it correctly, then u'll relise there isnt any difference between 2d and 3d objects (unless ur usn ortho or wotever, dw about that, thats something else) in order to test wot i mean just do a 2d object eg.
do a quad.
if it works, alter the points z co-ordinates u will c that it goes 3d. 3d is just placing 2d objects that join together 2 make a 3d object
so a 3d cube is: 6 2d quads.

so if u can print a 2d object to the screen u can definetly print a 3d object.
Title: Re: Cant make 3d objects =/
Post by: Hellekin on March 23, 2007, 11:40:50
The problem is that when i add a z coordinate to my points they dont draw unless i put then in 0. Does someone know where i can find the most simple possible code for drawing a cube or pyramid? Some reference would help me a lot. Thanks anyway.
Title: Re: Cant make 3d objects =/
Post by: bobjob on March 25, 2007, 06:06:24
ok have u tried a negative z value
if so then u may b in ortho view. but im thinkn its that u gotta have a negative z value
my layouts r all set forgot how the planes aline, but im pritty sure i rememeber that -z goes into the screen and + goes behind u if ur position is x0,y0,z0 and viewangle x0, y0
oh and ur far distance is 100 so make sure that ur z value is less the 100 away, so from -0.1 to -100.00
Title: Re: Cant make 3d objects =/
Post by: bobjob on March 25, 2007, 06:12:11
here is a code example that works


GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
                rotate++; // rotate gloabal float value

/** draw Pyramid on screen **/
GL11.glLoadIdentity(); // Reset The Current Modelview Matrix
GL11.glTranslatef(-1.5f,0.0f,-6.0f); // reletive to objects position
GL11.glRotatef(rotate,1.0f,1.0f,0.0f);

GL11.glBegin(GL11.GL_TRIANGLES);
// first face
GL11.glColor3f(1.0f,0.0f,0.0f); // Red
GL11.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
GL11.glColor3f(0.0f,1.0f,0.0f); // Green
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
GL11.glColor3f(0.0f,0.0f,1.0f); // Blue
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)

// secound face
GL11.glColor3f(1.0f,0.0f,0.0f); // Red
GL11.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
GL11.glColor3f(0.0f,0.0f,1.0f); // Blue
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
GL11.glColor3f(0.0f,1.0f,0.0f); // Green
GL11.glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)

//third face
GL11.glColor3f(1.0f,0.0f,0.0f); // Red
GL11.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
GL11.glColor3f(0.0f,1.0f,0.0f); // Green
GL11.glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
GL11.glColor3f(0.0f,0.0f,1.0f); // Blue
GL11.glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)

// forth face
GL11.glColor3f(1.0f,0.0f,0.0f); // Red
GL11.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
GL11.glColor3f(0.0f,0.0f,1.0f); // Blue
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
GL11.glColor3f(0.0f,1.0f,0.0f); // Green
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
GL11.glEnd();
/** finish drawing Pyramid **/

/** Draw Box on screen**/
GL11.glLoadIdentity(); // Reset The Current Modelview Matrix
GL11.glTranslatef(1.5f,0.0f,-7.0f); // Move Right And Into The Screen
GL11.glRotatef(rotate,1.0f,0.0f,0.5f); // Rotate The Cube On X, Y & Z
                GL11.glBegin(GL11.GL_QUADS); // Draw A Quad
//first face
GL11.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
// second face
GL11.glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
// third face
GL11.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
//forth face
GL11.glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)
//fifth face
GL11.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
// sixth face
GL11.glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
GL11.glEnd(); // Done Drawing The Quad

Title: Thx
Post by: Hellekin on March 26, 2007, 01:23:57
Thx dude, i got it working now xD.