/* rotate3d.cpp written by detour@metalshell.com
*
* Shows how to draw and rotate a 3d object.
*
*
* In order to get this to compile you must have the opengl
* libraries, and header files. Be sure to include opengl32.lib
* glut32.lib and glu32.lib when compiling.
*
* http://www.metalshell.com/
*
*/
#include <windows.h>
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/glut.h>
void doInit() {
/* Background and foreground color */
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);
glViewport(0,0,640,480);
/* Select the projection matrix and reset it then
setup our view perspective */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.0f,(GLfloat)640/(GLfloat)480,0.1f,100.0f);
/* Select the modelview matrix, which we alter with rotatef() */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearDepth(2.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void drawTriangle(double rang) {
/* Clear the screen with the clearcolor */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Reset the modelview matrix */
glLoadIdentity();
/* Move it back 13 to bring into view */
glTranslatef(0.0f,0.0f,-13.0f);
/* Rotate around the y-axis with the angle rang */
glRotatef(rang,0.0f,1.0f,0.0f);
/* Draw the bottom box */
glBegin(GL_QUADS);
/* Top */
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-2.0f,-1.0f,2.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(2.0f,-1.0f,2.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(2.0f,-1.0f,-2.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-2.0f,-1.0f,-2.0f);
/* Front */
glVertex3f(-2.0f,-1.0f,2.0f);
glVertex3f(-2.0f,-1.2f,2.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(2.0f,-1.2f,2.0f);
glVertex3f(2.0f,-1.0f,2.0f);
/* Left */
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-2.0f,-1.0f,2.0f);
glVertex3f(-2.0f,-1.2f,2.0f);
glVertex3f(-2.0f,-1.2f,-2.0f);
glVertex3f(-2.0f,-1.0f,-2.0f);
/* Back */
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-2.0f,-1.0f,-2.0f);
glVertex3f(-2.0f,-1.2f,-2.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(2.0f,-1.2f,-2.0f);
glVertex3f(2.0f,-1.0f,-2.0f);
/* Right */
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(2.0f,-1.0f,2.0f);
glVertex3f(2.0f,-1.2f,2.0f);
glVertex3f(2.0f,-1.2f,-2.0f);
glVertex3f(2.0f,-1.0f,-2.0f);
glEnd();
/* Draw the 3d triangle */
glBegin(GL_TRIANGLES);
/* Front */
glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.1f,0.5f,0.5f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(0.5f,0.5f,1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
/* Left */
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.1f,0.5f,0.5f);
glVertex3f(-1.0f,-1.0f,1.0f);
glColor3f(0.5f,0.5f,1.0f);
glVertex3f(-1.0f,-1.0,-1.0f);
/* Right */
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.5f,0.5f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glColor3f(0.1f,0.5f,0.5f);
glVertex3f(1.0f,-1.0,-1.0f);
/* Back */
glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.5f,0.5f,1.0f);
glVertex3f(-1.0f,-1.0f, -1.0f);
glColor3f(0.1f,0.5f,0.5f);
glVertex3f( 1.0f,-1.0f, -1.0f);
glEnd();
/* Dump the output to the screen */
glFlush();
/* Use double buffering when you are doing animation */
glutSwapBuffers();
}
void doDisplay() {
static double x = 0.0;
x += .30;
drawTriangle(x);
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
/* Double Buffer */
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Rotate 3D");
glutDisplayFunc(doDisplay);
glutIdleFunc(doDisplay);
doInit();
glutMainLoop();
return 0;
}
|