Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <cmath>
- #include <math.h>
- GLfloat fill = GL_LINE;
- float Rotate_x = 0.0f, Rotate_y = 0.0f, Rotate_z = 0.0f;
- float x = 0, y = 0, z = 0;
- float scrollX = 0.4, scrollY = 0.4, scrollZ = 0.4;
- float fz = 0.5;
- GLfloat phi = asin(fz / sqrt(2.0));
- GLfloat teta = asin(fz / sqrt(2.0 - fz * fz));
- GLfloat matrix[4][4] = {
- {1,0, 0, 0},
- {0,1, 0, 0},
- {0,0,1, 0},
- {0,0,0,1},
- };
- std::string vertexShader = "#version 430\n"
- "in vec3 pos;"
- "void main() {"
- "gl_Position = vec4(pos, 1);"
- "}";
- std::string fragmentShader = "#version 430\n"
- "void main() {"
- "gl_FragColor = vec4(1, 0, 0, 1);"
- "}";
- GLuint loadDataInBuffers()
- {
- GLfloat vertices[] = { // vertex coordinates
- 0.5f, -0.5f, -0.5f,
- 0.5f, 0.5f, -0.5f,
- -0.5f, 0.5f, -0.5f,
- -0.5f, -0.5f, -0.5f,
- 0.5f, -0.5f, 0.5f,
- 0.5f, 0.5f, 0.5f,
- -0.5f, 0.5f, 0.5f,
- -0.5f, -0.5f, 0.5f,
- 0.5f, -0.5f, -0.5f,
- 0.5f, 0.5f, -0.5f,
- 0.5f, 0.5f, 0.5f,
- 0.5f, -0.5f, 0.5f,
- -0.5f, -0.5f, 0.5f,
- -0.5f, 0.5f, 0.5f,
- -0.5f, 0.5f, -0.5f,
- -0.5f, -0.5f, -0.5f,
- 0.5f, 0.5f, 0.5f,
- 0.5f, 0.5f, -0.5f,
- -0.5f, 0.5f, -0.5f,
- -0.5f, 0.5f, 0.5f,
- 0.5f, -0.5f, -0.5f,
- 0.5f, -0.5f, 0.5f,
- -0.5f, -0.5f, 0.5f,
- -0.5f, -0.5f, -0.5f,
- /* {0.5f, -0.5f, -0.5f},
- {0.5f, 0.5f, -0.5f},
- {-0.5f, 0.5f, -0.5f},
- {-0.5f, -0.5f, -0.5},
- {0.5, -0.5, 0.5 },
- {0.5, 0.5, 0.5},
- {-0.5, 0.5, 0.5},
- {-0.5, -0.5, 0.5},
- {0.5, -0.5, -0.5},
- {0.5, 0.5, -0.5},
- {0.5, 0.5, 0.5},
- {0.5, -0.5, 0.5},
- {-0.5, -0.5, 0.5 },
- {-0.5, 0.5, 0.5},
- {-0.5, 0.5, -0.5},
- {-0.5, -0.5, -0.5},
- {0.5, 0.5, 0.5 },
- {0.5, 0.5, -0.5},
- {-0.5, 0.5, -0.5},
- {-0.5, 0.5, 0.5},
- {0.5, -0.5, -0.5 },
- {0.5, -0.5, 0.5},
- {-0.5, -0.5, 0.5},
- {-0.5, -0.5, -0.5},*/
- };
- GLuint vboId;
- // allocate buffer sapce and pass data to it
- glGenBuffers(1, &vboId);
- glBindBuffer(GL_ARRAY_BUFFER, vboId);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- // unbind the active buffer
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- return vboId;
- }
- GLuint linkProgram(GLuint vertexShaderId, GLuint fragmentShaderId)
- {
- GLuint programId = glCreateProgram(); // crate a program
- if (programId == 0) {
- std::cout << "Error Creating Shader Program";
- return 0;
- }
- // Attach both the shaders to it
- glAttachShader(programId, vertexShaderId);
- glAttachShader(programId, fragmentShaderId);
- // Create executable of this program
- glLinkProgram(programId);
- GLint linkStatus;
- // Get the link status for this program
- glGetProgramiv(programId, GL_LINK_STATUS, &linkStatus);
- if (!linkStatus) { // If the linking failed
- std::cout << "Error Linking program";
- glDetachShader(programId, vertexShaderId);
- glDetachShader(programId, fragmentShaderId);
- glDeleteProgram(programId);
- return 0;
- }
- return programId;
- }
- GLuint compileShaders(std::string shader, GLenum type)
- {
- const char* shaderCode = shader.c_str();
- GLuint shaderId = glCreateShader(type);
- if (shaderId == 0) { // Error: Cannot create shader object
- std::cout << "Error creating shaders";
- return 0;
- }
- // Attach source code to this object
- glShaderSource(shaderId, 1, &shaderCode, NULL);
- glCompileShader(shaderId); // compile the shader object
- GLint compileStatus;
- // check for compilation status
- glGetShaderiv(shaderId, GL_COMPILE_STATUS, &compileStatus);
- if (!compileStatus) { // If compilation was not successfull
- int length;
- glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &length);
- char* cMessage = new char[length];
- // Get additional information
- glGetShaderInfoLog(shaderId, length, &length, cMessage);
- std::cout << "Cannot Compile Shader: " << cMessage;
- delete[] cMessage;
- glDeleteShader(shaderId);
- return 0;
- }
- return shaderId;
- }
- void init()
- {
- // clear the framebuffer each frame with black color
- glClearColor(0, 0, 0, 0);
- GLuint vboId = loadDataInBuffers();
- GLuint vShaderId = compileShaders(vertexShader, GL_VERTEX_SHADER);
- GLuint fShaderId = compileShaders(fragmentShader, GL_FRAGMENT_SHADER);
- GLuint programId = linkProgram(vShaderId, fShaderId);
- //
- // Get the 'pos' variable location inside this program
- GLuint posAttributePosition = glGetAttribLocation(programId, "pos");
- GLuint vaoId;
- glGenVertexArrays(1, &vaoId); // Generate VAO
- // Bind it so that rest of vao operations affect this vao
- glBindVertexArray(vaoId);
- // buffer from which 'pos' will recive its data and the format of that data
- glBindBuffer(GL_ARRAY_BUFFER, vboId);
- glVertexAttribPointer(posAttributePosition, 3, GL_FLOAT, false, 0, 0);
- // Enable this attribute array linked to 'pos'
- glEnableVertexAttribArray(posAttributePosition);
- // Use this program for rendering.
- //glUseProgram(programId);
- }
- void display(GLFWwindow *window)
- {
- // clear the color buffer before each drawing
- glClear(GL_COLOR_BUFFER_BIT);
- glPolygonMode(GL_FRONT_AND_BACK, fill);
- // draw triangles starting from index 0 and
- // using 3 indices
- glDrawArrays(GL_QUADS, 0, 24);
- // swap the buffers and hence show the buffers
- // content to the screen
- //glfwPollEvents();
- //glfwSwapBuffers(window);
- }
- void keyCallback(GLFWwindow * window, int key, int scancode, int action, int mode) {
- if (action == GLFW_PRESS || action == GLFW_REPEAT) {
- switch (key) {
- case GLFW_KEY_RIGHT:
- Rotate_x -= 10;
- break;
- case GLFW_KEY_LEFT:
- Rotate_x += 10;
- break;
- case GLFW_KEY_DOWN:
- Rotate_y -= 10;
- break;
- case GLFW_KEY_UP:
- Rotate_y += 10;
- break;
- case GLFW_KEY_Q:
- Rotate_z += 10;
- break;
- case GLFW_KEY_E:
- Rotate_z -= 10;
- break;
- case GLFW_KEY_A:
- x -= 0.1;
- break;
- case GLFW_KEY_D:
- x += 0.1;
- break;
- case GLFW_KEY_W:
- y += 0.1;
- break;
- case GLFW_KEY_S:
- y -= 0.1;
- break;
- case GLFW_KEY_Z:
- scrollX += 0.1;
- scrollY += 0.1;
- scrollZ += 0.1;
- break;
- case GLFW_KEY_X:
- scrollX -= 0.1;
- scrollY -= 0.1;
- scrollZ -= 0.1;
- break;
- case GLFW_KEY_P:
- matrix[0][0] = cos(phi); matrix[0][1] = 0; matrix[0][2] = sin(phi); matrix[0][3] = 0;
- matrix[1][0] = sin(phi)*sin(teta); matrix[1][1] = cos(teta); matrix[1][2] = -cos(phi)*sin(teta); matrix[1][3] = 0;
- matrix[2][0] = sin(phi)*cos(teta); matrix[2][1] = -sin(teta); matrix[2][2] = -cos(phi)*cos(teta); matrix[2][3] = 0;
- matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
- break;
- case GLFW_KEY_O:
- matrix[0][0] = 1; matrix[0][1] = 0; matrix[0][2] = 0; matrix[0][3] = 0;
- matrix[1][0] = 0; matrix[1][1] = 1; matrix[1][2] = 0; matrix[1][3] = 0;
- matrix[2][0] = 0; matrix[2][1] = 0; matrix[2][2] = 1; matrix[2][3] = 0;
- matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
- break;
- }
- }
- }
- void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
- if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
- fill = GL_FILL;
- }
- if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
- fill = GL_LINE;
- }
- }
- int main(void) {
- if (!glfwInit()) {
- return -1;
- }
- int width = 800, height = 800;
- GLFWwindow *window = glfwCreateWindow(800, 800, "Cube", NULL, NULL);
- glfwMakeContextCurrent(window);
- glViewport(0, 0, width, height);
- glEnable(GL_DEPTH_TEST);
- glfwSetKeyCallback(window, keyCallback);
- glfwSetMouseButtonCallback(window, mouse_button_callback);
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMultMatrixf(*matrix);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- // glewInit();
- glewInit();
- init();
- glTranslatef(x, y, z);
- glRotatef(Rotate_x, 0, 1, 0);
- glRotatef(Rotate_y, 1, 0, 0);
- glRotatef(Rotate_z, 0, 0, 1);
- glScaled(scrollX, scrollY, scrollZ);
- //Draw_Tor();
- display(window);
- //Draw_small();
- glfwPollEvents();
- glfwSwapBuffers(window);
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement