Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
7.3 kB
2
Indexable
Never
#include <windows.h>  
#include <GL/glut.h>  // GLUT, include glu.h and gl.h
#include <iostream>

 /* 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;

GLfloat angle1 = 0.0;
// координаты вектора направления движения камеры
GLfloat lx = 0.0f, lz = -1.0f;
// XZ позиция камеры
GLfloat x = 0.0f, z = 5.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);                // 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();  // 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(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
    glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix
    
    glLoadIdentity();
    
    gluLookAt(x, 1.0f, z,
        x + lx, 1.0f, z + lz,
        0.0f, 1.0f, 0.0f);
    
    glPushMatrix();
    
    glTranslatef(2.0f, 0.0f, -7.0f); 
    glRotatef(angleCube, 1.0f, 1.0f, 1.0f);
    drawCube();
    glPopMatrix();
    
    glPushMatrix();
    
    glTranslatef(pyramidXPos, pyramidYPos, pyramidZPos);
    drawTriangle();
    glPopMatrix();

     // Swap the front and back frame buffers (double buffering)

    glutSwapBuffers();

    angleCube += 0.01f;
    //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)
{
    float fraction = 0.1f;
    switch(key)
    {
    case (GLUT_KEY_LEFT):
        angle -= 0.01f;
        lx = sin(angle);
        lz = -cos(angle);
        break;
    case (GLUT_KEY_RIGHT):
        angle += 0.01f;
        lx = sin(angle);
        lz = -cos(angle);
        break;
    case (GLUT_KEY_UP):
        x += lx * fraction;
        z += lz * fraction;
        break;
    case (GLUT_KEY_DOWN):
        x -= lx * fraction;
        z -= lz * fraction;
        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);    
    glutIdleFunc(display);
    glutSpecialFunc(specialKey);
    glutKeyboardFunc(keyboard);
    initGL();
    glutTimerFunc(0, timer, 0);
    glutMainLoop();                 
}



int main(int argc, char** argv) {
    glutInit(&argc, argv);        
    initAll();
    return 0;
}