Jump to content
  • Advertisement
Sign in to follow this  
mbanghart

3D Cube

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

Hi, I am trying to define a 3D cube. Is this correct? glVertex3f(-1,1,0); glVertex3f(1,1,0); glVertex3f(1,-1,0); glVertex3f(-1,-1,0); glVertex3f(0,0,-1); glVertex3f(0,0,1); Thanks

Share this post


Link to post
Share on other sites
Advertisement
How the vertices will be interpreted depends on what you pass to glBegin(), but either way your code will not define a cube. See here for more info.

Share this post


Link to post
Share on other sites
Quote:
Original post by mbanghart
Hi,

I am trying to define a 3D cube. Is this correct?

glVertex3f(-1,1,0);
glVertex3f(1,1,0);
glVertex3f(1,-1,0);
glVertex3f(-1,-1,0);
glVertex3f(0,0,-1);
glVertex3f(0,0,1);

Thanks


Where are the glBegin and glEnd calls?

Share this post


Link to post
Share on other sites
First off don't use GL_POLYGON ever, and you need glBegin() and glEnd() when you draw polygons in immediate mode.


glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)

glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)

glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)

glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)

glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right) glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Quad






there look through that, and check out the beautiful colored cube.

Share this post


Link to post
Share on other sites
Thanks. But this is for a school project and I have to use polygons. The cube is being displayed 2D (and I have to use 6 vertices) and my input data is 3D.

Share this post


Link to post
Share on other sites
Quote:
Original post by mbanghart
Thanks. But this is for a school project and I have to use polygons. The cube is being displayed 2D (and I have to use 6 vertices) and my input data is 3D.


Triangles and Quads are Polygons. Polygon just means a "many-sides" ;)

Secondly it's impossible to have a "cube" in 3D with only 6 vertices. Think about it, or better yet pick up a box and count the number of corners, there are 8. Any less than 8 and you cannot have a cube.

It is possible to represent something that looks like a cube using 6 projected vertices (since 2 will always be culled as they will be back-facing) but I don't think thats what your homework means.

Andy

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!