Advertisement
Guest User

main.c

a guest
Apr 16th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.93 KB | None | 0 0
  1. #include <math.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <SOIL/SOIL.h>
  6. #define GLEW STATIC
  7. #include <GL/glew.h>
  8. #include <GLFW/glfw3.h>
  9.  
  10. #include "functions.h"
  11.  
  12.  
  13. // Constansts and variables
  14. const GLuint WIDTH = 800, HEIGHT = 600;
  15. const GLchar *windowName = "OpenGLAAAAA";
  16. GLFWwindow *window;
  17. GLuint shaderProgram;
  18.  
  19. int main() {
  20.  
  21.     // Loading shaders
  22.     // Vertex shader
  23.     char *content001;
  24.     textFileRead("shaders/vertexShader.vert", &content001);
  25.     const GLchar *vertexShaderSourceExt = content001;
  26.     // Fragment shader
  27.     char *content002;
  28.     textFileRead("shaders/fragmentShader.frag", &content002);
  29.     const GLchar *fragmentShaderSourceExt = content002;
  30.  
  31.  
  32.     // Textures
  33.     GLuint texture;
  34.     glGenTextures(1, &texture);
  35.     glBindTexture(GL_TEXTURE_2D, texture);
  36.     // Set texture parameters
  37.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  38.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  39.     // Set texture filtering
  40.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  41.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  42.     // Loading texture
  43.     int width, height;
  44.     unsigned char *image = 0;
  45.     image = SOIL_load_image("res/textures/container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
  46.     if (image == 0) {
  47.         printf("Can not open file res/textures/container.jpg\n");
  48.         return -1;
  49.     }
  50.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  51.     //glGenerateMipmap(GL_TEXTURE_2D);
  52.     SOIL_free_image_data(image);
  53.     glBindTexture(GL_TEXTURE_2D, texture);
  54.  
  55.  
  56.     // Initialization
  57.     if (aaaInit(&window, WIDTH, HEIGHT, &windowName) != 0){
  58.         return -1;
  59.     }
  60.     aaaShaderCompile(&vertexShaderSourceExt, &fragmentShaderSourceExt, &shaderProgram);
  61.  
  62.  
  63.     // Set function for key callback
  64.     glfwSetKeyCallback(window, key_callback);
  65.  
  66.  
  67.     // Set up vertex data (and buffer(s)) and attribute pointers
  68.     GLfloat vertices[] = {
  69.     //  Positions             Colors              Texture Coords
  70.          0.0f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // Top
  71.         -0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // Bottom Left
  72.          0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 1.0f  // Bottom Right
  73.     };
  74.     GLuint indices[] = {  // Note that we start from 0!
  75.         0, 1, 2,  // First Triangle
  76.     };
  77.     GLuint VBO, VAO, EBO;
  78.     glGenVertexArrays(1, &VAO);
  79.     glGenBuffers(1, &VBO);
  80.     glGenBuffers(1, &EBO);
  81.  
  82.  
  83.     // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
  84.     glBindVertexArray(VAO);
  85.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  86.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  87.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  88.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  89.     // Pisistion attribute
  90.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
  91.     glEnableVertexAttribArray(0);
  92.     // Color attribute
  93.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  94.     glEnableVertexAttribArray(1);
  95.     // Texture cut coords attribute
  96.     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
  97.     glEnableVertexAttribArray(2);
  98.     glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
  99.     glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
  100.  
  101.  
  102.     // Uncommenting this call will result in wireframe polygons.
  103.     //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  104.  
  105.  
  106. // window main cycle
  107.  
  108.     while(!glfwWindowShouldClose(window))
  109.     {
  110.         glfwPollEvents();
  111.  
  112.     // Render commands here
  113.  
  114.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  115.         glClear(GL_COLOR_BUFFER_BIT);
  116.  
  117.         GLfloat timeValue = glfwGetTime();
  118.         GLfloat redValue = (sin(timeValue*8) / 2) + 0.5;
  119.         GLfloat greenValue = (cos(timeValue*8) / 2) + 0.5;
  120.  
  121.         GLint vertexPosLocation = glGetUniformLocation(shaderProgram, "ourPos2");
  122.         glUseProgram(shaderProgram);
  123.         glActiveTexture(GL_TEXTURE0);
  124.         glBindTexture(GL_TEXTURE_2D, texture);
  125.         glUniform1i(glGetUniformLocation(shaderProgram, "ourTexture"), 0);
  126.         glUniform4f(vertexPosLocation, redValue, greenValue, 1.0f, 1.0f);
  127.         glBindVertexArray(VAO);
  128.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  129.         glBindVertexArray(0);
  130.  
  131.         glfwSwapBuffers(window);    // эта хуйня нужна чтобы всё не зависло к ебеням!!
  132.     }
  133.     glDeleteVertexArrays(1, &VAO);
  134.     glDeleteBuffers(1, &VBO);
  135.     glDeleteBuffers(1, &EBO);
  136.     glfwTerminate();
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement