Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <glfw3.h>
- #include <cstdlib>
- #include <cmath>
- #define SCREEN_WIDTH 1024
- #define SCREEN_HEIGHT 720
- void keyCallback( GLFWwindow *window, int key, int scancode, int action, int mods );
- void DrawCube( GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength );
- void reSizeWindowCallback(GLFWwindow *window, int width, int height);
- void drawLines();
- GLfloat rotationX = 0.0f;
- GLfloat rotationY = 0.0f;
- GLfloat rotationZ = 0.0f;
- GLfloat halfScreenWidth = SCREEN_WIDTH / 2;
- GLfloat halfScreenHeight = SCREEN_HEIGHT / 2;
- GLfloat CubsLength = -500;
- GLfloat CubsHeight = 200;
- int gradation = 20;
- int main( void ){
- GLFWwindow *window;
- //инициализируем библиотеку
- if ( !glfwInit( ) ){
- return -1;
- }
- // Настраиваем размер окна
- window = glfwCreateWindow( SCREEN_WIDTH, SCREEN_HEIGHT, "Rotating Hyperboloid", NULL, NULL );
- glfwSetKeyCallback( window, keyCallback );
- glfwSetWindowSizeCallback(window, reSizeWindowCallback);
- glfwSetInputMode( window, GLFW_STICKY_KEYS, 1 );
- int screenWidth, screenHeight;
- glfwGetFramebufferSize( window, &screenWidth, &screenHeight );
- if (!window ){
- glfwTerminate( );
- return -1;
- }
- glfwMakeContextCurrent( window );
- glViewport( 0.0f, 0.0f, screenWidth, screenHeight );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity( );
- //создает наклонную проекцию
- glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1000 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity( );
- while ( !glfwWindowShouldClose(window)){
- glClearColor(0, 0,0 , 0);
- glClear( GL_COLOR_BUFFER_BIT );
- glPushMatrix();
- //Смещение координатной оси
- glTranslatef(halfScreenWidth, halfScreenHeight, -500 );
- // glOrtho(-100, 100, -100, 100, -100, 100);
- glRotatef(rotationX, 1, 0, 0 );
- glRotatef(rotationY, 0, 1, 0 );
- glRotatef(rotationZ, 0, 0, 1);
- glTranslatef(-halfScreenWidth, -halfScreenHeight, 500 );
- DrawCube(halfScreenWidth, halfScreenHeight, CubsLength, CubsHeight);
- // drawLines();
- glPopMatrix();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
- void reSizeWindowCallback(GLFWwindow *window, int width, int height){
- glViewport(0.0f, 0.0f, width, height);
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity( );
- //создает наклонную проекцию
- glOrtho(0, width, 0, height, 0, 1000 );
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- halfScreenHeight = SCREEN_HEIGHT/2;
- halfScreenWidth = SCREEN_WIDTH/2;
- DrawCube(width/2, height/2, CubsLength, CubsHeight);
- }
- void keyCallback( GLFWwindow *window, int key, int scancode, int action, int mods ){
- if (action == GLFW_PRESS || action == GLFW_REPEAT ){
- switch (key){
- case GLFW_KEY_UP:
- rotationX -= 20;
- break;
- case GLFW_KEY_DOWN:
- rotationX += 20;
- break;
- case GLFW_KEY_RIGHT:
- rotationY += 20;
- break;
- case GLFW_KEY_LEFT:
- rotationY -= 20;
- break;
- case GLFW_KEY_Z:
- rotationZ +=20;
- break;
- case GLFW_KEY_X:
- rotationZ -=20;
- break;
- case GLFW_KEY_EQUAL:
- CubsHeight += 4;
- break;
- case GLFW_KEY_MINUS:
- CubsHeight -= 4;
- break;
- case GLFW_KEY_W:
- halfScreenHeight +=5;
- break;
- case GLFW_KEY_A:
- halfScreenWidth -=5;
- break;
- case GLFW_KEY_S:
- halfScreenHeight -=5;
- break;
- case GLFW_KEY_D:
- halfScreenWidth +=5;
- break;
- case GLFW_KEY_F:
- glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
- break;
- case GLFW_KEY_L:
- glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
- break;
- case GLFW_KEY_ESCAPE:
- glfwSetWindowShouldClose(window,GL_TRUE);
- break;
- }
- }
- }
- void DrawCube( GLfloat X, GLfloat Y, GLfloat Z, GLfloat edgeLength ){
- GLfloat x, y, z, alpha, beta; // Storage for coordinates and angles
- int gradation = 50;
- glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
- for (alpha = - M_PI/2; alpha < M_PI/2; alpha += M_PI/gradation){
- glBegin(GL_TRIANGLE_STRIP);
- for (beta = -M_PI; beta < M_PI; beta += M_PI/gradation){
- x = 10 * cos(beta)/cos(alpha);
- y = 10 * sin(beta)/ cos(alpha);
- z = 10 * tan(alpha);
- glVertex3f(x + 300, y + 400, z);
- glColor3f(1, 1, 1);
- }
- glEnd();
- }
- glEnable(GL_DEPTH_TEST);
- // glEnableClientState( GL_COLOR_ARRAY );
- // glEnableClientState( GL_VERTEX_ARRAY );
- // glVertexPointer( 3, GL_FLOAT, 0, vertices );
- // glColorPointer(3, GL_FLOAT, 0, cubeColors);
- // glDrawArrays( GL_QUADS, 0, 24 );
- // glMatrixMode(GL_PROJECTION);
- // glPushMatrix();
- // glLoadIdentity();
- // glMatrixMode(GL_MODELVIEW);
- // glLoadIdentity();
- // glDisable(GL_CULL_FACE);
- // glClear(GL_DEPTH_BUFFER_BIT);
- //drawing cube
- glBegin(GL_QUADS);
- glColor3f(1.0f, 0.0f, 0.0);
- glVertex2f(850.0, 600.0);
- glVertex2f(950.0, 600.0);
- glVertex2f(950.0, 700.0);
- glVertex2f(850.0, 700.0);
- glColor3f(1.0f, 0.0f, 0.0);
- glVertex2f(950.0, 700.0);
- glVertex2f(975.0, 675.0);
- glVertex2f(975.0, 575.0);
- glVertex2f(950.0, 600.0);
- glColor3f(0.0f, 0.0f, 1.0);
- glVertex2f(950.0, 600.0);
- glVertex2f(975.0, 575.0 );
- glVertex2f(875.0, 575.0);
- glVertex2f(850.0, 600.0);
- glEnd();
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- glDisableClientState( GL_VERTEX_ARRAY );
- glDisableClientState(GL_COLOR_ARRAY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement