Está en la página 1de 5

Computer Graphics OpenGL (How to Start and Work)

1)

Simply Draw a PolyGon/Quad (Prog_01)

#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)

Make Colorful PolyGon (Prog_01_B)

#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(); }

Some More examples


// 1 Quad glBegin(GL_QUADS); glColor3f(1,0,0); glVertex3f( 1, 1,0); glVertex3f(-1, 1,0); glVertex3f(-1,-1,0); glVertex3f( 1,-1,0); glEnd(); // 2 Triangles glBegin(GL_TRIANGLES); glColor3f(1,0,0); glVertex3f( 1, 1,0); glVertex3f(-1, 1,0); glVertex3f(-1,-1,0); glVertex3f(-1,-1,0); glVertex3f( 1,-1,0); glVertex3f( 1, 1,0); glEnd();

3)

Simply Draw a PolyGon/Quad (Prog_01_C)

#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() ;

; ; ; ; ; ;

glBegin(GL_POINTS) ; glVertex2f(0,0) ; glVertex2f(0.5,0) ; glVertex2f(0.5,0.5) ; glVertex2f(-0.5,0) ; glVertex2f(-0.5,0.5) ; glVertex2f(-0.1,0) ; 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 ();

glOrtho(0.0, 4.0, 0.0, 4.0, -4.0, 4.0);


} int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,300); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); init(); glutMainLoop(); }

También podría gustarte