1.配置
2.测试代码:
2.1 在黑板色的窗口上画一个三角形
1 #include <iostream> 2 3 // GLEW 4 #define GLEW_STATIC 5 #include <GL/glew.h> 6 7 // GLFW 8 #include <GLFW/glfw3.h> 9 10 11 // Function prototypes 12 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); 13 14 // Window dimensions 15 const GLuint WIDTH = 800, HEIGHT = 600; 16 17 // Shaders 18 const GLchar* vertexShaderSource = "#version 330 core\n" 19 "layout (location = 0) in vec3 position;\n" 20 "void main()\n" 21 "{\n" 22 "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n" 23 "}\0"; 24 const GLchar* fragmentShaderSource = "#version 330 core\n" 25 "out vec4 color;\n" 26 "void main()\n" 27 "{\n" 28 "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" 29 "}\n\0"; 30 31 // The MAIN function, from here we start the application and run the game loop 32 int main() 33 { 34 // Init GLFW 35 glfwInit(); 36 // Set all the required options for GLFW 37 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 38 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 39 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 40 glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); 41 42 // Create a GLFWwindow object that we can use for GLFW's functions 43 GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr); 44 glfwMakeContextCurrent(window); 45 46 // Set the required callback functions 47 glfwSetKeyCallback(window, key_callback); 48 49 // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions 50 glewExperimental = GL_TRUE; 51 // Initialize GLEW to setup the OpenGL Function pointers 52 glewInit(); 53 54 // Define the viewport dimensions 55 int width, height; 56 glfwGetFramebufferSize(window, &width, &height); 57 glViewport(0, 0, width, height); 58 59 60 // Build and compile our shader program 61 // Vertex shader 62 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); 63 glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); 64 glCompileShader(vertexShader); 65 // Check for compile time errors 66 GLint success; 67 GLchar infoLog[512]; 68 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); 69 if (!success) 70 { 71 glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); 72 std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; 73 } 74 // Fragment shader 75 GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); 76 glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); 77 glCompileShader(fragmentShader); 78 // Check for compile time errors 79 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); 80 if (!success) 81 { 82 glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); 83 std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; 84 } 85 // Link shaders 86 GLuint shaderProgram = glCreateProgram(); 87 glAttachShader(shaderProgram, vertexShader); 88 glAttachShader(shaderProgram, fragmentShader); 89 glLinkProgram(shaderProgram); 90 // Check for linking errors 91 glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); 92 if (!success) { 93 glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); 94 std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; 95 } 96 glDeleteShader(vertexShader); 97 glDeleteShader(fragmentShader); 98 99 100 // Set up vertex data (and buffer(s)) and attribute pointers 101 GLfloat vertices[] = { 102 -0.5f, -0.5f, 0.0f, // Left 103 0.5f, -0.5f, 0.0f, // Right 104 0.0f, 0.5f, 0.0f // Top 105 }; 106 GLuint VBO, VAO; 107 glGenVertexArrays(1, &VAO); 108 glGenBuffers(1, &VBO); 109 // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s). 110 glBindVertexArray(VAO); 111 112 glBindBuffer(GL_ARRAY_BUFFER, VBO); 113 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 114 115 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); 116 glEnableVertexAttribArray(0); 117 118 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 119 120 glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs) 121 122 // Game loop 123 while (!glfwWindowShouldClose(window)) 124 { 125 // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions 126 glfwPollEvents(); 127 128 // Render 129 // Clear the colorbuffer 130 glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 131 glClear(GL_COLOR_BUFFER_BIT); 132 133 // Draw our first triangle 134 glUseProgram(shaderProgram); 135 glBindVertexArray(VAO); 136 glDrawArrays(GL_TRIANGLES, 0, 3); 137 glBindVertexArray(0); 138 139 // Swap the screen buffers 140 glfwSwapBuffers(window); 141 } 142 // Properly de-allocate all resources once they've outlived their purpose 143 glDeleteVertexArrays(1, &VAO); 144 glDeleteBuffers(1, &VBO); 145 // Terminate GLFW, clearing any resources allocated by GLFW. 146 glfwTerminate(); 147 return 0; 148 } 149 150 // Is called whenever a key is pressed/released via GLFW 151 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) 152 { 153 if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) 154 glfwSetWindowShouldClose(window, GL_TRUE); 155 }View Code
3.ps: glew32s.lib 和 glew32.dll 区别:
I haven't looked at any of the documentation, but I believe glew32s.lib is
the static version and glew32.lib is the dynamically linked DLL version --
the static version links the lib into the .exe, while the dynamic version
requires the glew32.dll at runtime. You can use #define GLEW_STATIC before
#include <GL/glew.h> to use the static version