OPENGL Lab Manual
OPENGL Lab Manual
OPENGL Lab Manual
1)
#include <gl\glut.h> void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); } int main(){ glutCreateWindow("Simple Square"); glutDisplayFunc(mydisplay); glutMainLoop(); }
2)
#include <gl\glut.h> void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glColor3f(1,0,0); glVertex2f(-0.5, -0.5); glColor3f(0,1,0); glVertex2f(-0.5, 0.5); glColor3f(0,0,1); glVertex2f(0.5, 0.5); glColor3f(0.5,0.5,0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); } int main(){ glutCreateWindow("Simple Square"); glutDisplayFunc(mydisplay); glutMainLoop(); }
3)
#include <gl\glut.h> void redraw() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINE_STRIP); // begin primitive assembly glColor3f(1,0,0); glVertex2f( 0, .5f); // red vertex glColor3f(0,1,0); glVertex2f(-.5f,-.5f); // green vertex glColor3f(0,0,1); glVertex2f( .5f,-.5f); // blue vertex glColor3f(1,0,0); glVertex2f( 0, .5f); // repeat first vertex glEnd(); // end primitive assembly glFlush(); } int main(){ glutCreateWindow("Triangle"); glutDisplayFunc(redraw); glutMainLoop(); }
Specify 6 points: glBegin(GL_POINTS) ; glVertex2f(0,0) glVertex2f(1,0) glVertex2f(1,1) glVertex2f(2,2) glVertex2f(3,2) glVertex2f(3,3) glEnd() ;
; ; ; ; ; ;
Specify 3 lines: glBegin(GL_LINES) ; glVertex2f(0,0) ; // line 0 glVertex2f(1,0) ; glVertex2f(1,1) ; // line 1 glVertex2f(2,2) ; glVertex2f(3,2) ; // line 2 glVertex2f(3,3) ; glEnd() ; Specify 2 triangles: glBegin(GL_TRIANGLES) ; glVertex2f(0,0) ; // triangle 0 glVertex2f(1,0) ; glVertex2f(1,1) ; glVertex2f(2,2) ; // triangle 1 glVertex2f(3,2) ; glVertex2f(3,3) ; glEnd() ; Specify 5 connected line segments: glBegin(GL_LINE_STRIP) ; glVertex2f(0,0) ; glVertex2f(1,0) ; glVertex2f(1,1) ; glVertex2f(2,2) ; glVertex2f(3,2) ; glVertex2f(3,3) ; glEnd() ; (0,0) is the first point of the first line and (3,3) is the last point of the last line.
Specify a convex polygon: glBegin(GL_POLYGON) ; glVertex2f(0,-1) ; // home plate for a baseball game glVertex2f(1,0) ; glVertex2f(1,1) ; glVertex2f(-1,1) ; glVertex2f(-1,0) ; glEnd() ; Loosely speaking, convex means no dents or dimples.
4) Intro to glOrtho()
(Prog_01_E)
#include <gl\glut.h> void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES) ; glVertex2f(0,0) ; glVertex2f(1,0) ; glVertex2f(1,1) ; glVertex2f(2,2) ; glVertex2f(3,2) ; glVertex2f(3,3) ; glEnd() ;
glFlush(); } void init() { glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0); glMatrixMode (GL_PROJECTION); glLoadIdentity ();