Sei sulla pagina 1di 5

Sample Fragment Code

On the left column is the OpenGL fragment code. On the right column is the output. OpenGL Fragment Code 1.) To draw 4 points. glPointSize(5.0); //To control the size of the points glBegin(GL_POINTS); // Dont forget to end with s glColor3f(1,0,0); glVertex2i(1,1); //red point glColor3f(0,1,0); glVertex2i(5,1); //green point glColor3f(0,0,1); glVertex2i(6,5); //blue point glColor3f(1,1,0); glVertex2i(2,5); //yellow point glEnd(); 2.) To draw lines. You must provide at least 2 points in order to draw a line. If you provide 3 points, it will only draw a line. glLineWidth(5.0); //To control the width of a line glBegin(GL_LINES); // Dont forget to end with s glColor3f(1,0,0); glVertex2i(1,1); glColor3f(0,1,0); glVertex2i(5,1); glColor3f(0,0,1); glVertex2i(6,5); glColor3f(1,1,0); glVertex2i(2,5); glEnd(); 3.) Not a close loop glLineWidth(5.0); glBegin(GL_LINE_STRIP); //same with the above glEnd(); 4.) A close loop glLineWidth(5.0); glBegin(GL_LINE_LOOP); //same with the above glEnd(); 5.) To draw two or more line-loops, you have to call glBegin() and glEnd() several times. glLineWidth(5.0); glBegin(GL_LINE_LOOP); glColor3f(1,0,0); glVertex2i(1,1); glColor3f(0,1,0); glVertex2i(5,1); glColor3f(0,0,1); glVertex2i(6,5); glEnd(); glBegin(GL_LINE_LOOP); Output

glColor3f(1,0,0); glVertex2i(1,2); glColor3f(0,1,0); glVertex2i(4,4); glColor3f(0,0,1); glVertex2i(1,6); glEnd(); 6.) Provide at least 3 points to draw a triangle. The internal region is filled with color. The order of the vertices is important. glBegin(GL_TRIANGLES); //Dont forget to end with s glColor3f(1,0,0); glVertex2i(1,1); glColor3f(0,1,0); glVertex2i(5,1); glColor3f(0,0,1); glVertex2i(6,5); glColor3f(1,0,0); glVertex2i(1,2); glColor3f(0,1,0); glVertex2i(4,4); glColor3f(0,0,1); glVertex2i(1,6); glEnd(); 7.) To connect all the vertices. The order of the vertices is important. glBegin(GL_POLYGON); glColor3f(1,0,0); glVertex2i(1,1); glColor3f(0,1,0); glVertex2i(5,1); glColor3f(0,0,1); glVertex2i(6,5); glColor3f(0,1,0); glVertex2i(4,4); glColor3f(0,0,1); glVertex2i(1,6); glColor3f(1,0,0); glVertex2i(1,2); glEnd(); Note: At home, you may try to replace the instance with GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, and GL_QUAD_STRIP. 8.) By default, all the built-in objects will be at the origin (0, 0, 0) such as the yellow teapot. The red teapot is moved away from the origin after the translation event below. When you implement any transformation command, please remember to include glPushMatrix() and glPopMatrix() around them; void draw() { glColor3f(1,1,0); glutSolidTeapot(2.0); //Appear at the origin glPushMatrix(); glColor3f(1,0,0); glTranslatef(4,5,0); //move away from the origin glutSolidTeapot(2.0); glPopMatrix(); }

9.) The Rotation and Scaling events can only happen at the origin. For the example on the right, we first move the yellow teapot away from the origin for the display purpose. After that, rotate the red teapot at the origin. Remember to include glPushMatrix() and glPopMatrix()at each set of the transformation commands. void draw() { glPushMatrix(); glColor3f(1,1,0); glTranslatef(-6,0,0); glutSolidTeapot(2.0); glPopMatrix(); glPushMatrix(); glColor3f(1,0,0); glRotatef(90,0,0,1); //Anti-clockwise order about Z-axis glutSolidTeapot(2.0); glPopMatrix(); } 10.) To scale an object at the origin, we apply 2 times wider, half (or ) of the original height and we didnt change the depth. void draw() { glPushMatrix(); glColor3f(1,1,0); glTranslatef(-9,0,0); glutSolidTeapot(2.0); glPopMatrix(); glPushMatrix(); glColor3f(1,0,0); glScalef(2, 0.5, 1); // Never assign any zero value. glutSolidTeapot(2.0); glPopMatrix(); } 11.) We can also use glScalef(..,..,..) for reflection purpose. We reflect the original yellow teapot to upside-down of red teapot by adding a negative - value in the glScalef() component. void draw() { glColor3f(1,1,0); glutSolidTeapot(2.0); glPushMatrix(); glColor3f(1,0,0); glScalef(1,-1,1); glutSolidTeapot(2.0); glPopMatrix();

} 12.) To apply any built-in primitive objects using the Quadric method (such as gluCylinder, gluDisk, or gluSphere), you have to first call the commands highlighted in green color below. void draw() { GLUquadricObj *pObj; pObj = gluNewQuadric(); gluQuadricNormals(pObj, GLU_SMOOTH); glColor3f(1,1,0); gluCylinder(pObj, 2.0, 1.0, 3.0, 25, 25); //gluSphere(pObj, 2.0, 25, 25); //gluDisk(pObj, 2.0, 4.0, 25, 25); glPopMatrix(); gluDeleteQuadric(pObj); } 13.) For any other primitive objects (such as cube, sphere, torus, teapot and etc), just call directly from the glut library. void draw() { glColor3f(1,1,0); glutSolidCube(3.0); // glutSolidSphere(GLdouble radius, GLint slices, GLint stacks); // glutSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks); // glutSolidTorus(GLdouble innerRadius, GLdouble outerRadius, GLint sides, GLint rings); // glutSolidTeapot(GLdouble size); } 14.) To repeatedly and continuously move an object from one position to another position, use the code below: static float X=0; void draw() { glColor3f(1,1,0); glPushMatrix(); glTranslatef(X,0,0); glutSolidTeapot(2.0); glPopMatrix(); } void myDisplayFunc(void) { :::::::::::: :::::::::::: X = X + 0.1; if(X>15) {

X = 0; } glutPostRedisplay(); }

Potrebbero piacerti anche