#include <windows.h>
#include <GL/glut.h> // GLUT, include glu.h and gl.h
#include <iostream>
#include "exampleWithCube.cpp"
/* Global variables */
GLfloat anglePyramid = 0.0f;
GLfloat angleCube = 0.0f;
GLfloat pyramidXPos = -2.5f;
GLfloat pyramidYPos = 0.0f;
GLfloat pyramidZPos = -6.0f;
GLfloat centerX = 0.0f;
GLfloat centerY = 0.0f;
GLfloat centerZ = 0.0f;
GLfloat eyeX = 0.0f;
GLfloat eyeY = 0.0f;
GLfloat eyeZ = 0.0f;
int refreshMills = 15;
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
}
void drawCube()
{
glBegin(GL_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(); // End of drawing color-cube
}
void drawTriangle()
{
glBegin(GL_TRIANGLES); // Begin drawing the pyramid with 4 triangles
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
// Right
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
// Back
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// Left
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd(); // Done drawing the pyramid
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
glLoadIdentity();
glTranslatef(2.0f, 0.0f, -7.0f);
glRotatef(angleCube, 1.0f, 1.0f, 1.0f);
drawCube();
gluLookAt(eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
0.0, 0.0, 0.0);
glLoadIdentity();
glTranslatef(pyramidXPos, pyramidYPos, pyramidZPos);
drawTriangle();
gluLookAt(eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
0.0, 0.0, 0.0);
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
glPushMatrix();
angleCube += 0.15f;
//anglePyramid -= 0.15f;
}
void reshape(GLsizei width, GLsizei height) {
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, aspect, 0.1f, 100.0f);
glutSwapBuffers();
}
void specialKey(int key, int x, int y)
{
switch(key)
{
case (GLUT_KEY_LEFT):
eyeX -= 0.05f;
centerX -= 0.1f;
break;
case (GLUT_KEY_RIGHT):
eyeX += 0.05f;
centerY += 0.1f;
break;
case (GLUT_KEY_UP):
eyeY += 0.05f;
centerX -= 0.1f;
break;
case (GLUT_KEY_DOWN):
eyeY -= 0.05f;
centerY + 0.1f;
break;
}
glutPostRedisplay();
}
char trianglePosV = 'r';
void timer(int value) {
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(refreshMills, timer, 0); // next timer call milliseconds later
switch (trianglePosV)
{
case('r'):
pyramidXPos += 0.01f;
if (std::roundf(pyramidXPos) > -2.0f) trianglePosV = 'u';
//std::cout << "right ";
break;
case('u'):
pyramidYPos += 0.01f;
if (std::roundf(pyramidYPos) > 1.0f) trianglePosV = 'l';
/* std::cout << "up ";*/
break;
case('l'):
pyramidXPos -= 0.01f;
if (std::roundf(pyramidXPos) < -2.5f) trianglePosV = 'd';
/*std::cout << "left ";*/
break;
case('d'):
pyramidYPos -= 0.01f;
if (std::roundf(pyramidYPos) < 0.0f) trianglePosV = 'r';
/*std::cout << "down ";*/
break;
}
/*std::cout << pyramidXPos << " " << pyramidYPos << std::endl;*/
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 27)
{
exit(0); // quit the program
}
}
void initAll()
{
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutInitWindowPosition(50, 50);
glutCreateWindow("Laba #2");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(specialKey);
glutKeyboardFunc(keyboard);
initGL();
glutTimerFunc(0, timer, 0);
glutMainLoop();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
initAll();
return 0;
}