SHOW:
|
|
- or go back to the newest paste.
| 1 | //Ported to GLFW by Mallot1 | |
| 2 | ||
| 3 | ---------------------------------------------------------- | |
| 4 | ||
| 5 | // Link statically with GLEW | |
| 6 | #define GLEW_STATIC | |
| 7 | ||
| 8 | // Headers | |
| 9 | #include <GL/glew.h> | |
| 10 | #include <GLFW/glfw3.h> | |
| 11 | #include <iostream> | |
| 12 | ||
| 13 | // Shader sources | |
| 14 | const GLchar* vertexSource = | |
| 15 | "#version 150 core\n" | |
| 16 | "in vec2 position;" | |
| 17 | "in vec3 color;" | |
| 18 | "out vec3 Color;" | |
| 19 | "void main() {"
| |
| 20 | " Color = color;" | |
| 21 | " gl_Position = vec4(position, 0.0, 1.0);" | |
| 22 | "}"; | |
| 23 | const GLchar* fragmentSource = | |
| 24 | "#version 150 core\n" | |
| 25 | "in vec3 Color;" | |
| 26 | "out vec4 outColor;" | |
| 27 | "void main() {"
| |
| 28 | " outColor = vec4(Color, 1.0);" | |
| 29 | "}"; | |
| 30 | ||
| 31 | int main(int argc, char* argv[]) | |
| 32 | {
| |
| 33 | glfwInit(); | |
| 34 | GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL", NULL, NULL); | |
| 35 | glfwMakeContextCurrent(window); | |
| 36 | ||
| 37 | // Initialize GLEW | |
| 38 | glewExperimental = GL_TRUE; | |
| 39 | glewInit(); | |
| 40 | ||
| 41 | // Create Vertex Array Object | |
| 42 | GLuint vao; | |
| 43 | glGenVertexArrays(1, &vao); | |
| 44 | glBindVertexArray(vao); | |
| 45 | ||
| 46 | // Create a Vertex Buffer Object and copy the vertex data to it | |
| 47 | GLuint vbo; | |
| 48 | glGenBuffers(1, &vbo); | |
| 49 | ||
| 50 | GLfloat vertices[] = {
| |
| 51 | -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Top-left | |
| 52 | 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top-right | |
| 53 | 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right | |
| 54 | -0.5f, -0.5f, 1.0f, 1.0f, 1.0f // Bottom-left | |
| 55 | }; | |
| 56 | ||
| 57 | glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
| 58 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | |
| 59 | ||
| 60 | // Create an element array | |
| 61 | GLuint ebo; | |
| 62 | glGenBuffers(1, &ebo); | |
| 63 | ||
| 64 | GLuint elements[] = {
| |
| 65 | 0, 1, 2, | |
| 66 | 2, 3, 0 | |
| 67 | }; | |
| 68 | ||
| 69 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); | |
| 70 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); | |
| 71 | ||
| 72 | // Create and compile the vertex shader | |
| 73 | GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); | |
| 74 | glShaderSource(vertexShader, 1, &vertexSource, NULL); | |
| 75 | glCompileShader(vertexShader); | |
| 76 | ||
| 77 | // Create and compile the fragment shader | |
| 78 | GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | |
| 79 | glShaderSource(fragmentShader, 1, &fragmentSource, NULL); | |
| 80 | glCompileShader(fragmentShader); | |
| 81 | ||
| 82 | // Link the vertex and fragment shader into a shader program | |
| 83 | GLuint shaderProgram = glCreateProgram(); | |
| 84 | glAttachShader(shaderProgram, vertexShader); | |
| 85 | glAttachShader(shaderProgram, fragmentShader); | |
| 86 | glBindFragDataLocation(shaderProgram, 0, "outColor"); | |
| 87 | glLinkProgram(shaderProgram); | |
| 88 | glUseProgram(shaderProgram); | |
| 89 | ||
| 90 | // Specify the layout of the vertex data | |
| 91 | GLint posAttrib = glGetAttribLocation(shaderProgram, "position"); | |
| 92 | glEnableVertexAttribArray(posAttrib); | |
| 93 | glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0); | |
| 94 | ||
| 95 | GLint colAttrib = glGetAttribLocation(shaderProgram, "color"); | |
| 96 | glEnableVertexAttribArray(colAttrib); | |
| 97 | glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat))); | |
| 98 | ||
| 99 | while(!glfwWindowShouldClose(window)) | |
| 100 | {
| |
| 101 | if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) | |
| 102 | glfwSetWindowShouldClose(window, GL_TRUE); | |
| 103 | ||
| 104 | glfwSwapBuffers(window); | |
| 105 | glfwPollEvents(); | |
| 106 | ||
| 107 | ||
| 108 | // Clear the screen to black | |
| 109 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
| 110 | glClear(GL_COLOR_BUFFER_BIT); | |
| 111 | ||
| 112 | // Draw a rectangle from the 2 triangles using 6 indices | |
| 113 | glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); | |
| 114 | ||
| 115 | // Swap buffers | |
| 116 | ||
| 117 | } | |
| 118 | glDeleteProgram(shaderProgram); | |
| 119 | glDeleteShader(fragmentShader); | |
| 120 | glDeleteShader(vertexShader); | |
| 121 | ||
| 122 | glDeleteBuffers(1, &ebo); | |
| 123 | glDeleteBuffers(1, &vbo); | |
| 124 | ||
| 125 | glDeleteVertexArrays(1, &vao); | |
| 126 | } |