Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cmath>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include "shader_util.h"
- void error_callback(GLint error, const GLchar* description);
- GLuint createSqrt(GLuint* vbo, GLuint* ebo, GLfloat dx, GLfloat dy);
- void createFigure(GLuint figures[4], GLuint* vbo, GLuint* ebo, GLint x, GLint y, GLfloat d[8]);
- void change_tone(GLfloat color[4], GLint op);
- int main( ) {
- glfwSetErrorCallback(error_callback);
- glfwInit( );
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(512, 512, "Tetris 1", nullptr, nullptr);
- glfwMakeContextCurrent(window);
- glewExperimental = GL_TRUE;
- GLenum status = glewInit( );
- if (status != GLEW_OK) {
- std::cerr << "Error: " << glewGetErrorString(status) << std::endl;
- return 1;
- }
- GLuint shaderProgram = linkShaderProgram("vertex_shader.glsl", "fragment_shader.glsl");
- glViewport(0, 0, 512, 512);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- GLfloat scale = 0.1;
- GLfloat I[8] = { 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 2.0f, 0.0f };
- GLfloat O[8] = { 0.0f, 0.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f, -1.0f };
- GLfloat Z[8] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 2.0f, -1.0f };
- GLfloat T[8] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, -1.0f, 0.0f };
- GLfloat L[8] = { 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, -2.0f, -1.0f, -2.0f };
- GLuint vbo, ebo;
- GLuint figures[5][4];
- createFigure(figures[0], &vbo, &ebo, -5.0f, 4.0f, I);
- createFigure(figures[1], &vbo, &ebo, -4.0f, -4.0f, O);
- createFigure(figures[2], &vbo, &ebo, 0.0f, 0.0f, Z);
- createFigure(figures[3], &vbo, &ebo, 5.0f, 4.0f, T);
- createFigure(figures[4], &vbo, &ebo, 4.0f, -4.0f, L);
- for (; !glfwWindowShouldClose(window); glfwSwapBuffers(window)) {
- glfwPollEvents( );
- glClear(GL_COLOR_BUFFER_BIT);
- GLint location = glGetUniformLocation(shaderProgram, "scale");
- glUniform1f(location, scale);
- glUseProgram(shaderProgram);
- GLfloat color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
- for (GLint i = 0; i < 5; ++i) {
- color[i % 3] = (color[i % 3] >= 0.5 ? 0.0f : 1.0f);
- for (GLint j = 0; j < 4; ++j) {
- change_tone(color, j % 2);
- GLint location_t = glGetUniformLocation(shaderProgram, "color");
- glUniform4f(location_t, color[0], color[1], color[2], color[3]);
- glBindVertexArray(figures[i][j]);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (GLvoid*)0);
- glBindVertexArray(0);
- }
- }
- }
- glDeleteBuffers(1, &vbo);
- glDeleteBuffers(1, &ebo);
- for (GLint i = 0; i < 5; ++i) {
- glDeleteVertexArrays(4, figures[i]);
- }
- glfwTerminate( );
- }
- void error_callback(GLint error, const GLchar* description) {
- std::cerr << "Error " << error << ": " << description << std::endl;
- std::cin.get();
- exit(1);
- }
- GLuint createSqrt(GLuint* vbo, GLuint* ebo, GLfloat dx, GLfloat dy) {
- GLfloat vertex[] = {
- -0.5f, -0.5f,
- 0.5f, -0.5f,
- -0.5f, 0.5f,
- 0.5f, 0.5f,
- };
- for (GLint i = 0; i < 4; ++i) {
- vertex[2 * i + 0] += dx;
- vertex[2 * i + 1] += dy;
- }
- GLuint elements[] = {
- 0, 1, 2,
- 1, 3, 2,
- };
- GLuint vao;
- glGenVertexArrays(1, &vao);
- glGenBuffers(1, vbo);
- glGenBuffers(1, ebo);
- glBindVertexArray(vao);
- glBindBuffer(GL_ARRAY_BUFFER, *vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *ebo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- return vao;
- }
- void createFigure(GLuint figures[4], GLuint* vbo, GLuint* ebo, GLint x, GLint y, GLfloat d[8]) {
- for (GLint i = 0; i < 4; ++i) {
- figures[i] = createSqrt(vbo, ebo, d[i * 2 + 0] + x, d[i * 2 + 1] + y);
- }
- }
- void change_tone(GLfloat color[4], GLint op) {
- for (GLint i = 0; i < 3; ++i) {
- color[i] = (color[i] >= 0.5f ? 1.0f - (op ? 0.0f : 0.2f) : 0.0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement