Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Programming In Glut (lesson 4), Making 3D objects
t3jem
post Nov 24 2006, 04:48 PM
Post #1


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 48
Joined: 23-November 06
Member No.: 17,478



Hello, in this tutorial we will be creating a 3D pyramid. We are building this tutorial from Lesson 3, but I took out the 2D objects and placed a 3D pyramid in there instead. The 3rd axis for drawing can be a litle confusing, but after you get the hand of it you'll do fine. Now when you are setting a 3D vertex just remember that the camera is on the positive end of the z axis. So things that have a more positive z axis value are closer than verteces with a more negative value. Well let's get started


We always start by including the glut library
CODE

#include<glut.h>


the time variable will be used to keep track of how much time has passed.
CODE

float time = 0;// set time variable to 0


Our first new function is glOrtho();, this function is much like gluOrtho2D(), but glOrtho() defines how far along the z axis you can view objects as well, this is very important when drawing 3D objects.

Next we want to enable depth testing to let our program test which polygons are where and draw them in the correct order for us, to do this we use glEnable() with the argument GL_DEPTH_TEST.
CODE

void init()
{
    glClearColor(0,0,0,0);
    glOrtho(-5,5,-5,5,-5,5);//(NEW) set up our viewing area

    glEnable(GL_DEPTH_TEST);//(NEW) Enable depth testing
}


In our display function we will create a 3D pyramid. To set the verteces we will use glVertex3f() instead of glVertex2f() so we can define where the point is on the z axis to add depth.

in our glClear() function we add a new argument that we seperate with a "|". The new argument is GL_DEPTH_BUFFER_BIT to enable the depth buffer so our program can draw 3D objects correctly.
CODE

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//(NEW) setup our buffers

    glPushMatrix();
    glRotatef(time,0.8,5,0);//rotate our objects

    glBegin(GL_TRIANGLES);
    //This is going to be the face facing the camera
    //We want this face to be red
    glColor3f(1,0,0);
    glVertex3f(-1,-1,1);//(NEW) We are now defining the vertex position on all three axis
    glVertex3f(0,1,0);//the top vertex
    glVertex3f(1,-1,1);//last vertex of this face

    //We do not need to enter glEnd() here because we are still drawing triangles

    //The back left face
    //This face will be green
    glColor3f(0,1,0);
    glVertex3f(-1,-1,1);//Vertex closest to us
    glVertex3f(0,1,0);//The top vertex
    glVertex3f(0,-1,-1);//The vertex farthest away

    //The back right face
    //This face will be blue
    glColor3f(0,0,1);
    glVertex3f(0,-1,-1);//The farthest vertex
    glVertex3f(0,1,0);//The heighest vertex
    glVertex3f(1,-1,1);//The closest vertex

    //The bottom face
    //This is face will be white
    glColor3f(1,1,1);
    glVertex3f(-1,-1,1);//Each corner of the base of the pyramid
    glVertex3f(0,-1,-1);
    glVertex3f(1,-1,1);

    glEnd();//done drawing our triangle

    glPopMatrix();

    glFlush();
    glutPostRedisplay();//This function is crucial in animation, it refreshes the screen
}


next we create our idle function which will update our time variable
CODE

void idle()
{
    time += 0.1;// increase our time variable

    if(time > 360)
        time = 0;// reset time variable
}


next is our main function. The only difference is in the gluInitDisplayMode() function, we added GLUT_DEPTH as an argument to tell the program to use the depth buffer.
CODE

void main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(800,600);
    glutInitWindowPosition(10,50);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);//tell the program we are running the depth buffer
    glutCreateWindow("Lesson 4");
    init();
    glutDisplayFunc(display);
    glutIdleFunc(idle);// This function calls our idle function to update our variables
    glutMainLoop();
}


depth testing is very important when creating 3D objects, you must enable, tell the program to clear it, and you must tell it that you are using the depth buffer. If you don't do all of these than your program will draw everything in the wrong order and it will look very confusing.

I hope you enjoyed yet another GLUT tutorial. The next tutorial will include texturing and keyboard interaction.

Edited on December 28 2006 to make more readable.

This post has been edited by t3jem: Dec 29 2006, 12:20 AM
Go to the top of the page
 
+Quote Post
Mark420
post Nov 25 2006, 08:26 PM
Post #2


The Modernator
Group Icon

Group: Members
Posts: 486
Joined: 6-August 06
From: The Interweb!
Member No.: 15,021



Really nice set of tutorials so far t3jem,



This is what Asthost is all about, helping others to help yourself.

I am too busy with learning php and mysql atm to learn another language but I am sure others on the board will learn a lot from these tutorials.



Keep up the good work wink.gif

Go to the top of the page
 
+Quote Post
t3jem
post Nov 26 2006, 02:46 AM
Post #3


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 48
Joined: 23-November 06
Member No.: 17,478



QUOTE(Mark420 @ Nov 25 2006, 01:26 PM) *

Really nice set of tutorials so far t3jem,



This is what Asthost is all about, helping others to help yourself.

I am too busy with learning php and mysql atm to learn another language but I am sure others on the board will learn a lot from these tutorials.



Keep up the good work wink.gif



Thank you, I hope to get another one up tomarrow, it will be texturing and basic keyboard interaction. I plan to take these tutorials quite a ways, not sure how far yet, but as far as I can.
Go to the top of the page
 
+Quote Post
iGuest
post May 4 2008, 02:59 PM
Post #4


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



a question about the opengl pyramid
Programming In Glut (lesson 4)

Replying to t3jem

Hello :)

If you still see this message, can you please tell me how to transform this code, the vertex coordinates in order to draw a pyramid given a certain size of it.
For example if we want to draw a pyramid with its side of 2, or 3 how can we modify these coordinates?
I need to draw a pyramid wih a dimension given by the user.
Thank you in advance.

-reply by Nora
Go to the top of the page
 
+Quote Post
iGuest
post May 4 2008, 02:57 PM
Post #5


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



a question about the pyramid
Programming In Glut (lesson 4)

Hello :)

If you still see this message, can you please tell me how to transform this code, the vertex coordinates in order to draw a pyramid given a certain size of it.
For example if we want to draw a pyramid with its side of 2, or 3 how can we modify these coordinates?
I need to draw a pyramid wih a dimension given by the user.
Thank you in advance.

-reply by Nora
Go to the top of the page
 
+Quote Post
iGuest
post May 13 2008, 06:28 PM
Post #6


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Hi,
I appriciate what you have done, thank you for all but...
I don't want to animate the object, I just want to see a cube(in 3d). When I wrote the program I only saw one face of the cube, no depth, no other faces!
I need this because I will render the cube, which I will define, with gouraud shading algorithm. So I need to see a 3d representation.
What should I do!?
Please help me!
Regards,Replying to t3jemReplying to t3jem

-reply by baris
Go to the top of the page
 
+Quote Post
iGuest
post Jul 29 2008, 03:07 AM
Post #7


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Awesome. Thanks for the taught. Really enjoy it.

-reply by tusy
Go to the top of the page
 
+Quote Post
iGuest
post Jul 29 2008, 03:07 AM
Post #8


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Awesome. Thanks for the taught. Really enjoy it.

-reply by tusy
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Programming In Glut (lesson 1)(3)
  2. Programming In Glut (lesson 2)(8)
  3. Programming In Glut (lesson 3)(1)
  4. Installing Glut To Dev C++(3)
  5. Programming In Glut (lesson 6)(1)


 



- Lo-Fi Version Time is now: 5th September 2008 - 09:49 AM