|
|
| 8.
CREATE 3D OBJECT WITH OPENGL |
|
|
|
1. |
In the previous tutorial, we only deal
with the 2-Dimensional object. In this tutorial, we will create a
simple 3-Dimensional object such as a cube.
|
|
2. |
First of all, let's do a simple sketch of
the 3D cube before we write it in the program. For this cube, there
will be 8 vertices involved and let's say we named it as V0 to
V7.
There will also be six surfaces on this cube and we named it as S1 to
S6.
The diagram below might be help you to
better visualize it.
|
|
3. |
Now, let's translate the sketch into
the program. You can copy the program
here to your program.
|
|
4.
|
From line 10 to line 17, we create an
array for each vertices to store their x, y and z coordinates.
GLdouble V0[] = { 0.0, 0.0, 0.0};
GLdouble V1[] = { 1.0f, 0.0, 0.0};
GLdouble V2[] = { 1.0f, 1.0f, 0.0};
GLdouble V3[] = { 0.0, 1.0f, 0.0};
GLdouble V4[] = { 0.0, 0.0, -1.0f};
GLdouble V5[] = { 1.0f, 0.0, -1.0f};
GLdouble V6[] = { 1.0f, 1.0f, -1.0f};
GLdouble V7[] = { 0.0, 1.0f, -1.0f}; |
|
|
5.
|
In the Display function, at line 26,
we used the glPolygonMode function with GL_LINE as the
second parameter, to draw the 3D object as a wireframe. We
will draw it as a wireframe model to show you clearly that the
object created is really in 3D form.
|
glPolygonMode(GL_FRONT_AND_BACK,
GL_LINE); |
To change it to the solid
form instead of wireframe form, replace the GL_LINE with GL_FILL.
|
| 6. |
At line 28, we will use GL_QUADS
command to create the surfaces of the cube which consists of six
squares.
|
| 7. |
Now, let' create Surface 1 first.
Surface 1 will used vertices V0, V1, V2 and V3 to build its
surface. Make sure the arrangement of the vertices is in counter
clockwise order when we look directly in front of the surface from
the outside. Remember, counter clockwise from the outside, not
from the inside. Please refer to the diagram below.
The arrangement will be useful when
it comes to things that related to normal such as lighting,
texture mapping etc. The command will be like this:
|
glVertex3dv(V0); glVertex3dv(V1);
glVertex3dv(V2); glVertex3dv(V3);
// Surface 1
|
As you can see, since we
have initialize the vertices values in step no 4, we have
simplified the codes in the program to create Surface 1. The
equivalent codes is:
glVertex3f( 0.0, 0.0,
0.0); //
V0 (0,0,0)
glVertex3f( 1.0f, 0.0,
0.0);
// V1 (1,0,0)
glVertex3f( 1.0f, 1.0f,
0.0); //
V2 (1,1,0)
glVertex3f( 0.0, 1.0f,
0.0);
// V3 (0,1,0)
|
|
|
8.
|
Next, we will create Surface 2 which
consists of vertices V1, V5, V6 and V2.
|
glVertex3dv(V1); glVertex3dv(V5);
glVertex3dv(V6); glVertex3dv(V2);
// Surface 2
|
The counter clockwise order
of the vertices will be the same as Surface 1. Please refer to the
diagram below.
As you can see, we do not need to
put in another 'glBegin(GL_QUADS)' to create the second
surface. Since we use GL_QUADS command which deals with
only four points to create a polygon, it will automatically
recognizes the 5th vertex as the beginning of a new polygon which
will end with the 8th vertex.
|
| 9. |
Surface 3, 4, 5 and 6 will work just
the way it is been explained in step no 8. Please refer to the
diagram below for the counter clockwise order of the vertices of
the surfaces. Notice the x, y and z axis when we look at it
directly from the outside of the surface.
|
| 10. |
In the end, after you have compile,
build and execute the program, you should see this output:
|
| 11. |
Now, you can try to create your own 3D
object. There are many ways of doing it such as building it
directly using the way like in this tutorial or create the object
in a 3D software first such as 3DMax and then exported it to this
program. You can explore in on the internet for more ways of doing
it. Good luck guys!
|
|
|
Last
Modified: 03-Feb-2006
|
|
|
|
|
PREVIOUS INDEX NEXT
|
|
|