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
 



OpenGL Programming for the X Window System    (ISBN: 0201483599)


 

 List Price: $49.99
 Our Price: $49.99
 Used Price: $25.00

 Release Date: 05 August, 1996
 Manufacturer: Addison-Wesley Pub Co (Paperback)
 Sales Rank: 59,818

 Author: Mark J. Kilgard









More Info

 Drawing with OpenGL 2002-03-22 19:59:57
  cpp 
Category: source:cpp:opengl
Description: Draw some basic shapes with opengl.
Platform: windows
Author: detour
Viewed: 4782
Rating: 3.3/5 (15 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
drawing.cpp
/* drawing.cpp written by detour@metalshell.com
 *
 * Shows how to draw a triangle, square and a line, using
 * OpenGL.
 *
 * Some other GLBegin() options that you can play with.
 * ------------------
 * GL_POINTS
 * GL_LINES
 * GL_LINE_LOOP
 * GL_LINE_STRIP
 * GL_TRIANGLES
 * GL_TRIANGLE_STRIP
 * GL_TRIANGLE_FAN
 * GL_QUADS
 * GL_QUAD_STRIP
 * GL_POLYGON
 * ------------------
 *
 * 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);

	/* Select the projection matrix and reset it */
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	/* Our screen coordinates */
	gluOrtho2D(-4,4,-4,4);
}

void doDisplay() {

	/* Clear the screen with the clearcolor */
	glClear(GL_COLOR_BUFFER_BIT);

	/* To start drawing you must use GL_BEGIN with the type of
	   drawing you are going to do.  Depending on what type of
	   drawing you choose will determine how many points you 
	   need to use. For a line you need 2 points, etc. If you
	   change the color between each point OpenGL will fade each
	   color automatically. */
	
	glBegin(GL_LINES);
		glVertex3f(2.0,1.0f,0.0f);
		glVertex3f(3.5,1.0f,0.0f);
	glEnd();
	
	glBegin(GL_TRIANGLES);
	    glColor3f(1.0f,1.0f,0.0f);
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glColor3f(0.0f,1.0f,1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
		glColor3f(1.0f,0.0f,1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);
	glEnd();

	glBegin(GL_QUADS);
	    glColor3f(1.0f,1.0f,0.0f);
		glVertex3f(-3.0f,1.0f,0.0f);
		glColor3f(0.0f,1.0f,0.0f);
		glVertex3f(-3.0f,3.0f,0.0f);
		glColor3f(1.0f,0.0f,0.5f);
		glVertex3f(-1.0f,3.0f,0.0f);
		glColor3f(0.0f,0.0f,1.0f);
		glVertex3f(-1.0f,1.0f,0.0f);
	glEnd();
    
	/* Dump the output to the screen */
	glFlush();
}


int main(int argc, char *argv[]) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(0,0);
	glutCreateWindow("Drawing");

	glutDisplayFunc(doDisplay);

	doInit();
	glutMainLoop();

	return 0;
}
Rate this code:
(Not Helpful)  (Very Helpful) 

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


Got Money?