Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <GL/freeglut.h>
- #include <bits/stdc++.h>
- //pyramid
- float ver[5][3] =
- {
- {0.0, 1.0, 0.0},
- {-1.0, -1.0, 1.0},
- {1.0, -1.0, 1.0},
- {1.0, -1.0, -1.0},
- {-1.0, -1.0, -1.0},
- };
- GLfloat color[5][3] =
- {
- {1.0, 0.0, 0.0},//red
- {0.0, 1.0, 0.0},//green
- {0.0, 0.0, 1.0},//blue
- {1.0, 1.0, 0.0},//yellow
- {1.0, 0.0, 1.0},//pink
- };
- void quad(int a, int b, int c, int d)
- {
- glBegin(GL_QUADS);
- glColor3fv(color[a]);
- glVertex3fv(ver[a]);
- glColor3fv(color[b]);
- glVertex3fv(ver[b]);
- glColor3fv(color[c]);
- glVertex3fv(ver[c]);
- glColor3fv(color[d]);
- glVertex3fv(ver[d]);
- glEnd();
- }
- void colorcube()
- {
- quad(0, 1, 2, 3);
- quad(0, 1, 4, 3);
- quad(0, 2, 3, 4);
- quad(1, 2, 3, 4);
- }
- //rotate
- double rotate_y = 0;
- double rotate_x = 0;
- void specialKeys(int key, int x, int y)
- {
- if (key == GLUT_KEY_RIGHT)
- rotate_y += 5;
- else if (key == GLUT_KEY_LEFT)
- rotate_y -= 5;
- else if (key == GLUT_KEY_UP)
- rotate_x += 5;
- else if (key == GLUT_KEY_DOWN)
- rotate_x -= 5;
- glutPostRedisplay();
- }
- // change the size of the cube with the mouse wheel
- // scale
- void mouseWheel(int button, int dir, int x, int y)
- {
- if (dir > 0)
- {
- for (int i = 0; i < 8; i++)
- {
- ver[i][0] *= 1.1;
- ver[i][1] *= 1.1;
- ver[i][2] *= 1.1;
- }
- }
- else
- {
- for (int i = 0; i < 8; i++)
- {
- ver[i][0] *= 0.9;
- ver[i][1] *= 0.9;
- ver[i][2] *= 0.9;
- }
- }
- glutPostRedisplay();
- }
- // change the position of the cube using W A S D keys without going out of the screen
- // translate
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 'w':
- for (int i = 0; i < 8; i++)
- ver[i][2] += 0.1;//z axis
- break;
- case 's':
- for (int i = 0; i < 8; i++)
- ver[i][2] -= 0.1;//z axis
- break;
- case 'a':
- for (int i = 0; i < 8; i++)
- ver[i][0] -= 0.1;//x axis
- break;
- case 'd':
- for (int i = 0; i < 8; i++)
- ver[i][0] += 0.1;//x axis
- break;
- }
- glutPostRedisplay();//repeat
- }
- void display()
- {
- glClearColor(1, 1, 1, 1);//window color
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);//background
- glLoadIdentity();//function ensures that each time when we enter the projection mode, the matrix will be reset to identity matrix
- int w = glutGet(GLUT_WINDOW_WIDTH);
- int h = glutGet(GLUT_WINDOW_HEIGHT);
- gluPerspective(60, w / h, 0.1, 100);//??
- glMatrixMode(GL_MODELVIEW);// object
- glLoadIdentity();
- gluLookAt(3, 3, 3,0, 0, 0,0, 0, 1);
- glRotatef(rotate_x, 1.0, 0.0, 0.0);
- glRotatef(rotate_y, 0.0, 1.0, 0.0);
- colorcube();
- glutSwapBuffers();
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);// graphics mode
- glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
- glutInitWindowSize(640, 480);
- glutCreateWindow("GLUT");
- glutDisplayFunc(display);
- glutSpecialFunc(specialKeys);
- glutMouseWheelFunc(mouseWheel);
- glutKeyboardFunc(keyboard);
- glEnable(GL_DEPTH_TEST);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement