Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import bindbc.opengl;
- import bindbc.glfw;
- alias stderr = std.stdio.stderr;
- const GLchar* vertexShaderSource = "#version 330 core
- layout (location = 0) in vec3 position;
- void main()
- {
- gl_Position = vec4(position.x, position.y, position.z, 1.0);
- }";
- const GLchar* fragmentShaderSource = "#version 330 core
- out vec4 color;
- void main()
- {
- color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
- }";
- void main()
- {
- writeln("OpenGL Test project.");
- // OpenGL context
- // Load GLFW
- GLFWSupport ret = loadGLFW();
- if(ret != glfwSupport)
- {
- writeln(ret);
- // Handle error. For most use cases, its reasonable to use the the error handling API in
- // bindbc-loader to retrieve error messages for logging and then abort. If necessary, it's
- // possible to determine the root cause via the return value:
- if(ret == GLFWSupport.noLibrary) {
- // GLFW shared library failed to load
- }
- else if(GLFWSupport.badLibrary) {
- // One or more symbols failed to load. The likely cause is that the
- // shared library is for a lower version than bindbc-glfw was configured
- // to load (via GLFW_31, GLFW_32 etc.)
- }
- }
- if (!glfwInit())
- {
- stderr.writeln("Impossible de charger GLFW");
- }
- //glfwSetErrorCallback(&errCb);
- glfwWindowHint(GLFW_SAMPLES, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- GLFWwindow* window = glfwCreateWindow(1280, 720, "OpenGL - GLFW", null, null);
- if (!window)
- {
- stderr.writeln("Failed to initialize window");
- glfwTerminate();
- }
- int screenWidth, screenHeight;
- glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
- glfwMakeContextCurrent(window);
- // loading OpenGL
- GLSupport retGL = loadOpenGL();
- writefln("OpenGL version: %s", retGL);
- // Define the viewport dimensions
- glViewport(0, 0, 1920, 1080);
- // Build and compile our shader program
- // Vertex shader
- GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexShader, 1, &vertexShaderSource, null);
- glCompileShader(vertexShader);
- // Check for compile time errors
- GLint success;
- GLchar[512] infoLog;
- glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(vertexShader, 512, null, infoLog.ptr);
- writefln("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s", infoLog);
- }
- // Fragment shader
- GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShader, 1, &fragmentShaderSource, null);
- glCompileShader(fragmentShader);
- // Check for compile time errors
- glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(fragmentShader, 512, null, infoLog.ptr);
- writefln("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s", infoLog);
- }
- // Link shaders
- GLuint shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- // Check for linking errors
- glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
- if (!success)
- {
- glGetProgramInfoLog(shaderProgram, 512, null, infoLog.ptr);
- writefln("ERROR::SHADER::PROGRAM::LINKING_FAILED\n%s", infoLog);
- }
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- // Set up vertex data (and buffer(s)) and attribute pointers
- GLfloat[] vertices =
- [
- -0.5f, -0.5f, 0.0f, // Left
- 0.5f, -0.5f, 0.0f, // Right
- 0.0f, 0.5f, 0.0f // Top
- ];
- GLuint VBO, VAO;
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, vertices.sizeof, vertices.ptr, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * GLfloat.sizeof, cast(GLvoid*) 0);
- glEnableVertexAttribArray(0);
- 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
- glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
- // Game loop
- while (!glfwWindowShouldClose(window))
- {
- // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
- glfwPollEvents();
- // Render
- // Clear the colorbuffer
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- // Draw our first triangle
- glUseProgram(shaderProgram);
- glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glBindVertexArray(0);
- // Swap the screen buffers
- glfwSwapBuffers(window);
- }
- // Properly de-allocate all resources once they've outlived their purpose
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- // Terminate GLFW, clearing any resources allocated by GLFW.
- glfwTerminate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement