Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Game Programming Gems 3    (ISBN: 1584502339)


 

 List Price: $69.95
 Our Price: $55.96
 Used Price: $44.77

 Release Date: July, 2002
 Manufacturer: Charles River Media (Hardcover)
 Sales Rank: 16,010

 Author: Dante Treglia, Mark Deloura









More Info

 Rotate 3D Object 2002-03-24 22:47:29
  cpp 
Category: source:cpp:opengl
Description: Draws a 3d triangle on a square platform, and rotates it.
Platform: windows
Author: detour
Viewed: 5549
Rating: 3/5 (17 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
rotate3d.cpp
/* 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;
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?