#include #include #include #define GLSL(src) "#version 150 core\n" #src void errfunc(int code, const char* str) { printf("GLFW error %d: %s\n", code, str); } int main() { // -------------------------------- INIT ------------------------------- // // Init GLFW glfwSetErrorCallback(errfunc); if (glfwInit() != GL_TRUE) { fprintf(stderr, "Failed to initialize GLFW\n"); return -1; } // Create a rendering window with OpenGL 3.2 context glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", NULL, NULL); glfwMakeContextCurrent(window); // Init GLEW glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { fprintf(stderr, "Failed to initialize GLEW\n"); return -1; } // ----------------------------- RESOURCES ----------------------------- // // Create Vertex Array Object GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); // Create a Vertex Buffer Object and copy the vertex data to it GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); float vertices[] = { 0.0f, 0.5f, // Vertex 1 (X, Y) 0.5f, -0.5f, // Vertex 2 (X, Y) -0.5f, -0.5f // Vertex 3 (X, Y) }; glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // Create and compile the vertex shader const char* vertexSource = GLSL( in vec2 position; void main() { gl_Position = vec4(position, 0.0, 1.0); } ); GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexSource, NULL); glCompileShader(vertexShader); // Create and compile the fragment shader const char* fragmentSource = GLSL( out vec4 outColor; void main() { outColor = vec4(1.0f, 1.0f, 1.0f, 1.0f); } ); GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentSource, NULL); glCompileShader(fragmentShader); // Create and compile the fragment shader const char* fragmentSource2 = "#version 150 core\n" "#extension GL_ARB_shader_image_load_store : enable\n" "layout(early_fragment_tests) in;\n" "\n" "out vec4 outColor;\n" "\n" "void main() {\n" " outColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n" "}\n"; GLuint fragmentShader2 = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader2, 1, &fragmentSource2, NULL); glCompileShader(fragmentShader2); // Link the vertex and fragment shader into a shader program GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glBindFragDataLocation(shaderProgram, 0, "outColor"); glLinkProgram(shaderProgram); GLuint shaderProgram2 = glCreateProgram(); glAttachShader(shaderProgram2, vertexShader); glAttachShader(shaderProgram2, fragmentShader2); glBindFragDataLocation(shaderProgram2, 0, "outColor"); glLinkProgram(shaderProgram2); // ---------------------------- RENDERING ------------------------------ // //while(false && !glfwWindowShouldClose(window)) { // Clear the screen to black glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); for (int i = 0; i < 100000; ++i) { glUseProgram(shaderProgram); // Specify the layout of the vertex data GLint posAttrib = glGetAttribLocation(shaderProgram, "position"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); // Draw a triangle from the 3 vertices glDrawArrays(GL_TRIANGLES, 0, 3); glUseProgram(shaderProgram2); // Specify the layout of the vertex data posAttrib = glGetAttribLocation(shaderProgram2, "position"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); // Draw a triangle from the 3 vertices glDrawArrays(GL_TRIANGLES, 0, 3); } // Swap buffers and poll window events glfwSwapBuffers(window); glfwPollEvents(); } // ---------------------------- CLEARING ------------------------------ // // Delete allocated resources glDeleteProgram(shaderProgram); glDeleteProgram(shaderProgram2); glDeleteShader(fragmentShader); glDeleteShader(fragmentShader2); glDeleteShader(vertexShader); glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); // ---------------------------- TERMINATE ----------------------------- // // Terminate GLFW glfwTerminate(); return 0; }