Advertisement
Guest User

OpenGL_test

a guest
Jan 25th, 2020
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 5.13 KB | None | 0 0
  1. import std.stdio;
  2.  
  3. import bindbc.opengl;
  4. import bindbc.glfw;
  5.  
  6. alias stderr = std.stdio.stderr;
  7.  
  8. const GLchar* vertexShaderSource = "#version 330 core
  9. layout (location = 0) in vec3 position;
  10. void main()
  11. {
  12. gl_Position = vec4(position.x, position.y, position.z, 1.0);
  13. }";
  14. const GLchar* fragmentShaderSource = "#version 330 core
  15. out vec4 color;
  16. void main()
  17. {
  18. color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
  19. }";
  20.  
  21. void main()
  22. {
  23.     writeln("OpenGL Test project.");
  24.    
  25.     // OpenGL context
  26.     // Load GLFW
  27.     GLFWSupport ret = loadGLFW();
  28.     if(ret != glfwSupport)
  29.     {
  30.         writeln(ret);
  31.         // Handle error. For most use cases, its reasonable to use the the error handling API in
  32.         // bindbc-loader to retrieve error messages for logging and then abort. If necessary, it's
  33.         // possible to determine the root cause via the return value:
  34.  
  35.         if(ret == GLFWSupport.noLibrary) {
  36.             // GLFW shared library failed to load
  37.         }
  38.         else if(GLFWSupport.badLibrary) {
  39.             // One or more symbols failed to load. The likely cause is that the
  40.             // shared library is for a lower version than bindbc-glfw was configured
  41.             // to load (via GLFW_31, GLFW_32 etc.)
  42.         }
  43.     }
  44.  
  45.     if (!glfwInit())
  46.     {
  47.         stderr.writeln("Impossible de charger GLFW");
  48.     }
  49.     //glfwSetErrorCallback(&errCb);
  50.  
  51.     glfwWindowHint(GLFW_SAMPLES, 4);
  52.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  53.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  54.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  55.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  56.    
  57.     GLFWwindow* window = glfwCreateWindow(1280, 720, "OpenGL - GLFW", null, null);
  58.     if (!window)
  59.     {
  60.         stderr.writeln("Failed to initialize window");
  61.         glfwTerminate();
  62.     }
  63.    
  64.     int screenWidth, screenHeight;
  65.     glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
  66.    
  67.     glfwMakeContextCurrent(window);
  68.    
  69.    
  70.  
  71.     // loading OpenGL
  72.     GLSupport retGL = loadOpenGL();
  73.     writefln("OpenGL version: %s", retGL);
  74.    
  75.    
  76.    
  77.     // Define the viewport dimensions
  78.     glViewport(0, 0, 1920, 1080);
  79.  
  80.  
  81.     // Build and compile our shader program
  82.     // Vertex shader
  83.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  84.     glShaderSource(vertexShader, 1, &vertexShaderSource, null);
  85.     glCompileShader(vertexShader);
  86.  
  87.     // Check for compile time errors
  88.     GLint success;
  89.     GLchar[512] infoLog;
  90.  
  91.     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  92.     if (!success)
  93.     {
  94.         glGetShaderInfoLog(vertexShader, 512, null, infoLog.ptr);
  95.         writefln("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s", infoLog);
  96.     }
  97.  
  98.     // Fragment shader
  99.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  100.     glShaderSource(fragmentShader, 1, &fragmentShaderSource, null);
  101.     glCompileShader(fragmentShader);
  102.  
  103.     // Check for compile time errors
  104.     glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  105.  
  106.     if (!success)
  107.     {
  108.         glGetShaderInfoLog(fragmentShader, 512, null, infoLog.ptr);
  109.         writefln("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s", infoLog);
  110.     }
  111.  
  112.     // Link shaders
  113.     GLuint shaderProgram = glCreateProgram();
  114.     glAttachShader(shaderProgram, vertexShader);
  115.     glAttachShader(shaderProgram, fragmentShader);
  116.     glLinkProgram(shaderProgram);
  117.  
  118.     // Check for linking errors
  119.     glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  120.  
  121.     if (!success)
  122.     {
  123.         glGetProgramInfoLog(shaderProgram, 512, null, infoLog.ptr);
  124.         writefln("ERROR::SHADER::PROGRAM::LINKING_FAILED\n%s", infoLog);
  125.     }
  126.  
  127.     glDeleteShader(vertexShader);
  128.     glDeleteShader(fragmentShader);
  129.  
  130.  
  131.     // Set up vertex data (and buffer(s)) and attribute pointers
  132.     GLfloat[] vertices =
  133.     [
  134.         -0.5f, -0.5f, 0.0f, // Left
  135.         0.5f, -0.5f, 0.0f, // Right
  136.         0.0f,  0.5f, 0.0f  // Top
  137.     ];
  138.  
  139.     GLuint VBO, VAO;
  140.     glGenVertexArrays(1, &VAO);
  141.     glGenBuffers(1, &VBO);
  142.     // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
  143.     glBindVertexArray(VAO);
  144.  
  145.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  146.     glBufferData(GL_ARRAY_BUFFER, vertices.sizeof, vertices.ptr, GL_STATIC_DRAW);
  147.  
  148.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * GLfloat.sizeof, cast(GLvoid*) 0);
  149.     glEnableVertexAttribArray(0);
  150.  
  151.     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
  152.  
  153.     glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
  154.  
  155.     // Game loop
  156.     while (!glfwWindowShouldClose(window))
  157.     {
  158.         // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
  159.         glfwPollEvents();
  160.        
  161.         // Render
  162.         // Clear the colorbuffer
  163.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  164.         glClear(GL_COLOR_BUFFER_BIT);
  165.        
  166.         // Draw our first triangle
  167.         glUseProgram(shaderProgram);
  168.         glBindVertexArray(VAO);
  169.         glDrawArrays(GL_TRIANGLES, 0, 3);
  170.         glBindVertexArray(0);
  171.        
  172.         // Swap the screen buffers
  173.         glfwSwapBuffers(window);
  174.     }
  175.  
  176.     // Properly de-allocate all resources once they've outlived their purpose
  177.     glDeleteVertexArrays(1, &VAO);
  178.     glDeleteBuffers(1, &VBO);
  179.  
  180.     // Terminate GLFW, clearing any resources allocated by GLFW.
  181.     glfwTerminate();
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement