Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Programming In Glut (lesson 4), Creating 3D objects
Rating 5 V
t3jem
post Apr 29 2007, 06:55 PM
Post #1


Privileged Member
*********

Group: [HOSTED]
Posts: 522
Joined: 9-February 07
Member No.: 38,519



Lesson 4 of 6. I hope you are enjoying them laugh.gif .

QUOTE
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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. A Guide To Css And Creating A Stylesheet(15)
  2. Creating Personal Alarm(2)
  3. Creating Your Own Icon(23)
  4. Creating Rollovers With Buttons(2)
  5. Centered Website With Fixed Width(2)
  6. Delete Files And Directories Using Php(7)
  7. Getting Started With Amfphp And Rias(2)
  8. Creating A Simple Image Viewer(3)
  9. Creating A Timer Program(8)
  10. Tutorial: Creating Custom Icons For Devices(0)
  11. Tutorial: Help About C & C++ Programming Langauge(1)
  12. Installing Glut To Dev(3)
  13. Programming In Glut (lesson 1)(0)
  14. Programming Glut (lesson 2)(0)
  15. Programming In Glut (lesson 3)(0)
  1. Programming In Glut (lesson 5)(0)
  2. Programming In Glut (lesson 6)(2)
  3. Creating A Resume(1)
  4. Programming Using Basic Language!(0)
  5. Creating Navigation For Html Websites(12)
  6. Bryce 5 --- Boolean Objects(0)
  7. Getting Started With Mysql(2)
  8. How To Start Your First Game Project(0)
  9. Create Dynamic Html/php Pages Using Simple Vb.net Code(1)


 



- Lo-Fi Version Time is now: 5th September 2008 - 12:18 PM