Programming In Glut (lesson 4) - Making 3D objects

free web hosting
Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > MISC (not applicable to above Programming Categories)

Programming In Glut (lesson 4) - Making 3D objects

t3jem
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.

 

 

 


Reply

Mark420
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


Reply

t3jem
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.

Reply

iGuest
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

Reply

iGuest
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

Reply

iGuest
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

Reply

iGuest
Awesome. Thanks for the taught. Really enjoy it.

-reply by tusy

Reply

iGuest
Awesome. Thanks for the taught. Really enjoy it.

-reply by tusy

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. glut 3d object - 10.44 hr back. (1)
  2. opengl draw axes - 28.13 hr back. (1)
  3. opengl draw 3d objects - 69.36 hr back. (1)
  4. opengl keyboard interaction - 81.97 hr back. (1)
  5. glut 3d polygons - 68.28 hr back. (2)
  6. glut camera - 3.20 hr back. (2)
  7. glut write buffer to file - 88.35 hr back. (1)
  8. using glut for 2d example - 90.75 hr back. (1)
  9. glut 3d - 3.94 hr back. (5)
  10. glut 3d tutorials - 125.38 hr back. (1)
  11. glut draw random polygon tutorial - 132.34 hr back. (1)
  12. glut draw - 128.88 hr back. (2)
  13. java glut example - 141.67 hr back. (1)
  14. making 3d object using dev - 142.45 hr back. (1)
Similar Topics

Keywords : programming, glut, lesson, 4, making, 3d, objects

  1. Programming In Glut (lesson 6)
    Texture filters and lighting (1)
  2. Programming In Glut (lesson 5)
    How to texture map and use keyboard controls. (0)
    In this tutorial I am going to teach you how to texture map your polygons and implement basic
    keyboard control. There is alot of new material in this tutorial and I hope I have explained it
    enough. Before you get started you will want to name a bmp file "image.bmp" or "image" depending on
    your computer settings (if one doesn't work then use the other) and place it in the same folder
    as your project. Now to start we will be making a seperate header file where we will write our
    function to load a bitmap file. You should name it "bmpload.h". The code to write in it ....
  3. Installing Glut To Dev C++
    A tutorial to install GLUT on Dev C++ (3)
    This is a tutorial that someone submitted to my programming site when it was up because I didn't
    know how to install glut on any other compiler than my own. I though it would be helpful to post
    this up for everyone here so they can use GLUT as well without having to google for hours. I'm
    not sure if I'm allowed, but I have quoted it and have given credit where credit is due, so if
    this post is allowed I hope this helps those people who couldn't program in GLUT yet. QUOTE
    Installing G.L.U.T. to Dev C++ By James Duran (email: vrok137@yahoo.com) ....
  4. Programming In Glut (lesson 3)
    How to animate objects in GLUT (1)
    In this tutorial I am going to talk about how to get animation in your program. I will be working
    directly off of Lesson 2 and will now take out the notes left behind from Lesson 1, but I will leave
    the notes from Lesson 2. CODE #include<glut.h>//include our glut library First we
    are going to initialize a variable called "time" which will record how much time has passed so we
    can use it to determine how to animate the objects. Then we have our init function that initializes
    some stuff in GLUT CODE float time = 0;//(NEW) variable to recor....
  5. Programming In Glut (lesson 2)
    Learn how to draw 2D polygons in this tutorial (8)
    This is the second lesson in my series of tutorials on how to use GLUT to create graphics. In this
    tutorial I am going to be teaching you how to create different types of polygons. I am going to be
    adding on to last tutorial's code and will leave the notes in to help you remember what all the
    function are. I will also be noting the new functions that we will be using and how to use them.
    CODE #include<glut.h>//Include the GLUT functions This time we are going to not only
    set the background color, but set the area that we are viewing as well. glu....
  6. Programming In Glut (lesson 1)
    The first of a series of tutorials on how to use the OpenGL Utility To (3)
    Hello, I'm starting a series on how to program in OpenGL using the OpenGL Utility Toolkit,
    a.k.a. GLUT. I chose GLUT because it is quick and easy to write, and very easy to learn. In this
    tutorial I am going to teach you how to create a basic window which we will build off of in later
    tutorials. Throughout the tutorial I will leave notes to let you know what each command does, and
    how you can modify it to fit your needs. Everything in the code section can be copied and pasted
    into your compiler and it should compile proporly, if it does not, please let me know, a....
  7. Programming With Coffee
    Begining Programming with Java (2)
    Since Java is a complete programming language, one tutorial is not going to cover everything.
    However, I will try to cover the basics and possibly extend on them later, and will assume you know
    very little about programming. If something seems unclear, it is because I am not very good at
    explaining. Read on and if you reach the end of the tutorial and still don’t get it, tell me. Tell
    me even if you figure it out, but something is unclear, I will try to fix it. Any style
    guides/conventions are not mandatory at all, it’s just how many programmers write their code and ma....

    1. Looking for programming, glut, lesson, 4, making, 3d, objects

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for programming, glut, lesson, 4, making, 3d, objects
advertisement




Programming In Glut (lesson 4) - Making 3D objects



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE