SHARE
TWEET

Untitled

a guest Aug 21st, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #define GL_GLEXT_PROTOTYPES
  3. #include <GLFW/glfw3.h>
  4.  
  5. #define GL_CHECK_ERROR()                                                \
  6.     do {                                                                \
  7.         GLint error = glGetError();                                     \
  8.         if (error != GL_NO_ERROR)                                       \
  9.             dprintf(2, "GL error %#x at line %d.\n", error, __LINE__ - 1); \
  10.     } while (0)
  11.  
  12. static const char* vertexShaderSource =
  13. "#version 410 core\n"
  14. "in vec2 vertex;"
  15. "in vec2 texcoords;"
  16. "invariant gl_Position;"
  17. "out vec2 Texcoords;"
  18. "void main()"
  19. "{"
  20. "    gl_Position = vec4(vertex, 0.0, 1.0);"
  21. "    Texcoords = texcoords;"
  22. "}";
  23.  
  24. static const char* fragmentShaderSource =
  25. "#version 410 core\n"
  26. "in vec2 Texcoords;"
  27. "uniform sampler2D tex;"
  28. "out vec4 color;"
  29. "void main()"
  30. "{"
  31. //"    color = vec4(vec2(textureSize(tex, 0)) / 2.0f, 0.0f, 1.0f);"
  32. "    color = texture(tex, Texcoords);"
  33. //"    color = vec4(texture(tex, Texcoords).xyz, 1.0);"
  34. //"    color = vec4(Texcoords, 1.0, 1.0);"
  35. "}";
  36.  
  37. void print_glfw_error(int errcode, const char* str)
  38. {
  39.     dprintf(2, "[GLFW] Error %#x: %s\n", errcode, str);
  40. }
  41.  
  42. int main()
  43. {
  44.     const GLfloat vertices[] = {
  45.         /* Vertices */      /* Texture coords */
  46.         -1.0f, -1.0f,       0.0f, 0.0f,
  47.         1.0f, -1.0f,        1.0f, 0.0f,
  48.         1.0f, 1.0f,         1.0f, 1.0f,
  49.         -1.0f, 1.0f,        0.0f, 1.0f
  50.     };
  51.     const GLubyte pixels[] = {
  52.         255, 255, 255, 255,     0, 0, 0, 255,
  53.         0, 0, 0, 255,           255, 255, 255, 255,
  54.     };
  55.     char buffer[65536] = {0};
  56.  
  57.     glfwSetErrorCallback(print_glfw_error);
  58.     glfwInit();
  59.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  60.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
  61.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  62.     GLFWwindow* window = glfwCreateWindow(800, 800, "GLFW", NULL, NULL);
  63.     glfwMakeContextCurrent(window);
  64.  
  65.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  66.     GL_CHECK_ERROR();
  67.     glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  68.     GL_CHECK_ERROR();
  69.     glCompileShader(vertexShader);
  70.     GL_CHECK_ERROR();
  71.     glGetShaderInfoLog(vertexShader, sizeof(buffer), NULL, buffer);
  72.     GL_CHECK_ERROR();
  73.     dprintf(2, "Vertex shader log:\n%s", buffer);
  74.  
  75.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  76.     GL_CHECK_ERROR();
  77.     glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  78.     GL_CHECK_ERROR();
  79.     glCompileShader(fragmentShader);
  80.     GL_CHECK_ERROR();
  81.     glGetShaderInfoLog(fragmentShader, sizeof(buffer), NULL, buffer);
  82.     GL_CHECK_ERROR();
  83.     dprintf(2, "Fragment shader log:\n%s", buffer);
  84.  
  85.     GLuint shaderProgram = glCreateProgram();
  86.     GL_CHECK_ERROR();
  87.     glAttachShader(shaderProgram, vertexShader);
  88.     GL_CHECK_ERROR();
  89.     glAttachShader(shaderProgram, fragmentShader);
  90.     GL_CHECK_ERROR();
  91.     glLinkProgram(shaderProgram);
  92.     GL_CHECK_ERROR();
  93.     glGetProgramInfoLog(shaderProgram, sizeof(buffer), NULL, buffer);
  94.     GL_CHECK_ERROR();
  95.     dprintf(2, "Shader program log:\n%s", buffer);
  96.  
  97.     GLuint vertices_location = glGetAttribLocation(shaderProgram, "vertex");
  98.     GL_CHECK_ERROR();
  99.     GLuint texcoords_location = glGetAttribLocation(shaderProgram, "texcoords");
  100.     GL_CHECK_ERROR();
  101.     GLuint texture_location = glGetUniformLocation(shaderProgram, "tex");
  102.     GL_CHECK_ERROR();
  103.     GLuint vba;
  104.     GLuint vertices_buffer;
  105.     GLuint texture_buffer;
  106.     GLuint texture;
  107.  
  108.     glGenVertexArrays(1, &vba);
  109.     GL_CHECK_ERROR();
  110.     glBindVertexArray(vba);
  111.     GL_CHECK_ERROR();
  112.     glGenBuffers(1, &vertices_buffer);
  113.     GL_CHECK_ERROR();
  114.  
  115.     glGenTextures(1, &texture);
  116.     GL_CHECK_ERROR();
  117.     glActiveTexture(GL_TEXTURE0);
  118.     GL_CHECK_ERROR();
  119.     glBindTexture(GL_TEXTURE_2D, texture);
  120.     GL_CHECK_ERROR();
  121.     glGenBuffers(1, &texture_buffer);
  122.     GL_CHECK_ERROR();
  123.     glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture_buffer);
  124.     GL_CHECK_ERROR();
  125.     glBufferData(GL_PIXEL_UNPACK_BUFFER, sizeof(pixels), pixels, GL_STATIC_DRAW);
  126.     GL_CHECK_ERROR();
  127.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)0);
  128.     GL_CHECK_ERROR();
  129.  
  130.     while (!glfwWindowShouldClose(window))
  131.     {
  132.         glClear(GL_COLOR_BUFFER_BIT);
  133.         GL_CHECK_ERROR();
  134.         glUseProgram(shaderProgram);
  135.         GL_CHECK_ERROR();
  136.         glBindVertexArray(vba);
  137.         GL_CHECK_ERROR();
  138.         glBindBuffer(GL_ARRAY_BUFFER, vertices_buffer);
  139.         GL_CHECK_ERROR();
  140.         glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
  141.         GL_CHECK_ERROR();
  142.         glVertexAttribPointer(vertices_location, 2, GL_FLOAT, GL_FALSE, 16, (GLvoid*)0);
  143.         GL_CHECK_ERROR();
  144.         glVertexAttribPointer(texcoords_location, 2, GL_FLOAT, GL_FALSE, 16, (GLvoid*)8);
  145.         GL_CHECK_ERROR();
  146.         glUniform1i(texture_location, 0);
  147.         GL_CHECK_ERROR();
  148.         glEnableVertexAttribArray(vertices_location);
  149.         GL_CHECK_ERROR();
  150.         glEnableVertexAttribArray(texcoords_location);
  151.         GL_CHECK_ERROR();
  152.         glActiveTexture(GL_TEXTURE0);
  153.         GL_CHECK_ERROR();
  154.         glBindTexture(GL_TEXTURE_2D, texture);
  155.         GL_CHECK_ERROR();
  156.         glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  157.         GL_CHECK_ERROR();
  158.         glDisableVertexAttribArray(texcoords_location);
  159.         GL_CHECK_ERROR();
  160.         glDisableVertexAttribArray(vertices_location);
  161.         GL_CHECK_ERROR();
  162.         glfwSwapBuffers(window);
  163.         glfwWaitEvents();
  164.     }
  165.     glfwTerminate();
  166.     return 0;
  167. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top