Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<GL/gl.h>
- #include<GL/glu.h>
- #include<GL/glut.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<windows.h>
- int windowWidth = 800;
- int windowHeight = 600;
- int rot = 0;
- float Txval = 0;
- float Tyval = 0;
- float sval = 1.00;
- float rval = 1.00;
- bool flagScale = false;
- bool firstFlag = true;
- bool lastFlag = false;
- void triangle()
- {
- glBegin(GL_TRIANGLES);
- // Define a group of vertices that define one or more primitives
- glColor3f(1.0, 1.0, 1.0);
- glVertex2f(2.0, 2.0);
- glColor3f(0.0, 1.0, 1.0);
- glVertex2f(2.0, 0.0);
- glColor3f(0.0, 1.0, 0.0);
- glVertex2f(0.0, 0.0);
- glEnd();
- // Terminates a list of vertices that specifies a primitive initiated by glBegin
- }
- void rectangle()
- {
- glBegin(GL_QUADS);
- glColor3f(0.0, 1.0, 0.0);
- glVertex2f(1.0, 2.0);
- glColor3f(0.0, 1.0, 0.0);
- glVertex2f(0.0, 2.0);
- glColor3f(1.0, 0.0, 0.0);
- glVertex2f(0.0, 0.0);
- glColor3f(1.0, 0.0, 0.0);
- glVertex2f(1.0, 0.0);
- glEnd();
- }
- void cube()
- {
- glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
- // Top face (y = 1.0f)
- // Define vertices in counter-clockwise (CCW) order with normal pointing out
- glColor3f(0.0f, 1.0f, 0.0f); // Green
- glVertex3f( 1.0f, 1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f( 1.0f, 1.0f, 1.0f);
- // Bottom face (y = -1.0f)
- glColor3f(1.0f, 0.5f, 0.0f); // Orange
- glVertex3f( 1.0f, -1.0f, 1.0f);
- glVertex3f(-1.0f, -1.0f, 1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f( 1.0f, -1.0f, -1.0f);
- // Front face (z = 1.0f)
- glColor3f(1.0f, 0.0f, 0.0f); // Red
- glVertex3f( 1.0f, 1.0f, 1.0f);
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f(-1.0f, -1.0f, 1.0f);
- glVertex3f( 1.0f, -1.0f, 1.0f);
- // Back face (z = -1.0f)
- glColor3f(1.0f, 1.0f, 0.0f); // Yellow
- glVertex3f( 1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f( 1.0f, 1.0f, -1.0f);
- // Left face (x = -1.0f)
- glColor3f(0.0f, 0.0f, 1.0f); // Blue
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, 1.0f);
- // Right face (x = 1.0f)
- glColor3f(1.0f, 0.0f, 1.0f); // Magenta
- glVertex3f(1.0f, 1.0f, -1.0f);
- glVertex3f(1.0f, 1.0f, 1.0f);
- glVertex3f(1.0f, -1.0f, 1.0f);
- glVertex3f(1.0f, -1.0f, -1.0f);
- glEnd();
- }
- void display_first(void)
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glViewport(0, 0, windowWidth/2, windowHeight/2);
- rectangle();
- }
- void myKeyboardFunc( unsigned char key, int x, int y)
- {
- switch(key)
- {
- case '2':
- flagScale=true;
- firstFlag=false;
- lastFlag=false;
- break;
- case '1':
- flagScale=false;
- firstFlag=true;
- lastFlag=false;
- break;
- case '3':
- flagScale=false;
- firstFlag=false;
- lastFlag=true;
- break;
- case 27:
- exit(1);
- }
- glutPostRedisplay();
- }
- void animate()
- {
- if(flagScale)
- {
- rval += 0.5;
- if(rval >= 359)
- rval = 1.0;
- }
- glutPostRedisplay();
- }
- void translate()
- {
- glMatrixMode(GL_MODELVIEW);
- glTranslatef(0.0, 0.0, 0.0);
- }
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-3, 3, -3, 3, -3, 10);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- if(flagScale)
- {
- glPushMatrix();
- glRotatef(rval, 0, 0, 1);
- triangle();
- glPopMatrix();
- }
- if(firstFlag)
- {
- glPushMatrix();
- rectangle();
- glPopMatrix();
- }
- if(lastFlag)
- {
- glPushMatrix();
- glRotatef(45,0,1,0);
- glRotatef(45,1,0,0);
- glRotatef(rot,1,0,1);
- cube();
- glPopMatrix();
- }
- glFlush();
- glutSwapBuffers();
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(windowWidth/2, windowHeight/2);
- glutCreateWindow("Lab-1 assignment");
- glShadeModel(GL_SMOOTH);
- glEnable(GL_DEPTH_TEST);
- glutDisplayFunc(display);
- glutIdleFunc(animate);
- glutKeyboardFunc(myKeyboardFunc);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement