Advertisement
libchm

GL test

Sep 19th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <stdio.h>
  2. //GLEW
  3. #define GLEW_STATIC
  4. #include <GL\glew.h>
  5. //GLFW
  6. #include <glfw3.h>
  7.  
  8. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
  9.  
  10. // WINDOW DIMENSIONS
  11. const GLuint WIDTH = 1280, HEIGHT = 720;
  12.  
  13. // SHADERS
  14. const GLchar* vertexShaderSource = "#version 330 core\n"
  15. "layout (location = 0) in vec3 position;\n"
  16. "void main()\n"
  17. "{\n"
  18. "gl_Position = vec4(position.x, position.y, position.z 1.0);\n"
  19. "}\0";
  20. const GLchar* fragmentShaderSource = "#version 330 core\n"
  21. "out vec4 color;\n"
  22. "void main()\n"
  23. "{\n"
  24. "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
  25. "}\0";
  26.  
  27. int main(void)
  28. {
  29.     // INIT CODE
  30.     glfwInit();
  31.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  32.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  33.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  34.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  35.  
  36.     GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearningOpenGL", nullptr, nullptr);
  37.     if (window == nullptr) {
  38.         printf("Failed to create GLFW window\n");
  39.         glfwTerminate();
  40.         return -1;
  41.     }
  42.     glfwMakeContextCurrent(window);
  43.  
  44.     glfwSetKeyCallback(window, key_callback);
  45.  
  46.     glewExperimental = GL_TRUE;
  47.     if (glewInit() != GLEW_OK) {
  48.         printf("Failed to init GLEW\n");
  49.         return -1;
  50.     }
  51.  
  52.     int width, height;
  53.     glfwGetFramebufferSize(window, &width, &height);  
  54.     glViewport(0, 0, width, height);
  55.  
  56.     // SETUP RENDER SETTINGS
  57.     GLfloat vertices[] = {
  58.         -0.5f, -0.5f, 0.0f,
  59.          0.5f, -0.5f, 0.0f,
  60.          0.0f,  0.5f, 0.0f
  61.     };
  62.  
  63.     GLuint VBO;
  64.     glGenBuffers(1, &VBO);
  65.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  66.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  67.  
  68.     // SHADERS!!!!
  69.  
  70.     // VERTEX SHADER
  71.     GLuint vertexShader;
  72.     vertexShader = glCreateShader(GL_VERTEX_SHADER);
  73.     glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  74.     glCompileShader(vertexShader);
  75.     GLint success;
  76.     GLchar infoLog[512];
  77.     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  78.     if (!success) {
  79.         glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  80.         printf("ERROR SHADER VERTEX COMPILATION_FAILED\n%s", infoLog);
  81.     }
  82.  
  83.     // FRAGMENT SHADER
  84.     GLuint fragmentShader;
  85.     fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  86.     glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  87.     glCompileShader(fragmentShader);
  88.     glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  89.     if (!success) {
  90.         glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
  91.         printf("ERROR SHADER FRAGMENT COMPILATION_FAILED\n%s", infoLog);
  92.     }
  93.  
  94.     // SHADER LINKING IN SHADER PROGRAM
  95.     GLuint shaderProgram;
  96.     shaderProgram = glCreateProgram();
  97.     glAttachShader(shaderProgram, vertexShader);
  98.     glAttachShader(shaderProgram, fragmentShader);
  99.     glLinkProgram(shaderProgram);
  100.     glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  101.     if (!success) {
  102.         glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  103.         printf("ERROR SHADER PROGRAM LINKING_FAILED\n%s", infoLog);
  104.     }
  105.  
  106.     glDeleteShader(vertexShader);
  107.     glDeleteShader(fragmentShader);
  108.  
  109.     glUseProgram(shaderProgram);
  110.  
  111.  
  112.     // MAIN GAME LOOP
  113.     while (!glfwWindowShouldClose(window)) {
  114.         glfwPollEvents();
  115.  
  116.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  117.         glClear(GL_COLOR_BUFFER_BIT);
  118.  
  119.  
  120.  
  121.  
  122.         glfwSwapBuffers(window);
  123.     }
  124.  
  125.     glfwTerminate();
  126.     return 0;
  127. }
  128.  
  129. // KAY CALLBACK FUNCTIONS
  130. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  131. {
  132.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
  133.         glfwSetWindowShouldClose(window, GL_TRUE);
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement