| |
 |
Tutorial on creating an OpenGL Cube using GLUT
|
|
If you would like to see this site updated, please help out and
|
After reading this opengl tutorial you should have a fair idea on how to create predefined opengl shapes, specifically cubes using glut. Glut has many different preset 3d shapes for use in opengl which I will list below. You will be able to create both solid and wire opengl cubes with a single line of code.
In glut we are given a group of different 3D shapes to play with
one of them is the cube, it can be drawn with the line:
glutWireCube(2);
or:
glutSolidCube(2);
The 2 is the length of the sides, as it is a cube, all sides are the same.
some other 3D shapes to play with are the:
(for these 3, the more slices and stacks, the smoother the surface.)
glutSolidCone (base length, height, slices, stacks);
glutSolidSphere (radius, slices, stacks);
glutSolidTorus (inner radius, outter radius, slices, stacks);
alternatively there is also the Wire version which is done by replacing
the word Solid with Wire.
There is also a little teapot when playing with different effects, so that
you can see how it would change a more 'modelled' shape which can be called with
glutSolidTeapot(size);
If you have any questions, just email swiftless@gmail.com :-)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
| | | #include <GL/gl.h>
#include <GL/glut.h>
void cube (void) {
glColor3f(1.0, 0.0, 0.0); //color the cube red
glutWireCube(2); //draw a wired cube with side lengths of 2
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
cube();
glFlush();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}
|
Download C++ Source Code for this Tutorial
Download Visual Basic Source Code for this Tutorial
Comments:
| Name: Wrong |
| Date: 2010-02-05 08:01:08 |
Comment:
code does not even compile!
|
| Name: Donald Urquhart |
| Date: 2010-02-05 09:15:02 |
Comment:
Hi Wrong,
Can you tell us why it does not compile? In the many years this tutorial has been up, I have not had a single complaint. Maybe I can help you get it to work?
Cheers, Donald
|
| Name: Simon |
| Date: 2010-02-07 10:22:05 |
Comment:
Hi,
I have got a problem when compiling the c++ source code. The following error shows up:
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup 1>C:\Users\Simon\Documents\Visual Studio 2008\Projects\OpenGLtest\Debug\OpenGLtest.exe : fatal error LNK1120: 1 unresolved externals
Any idea why this happened? It also happened with others opengl tutorial...
|
| Name: Donald Urquhart |
| Date: 2010-02-07 10:24:49 |
Comment:
Hi Simon,
If your application is looking for a WinMain function, then you are creating the wrong type of project in Visual Studio. You will want to create a Console Application so that your application will look for your 'main' function instead of a 'WinMain'.
Cheers, Donald
|
| Name: Simon |
| Date: 2010-02-12 04:57:21 |
Comment:
Hi Donald, Thanks a lot for your advice. I created a Win32 Console Application instead, but it came with other errors, coming from the gl.h file (see extract from build sample below). I had to include "stdafx.h" to make it work. Thanks a again, and I will now go through the tutorial in details!
Cheers, Simon
1>------ Build started: Project: OpenGL, Configuration: Debug Win32 ------ 1>Compiling... 1>Main.cpp 1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2144: syntax error : 'void' should be preceded by ';' 1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2146: syntax error : missing ';' before identifier 'glAccum' 1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2182: 'APIENTRY' : illegal use of type 'void' 1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1153) : error C2144: syntax error : 'void' should be preceded by ';' 1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1153) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
|
| Name: Donald Urquhart |
| Date: 2010-02-17 17:02:47 |
Comment:
Hi Simon,
Glad to hear you got it working. Can I ask what version of Visual Studio you are using? I think from 2008 onwards, you can turn off the use of precompiled header files, which should mean you don't need stdafx.h.
Cheers, Donald
|
Name Email
|
 |