110 likes | 123 Views
Learn how to create basic graphics using OpenGL and GLUT with this example code. Get insights on initializing the display, setting up windows, defining matrices, and drawing shapes. Find more details and resources at www.opengl.org. Recommended reading: "OpenGL Programmer’s Guide".
E N D
Introduction to GL Geb Thomas
Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowPosition(100,100); glutInitWindowSize(300,300); glutCreateWindow ("square"); glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0); glutDisplayFunc(display); glutMainLoop(); return 0; void display(void) { glClear( GL_COLOR_BUFFER_BIT); glColor3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON); glVertex3f(2.0, 4.0, 0.0); glVertex3f(8.0, 4.0, 0.0); glVertex3f(8.0, 6.0, 0.0); glVertex3f(2.0, 6.0, 0.0); glEnd(); glFlush(); }
What’s Going On? • OpenGL – The graphics calls • GLUT – Utility for opening windows • Both have header files and libraries • Opengl32.dll, glut32.dll • opengl.h, glut32.h
Using GLUT To Initialize the Display glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
Setting Up and Opening the Window glutInitWindowPosition(100,100); glutInitWindowSize(300,300); glutCreateWindow ("square");
Setting Up the Matrices glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
Defining the Drawing Routine glutDisplayFunc(display);
Drawing Some Stuff void display(void) { glClear( GL_COLOR_BUFFER_BIT); glColor3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON); glVertex3f(2.0, 4.0, 0.0); glVertex3f(8.0, 4.0, 0.0); glVertex3f(8.0, 6.0, 0.0); glVertex3f(2.0, 6.0, 0.0); glEnd(); glFlush(); }
Repeating the Loop glutMainLoop();
Details are Here www.opengl.org Good book: Open GL Programmer’s Guide