├── .gitignore ├── Drawing_primitives ├── ex_4 │ ├── ex_4.cpp │ └── shaders │ │ ├── frag.shader │ │ └── vert.shader ├── ex_5 │ ├── ex_5.cpp │ └── shaders │ │ ├── frag.shader │ │ └── vert.shader ├── ex_6 │ ├── ex_6.cpp │ └── shaders │ │ ├── frag.shader │ │ └── vert.shader ├── ex_7 │ ├── ex_7.cpp │ └── shaders │ │ ├── frag.shader │ │ └── vert.shader ├── ex_8 │ ├── ex_8.cpp │ └── shaders │ │ ├── frag.shader │ │ └── vert.shader └── ex_9 │ ├── ex_9.cpp │ └── shaders │ ├── frag.shader │ └── vert.shader ├── Getting_Started ├── ex_0.cpp ├── ex_1.cpp ├── ex_2.cpp ├── ex_3.cpp ├── ex_4.cpp └── ex_5.cpp ├── README.md ├── Textures ├── ex_10 │ ├── ex_10.cpp │ ├── shaders │ │ ├── frag.shader │ │ └── vert.shader │ └── squirrel.jpg ├── ex_11 │ ├── ex_11.cpp │ ├── shaders │ │ ├── frag.shader │ │ └── vert.shader │ └── squirrel.jpg └── ex_12 │ ├── ex_12.cpp │ ├── shaders │ ├── frag.shader │ └── vert.shader │ └── squirrel.jpg └── Transformations ├── ex_14.cpp ├── shaders ├── frag.shader └── vert.shader └── squirrel.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.out 3 | 4 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_4/ex_4.cpp: -------------------------------------------------------------------------------- 1 | // Draw four triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // Read a shader source from a file 10 | // store the shader source in a std::vector 11 | void read_shader_src(const char *fname, std::vector &buffer); 12 | 13 | // Compile a shader 14 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 15 | 16 | // Create a program from two shaders 17 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 18 | 19 | // Called when the window is resized 20 | void GLFWCALL window_resized(int width, int height); 21 | 22 | // Called for keyboard events 23 | void keyboard(int key, int action); 24 | 25 | // Render scene 26 | void display(GLuint &vao); 27 | 28 | // Initialize the data to be rendered 29 | void initialize(GLuint &vao); 30 | 31 | int main () { 32 | // Initialize GLFW 33 | if ( !glfwInit()) { 34 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 35 | exit(-1); 36 | } 37 | 38 | // Use OpenGL 3.2 core profile 39 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 40 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 41 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 42 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 43 | 44 | // Open a window and attach an OpenGL rendering context to the window surface 45 | if( !glfwOpenWindow(500, 500, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 46 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 47 | glfwTerminate(); 48 | exit(-1); 49 | } 50 | 51 | // Register a callback function for window resize events 52 | glfwSetWindowSizeCallback( window_resized ); 53 | 54 | // Register a callback function for keyboard pressed events 55 | glfwSetKeyCallback(keyboard); 56 | 57 | // Print the OpenGL version 58 | int major, minor, rev; 59 | glfwGetGLVersion(&major, &minor, &rev); 60 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 61 | 62 | // Initialize GLEW 63 | glewExperimental = GL_TRUE; 64 | if(glewInit() != GLEW_OK) { 65 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 66 | glfwTerminate(); 67 | exit(-1); 68 | } 69 | 70 | // Create a vertex array object 71 | GLuint vao; 72 | 73 | // Initialize the data to be rendered 74 | initialize(vao); 75 | 76 | // Create a rendering loop 77 | int running = GL_TRUE; 78 | 79 | while(running) { 80 | // Display scene 81 | display(vao); 82 | 83 | // Pool for events 84 | glfwPollEvents(); 85 | // Check if the window was closed 86 | running = glfwGetWindowParam(GLFW_OPENED); 87 | } 88 | 89 | // Terminate GLFW 90 | glfwTerminate(); 91 | 92 | return 0; 93 | } 94 | 95 | // Render scene 96 | void display(GLuint &vao) { 97 | glClear(GL_COLOR_BUFFER_BIT); 98 | 99 | glBindVertexArray(vao); 100 | glDrawArrays(GL_TRIANGLES, 0, 12); 101 | 102 | // Swap front and back buffers 103 | glfwSwapBuffers(); 104 | } 105 | 106 | void initialize(GLuint &vao) { 107 | // Use a Vertex Array Object 108 | glGenVertexArrays(1, &vao); 109 | glBindVertexArray(vao); 110 | 111 | // 4 triangles to be rendered 112 | GLfloat vertices_position[24] = { 113 | 0.0, 0.0, 114 | 0.5, 0.0, 115 | 0.5, 0.5, 116 | 117 | 0.0, 0.0, 118 | 0.0, 0.5, 119 | -0.5, 0.5, 120 | 121 | 0.0, 0.0, 122 | -0.5, 0.0, 123 | -0.5, -0.5, 124 | 125 | 0.0, 0.0, 126 | 0.0, -0.5, 127 | 0.5, -0.5, 128 | }; 129 | 130 | // Create a Vector Buffer Object that will store the vertices on video memory 131 | GLuint vbo; 132 | glGenBuffers(1, &vbo); 133 | 134 | // Allocate space and upload the data from CPU to GPU 135 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 136 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position), vertices_position, GL_STATIC_DRAW); 137 | 138 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 139 | 140 | // Get the location of the attributes that enters in the vertex shader 141 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 142 | 143 | // Specify how the data for position can be accessed 144 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 145 | 146 | // Enable the attribute 147 | glEnableVertexAttribArray(position_attribute); 148 | } 149 | 150 | // Called when the window is resized 151 | void GLFWCALL window_resized(int width, int height) { 152 | // Use red to clear the screen 153 | glClearColor(1, 0, 0, 1); 154 | 155 | // Set the viewport 156 | glViewport(0, 0, width, height); 157 | 158 | glClear(GL_COLOR_BUFFER_BIT); 159 | glfwSwapBuffers(); 160 | } 161 | 162 | // Called for keyboard events 163 | void keyboard(int key, int action) { 164 | if(key == 'Q' && action == GLFW_PRESS) { 165 | glfwTerminate(); 166 | exit(0); 167 | } 168 | } 169 | 170 | // Read a shader source from a file 171 | // store the shader source in a std::vector 172 | void read_shader_src(const char *fname, std::vector &buffer) { 173 | std::ifstream in; 174 | in.open(fname, std::ios::binary); 175 | 176 | if(in.is_open()) { 177 | // Get the number of bytes stored in this file 178 | in.seekg(0, std::ios::end); 179 | size_t length = (size_t)in.tellg(); 180 | 181 | // Go to start of the file 182 | in.seekg(0, std::ios::beg); 183 | 184 | // Read the content of the file in a buffer 185 | buffer.resize(length + 1); 186 | in.read(&buffer[0], length); 187 | in.close(); 188 | // Add a valid C - string end 189 | buffer[length] = '\0'; 190 | } 191 | else { 192 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 193 | exit(-1); 194 | } 195 | } 196 | 197 | // Compile a shader 198 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 199 | // Load a shader from an external file 200 | std::vector buffer; 201 | read_shader_src(fname, buffer); 202 | const char *src = &buffer[0]; 203 | 204 | // Compile the shader 205 | GLuint shader = glCreateShader(shaderType); 206 | glShaderSource(shader, 1, &src, NULL); 207 | glCompileShader(shader); 208 | // Check the result of the compilation 209 | GLint test; 210 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 211 | if(!test) { 212 | std::cerr << "Shader compilation failed with this message:" << std::endl; 213 | std::vector compilation_log(512); 214 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 215 | std::cerr << &compilation_log[0] << std::endl; 216 | glfwTerminate(); 217 | exit(-1); 218 | } 219 | return shader; 220 | } 221 | 222 | // Create a program from two shaders 223 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 224 | // Load and compile the vertex and fragment shaders 225 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 226 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 227 | 228 | // Attach the above shader to a program 229 | GLuint shaderProgram = glCreateProgram(); 230 | glAttachShader(shaderProgram, vertexShader); 231 | glAttachShader(shaderProgram, fragmentShader); 232 | 233 | // Flag the shaders for deletion 234 | glDeleteShader(vertexShader); 235 | glDeleteShader(fragmentShader); 236 | 237 | // Link and use the program 238 | glLinkProgram(shaderProgram); 239 | glUseProgram(shaderProgram); 240 | 241 | return shaderProgram; 242 | } 243 | 244 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_4/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | out vec4 out_color; 4 | 5 | void main() { 6 | out_color = vec4(1.0, 1.0, 1.0, 1.0); 7 | } 8 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_4/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | 5 | void main() { 6 | gl_Position = position; 7 | } 8 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_5/ex_5.cpp: -------------------------------------------------------------------------------- 1 | // Draw 9 points on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // Read a shader source from a file 10 | // store the shader source in a std::vector 11 | void read_shader_src(const char *fname, std::vector &buffer); 12 | 13 | // Compile a shader 14 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 15 | 16 | // Create a program from two shaders 17 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 18 | 19 | // Called when the window is resized 20 | void GLFWCALL window_resized(int width, int height); 21 | 22 | // Called for keyboard events 23 | void keyboard(int key, int action); 24 | 25 | // Render scene 26 | void display(GLuint &vao); 27 | 28 | // Initialize the data to be rendered 29 | void initialize(GLuint &vao); 30 | 31 | int main () { 32 | // Initialize GLFW 33 | if ( !glfwInit()) { 34 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 35 | exit(-1); 36 | } 37 | 38 | // Use OpenGL 3.2 core profile 39 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 40 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 41 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 42 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 43 | 44 | // Open a window and attach an OpenGL rendering context to the window surface 45 | if( !glfwOpenWindow(500, 500, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 46 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 47 | glfwTerminate(); 48 | exit(-1); 49 | } 50 | 51 | // Register a callback function for window resize events 52 | glfwSetWindowSizeCallback( window_resized ); 53 | 54 | // Register a callback function for keyboard pressed events 55 | glfwSetKeyCallback(keyboard); 56 | 57 | // Print the OpenGL version 58 | int major, minor, rev; 59 | glfwGetGLVersion(&major, &minor, &rev); 60 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 61 | 62 | // Initialize GLEW 63 | glewExperimental = GL_TRUE; 64 | if(glewInit() != GLEW_OK) { 65 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 66 | glfwTerminate(); 67 | exit(-1); 68 | } 69 | 70 | // Create a vertex array object 71 | GLuint vao; 72 | 73 | // Initialize the data to be rendered 74 | initialize(vao); 75 | 76 | // Create a rendering loop 77 | int running = GL_TRUE; 78 | 79 | while(running) { 80 | // Display scene 81 | display(vao); 82 | 83 | // Pool for events 84 | glfwPollEvents(); 85 | // Check if the window was closed 86 | running = glfwGetWindowParam(GLFW_OPENED); 87 | } 88 | 89 | // Terminate GLFW 90 | glfwTerminate(); 91 | 92 | return 0; 93 | } 94 | 95 | // Render scene 96 | void display(GLuint &vao) { 97 | glClear(GL_COLOR_BUFFER_BIT); 98 | 99 | glBindVertexArray(vao); 100 | glDrawArrays(GL_POINTS, 0, 12); 101 | 102 | // Swap front and back buffers 103 | glfwSwapBuffers(); 104 | } 105 | 106 | void initialize(GLuint &vao) { 107 | // Use a Vertex Array Object 108 | glGenVertexArrays(1, &vao); 109 | glBindVertexArray(vao); 110 | 111 | // 4 triangles to be rendered 112 | GLfloat vertices_position[24] = { 113 | 0.0, 0.0, 114 | 0.5, 0.0, 115 | 0.5, 0.5, 116 | 117 | 0.0, 0.0, 118 | 0.0, 0.5, 119 | -0.5, 0.5, 120 | 121 | 0.0, 0.0, 122 | -0.5, 0.0, 123 | -0.5, -0.5, 124 | 125 | 0.0, 0.0, 126 | 0.0, -0.5, 127 | 0.5, -0.5, 128 | }; 129 | 130 | // Create a Vector Buffer Object that will store the vertices on video memory 131 | GLuint vbo; 132 | glGenBuffers(1, &vbo); 133 | 134 | // Allocate space and upload the data from CPU to GPU 135 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 136 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position), vertices_position, GL_STATIC_DRAW); 137 | 138 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 139 | 140 | // Get the location of the attributes that enters in the vertex shader 141 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 142 | 143 | // Specify how the data for position can be accessed 144 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 145 | 146 | // Enable the attribute 147 | glEnableVertexAttribArray(position_attribute); 148 | 149 | // Enable point size 150 | glEnable(GL_PROGRAM_POINT_SIZE); 151 | } 152 | 153 | // Called when the window is resized 154 | void GLFWCALL window_resized(int width, int height) { 155 | // Use red to clear the screen 156 | glClearColor(1, 0, 0, 1); 157 | 158 | // Set the viewport 159 | glViewport(0, 0, width, height); 160 | 161 | glClear(GL_COLOR_BUFFER_BIT); 162 | glfwSwapBuffers(); 163 | } 164 | 165 | // Called for keyboard events 166 | void keyboard(int key, int action) { 167 | if(key == 'Q' && action == GLFW_PRESS) { 168 | glfwTerminate(); 169 | exit(0); 170 | } 171 | } 172 | 173 | // Read a shader source from a file 174 | // store the shader source in a std::vector 175 | void read_shader_src(const char *fname, std::vector &buffer) { 176 | std::ifstream in; 177 | in.open(fname, std::ios::binary); 178 | 179 | if(in.is_open()) { 180 | // Get the number of bytes stored in this file 181 | in.seekg(0, std::ios::end); 182 | size_t length = (size_t)in.tellg(); 183 | 184 | // Go to start of the file 185 | in.seekg(0, std::ios::beg); 186 | 187 | // Read the content of the file in a buffer 188 | buffer.resize(length + 1); 189 | in.read(&buffer[0], length); 190 | in.close(); 191 | // Add a valid C - string end 192 | buffer[length] = '\0'; 193 | } 194 | else { 195 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 196 | exit(-1); 197 | } 198 | } 199 | 200 | // Compile a shader 201 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 202 | // Load a shader from an external file 203 | std::vector buffer; 204 | read_shader_src(fname, buffer); 205 | const char *src = &buffer[0]; 206 | 207 | // Compile the shader 208 | GLuint shader = glCreateShader(shaderType); 209 | glShaderSource(shader, 1, &src, NULL); 210 | glCompileShader(shader); 211 | // Check the result of the compilation 212 | GLint test; 213 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 214 | if(!test) { 215 | std::cerr << "Shader compilation failed with this message:" << std::endl; 216 | std::vector compilation_log(512); 217 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 218 | std::cerr << &compilation_log[0] << std::endl; 219 | glfwTerminate(); 220 | exit(-1); 221 | } 222 | return shader; 223 | } 224 | 225 | // Create a program from two shaders 226 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 227 | // Load and compile the vertex and fragment shaders 228 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 229 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 230 | 231 | // Attach the above shader to a program 232 | GLuint shaderProgram = glCreateProgram(); 233 | glAttachShader(shaderProgram, vertexShader); 234 | glAttachShader(shaderProgram, fragmentShader); 235 | 236 | // Flag the shaders for deletion 237 | glDeleteShader(vertexShader); 238 | glDeleteShader(fragmentShader); 239 | 240 | // Link and use the program 241 | glLinkProgram(shaderProgram); 242 | glUseProgram(shaderProgram); 243 | 244 | return shaderProgram; 245 | } 246 | 247 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_5/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | out vec4 out_color; 4 | 5 | void main() { 6 | out_color = vec4(1.0, 1.0, 1.0, 1.0); 7 | } 8 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_5/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | 5 | void main() { 6 | gl_Position = position; 7 | gl_PointSize = 10.0; 8 | } 9 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_6/ex_6.cpp: -------------------------------------------------------------------------------- 1 | // Draw four wireframe triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // Read a shader source from a file 10 | // store the shader source in a std::vector 11 | void read_shader_src(const char *fname, std::vector &buffer); 12 | 13 | // Compile a shader 14 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 15 | 16 | // Create a program from two shaders 17 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 18 | 19 | // Called when the window is resized 20 | void GLFWCALL window_resized(int width, int height); 21 | 22 | // Called for keyboard events 23 | void keyboard(int key, int action); 24 | 25 | // Render scene 26 | void display(GLuint &vao); 27 | 28 | // Initialize the data to be rendered 29 | void initialize(GLuint &vao); 30 | 31 | int main () { 32 | // Initialize GLFW 33 | if ( !glfwInit()) { 34 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 35 | exit(-1); 36 | } 37 | 38 | // Use OpenGL 3.2 core profile 39 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 40 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 41 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 42 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 43 | 44 | // Open a window and attach an OpenGL rendering context to the window surface 45 | if( !glfwOpenWindow(500, 500, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 46 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 47 | glfwTerminate(); 48 | exit(-1); 49 | } 50 | 51 | // Register a callback function for window resize events 52 | glfwSetWindowSizeCallback( window_resized ); 53 | 54 | // Register a callback function for keyboard pressed events 55 | glfwSetKeyCallback(keyboard); 56 | 57 | // Print the OpenGL version 58 | int major, minor, rev; 59 | glfwGetGLVersion(&major, &minor, &rev); 60 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 61 | 62 | // Initialize GLEW 63 | glewExperimental = GL_TRUE; 64 | if(glewInit() != GLEW_OK) { 65 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 66 | glfwTerminate(); 67 | exit(-1); 68 | } 69 | 70 | // Create a vertex array object 71 | GLuint vao; 72 | 73 | // Initialize the data to be rendered 74 | initialize(vao); 75 | 76 | // Create a rendering loop 77 | int running = GL_TRUE; 78 | 79 | while(running) { 80 | // Display scene 81 | display(vao); 82 | 83 | // Pool for events 84 | glfwPollEvents(); 85 | // Check if the window was closed 86 | running = glfwGetWindowParam(GLFW_OPENED); 87 | } 88 | 89 | // Terminate GLFW 90 | glfwTerminate(); 91 | 92 | return 0; 93 | } 94 | 95 | // Render scene 96 | void display(GLuint &vao) { 97 | glClear(GL_COLOR_BUFFER_BIT); 98 | 99 | glBindVertexArray(vao); 100 | glDrawArrays(GL_TRIANGLES, 0, 12); 101 | 102 | // Swap front and back buffers 103 | glfwSwapBuffers(); 104 | } 105 | 106 | void initialize(GLuint &vao) { 107 | // Use a Vertex Array Object 108 | glGenVertexArrays(1, &vao); 109 | glBindVertexArray(vao); 110 | 111 | // 4 triangles to be rendered 112 | GLfloat vertices_position[24] = { 113 | 0.0, 0.0, 114 | 0.5, 0.0, 115 | 0.5, 0.5, 116 | 117 | 0.0, 0.0, 118 | 0.0, 0.5, 119 | -0.5, 0.5, 120 | 121 | 0.0, 0.0, 122 | -0.5, 0.0, 123 | -0.5, -0.5, 124 | 125 | 0.0, 0.0, 126 | 0.0, -0.5, 127 | 0.5, -0.5, 128 | }; 129 | 130 | // Create a Vector Buffer Object that will store the vertices on video memory 131 | GLuint vbo; 132 | glGenBuffers(1, &vbo); 133 | 134 | // Allocate space and upload the data from CPU to GPU 135 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 136 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position), vertices_position, GL_STATIC_DRAW); 137 | 138 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 139 | 140 | // Get the location of the attributes that enters in the vertex shader 141 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 142 | 143 | // Specify how the data for position can be accessed 144 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 145 | 146 | // Enable the attribute 147 | glEnableVertexAttribArray(position_attribute); 148 | 149 | // Draw wireframe triangles: 150 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 151 | } 152 | 153 | // Called when the window is resized 154 | void GLFWCALL window_resized(int width, int height) { 155 | // Use red to clear the screen 156 | glClearColor(1, 0, 0, 1); 157 | 158 | // Set the viewport 159 | glViewport(0, 0, width, height); 160 | 161 | glClear(GL_COLOR_BUFFER_BIT); 162 | glfwSwapBuffers(); 163 | } 164 | 165 | // Called for keyboard events 166 | void keyboard(int key, int action) { 167 | if(key == 'Q' && action == GLFW_PRESS) { 168 | glfwTerminate(); 169 | exit(0); 170 | } 171 | } 172 | 173 | // Read a shader source from a file 174 | // store the shader source in a std::vector 175 | void read_shader_src(const char *fname, std::vector &buffer) { 176 | std::ifstream in; 177 | in.open(fname, std::ios::binary); 178 | 179 | if(in.is_open()) { 180 | // Get the number of bytes stored in this file 181 | in.seekg(0, std::ios::end); 182 | size_t length = (size_t)in.tellg(); 183 | 184 | // Go to start of the file 185 | in.seekg(0, std::ios::beg); 186 | 187 | // Read the content of the file in a buffer 188 | buffer.resize(length + 1); 189 | in.read(&buffer[0], length); 190 | in.close(); 191 | // Add a valid C - string end 192 | buffer[length] = '\0'; 193 | } 194 | else { 195 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 196 | exit(-1); 197 | } 198 | } 199 | 200 | // Compile a shader 201 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 202 | // Load a shader from an external file 203 | std::vector buffer; 204 | read_shader_src(fname, buffer); 205 | const char *src = &buffer[0]; 206 | 207 | // Compile the shader 208 | GLuint shader = glCreateShader(shaderType); 209 | glShaderSource(shader, 1, &src, NULL); 210 | glCompileShader(shader); 211 | // Check the result of the compilation 212 | GLint test; 213 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 214 | if(!test) { 215 | std::cerr << "Shader compilation failed with this message:" << std::endl; 216 | std::vector compilation_log(512); 217 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 218 | std::cerr << &compilation_log[0] << std::endl; 219 | glfwTerminate(); 220 | exit(-1); 221 | } 222 | return shader; 223 | } 224 | 225 | // Create a program from two shaders 226 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 227 | // Load and compile the vertex and fragment shaders 228 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 229 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 230 | 231 | // Attach the above shader to a program 232 | GLuint shaderProgram = glCreateProgram(); 233 | glAttachShader(shaderProgram, vertexShader); 234 | glAttachShader(shaderProgram, fragmentShader); 235 | 236 | // Flag the shaders for deletion 237 | glDeleteShader(vertexShader); 238 | glDeleteShader(fragmentShader); 239 | 240 | // Link and use the program 241 | glLinkProgram(shaderProgram); 242 | glUseProgram(shaderProgram); 243 | 244 | return shaderProgram; 245 | } 246 | 247 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_6/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | out vec4 out_color; 4 | 5 | void main() { 6 | out_color = vec4(1.0, 1.0, 1.0, 1.0); 7 | } 8 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_6/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | 5 | void main() { 6 | gl_Position = position; 7 | } 8 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_7/ex_7.cpp: -------------------------------------------------------------------------------- 1 | // Draw four triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | // Read a shader source from a file 11 | // store the shader source in a std::vector 12 | void read_shader_src(const char *fname, std::vector &buffer); 13 | 14 | // Compile a shader 15 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 16 | 17 | // Create a program from two shaders 18 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 19 | 20 | // Called when the window is resized 21 | void GLFWCALL window_resized(int width, int height); 22 | 23 | // Called for keyboard events 24 | void keyboard(int key, int action); 25 | 26 | // Render scene 27 | void display(GLuint &vao); 28 | 29 | // Initialize the data to be rendered 30 | void initialize(GLuint &vao); 31 | 32 | int main () { 33 | // Initialize GLFW 34 | if ( !glfwInit()) { 35 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 36 | exit(-1); 37 | } 38 | 39 | // Use OpenGL 3.2 core profile 40 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 41 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 42 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 43 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 44 | 45 | // Open a window and attach an OpenGL rendering context to the window surface 46 | if( !glfwOpenWindow(500, 500, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 47 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 48 | glfwTerminate(); 49 | exit(-1); 50 | } 51 | 52 | // Register a callback function for window resize events 53 | glfwSetWindowSizeCallback( window_resized ); 54 | 55 | // Register a callback function for keyboard pressed events 56 | glfwSetKeyCallback(keyboard); 57 | 58 | // Print the OpenGL version 59 | int major, minor, rev; 60 | glfwGetGLVersion(&major, &minor, &rev); 61 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 62 | 63 | // Initialize GLEW 64 | glewExperimental = GL_TRUE; 65 | if(glewInit() != GLEW_OK) { 66 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 67 | glfwTerminate(); 68 | exit(-1); 69 | } 70 | 71 | // Create a vertex array object 72 | GLuint vao; 73 | 74 | // Initialize the data to be rendered 75 | initialize(vao); 76 | 77 | // Create a rendering loop 78 | int running = GL_TRUE; 79 | 80 | while(running) { 81 | // Display scene 82 | display(vao); 83 | 84 | // Pool for events 85 | glfwPollEvents(); 86 | // Check if the window was closed 87 | running = glfwGetWindowParam(GLFW_OPENED); 88 | } 89 | 90 | // Terminate GLFW 91 | glfwTerminate(); 92 | 93 | return 0; 94 | } 95 | 96 | // Render scene 97 | void display(GLuint &vao) { 98 | glClear(GL_COLOR_BUFFER_BIT); 99 | 100 | glBindVertexArray(vao); 101 | glDrawArrays(GL_TRIANGLES, 0, 12); 102 | 103 | // Swap front and back buffers 104 | glfwSwapBuffers(); 105 | } 106 | 107 | void initialize(GLuint &vao) { 108 | // Use a Vertex Array Object 109 | glGenVertexArrays(1, &vao); 110 | glBindVertexArray(vao); 111 | 112 | // 4 triangles to be rendered 113 | GLfloat vertices_position[24] = { 114 | 0.0, 0.0, 115 | 0.5, 0.0, 116 | 0.5, 0.5, 117 | 118 | 0.0, 0.0, 119 | 0.0, 0.5, 120 | -0.5, 0.5, 121 | 122 | 0.0, 0.0, 123 | -0.5, 0.0, 124 | -0.5, -0.5, 125 | 126 | 0.0, 0.0, 127 | 0.0, -0.5, 128 | 0.5, -0.5, 129 | }; 130 | 131 | GLfloat colors[36]; 132 | 133 | // Initialize the random seed from the system time 134 | srand(time(NULL)); 135 | 136 | // Fill colors with random numbers from 0 to 1, use continuous polynomials for r,g,b: 137 | int k = 0; 138 | for(int i = 0; i < sizeof(colors)/sizeof(float)/3; ++i) { 139 | float t = (float)rand()/(float)RAND_MAX; 140 | colors[k] = 9*(1-t)*t*t*t; 141 | k++; 142 | colors[k] = 15*(1-t)*(1-t)*t*t; 143 | k++; 144 | colors[k] = 8.5*(1-t)*(1-t)*(1-t)*t; 145 | k++; 146 | 147 | } 148 | 149 | // Create a Vector Buffer Object that will store the vertices on video memory 150 | GLuint vbo; 151 | glGenBuffers(1, &vbo); 152 | 153 | // Allocate space for vertex positions and colors 154 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 155 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position) + sizeof(colors), NULL, GL_STATIC_DRAW); 156 | 157 | // Transfer the vertex positions: 158 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices_position), vertices_position); 159 | 160 | // Transfer the vertex colors: 161 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices_position), sizeof(colors), colors); 162 | 163 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 164 | 165 | // Get the location of the attributes that enters in the vertex shader 166 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 167 | 168 | // Specify how the data for position can be accessed 169 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 170 | 171 | // Enable the attribute 172 | glEnableVertexAttribArray(position_attribute); 173 | 174 | // Color attribute 175 | GLint color_attribute = glGetAttribLocation(shaderProgram, "color"); 176 | glVertexAttribPointer(color_attribute, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)sizeof(vertices_position)); 177 | glEnableVertexAttribArray(color_attribute); 178 | 179 | } 180 | 181 | // Called when the window is resized 182 | void GLFWCALL window_resized(int width, int height) { 183 | // Use red to clear the screen 184 | glClearColor(1, 0, 0, 1); 185 | 186 | // Set the viewport 187 | glViewport(0, 0, width, height); 188 | 189 | glClear(GL_COLOR_BUFFER_BIT); 190 | glfwSwapBuffers(); 191 | } 192 | 193 | // Called for keyboard events 194 | void keyboard(int key, int action) { 195 | if(key == 'Q' && action == GLFW_PRESS) { 196 | glfwTerminate(); 197 | exit(0); 198 | } 199 | } 200 | 201 | // Read a shader source from a file 202 | // store the shader source in a std::vector 203 | void read_shader_src(const char *fname, std::vector &buffer) { 204 | std::ifstream in; 205 | in.open(fname, std::ios::binary); 206 | 207 | if(in.is_open()) { 208 | // Get the number of bytes stored in this file 209 | in.seekg(0, std::ios::end); 210 | size_t length = (size_t)in.tellg(); 211 | 212 | // Go to start of the file 213 | in.seekg(0, std::ios::beg); 214 | 215 | // Read the content of the file in a buffer 216 | buffer.resize(length + 1); 217 | in.read(&buffer[0], length); 218 | in.close(); 219 | // Add a valid C - string end 220 | buffer[length] = '\0'; 221 | } 222 | else { 223 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 224 | exit(-1); 225 | } 226 | } 227 | 228 | // Compile a shader 229 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 230 | // Load a shader from an external file 231 | std::vector buffer; 232 | read_shader_src(fname, buffer); 233 | const char *src = &buffer[0]; 234 | 235 | // Compile the shader 236 | GLuint shader = glCreateShader(shaderType); 237 | glShaderSource(shader, 1, &src, NULL); 238 | glCompileShader(shader); 239 | // Check the result of the compilation 240 | GLint test; 241 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 242 | if(!test) { 243 | std::cerr << "Shader compilation failed with this message:" << std::endl; 244 | std::vector compilation_log(512); 245 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 246 | std::cerr << &compilation_log[0] << std::endl; 247 | glfwTerminate(); 248 | exit(-1); 249 | } 250 | return shader; 251 | } 252 | 253 | // Create a program from two shaders 254 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 255 | // Load and compile the vertex and fragment shaders 256 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 257 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 258 | 259 | // Attach the above shader to a program 260 | GLuint shaderProgram = glCreateProgram(); 261 | glAttachShader(shaderProgram, vertexShader); 262 | glAttachShader(shaderProgram, fragmentShader); 263 | 264 | // Flag the shaders for deletion 265 | glDeleteShader(vertexShader); 266 | glDeleteShader(fragmentShader); 267 | 268 | // Link and use the program 269 | glLinkProgram(shaderProgram); 270 | glUseProgram(shaderProgram); 271 | 272 | return shaderProgram; 273 | } 274 | 275 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_7/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 color_from_vshader; 4 | out vec4 out_color; 5 | 6 | void main() { 7 | out_color = color_from_vshader; 8 | } 9 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_7/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | in vec4 color; 5 | out vec4 color_from_vshader; 6 | 7 | void main() { 8 | gl_Position = position; 9 | color_from_vshader = color; 10 | } 11 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_8/ex_8.cpp: -------------------------------------------------------------------------------- 1 | // Draw four triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | // Read a shader source from a file 11 | // store the shader source in a std::vector 12 | void read_shader_src(const char *fname, std::vector &buffer); 13 | 14 | // Compile a shader 15 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 16 | 17 | // Create a program from two shaders 18 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 19 | 20 | // Called when the window is resized 21 | void GLFWCALL window_resized(int width, int height); 22 | 23 | // Called for keyboard events 24 | void keyboard(int key, int action); 25 | 26 | // Render scene 27 | void display(GLuint &vao); 28 | 29 | // Initialize the data to be rendered 30 | void initialize(GLuint &vao); 31 | 32 | int main () { 33 | // Initialize GLFW 34 | if ( !glfwInit()) { 35 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 36 | exit(-1); 37 | } 38 | 39 | // Use OpenGL 3.2 core profile 40 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 41 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 42 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 43 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 44 | 45 | // Open a window and attach an OpenGL rendering context to the window surface 46 | if( !glfwOpenWindow(500, 500, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 47 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 48 | glfwTerminate(); 49 | exit(-1); 50 | } 51 | 52 | // Register a callback function for window resize events 53 | glfwSetWindowSizeCallback( window_resized ); 54 | 55 | // Register a callback function for keyboard pressed events 56 | glfwSetKeyCallback(keyboard); 57 | 58 | // Print the OpenGL version 59 | int major, minor, rev; 60 | glfwGetGLVersion(&major, &minor, &rev); 61 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 62 | 63 | // Initialize GLEW 64 | glewExperimental = GL_TRUE; 65 | if(glewInit() != GLEW_OK) { 66 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 67 | glfwTerminate(); 68 | exit(-1); 69 | } 70 | 71 | // Create a vertex array object 72 | GLuint vao; 73 | 74 | // Initialize the data to be rendered 75 | initialize(vao); 76 | 77 | // Create a rendering loop 78 | int running = GL_TRUE; 79 | 80 | while(running) { 81 | // Display scene 82 | display(vao); 83 | 84 | // Pool for events 85 | glfwPollEvents(); 86 | // Check if the window was closed 87 | running = glfwGetWindowParam(GLFW_OPENED); 88 | } 89 | 90 | // Terminate GLFW 91 | glfwTerminate(); 92 | 93 | return 0; 94 | } 95 | 96 | // Render scene 97 | void display(GLuint &vao) { 98 | glClear(GL_COLOR_BUFFER_BIT); 99 | 100 | glBindVertexArray(vao); 101 | glDrawArrays(GL_TRIANGLES, 0, 6); 102 | 103 | // Swap front and back buffers 104 | glfwSwapBuffers(); 105 | } 106 | 107 | void initialize(GLuint &vao) { 108 | // Use a Vertex Array Object 109 | glGenVertexArrays(1, &vao); 110 | glBindVertexArray(vao); 111 | 112 | // 1 square (made by 2 triangles) to be rendered 113 | GLfloat vertices_position[12] = { 114 | -0.5, -0.5, 115 | 0.5, -0.5, 116 | 0.5, 0.5, 117 | 118 | 0.5, 0.5, 119 | -0.5, 0.5, 120 | -0.5, -0.5 121 | }; 122 | 123 | GLfloat colors[18]; 124 | 125 | // Initialize the random seed from the system time 126 | srand(time(NULL)); 127 | 128 | // Fill colors with random numbers from 0 to 1, use continuous polynomials for r,g,b: 129 | int k = 0; 130 | for(int i = 0; i < sizeof(colors)/sizeof(float)/3; ++i) { 131 | float t = (float)rand()/(float)RAND_MAX; 132 | colors[k] = 9*(1-t)*t*t*t; 133 | k++; 134 | colors[k] = 15*(1-t)*(1-t)*t*t; 135 | k++; 136 | colors[k] = 8.5*(1-t)*(1-t)*(1-t)*t; 137 | k++; 138 | 139 | } 140 | 141 | // Create a Vector Buffer Object that will store the vertices on video memory 142 | GLuint vbo; 143 | glGenBuffers(1, &vbo); 144 | 145 | // Allocate space for vertex positions and colors 146 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 147 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position) + sizeof(colors), NULL, GL_STATIC_DRAW); 148 | 149 | // Transfer the vertex positions: 150 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices_position), vertices_position); 151 | 152 | // Transfer the vertex colors: 153 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices_position), sizeof(colors), colors); 154 | 155 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 156 | 157 | // Get the location of the attributes that enters in the vertex shader 158 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 159 | 160 | // Specify how the data for position can be accessed 161 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 162 | 163 | // Enable the attribute 164 | glEnableVertexAttribArray(position_attribute); 165 | 166 | // Color attribute 167 | GLint color_attribute = glGetAttribLocation(shaderProgram, "color"); 168 | glVertexAttribPointer(color_attribute, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)sizeof(vertices_position)); 169 | glEnableVertexAttribArray(color_attribute); 170 | 171 | } 172 | 173 | // Called when the window is resized 174 | void GLFWCALL window_resized(int width, int height) { 175 | // Use red to clear the screen 176 | glClearColor(1, 0, 0, 1); 177 | 178 | // Set the viewport 179 | glViewport(0, 0, width, height); 180 | 181 | glClear(GL_COLOR_BUFFER_BIT); 182 | glfwSwapBuffers(); 183 | } 184 | 185 | // Called for keyboard events 186 | void keyboard(int key, int action) { 187 | if(key == 'Q' && action == GLFW_PRESS) { 188 | glfwTerminate(); 189 | exit(0); 190 | } 191 | } 192 | 193 | // Read a shader source from a file 194 | // store the shader source in a std::vector 195 | void read_shader_src(const char *fname, std::vector &buffer) { 196 | std::ifstream in; 197 | in.open(fname, std::ios::binary); 198 | 199 | if(in.is_open()) { 200 | // Get the number of bytes stored in this file 201 | in.seekg(0, std::ios::end); 202 | size_t length = (size_t)in.tellg(); 203 | 204 | // Go to start of the file 205 | in.seekg(0, std::ios::beg); 206 | 207 | // Read the content of the file in a buffer 208 | buffer.resize(length + 1); 209 | in.read(&buffer[0], length); 210 | in.close(); 211 | // Add a valid C - string end 212 | buffer[length] = '\0'; 213 | } 214 | else { 215 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 216 | exit(-1); 217 | } 218 | } 219 | 220 | // Compile a shader 221 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 222 | // Load a shader from an external file 223 | std::vector buffer; 224 | read_shader_src(fname, buffer); 225 | const char *src = &buffer[0]; 226 | 227 | // Compile the shader 228 | GLuint shader = glCreateShader(shaderType); 229 | glShaderSource(shader, 1, &src, NULL); 230 | glCompileShader(shader); 231 | // Check the result of the compilation 232 | GLint test; 233 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 234 | if(!test) { 235 | std::cerr << "Shader compilation failed with this message:" << std::endl; 236 | std::vector compilation_log(512); 237 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 238 | std::cerr << &compilation_log[0] << std::endl; 239 | glfwTerminate(); 240 | exit(-1); 241 | } 242 | return shader; 243 | } 244 | 245 | // Create a program from two shaders 246 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 247 | // Load and compile the vertex and fragment shaders 248 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 249 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 250 | 251 | // Attach the above shader to a program 252 | GLuint shaderProgram = glCreateProgram(); 253 | glAttachShader(shaderProgram, vertexShader); 254 | glAttachShader(shaderProgram, fragmentShader); 255 | 256 | // Flag the shaders for deletion 257 | glDeleteShader(vertexShader); 258 | glDeleteShader(fragmentShader); 259 | 260 | // Link and use the program 261 | glLinkProgram(shaderProgram); 262 | glUseProgram(shaderProgram); 263 | 264 | return shaderProgram; 265 | } 266 | 267 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_8/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 color_from_vshader; 4 | out vec4 out_color; 5 | 6 | void main() { 7 | out_color = color_from_vshader; 8 | } 9 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_8/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | in vec4 color; 5 | out vec4 color_from_vshader; 6 | 7 | void main() { 8 | gl_Position = position; 9 | color_from_vshader = color; 10 | } 11 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_9/ex_9.cpp: -------------------------------------------------------------------------------- 1 | // Draw four triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | // Read a shader source from a file 11 | // store the shader source in a std::vector 12 | void read_shader_src(const char *fname, std::vector &buffer); 13 | 14 | // Compile a shader 15 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 16 | 17 | // Create a program from two shaders 18 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 19 | 20 | // Called when the window is resized 21 | void GLFWCALL window_resized(int width, int height); 22 | 23 | // Called for keyboard events 24 | void keyboard(int key, int action); 25 | 26 | // Render scene 27 | void display(GLuint &vao); 28 | 29 | // Initialize the data to be rendered 30 | void initialize(GLuint &vao); 31 | 32 | int main () { 33 | // Initialize GLFW 34 | if ( !glfwInit()) { 35 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 36 | exit(-1); 37 | } 38 | 39 | // Use OpenGL 3.2 core profile 40 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 41 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 42 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 43 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 44 | 45 | // Open a window and attach an OpenGL rendering context to the window surface 46 | if( !glfwOpenWindow(500, 500, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 47 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 48 | glfwTerminate(); 49 | exit(-1); 50 | } 51 | 52 | // Register a callback function for window resize events 53 | glfwSetWindowSizeCallback( window_resized ); 54 | 55 | // Register a callback function for keyboard pressed events 56 | glfwSetKeyCallback(keyboard); 57 | 58 | // Print the OpenGL version 59 | int major, minor, rev; 60 | glfwGetGLVersion(&major, &minor, &rev); 61 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 62 | 63 | // Initialize GLEW 64 | glewExperimental = GL_TRUE; 65 | if(glewInit() != GLEW_OK) { 66 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 67 | glfwTerminate(); 68 | exit(-1); 69 | } 70 | 71 | // Create a vertex array object 72 | GLuint vao; 73 | 74 | // Initialize the data to be rendered 75 | initialize(vao); 76 | 77 | // Create a rendering loop 78 | int running = GL_TRUE; 79 | 80 | while(running) { 81 | // Display scene 82 | display(vao); 83 | 84 | // Pool for events 85 | glfwPollEvents(); 86 | // Check if the window was closed 87 | running = glfwGetWindowParam(GLFW_OPENED); 88 | } 89 | 90 | // Terminate GLFW 91 | glfwTerminate(); 92 | 93 | return 0; 94 | } 95 | 96 | // Render scene 97 | void display(GLuint &vao) { 98 | glClear(GL_COLOR_BUFFER_BIT); 99 | 100 | glBindVertexArray(vao); 101 | glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 102 | 103 | // Swap front and back buffers 104 | glfwSwapBuffers(); 105 | } 106 | 107 | void initialize(GLuint &vao) { 108 | // Use a Vertex Array Object 109 | glGenVertexArrays(1, &vao); 110 | glBindVertexArray(vao); 111 | 112 | // 1 square (made by 2 triangles) to be rendered 113 | GLfloat vertices_position[8] = { 114 | -0.5, -0.5, 115 | 0.5, -0.5, 116 | 0.5, 0.5, 117 | -0.5, 0.5, 118 | }; 119 | 120 | GLuint indices[6] = { 121 | 0, 1, 2, 122 | 2, 3, 0 123 | }; 124 | 125 | GLfloat colors[12]; 126 | 127 | // Initialize the random seed from the system time 128 | srand(time(NULL)); 129 | 130 | // Fill colors with random numbers from 0 to 1, use continuous polynomials for r,g,b: 131 | int k = 0; 132 | for(int i = 0; i < sizeof(colors)/sizeof(float)/3; ++i) { 133 | float t = (float)rand()/(float)RAND_MAX; 134 | colors[k] = 9*(1-t)*t*t*t; 135 | k++; 136 | colors[k] = 15*(1-t)*(1-t)*t*t; 137 | k++; 138 | colors[k] = 8.5*(1-t)*(1-t)*(1-t)*t; 139 | k++; 140 | 141 | } 142 | 143 | // Create a Vector Buffer Object that will store the vertices on video memory 144 | GLuint vbo; 145 | glGenBuffers(1, &vbo); 146 | 147 | // Allocate space for vertex positions and colors 148 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 149 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position) + sizeof(colors), NULL, GL_STATIC_DRAW); 150 | 151 | // Transfer the vertex positions: 152 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices_position), vertices_position); 153 | 154 | // Transfer the vertex colors: 155 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices_position), sizeof(colors), colors); 156 | 157 | // Create an Element Array Buffer that will store the indices array: 158 | GLuint eab; 159 | glGenBuffers(1, &eab); 160 | 161 | // Transfer the data from indices to eab 162 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab); 163 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 164 | 165 | 166 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 167 | 168 | // Get the location of the attributes that enters in the vertex shader 169 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 170 | 171 | // Specify how the data for position can be accessed 172 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 173 | 174 | // Enable the attribute 175 | glEnableVertexAttribArray(position_attribute); 176 | 177 | // Color attribute 178 | GLint color_attribute = glGetAttribLocation(shaderProgram, "color"); 179 | glVertexAttribPointer(color_attribute, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)sizeof(vertices_position)); 180 | glEnableVertexAttribArray(color_attribute); 181 | 182 | } 183 | 184 | // Called when the window is resized 185 | void GLFWCALL window_resized(int width, int height) { 186 | // Use red to clear the screen 187 | glClearColor(1, 0, 0, 1); 188 | 189 | // Set the viewport 190 | glViewport(0, 0, width, height); 191 | 192 | glClear(GL_COLOR_BUFFER_BIT); 193 | glfwSwapBuffers(); 194 | } 195 | 196 | // Called for keyboard events 197 | void keyboard(int key, int action) { 198 | if(key == 'Q' && action == GLFW_PRESS) { 199 | glfwTerminate(); 200 | exit(0); 201 | } 202 | } 203 | 204 | // Read a shader source from a file 205 | // store the shader source in a std::vector 206 | void read_shader_src(const char *fname, std::vector &buffer) { 207 | std::ifstream in; 208 | in.open(fname, std::ios::binary); 209 | 210 | if(in.is_open()) { 211 | // Get the number of bytes stored in this file 212 | in.seekg(0, std::ios::end); 213 | size_t length = (size_t)in.tellg(); 214 | 215 | // Go to start of the file 216 | in.seekg(0, std::ios::beg); 217 | 218 | // Read the content of the file in a buffer 219 | buffer.resize(length + 1); 220 | in.read(&buffer[0], length); 221 | in.close(); 222 | // Add a valid C - string end 223 | buffer[length] = '\0'; 224 | } 225 | else { 226 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 227 | exit(-1); 228 | } 229 | } 230 | 231 | // Compile a shader 232 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 233 | // Load a shader from an external file 234 | std::vector buffer; 235 | read_shader_src(fname, buffer); 236 | const char *src = &buffer[0]; 237 | 238 | // Compile the shader 239 | GLuint shader = glCreateShader(shaderType); 240 | glShaderSource(shader, 1, &src, NULL); 241 | glCompileShader(shader); 242 | // Check the result of the compilation 243 | GLint test; 244 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 245 | if(!test) { 246 | std::cerr << "Shader compilation failed with this message:" << std::endl; 247 | std::vector compilation_log(512); 248 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 249 | std::cerr << &compilation_log[0] << std::endl; 250 | glfwTerminate(); 251 | exit(-1); 252 | } 253 | return shader; 254 | } 255 | 256 | // Create a program from two shaders 257 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 258 | // Load and compile the vertex and fragment shaders 259 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 260 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 261 | 262 | // Attach the above shader to a program 263 | GLuint shaderProgram = glCreateProgram(); 264 | glAttachShader(shaderProgram, vertexShader); 265 | glAttachShader(shaderProgram, fragmentShader); 266 | 267 | // Flag the shaders for deletion 268 | glDeleteShader(vertexShader); 269 | glDeleteShader(fragmentShader); 270 | 271 | // Link and use the program 272 | glLinkProgram(shaderProgram); 273 | glUseProgram(shaderProgram); 274 | 275 | return shaderProgram; 276 | } 277 | 278 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_9/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 color_from_vshader; 4 | out vec4 out_color; 5 | 6 | void main() { 7 | out_color = color_from_vshader; 8 | } 9 | -------------------------------------------------------------------------------- /Drawing_primitives/ex_9/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | in vec4 color; 5 | out vec4 color_from_vshader; 6 | 7 | void main() { 8 | gl_Position = position; 9 | color_from_vshader = color; 10 | } 11 | -------------------------------------------------------------------------------- /Getting_Started/ex_0.cpp: -------------------------------------------------------------------------------- 1 | // Open a window with GLFW, check default OpenGL version, set background color red 2 | #include 3 | #include 4 | #include 5 | 6 | int main () { 7 | // Initialize GLFW 8 | if ( !glfwInit()) { 9 | std::cerr << "Failed to initialize GLFW! I'm out!" << '\n'; 10 | exit(-1); 11 | } 12 | 13 | // Open a window and attach an OpenGL context to the window surface 14 | GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL 101", NULL, NULL); 15 | if (!window) 16 | { 17 | std::cerr << "Failed to open a window! I'm out!" << '\n'; 18 | glfwTerminate(); 19 | exit(-1); 20 | } 21 | 22 | // Set the window context current 23 | glfwMakeContextCurrent(window); 24 | 25 | // Set the swap interval, 1 will use your screen refresh rate (vsync) 26 | glfwSwapInterval(1); 27 | 28 | // Optionally, setup an OpenGL extension loader library 29 | // ... 30 | 31 | // After this point you can start using any available OpenGL function 32 | // ================================================================== 33 | 34 | // Print the OpenGL version currently enabled on your machine 35 | std::cout << glGetString(GL_VERSION) << '\n'; 36 | 37 | // Use red to clear the screen 38 | glClearColor(1, 0, 0, 1); 39 | 40 | // Create a rendering loop that runs until the window is closed 41 | while (!glfwWindowShouldClose(window)) { 42 | // Clear the screen (window background) 43 | glClear(GL_COLOR_BUFFER_BIT); 44 | 45 | // Draw 46 | // ... 47 | 48 | // Swap front and back buffers for the current window 49 | glfwSwapBuffers(window); 50 | 51 | // Poll for events 52 | glfwPollEvents(); 53 | } 54 | 55 | // Destroy the window and its context 56 | glfwDestroyWindow(window); 57 | 58 | // Terminate GLFW 59 | glfwTerminate(); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Getting_Started/ex_1.cpp: -------------------------------------------------------------------------------- 1 | // Open a window with GLFW, check default OpenGL version, set background color red 2 | // Register callback functions for GLFW errors, key pressed and window resized events 3 | #include 4 | #include 5 | #include 6 | 7 | // Define a few callback functions: 8 | void window_resized(GLFWwindow* window, int width, int height); 9 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods); 10 | void show_glfw_error(int error, const char* description); 11 | 12 | 13 | int main () { 14 | // Register the GLFW error callback function 15 | glfwSetErrorCallback(show_glfw_error); 16 | 17 | // Initialize GLFW 18 | if ( !glfwInit()) { 19 | std::cerr << "Failed to initialize GLFW! I'm out!" << '\n'; 20 | exit(-1); 21 | } 22 | 23 | // Open a window and attach an OpenGL context to the window surface 24 | GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL 101", NULL, NULL); 25 | if (!window) 26 | { 27 | std::cerr << "Failed to open a window! I'm out!" << '\n'; 28 | glfwTerminate(); 29 | exit(-1); 30 | } 31 | 32 | // Set the window context current 33 | glfwMakeContextCurrent(window); 34 | 35 | // Register the GLFW window resized callback function 36 | glfwSetWindowSizeCallback(window, window_resized); 37 | 38 | // Register the GLFW window key pressed callback function 39 | glfwSetKeyCallback(window, key_pressed); 40 | 41 | 42 | // Set the swap interval, 1 will use your screen refresh rate (vsync) 43 | glfwSwapInterval(1); 44 | 45 | // Optionally, setup an OpenGL extension loader library 46 | // ... 47 | 48 | // After this point you can start using any available OpenGL function 49 | // ================================================================== 50 | 51 | // Print the OpenGL version currently enabled on your machine 52 | std::cout << glGetString(GL_VERSION) << '\n'; 53 | 54 | // Use red to clear the screen 55 | glClearColor(1, 0, 0, 1); 56 | 57 | // Create a rendering loop that runs until the window is closed 58 | while (!glfwWindowShouldClose(window)) { 59 | // Clear the screen (window background) 60 | glClear(GL_COLOR_BUFFER_BIT); 61 | 62 | // Draw 63 | // ... 64 | 65 | // Swap front and back buffers for the current window 66 | glfwSwapBuffers(window); 67 | 68 | // Poll for events 69 | glfwPollEvents(); 70 | } 71 | 72 | // Destroy the window and its context 73 | glfwDestroyWindow(window); 74 | 75 | // Terminate GLFW 76 | glfwTerminate(); 77 | return 0; 78 | } 79 | 80 | void show_glfw_error(int error, const char* description) { 81 | std::cerr << "Error: " << description << '\n'; 82 | } 83 | 84 | void window_resized(GLFWwindow* window, int width, int height) { 85 | std::cout << "Window resized, new window size: " << width << " x " << height << '\n'; 86 | } 87 | 88 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods) { 89 | if(key == 'Q' && action == GLFW_PRESS) { 90 | glfwTerminate(); 91 | exit(0); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /Getting_Started/ex_2.cpp: -------------------------------------------------------------------------------- 1 | // Open a window with GLFW, check default OpenGL version, set background color red 2 | // Register callback functions for GLFW errors, key pressed and window resized events 3 | // Draw a square using the deprecated fixed pipeline functionality (e.g. glBegin/glEnd) 4 | #include 5 | #include 6 | #include 7 | 8 | // Define a few callback functions: 9 | void window_resized(GLFWwindow* window, int width, int height); 10 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods); 11 | void show_glfw_error(int error, const char* description); 12 | 13 | 14 | int main () { 15 | // Register the GLFW error callback function 16 | glfwSetErrorCallback(show_glfw_error); 17 | 18 | // Initialize GLFW 19 | if ( !glfwInit()) { 20 | std::cerr << "Failed to initialize GLFW! I'm out!" << '\n'; 21 | exit(-1); 22 | } 23 | 24 | // Open a window and attach an OpenGL context to the window surface 25 | GLFWwindow* window = glfwCreateWindow(600, 600, "OpenGL 101", NULL, NULL); 26 | if (!window) 27 | { 28 | std::cerr << "Failed to open a window! I'm out!" << '\n'; 29 | glfwTerminate(); 30 | exit(-1); 31 | } 32 | 33 | // Set the window context current 34 | glfwMakeContextCurrent(window); 35 | 36 | // Register the GLFW window resized callback function 37 | glfwSetWindowSizeCallback(window, window_resized); 38 | 39 | // Register the GLFW window key pressed callback function 40 | glfwSetKeyCallback(window, key_pressed); 41 | 42 | 43 | // Set the swap interval, 1 will use your screen refresh rate (vsync) 44 | glfwSwapInterval(1); 45 | 46 | // Optionally, setup an OpenGL extension loader library 47 | // ... 48 | 49 | // After this point you can start using any available OpenGL function 50 | // ================================================================== 51 | 52 | // Print the OpenGL version currently enabled on your machine 53 | std::cout << glGetString(GL_VERSION) << '\n'; 54 | 55 | // Use red to clear the screen 56 | glClearColor(1, 0, 0, 1); 57 | 58 | // Create a rendering loop that runs until the window is closed 59 | while (!glfwWindowShouldClose(window)) { 60 | // Clear the screen (window background) 61 | glClear(GL_COLOR_BUFFER_BIT); 62 | 63 | // Draw a square using the (deprecated) fixed pipeline functionality 64 | glColor3f(1.0f, 1.0f, 0.0f); 65 | glBegin(GL_QUADS); 66 | glVertex2f(-0.5f, -0.5f); 67 | glVertex2f(0.5f, -0.5f); 68 | glVertex2f(0.5f, 0.5f); 69 | glVertex2f(-0.5, 0.5f); 70 | glEnd(); 71 | 72 | // Swap front and back buffers for the current window 73 | glfwSwapBuffers(window); 74 | 75 | // Poll for events 76 | glfwPollEvents(); 77 | } 78 | 79 | // Destroy the window and its context 80 | glfwDestroyWindow(window); 81 | 82 | // Terminate GLFW 83 | glfwTerminate(); 84 | return 0; 85 | } 86 | 87 | void show_glfw_error(int error, const char* description) { 88 | std::cerr << "Error: " << description << '\n'; 89 | } 90 | 91 | void window_resized(GLFWwindow* window, int width, int height) { 92 | std::cout << "Window resized, new window size: " << width << " x " << height << '\n'; 93 | } 94 | 95 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods) { 96 | if(key == 'Q' && action == GLFW_PRESS) { 97 | glfwTerminate(); 98 | exit(0); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Getting_Started/ex_3.cpp: -------------------------------------------------------------------------------- 1 | // Open a window with GLFW, check default OpenGL version, set background color red 2 | // Register callback functions for GLFW errors, key pressed and window resized events 3 | // Require minimum OpenGL 3.2 core profile and remove the fixed pipeline functionality 4 | // Note that the code for drawing a square using glBegin/glEnd is ignored (has no effect) 5 | #include 6 | #include 7 | #include 8 | 9 | // Define a few callback functions: 10 | void window_resized(GLFWwindow* window, int width, int height); 11 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods); 12 | void show_glfw_error(int error, const char* description); 13 | 14 | 15 | int main () { 16 | // Register the GLFW error callback function 17 | glfwSetErrorCallback(show_glfw_error); 18 | 19 | // Initialize GLFW 20 | if ( !glfwInit()) { 21 | std::cerr << "Failed to initialize GLFW! I'm out!" << '\n'; 22 | exit(-1); 23 | } 24 | 25 | // Require minimum OpenGL 3.2 core profile and remove the fixed pipeline functionality 26 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 27 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 28 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 29 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 30 | 31 | // Open a window and attach an OpenGL context to the window surface 32 | GLFWwindow* window = glfwCreateWindow(600, 600, "OpenGL 101", NULL, NULL); 33 | if (!window) 34 | { 35 | std::cerr << "Failed to open a window! I'm out!" << '\n'; 36 | glfwTerminate(); 37 | exit(-1); 38 | } 39 | 40 | // Set the window context current 41 | glfwMakeContextCurrent(window); 42 | 43 | // Register the GLFW window resized callback function 44 | glfwSetWindowSizeCallback(window, window_resized); 45 | 46 | // Register the GLFW window key pressed callback function 47 | glfwSetKeyCallback(window, key_pressed); 48 | 49 | 50 | // Set the swap interval, 1 will use your screen refresh rate (vsync) 51 | glfwSwapInterval(1); 52 | 53 | // Optionally, setup an OpenGL extension loader library 54 | // ... 55 | 56 | // After this point you can start using any available OpenGL function 57 | // ================================================================== 58 | 59 | // Print the OpenGL version currently enabled on your machine 60 | std::cout << glGetString(GL_VERSION) << '\n'; 61 | 62 | // Use red to clear the screen 63 | glClearColor(1, 0, 0, 1); 64 | 65 | // Create a rendering loop that runs until the window is closed 66 | while (!glfwWindowShouldClose(window)) { 67 | // Clear the screen (window background) 68 | glClear(GL_COLOR_BUFFER_BIT); 69 | 70 | // The code between glBegin/glEnd has no effect when you use only the core OpenGL functionality 71 | // Draw a square using the (deprecated) fixed pipeline functionality 72 | glColor3f(1.0f, 1.0f, 0.0f); 73 | glBegin(GL_QUADS); 74 | glVertex2f(-0.5f, -0.5f); 75 | glVertex2f(0.5f, -0.5f); 76 | glVertex2f(0.5f, 0.5f); 77 | glVertex2f(-0.5, 0.5f); 78 | glEnd(); 79 | 80 | // Swap front and back buffers for the current window 81 | glfwSwapBuffers(window); 82 | 83 | // Poll for events 84 | glfwPollEvents(); 85 | } 86 | 87 | // Destroy the window and its context 88 | glfwDestroyWindow(window); 89 | 90 | // Terminate GLFW 91 | glfwTerminate(); 92 | return 0; 93 | } 94 | 95 | void show_glfw_error(int error, const char* description) { 96 | std::cerr << "Error: " << description << '\n'; 97 | } 98 | 99 | void window_resized(GLFWwindow* window, int width, int height) { 100 | std::cout << "Window resized, new window size: " << width << " x " << height << '\n'; 101 | } 102 | 103 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods) { 104 | if(key == 'Q' && action == GLFW_PRESS) { 105 | glfwTerminate(); 106 | exit(0); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Getting_Started/ex_4.cpp: -------------------------------------------------------------------------------- 1 | // THIS WILL GENERATE A COMPILATION ERROR, see ex_5.cpp for the corrected version 2 | // Open a window with GLFW, check default OpenGL version, set background color red 3 | // Register callback functions for GLFW errors, key pressed and window resized events 4 | // Require minimum OpenGL 3.2 core profile and remove the fixed pipeline functionality 5 | #include 6 | #include 7 | #include 8 | 9 | // Define a few callback functions: 10 | void window_resized(GLFWwindow* window, int width, int height); 11 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods); 12 | void show_glfw_error(int error, const char* description); 13 | 14 | 15 | int main () { 16 | // Register the GLFW error callback function 17 | glfwSetErrorCallback(show_glfw_error); 18 | 19 | // Initialize GLFW 20 | if ( !glfwInit()) { 21 | std::cerr << "Failed to initialize GLFW! I'm out!" << '\n'; 22 | exit(-1); 23 | } 24 | 25 | // Require minimum OpenGL 3.2 core profile and remove the fixed pipeline functionality 26 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 27 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 28 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 29 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 30 | 31 | // Open a window and attach an OpenGL context to the window surface 32 | GLFWwindow* window = glfwCreateWindow(600, 600, "OpenGL 101", NULL, NULL); 33 | if (!window) 34 | { 35 | std::cerr << "Failed to open a window! I'm out!" << '\n'; 36 | glfwTerminate(); 37 | exit(-1); 38 | } 39 | 40 | // Set the window context current 41 | glfwMakeContextCurrent(window); 42 | 43 | // Register the GLFW window resized callback function 44 | glfwSetWindowSizeCallback(window, window_resized); 45 | 46 | // Register the GLFW window key pressed callback function 47 | glfwSetKeyCallback(window, key_pressed); 48 | 49 | 50 | // Set the swap interval, 1 will use your screen refresh rate (vsync) 51 | glfwSwapInterval(1); 52 | 53 | // Optionally, setup an OpenGL extension loader library 54 | // ... 55 | 56 | // After this point you can start using any available OpenGL function 57 | // ================================================================== 58 | 59 | // Print the OpenGL version currently enabled on your machine 60 | std::cout << glGetString(GL_VERSION) << '\n'; 61 | 62 | // Print the available OpenGL extensions 63 | // This code should generate a compiler error on Windows, Linux and macOS, because the function 64 | // "glGetStringi" is not loaded by default 65 | // In order to get rid of the above error we need to write our own version or use an OpenGL extension loader library 66 | // like Glew, Glad ... 67 | int nr_extensions = 0; 68 | glGetIntegerv(GL_EXTENSIONS, &nr_extensions); 69 | 70 | for(int i = 0; i < nr_extensions; ++i) { 71 | std::cout << glGetStringi(GL_EXTENSIONS, i) << '\n'; 72 | } 73 | 74 | 75 | 76 | // Use red to clear the screen 77 | glClearColor(1, 0, 0, 1); 78 | 79 | // Create a rendering loop that runs until the window is closed 80 | while (!glfwWindowShouldClose(window)) { 81 | // Clear the screen (window background) 82 | glClear(GL_COLOR_BUFFER_BIT); 83 | 84 | // Draw code here ... 85 | 86 | // Swap front and back buffers for the current window 87 | glfwSwapBuffers(window); 88 | 89 | // Poll for events 90 | glfwPollEvents(); 91 | } 92 | 93 | // Destroy the window and its context 94 | glfwDestroyWindow(window); 95 | 96 | // Terminate GLFW 97 | glfwTerminate(); 98 | return 0; 99 | } 100 | 101 | void show_glfw_error(int error, const char* description) { 102 | std::cerr << "Error: " << description << '\n'; 103 | } 104 | 105 | void window_resized(GLFWwindow* window, int width, int height) { 106 | std::cout << "Window resized, new window size: " << width << " x " << height << '\n'; 107 | } 108 | 109 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods) { 110 | if(key == 'Q' && action == GLFW_PRESS) { 111 | glfwTerminate(); 112 | exit(0); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /Getting_Started/ex_5.cpp: -------------------------------------------------------------------------------- 1 | // Open a window with GLFW, check default OpenGL version, set background color red 2 | // Register callback functions for GLFW errors, key pressed and window resized events 3 | // Require minimum OpenGL 3.2 core profile and remove the fixed pipeline functionality 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // Define a few callback functions: 10 | void window_resized(GLFWwindow* window, int width, int height); 11 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods); 12 | void show_glfw_error(int error, const char* description); 13 | 14 | 15 | int main () { 16 | // Register the GLFW error callback function 17 | glfwSetErrorCallback(show_glfw_error); 18 | 19 | // Initialize GLFW 20 | if ( !glfwInit()) { 21 | std::cerr << "Failed to initialize GLFW! I'm out!" << '\n'; 22 | exit(-1); 23 | } 24 | 25 | // Require minimum OpenGL 3.2 core profile and remove the fixed pipeline functionality 26 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 27 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 28 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 29 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 30 | 31 | // Open a window and attach an OpenGL context to the window surface 32 | GLFWwindow* window = glfwCreateWindow(600, 600, "OpenGL 101", NULL, NULL); 33 | if (!window) 34 | { 35 | std::cerr << "Failed to open a window! I'm out!" << '\n'; 36 | glfwTerminate(); 37 | exit(-1); 38 | } 39 | 40 | // Set the window context current 41 | glfwMakeContextCurrent(window); 42 | 43 | // Register the GLFW window resized callback function 44 | glfwSetWindowSizeCallback(window, window_resized); 45 | 46 | // Register the GLFW window key pressed callback function 47 | glfwSetKeyCallback(window, key_pressed); 48 | 49 | 50 | // Set the swap interval, 1 will use your screen refresh rate (vsync) 51 | glfwSwapInterval(1); 52 | 53 | // Optionally, setup an OpenGL extension loader library 54 | GLenum err = glewInit(); 55 | if(err != GLEW_OK) { 56 | std::cerr << "GLEW failed to initialize with error: " << glewGetErrorString(err) << '\n'; 57 | glfwTerminate(); 58 | exit(-1); 59 | } 60 | 61 | // After this point you can start using any available OpenGL function 62 | // ================================================================== 63 | 64 | // Print the OpenGL version currently enabled on your machine 65 | std::cout << glGetString(GL_VERSION) << '\n'; 66 | 67 | // Print the available OpenGL extensions 68 | // This code should generate a compiler error on Windows, Linux and macOS, because the function 69 | // "glGetStringi" is not loaded by default 70 | // In order to get rid of the above error we need to write our own version or use an OpenGL extension loader library 71 | // like Glew, Glad ... 72 | int nr_extensions = 0; 73 | glGetIntegerv(GL_NUM_EXTENSIONS, &nr_extensions); 74 | 75 | for(int i = 0; i < nr_extensions; ++i) { 76 | std::cout << glGetStringi(GL_EXTENSIONS, i) << '\n'; 77 | } 78 | 79 | // Use red to clear the screen 80 | glClearColor(1, 0, 0, 1); 81 | 82 | // Create a rendering loop that runs until the window is closed 83 | while (!glfwWindowShouldClose(window)) { 84 | // Clear the screen (window background) 85 | glClear(GL_COLOR_BUFFER_BIT); 86 | 87 | // Draw code here ... 88 | 89 | // Swap front and back buffers for the current window 90 | glfwSwapBuffers(window); 91 | 92 | // Poll for events 93 | glfwPollEvents(); 94 | } 95 | 96 | // Destroy the window and its context 97 | glfwDestroyWindow(window); 98 | 99 | // Terminate GLFW 100 | glfwTerminate(); 101 | return 0; 102 | } 103 | 104 | void show_glfw_error(int error, const char* description) { 105 | std::cerr << "Error: " << description << '\n'; 106 | } 107 | 108 | void window_resized(GLFWwindow* window, int width, int height) { 109 | std::cout << "Window resized, new window size: " << width << " x " << height << '\n'; 110 | } 111 | 112 | void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods) { 113 | if(key == 'Q' && action == GLFW_PRESS) { 114 | glfwTerminate(); 115 | exit(0); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | OpenGL-101 2 | ============== 3 | 4 | Here you could find the code for **OpenGL 101**, for more informations visit the project webpages: 5 | 6 | https://solarianprogrammer.com/2013/05/10/opengl-101-windows-osx-linux-getting-started/ 7 | 8 | https://solarianprogrammer.com/2013/05/13/opengl-101-drawing-primitives/ 9 | 10 | https://solarianprogrammer.com/2013/05/17/opengl-101-textures/ 11 | 12 | https://solarianprogrammer.com/2013/05/22/opengl-101-matrices-projection-view-model/ 13 | 14 | You could use these programs under the terms of GPL v3, for more details see: 15 | 16 | http://www.gnu.org/copyleft/gpl.html 17 | 18 | Copyright 2016 Sol from www.solarianprogrammer.com 19 | -------------------------------------------------------------------------------- /Textures/ex_10/ex_10.cpp: -------------------------------------------------------------------------------- 1 | // Draw four triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | //#include 9 | #include 10 | 11 | // Read a shader source from a file 12 | // store the shader source in a std::vector 13 | void read_shader_src(const char *fname, std::vector &buffer); 14 | 15 | // Compile a shader 16 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 17 | 18 | // Create a program from two shaders 19 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 20 | 21 | // Called when the window is resized 22 | void GLFWCALL window_resized(int width, int height); 23 | 24 | // Called for keyboard events 25 | void keyboard(int key, int action); 26 | 27 | // Render scene 28 | void display(GLuint &vao); 29 | 30 | // Initialize the data to be rendered 31 | void initialize(GLuint &vao); 32 | 33 | // Load an image from the disk with FreeImage 34 | void load_image(const char *fname); 35 | 36 | int main () { 37 | // Initialize GLFW 38 | if ( !glfwInit()) { 39 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 40 | exit(-1); 41 | } 42 | 43 | // Use OpenGL 3.2 core profile 44 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 45 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 46 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 47 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 48 | 49 | // Open a window and attach an OpenGL rendering context to the window surface 50 | if( !glfwOpenWindow(800, 600, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 51 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 52 | glfwTerminate(); 53 | exit(-1); 54 | } 55 | 56 | // Register a callback function for window resize events 57 | glfwSetWindowSizeCallback( window_resized ); 58 | 59 | // Register a callback function for keyboard pressed events 60 | glfwSetKeyCallback(keyboard); 61 | 62 | // Print the OpenGL version 63 | int major, minor, rev; 64 | glfwGetGLVersion(&major, &minor, &rev); 65 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 66 | 67 | // Initialize GLEW 68 | glewExperimental = GL_TRUE; 69 | if(glewInit() != GLEW_OK) { 70 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 71 | glfwTerminate(); 72 | exit(-1); 73 | } 74 | 75 | // Create a vertex array object 76 | GLuint vao; 77 | 78 | // Initialize the data to be rendered 79 | initialize(vao); 80 | 81 | // Create a rendering loop 82 | int running = GL_TRUE; 83 | 84 | while(running) { 85 | // Display scene 86 | display(vao); 87 | 88 | // Pool for events 89 | glfwPollEvents(); 90 | // Check if the window was closed 91 | running = glfwGetWindowParam(GLFW_OPENED); 92 | } 93 | 94 | // Terminate GLFW 95 | glfwTerminate(); 96 | 97 | return 0; 98 | } 99 | 100 | // Render scene 101 | void display(GLuint &vao) { 102 | glClear(GL_COLOR_BUFFER_BIT); 103 | 104 | glBindVertexArray(vao); 105 | glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 106 | 107 | // Swap front and back buffers 108 | glfwSwapBuffers(); 109 | } 110 | 111 | void initialize(GLuint &vao) { 112 | // Use a Vertex Array Object 113 | glGenVertexArrays(1, &vao); 114 | glBindVertexArray(vao); 115 | 116 | // 1 square (made by 2 triangles) to be rendered 117 | GLfloat vertices_position[8] = { 118 | -0.5, -0.5, 119 | 0.5, -0.5, 120 | 0.5, 0.5, 121 | -0.5, 0.5, 122 | }; 123 | 124 | GLfloat texture_coord[8] = { 125 | 0.0, 0.0, 126 | 1.0, 0.0, 127 | 1.0, 1.0, 128 | 0.0, 1.0, 129 | }; 130 | 131 | GLuint indices[6] = { 132 | 0, 1, 2, 133 | 2, 3, 0 134 | }; 135 | 136 | // Create a Vector Buffer Object that will store the vertices on video memory 137 | GLuint vbo; 138 | glGenBuffers(1, &vbo); 139 | 140 | // Allocate space for vertex positions and texture coordinates 141 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 142 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position) + sizeof(texture_coord), NULL, GL_STATIC_DRAW); 143 | 144 | // Transfer the vertex positions: 145 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices_position), vertices_position); 146 | 147 | // Transfer the texture coordinates: 148 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices_position), sizeof(texture_coord), texture_coord); 149 | 150 | // Create an Element Array Buffer that will store the indices array: 151 | GLuint eab; 152 | glGenBuffers(1, &eab); 153 | 154 | // Transfer the data from indices to eab 155 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab); 156 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 157 | 158 | // Create a texture 159 | GLuint texture; 160 | glGenTextures(1, &texture); 161 | 162 | // Specify that we work with a 2D texture 163 | glBindTexture(GL_TEXTURE_2D, texture); 164 | 165 | load_image("squirrel.jpg"); 166 | 167 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 168 | 169 | // Get the location of the attributes that enters in the vertex shader 170 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 171 | 172 | // Specify how the data for position can be accessed 173 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 174 | 175 | // Enable the attribute 176 | glEnableVertexAttribArray(position_attribute); 177 | 178 | // Texture coord attribute 179 | GLint texture_coord_attribute = glGetAttribLocation(shaderProgram, "texture_coord"); 180 | glVertexAttribPointer(texture_coord_attribute, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid *)sizeof(vertices_position)); 181 | glEnableVertexAttribArray(texture_coord_attribute); 182 | 183 | } 184 | 185 | void load_image(const char *fname) { 186 | 187 | // active only for static linking 188 | #ifdef FREEIMAGE_LIB 189 | FreeImage_Initialise(); 190 | #endif 191 | 192 | FIBITMAP *bitmap; 193 | // Get the format of the image file 194 | FREE_IMAGE_FORMAT fif =FreeImage_GetFileType(fname, 0); 195 | 196 | // If the format can't be determined, try to guess the format from the file name 197 | if(fif == FIF_UNKNOWN) { 198 | fif = FreeImage_GetFIFFromFilename(fname); 199 | } 200 | 201 | // Load the data in bitmap if possible 202 | if(fif != FIF_UNKNOWN && FreeImage_FIFSupportsReading(fif)) { 203 | bitmap = FreeImage_Load(fif, fname); 204 | } 205 | else { 206 | bitmap = NULL; 207 | } 208 | 209 | // PROCESS IMAGE if bitmap was successfully initialized 210 | if(bitmap) { 211 | unsigned int w = FreeImage_GetWidth(bitmap); 212 | unsigned int h = FreeImage_GetHeight(bitmap); 213 | unsigned pixel_size = FreeImage_GetBPP(bitmap); 214 | 215 | // Get a pointer to the pixel data 216 | BYTE *data = (BYTE*)FreeImage_GetBits(bitmap); 217 | 218 | // Process only RGB and RGBA images 219 | if(pixel_size == 24) { 220 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, (GLvoid*)data); 221 | } 222 | else if (pixel_size == 32) { 223 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)data); 224 | } 225 | else { 226 | std::cerr << "pixel size = " << pixel_size << " don't know how to process this case. I'm out!" << std::endl; 227 | exit(-1); 228 | } 229 | 230 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 231 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 232 | } 233 | else { 234 | std::cerr << "Unable to load the image file " << fname << " I'm out!" << std::endl; 235 | exit(-1); 236 | } 237 | 238 | // Clean bitmap; 239 | FreeImage_Unload(bitmap); 240 | 241 | // active only for static linking 242 | #ifdef FREEIMAGE_LIB 243 | FreeImage_DeInitialise(); 244 | #endif 245 | } 246 | 247 | 248 | // Called when the window is resized 249 | void GLFWCALL window_resized(int width, int height) { 250 | // Use red to clear the screen 251 | //glClearColor(1, 0, 0, 1); 252 | 253 | // Set the viewport 254 | glViewport(0, 0, width, height); 255 | 256 | glClear(GL_COLOR_BUFFER_BIT); 257 | glfwSwapBuffers(); 258 | } 259 | 260 | // Called for keyboard events 261 | void keyboard(int key, int action) { 262 | if(key == 'Q' && action == GLFW_PRESS) { 263 | glfwTerminate(); 264 | exit(0); 265 | } 266 | } 267 | 268 | // Read a shader source from a file 269 | // store the shader source in a std::vector 270 | void read_shader_src(const char *fname, std::vector &buffer) { 271 | std::ifstream in; 272 | in.open(fname, std::ios::binary); 273 | 274 | if(in.is_open()) { 275 | // Get the number of bytes stored in this file 276 | in.seekg(0, std::ios::end); 277 | size_t length = (size_t)in.tellg(); 278 | 279 | // Go to start of the file 280 | in.seekg(0, std::ios::beg); 281 | 282 | // Read the content of the file in a buffer 283 | buffer.resize(length + 1); 284 | in.read(&buffer[0], length); 285 | in.close(); 286 | // Add a valid C - string end 287 | buffer[length] = '\0'; 288 | } 289 | else { 290 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 291 | exit(-1); 292 | } 293 | } 294 | 295 | // Compile a shader 296 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 297 | // Load a shader from an external file 298 | std::vector buffer; 299 | read_shader_src(fname, buffer); 300 | const char *src = &buffer[0]; 301 | 302 | // Compile the shader 303 | GLuint shader = glCreateShader(shaderType); 304 | glShaderSource(shader, 1, &src, NULL); 305 | glCompileShader(shader); 306 | // Check the result of the compilation 307 | GLint test; 308 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 309 | if(!test) { 310 | std::cerr << "Shader compilation failed with this message:" << std::endl; 311 | std::vector compilation_log(512); 312 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 313 | std::cerr << &compilation_log[0] << std::endl; 314 | glfwTerminate(); 315 | exit(-1); 316 | } 317 | return shader; 318 | } 319 | 320 | // Create a program from two shaders 321 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 322 | // Load and compile the vertex and fragment shaders 323 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 324 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 325 | 326 | // Attach the above shader to a program 327 | GLuint shaderProgram = glCreateProgram(); 328 | glAttachShader(shaderProgram, vertexShader); 329 | glAttachShader(shaderProgram, fragmentShader); 330 | 331 | // Flag the shaders for deletion 332 | glDeleteShader(vertexShader); 333 | glDeleteShader(fragmentShader); 334 | 335 | // Link and use the program 336 | glLinkProgram(shaderProgram); 337 | glUseProgram(shaderProgram); 338 | 339 | return shaderProgram; 340 | } 341 | 342 | -------------------------------------------------------------------------------- /Textures/ex_10/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec2 texture_coord_from_vshader; 4 | out vec4 out_color; 5 | 6 | uniform sampler2D texture_sampler; 7 | 8 | void main() { 9 | out_color = texture(texture_sampler, texture_coord_from_vshader); 10 | } 11 | -------------------------------------------------------------------------------- /Textures/ex_10/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | in vec2 texture_coord; 5 | out vec2 texture_coord_from_vshader; 6 | 7 | void main() { 8 | gl_Position = position; 9 | texture_coord_from_vshader = texture_coord; 10 | } 11 | -------------------------------------------------------------------------------- /Textures/ex_10/squirrel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sol-prog/OpenGL-101/ac06e83509d8c16deb3c7a154a06c252ebb946c8/Textures/ex_10/squirrel.jpg -------------------------------------------------------------------------------- /Textures/ex_11/ex_11.cpp: -------------------------------------------------------------------------------- 1 | // Draw four triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | //#include 9 | #include 10 | 11 | // Read a shader source from a file 12 | // store the shader source in a std::vector 13 | void read_shader_src(const char *fname, std::vector &buffer); 14 | 15 | // Compile a shader 16 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 17 | 18 | // Create a program from two shaders 19 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 20 | 21 | // Called when the window is resized 22 | void GLFWCALL window_resized(int width, int height); 23 | 24 | // Called for keyboard events 25 | void keyboard(int key, int action); 26 | 27 | // Render scene 28 | void display(GLuint &vao); 29 | 30 | // Initialize the data to be rendered 31 | void initialize(GLuint &vao); 32 | 33 | // Load an image from the disk with FreeImage 34 | void load_image(const char *fname); 35 | 36 | int main () { 37 | // Initialize GLFW 38 | if ( !glfwInit()) { 39 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 40 | exit(-1); 41 | } 42 | 43 | // Use OpenGL 3.2 core profile 44 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 45 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 46 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 47 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 48 | 49 | // Open a window and attach an OpenGL rendering context to the window surface 50 | if( !glfwOpenWindow(800, 600, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 51 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 52 | glfwTerminate(); 53 | exit(-1); 54 | } 55 | 56 | // Register a callback function for window resize events 57 | glfwSetWindowSizeCallback( window_resized ); 58 | 59 | // Register a callback function for keyboard pressed events 60 | glfwSetKeyCallback(keyboard); 61 | 62 | // Print the OpenGL version 63 | int major, minor, rev; 64 | glfwGetGLVersion(&major, &minor, &rev); 65 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 66 | 67 | // Initialize GLEW 68 | glewExperimental = GL_TRUE; 69 | if(glewInit() != GLEW_OK) { 70 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 71 | glfwTerminate(); 72 | exit(-1); 73 | } 74 | 75 | // Create a vertex array object 76 | GLuint vao; 77 | 78 | // Initialize the data to be rendered 79 | initialize(vao); 80 | 81 | // Create a rendering loop 82 | int running = GL_TRUE; 83 | 84 | while(running) { 85 | // Display scene 86 | display(vao); 87 | 88 | // Pool for events 89 | glfwPollEvents(); 90 | // Check if the window was closed 91 | running = glfwGetWindowParam(GLFW_OPENED); 92 | } 93 | 94 | // Terminate GLFW 95 | glfwTerminate(); 96 | 97 | return 0; 98 | } 99 | 100 | // Render scene 101 | void display(GLuint &vao) { 102 | glClear(GL_COLOR_BUFFER_BIT); 103 | 104 | glBindVertexArray(vao); 105 | glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 106 | 107 | // Swap front and back buffers 108 | glfwSwapBuffers(); 109 | } 110 | 111 | void initialize(GLuint &vao) { 112 | // Use a Vertex Array Object 113 | glGenVertexArrays(1, &vao); 114 | glBindVertexArray(vao); 115 | 116 | // 1 square (made by 2 triangles) to be rendered 117 | GLfloat vertices_position[8] = { 118 | -1.0, -1.0, 119 | 1.0, -1.0, 120 | 1.0, 1.0, 121 | -1.0, 1.0, 122 | }; 123 | 124 | GLfloat texture_coord[8] = { 125 | 0.0, 0.0, 126 | 1.0, 0.0, 127 | 1.0, 1.0, 128 | 0.0, 1.0, 129 | }; 130 | 131 | GLuint indices[6] = { 132 | 0, 1, 2, 133 | 2, 3, 0 134 | }; 135 | 136 | // Create a Vector Buffer Object that will store the vertices on video memory 137 | GLuint vbo; 138 | glGenBuffers(1, &vbo); 139 | 140 | // Allocate space for vertex positions and texture coordinates 141 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 142 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position) + sizeof(texture_coord), NULL, GL_STATIC_DRAW); 143 | 144 | // Transfer the vertex positions: 145 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices_position), vertices_position); 146 | 147 | // Transfer the texture coordinates: 148 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices_position), sizeof(texture_coord), texture_coord); 149 | 150 | // Create an Element Array Buffer that will store the indices array: 151 | GLuint eab; 152 | glGenBuffers(1, &eab); 153 | 154 | // Transfer the data from indices to eab 155 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab); 156 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 157 | 158 | // Create a texture 159 | GLuint texture; 160 | glGenTextures(1, &texture); 161 | 162 | // Specify that we work with a 2D texture 163 | glBindTexture(GL_TEXTURE_2D, texture); 164 | 165 | load_image("squirrel.jpg"); 166 | 167 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 168 | 169 | // Get the location of the attributes that enters in the vertex shader 170 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 171 | 172 | // Specify how the data for position can be accessed 173 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 174 | 175 | // Enable the attribute 176 | glEnableVertexAttribArray(position_attribute); 177 | 178 | // Texture coord attribute 179 | GLint texture_coord_attribute = glGetAttribLocation(shaderProgram, "texture_coord"); 180 | glVertexAttribPointer(texture_coord_attribute, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid *)sizeof(vertices_position)); 181 | glEnableVertexAttribArray(texture_coord_attribute); 182 | 183 | } 184 | 185 | void load_image(const char *fname) { 186 | 187 | // active only for static linking 188 | #ifdef FREEIMAGE_LIB 189 | FreeImage_Initialise(); 190 | #endif 191 | 192 | FIBITMAP *bitmap; 193 | // Get the format of the image file 194 | FREE_IMAGE_FORMAT fif =FreeImage_GetFileType(fname, 0); 195 | 196 | // If the format can't be determined, try to guess the format from the file name 197 | if(fif == FIF_UNKNOWN) { 198 | fif = FreeImage_GetFIFFromFilename(fname); 199 | } 200 | 201 | // Load the data in bitmap if possible 202 | if(fif != FIF_UNKNOWN && FreeImage_FIFSupportsReading(fif)) { 203 | bitmap = FreeImage_Load(fif, fname); 204 | } 205 | else { 206 | bitmap = NULL; 207 | } 208 | 209 | // PROCESS IMAGE if bitmap was successfully initialized 210 | if(bitmap) { 211 | unsigned int w = FreeImage_GetWidth(bitmap); 212 | unsigned int h = FreeImage_GetHeight(bitmap); 213 | unsigned pixel_size = FreeImage_GetBPP(bitmap); 214 | 215 | // Get a pointer to the pixel data 216 | BYTE *data = (BYTE*)FreeImage_GetBits(bitmap); 217 | 218 | // Process only RGB and RGBA images 219 | if(pixel_size == 24) { 220 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, (GLvoid*)data); 221 | } 222 | else if (pixel_size == 32) { 223 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)data); 224 | } 225 | else { 226 | std::cerr << "pixel size = " << pixel_size << " don't know how to process this case. I'm out!" << std::endl; 227 | exit(-1); 228 | } 229 | 230 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 231 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 232 | } 233 | else { 234 | std::cerr << "Unable to load the image file " << fname << " I'm out!" << std::endl; 235 | exit(-1); 236 | } 237 | 238 | // Clean bitmap; 239 | FreeImage_Unload(bitmap); 240 | 241 | // active only for static linking 242 | #ifdef FREEIMAGE_LIB 243 | FreeImage_DeInitialise(); 244 | #endif 245 | } 246 | 247 | 248 | // Called when the window is resized 249 | void GLFWCALL window_resized(int width, int height) { 250 | // Use red to clear the screen 251 | //glClearColor(1, 0, 0, 1); 252 | 253 | // Set the viewport 254 | glViewport(0, 0, width, height); 255 | 256 | glClear(GL_COLOR_BUFFER_BIT); 257 | glfwSwapBuffers(); 258 | } 259 | 260 | // Called for keyboard events 261 | void keyboard(int key, int action) { 262 | if(key == 'Q' && action == GLFW_PRESS) { 263 | glfwTerminate(); 264 | exit(0); 265 | } 266 | } 267 | 268 | // Read a shader source from a file 269 | // store the shader source in a std::vector 270 | void read_shader_src(const char *fname, std::vector &buffer) { 271 | std::ifstream in; 272 | in.open(fname, std::ios::binary); 273 | 274 | if(in.is_open()) { 275 | // Get the number of bytes stored in this file 276 | in.seekg(0, std::ios::end); 277 | size_t length = (size_t)in.tellg(); 278 | 279 | // Go to start of the file 280 | in.seekg(0, std::ios::beg); 281 | 282 | // Read the content of the file in a buffer 283 | buffer.resize(length + 1); 284 | in.read(&buffer[0], length); 285 | in.close(); 286 | // Add a valid C - string end 287 | buffer[length] = '\0'; 288 | } 289 | else { 290 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 291 | exit(-1); 292 | } 293 | } 294 | 295 | // Compile a shader 296 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 297 | // Load a shader from an external file 298 | std::vector buffer; 299 | read_shader_src(fname, buffer); 300 | const char *src = &buffer[0]; 301 | 302 | // Compile the shader 303 | GLuint shader = glCreateShader(shaderType); 304 | glShaderSource(shader, 1, &src, NULL); 305 | glCompileShader(shader); 306 | // Check the result of the compilation 307 | GLint test; 308 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 309 | if(!test) { 310 | std::cerr << "Shader compilation failed with this message:" << std::endl; 311 | std::vector compilation_log(512); 312 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 313 | std::cerr << &compilation_log[0] << std::endl; 314 | glfwTerminate(); 315 | exit(-1); 316 | } 317 | return shader; 318 | } 319 | 320 | // Create a program from two shaders 321 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 322 | // Load and compile the vertex and fragment shaders 323 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 324 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 325 | 326 | // Attach the above shader to a program 327 | GLuint shaderProgram = glCreateProgram(); 328 | glAttachShader(shaderProgram, vertexShader); 329 | glAttachShader(shaderProgram, fragmentShader); 330 | 331 | // Flag the shaders for deletion 332 | glDeleteShader(vertexShader); 333 | glDeleteShader(fragmentShader); 334 | 335 | // Link and use the program 336 | glLinkProgram(shaderProgram); 337 | glUseProgram(shaderProgram); 338 | 339 | return shaderProgram; 340 | } 341 | 342 | -------------------------------------------------------------------------------- /Textures/ex_11/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec2 texture_coord_from_vshader; 4 | out vec4 out_color; 5 | 6 | uniform sampler2D texture_sampler; 7 | 8 | void main() { 9 | out_color = texture(texture_sampler, texture_coord_from_vshader); 10 | float average_color = (out_color.r + out_color.g + out_color.b)/3.0; 11 | out_color = vec4(average_color, average_color, average_color, 1.0); 12 | } 13 | -------------------------------------------------------------------------------- /Textures/ex_11/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | in vec2 texture_coord; 5 | out vec2 texture_coord_from_vshader; 6 | 7 | void main() { 8 | gl_Position = position; 9 | texture_coord_from_vshader = texture_coord; 10 | } 11 | -------------------------------------------------------------------------------- /Textures/ex_11/squirrel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sol-prog/OpenGL-101/ac06e83509d8c16deb3c7a154a06c252ebb946c8/Textures/ex_11/squirrel.jpg -------------------------------------------------------------------------------- /Textures/ex_12/ex_12.cpp: -------------------------------------------------------------------------------- 1 | // Draw four triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | //#include 9 | #include 10 | 11 | // Read a shader source from a file 12 | // store the shader source in a std::vector 13 | void read_shader_src(const char *fname, std::vector &buffer); 14 | 15 | // Compile a shader 16 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 17 | 18 | // Create a program from two shaders 19 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 20 | 21 | // Called when the window is resized 22 | void GLFWCALL window_resized(int width, int height); 23 | 24 | // Called for keyboard events 25 | void keyboard(int key, int action); 26 | 27 | // Render scene 28 | void display(GLuint &vao); 29 | 30 | // Initialize the data to be rendered 31 | void initialize(GLuint &vao); 32 | 33 | // Load an image from the disk with FreeImage 34 | void load_image(const char *fname); 35 | 36 | // Show the effect of the wrapping options on the current texture 37 | void wrap_tests(); 38 | 39 | // Global variable for switching through the warpping modes 40 | GLuint wrap_option = 0; 41 | 42 | int main () { 43 | // Initialize GLFW 44 | if ( !glfwInit()) { 45 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 46 | exit(-1); 47 | } 48 | 49 | // Use OpenGL 3.2 core profile 50 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 51 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 52 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 53 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 54 | 55 | // Open a window and attach an OpenGL rendering context to the window surface 56 | if( !glfwOpenWindow(800, 600, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 57 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 58 | glfwTerminate(); 59 | exit(-1); 60 | } 61 | 62 | // Register a callback function for window resize events 63 | glfwSetWindowSizeCallback( window_resized ); 64 | 65 | // Register a callback function for keyboard pressed events 66 | glfwSetKeyCallback(keyboard); 67 | 68 | // Print the OpenGL version 69 | int major, minor, rev; 70 | glfwGetGLVersion(&major, &minor, &rev); 71 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 72 | 73 | // Initialize GLEW 74 | glewExperimental = GL_TRUE; 75 | if(glewInit() != GLEW_OK) { 76 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 77 | glfwTerminate(); 78 | exit(-1); 79 | } 80 | 81 | // Create a vertex array object 82 | GLuint vao; 83 | 84 | // Initialize the data to be rendered 85 | initialize(vao); 86 | 87 | // Create a rendering loop 88 | int running = GL_TRUE; 89 | 90 | while(running) { 91 | // Display scene 92 | display(vao); 93 | 94 | // Pool for events 95 | glfwPollEvents(); 96 | // Check if the window was closed 97 | running = glfwGetWindowParam(GLFW_OPENED); 98 | } 99 | 100 | // Terminate GLFW 101 | glfwTerminate(); 102 | 103 | return 0; 104 | } 105 | 106 | // Render scene 107 | void display(GLuint &vao) { 108 | glClear(GL_COLOR_BUFFER_BIT); 109 | 110 | glBindVertexArray(vao); 111 | glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 112 | 113 | // Swap front and back buffers 114 | glfwSwapBuffers(); 115 | } 116 | 117 | void initialize(GLuint &vao) { 118 | // Use a Vertex Array Object 119 | glGenVertexArrays(1, &vao); 120 | glBindVertexArray(vao); 121 | 122 | // 1 square (made by 2 triangles) to be rendered 123 | GLfloat vertices_position[8] = { 124 | -1.0, -1.0, 125 | 1.0, -1.0, 126 | 1.0, 1.0, 127 | -1.0, 1.0, 128 | }; 129 | 130 | GLfloat texture_coord[8] = { 131 | -1.0, -1.0, 132 | 2.0, -1.0, 133 | 2.0, 2.0, 134 | -1.0, 2.0, 135 | }; 136 | 137 | GLuint indices[6] = { 138 | 0, 1, 2, 139 | 2, 3, 0 140 | }; 141 | 142 | // Create a Vector Buffer Object that will store the vertices on video memory 143 | GLuint vbo; 144 | glGenBuffers(1, &vbo); 145 | 146 | // Allocate space for vertex positions and texture coordinates 147 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 148 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position) + sizeof(texture_coord), NULL, GL_STATIC_DRAW); 149 | 150 | // Transfer the vertex positions: 151 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices_position), vertices_position); 152 | 153 | // Transfer the texture coordinates: 154 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices_position), sizeof(texture_coord), texture_coord); 155 | 156 | // Create an Element Array Buffer that will store the indices array: 157 | GLuint eab; 158 | glGenBuffers(1, &eab); 159 | 160 | // Transfer the data from indices to eab 161 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab); 162 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 163 | 164 | // Create a texture 165 | GLuint texture; 166 | glGenTextures(1, &texture); 167 | 168 | // Specify that we work with a 2D texture 169 | glBindTexture(GL_TEXTURE_2D, texture); 170 | 171 | load_image("squirrel.jpg"); 172 | 173 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 174 | 175 | // Get the location of the attributes that enters in the vertex shader 176 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 177 | 178 | // Specify how the data for position can be accessed 179 | glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); 180 | 181 | // Enable the attribute 182 | glEnableVertexAttribArray(position_attribute); 183 | 184 | // Texture coord attribute 185 | GLint texture_coord_attribute = glGetAttribLocation(shaderProgram, "texture_coord"); 186 | glVertexAttribPointer(texture_coord_attribute, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid *)sizeof(vertices_position)); 187 | glEnableVertexAttribArray(texture_coord_attribute); 188 | 189 | } 190 | 191 | void load_image(const char *fname) { 192 | 193 | // active only for static linking 194 | #ifdef FREEIMAGE_LIB 195 | FreeImage_Initialise(); 196 | #endif 197 | 198 | FIBITMAP *bitmap; 199 | // Get the format of the image file 200 | FREE_IMAGE_FORMAT fif =FreeImage_GetFileType(fname, 0); 201 | 202 | // If the format can't be determined, try to guess the format from the file name 203 | if(fif == FIF_UNKNOWN) { 204 | fif = FreeImage_GetFIFFromFilename(fname); 205 | } 206 | 207 | // Load the data in bitmap if possible 208 | if(fif != FIF_UNKNOWN && FreeImage_FIFSupportsReading(fif)) { 209 | bitmap = FreeImage_Load(fif, fname); 210 | } 211 | else { 212 | bitmap = NULL; 213 | } 214 | 215 | // PROCESS IMAGE if bitmap was successfully initialized 216 | if(bitmap) { 217 | unsigned int w = FreeImage_GetWidth(bitmap); 218 | unsigned int h = FreeImage_GetHeight(bitmap); 219 | unsigned pixel_size = FreeImage_GetBPP(bitmap); 220 | 221 | // Get a pointer to the pixel data 222 | BYTE *data = (BYTE*)FreeImage_GetBits(bitmap); 223 | 224 | // Process only RGB and RGBA images 225 | if(pixel_size == 24) { 226 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, (GLvoid*)data); 227 | } 228 | else if (pixel_size == 32) { 229 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)data); 230 | } 231 | else { 232 | std::cerr << "pixel size = " << pixel_size << " don't know how to process this case. I'm out!" << std::endl; 233 | exit(-1); 234 | } 235 | 236 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 237 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 238 | 239 | wrap_tests(); 240 | } 241 | else { 242 | std::cerr << "Unable to load the image file " << fname << " I'm out!" << std::endl; 243 | exit(-1); 244 | } 245 | 246 | // Clean bitmap; 247 | FreeImage_Unload(bitmap); 248 | 249 | // active only for static linking 250 | #ifdef FREEIMAGE_LIB 251 | FreeImage_DeInitialise(); 252 | #endif 253 | } 254 | 255 | void wrap_tests() { 256 | if (wrap_option == 0) { 257 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 258 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 259 | std::cout << "Using GL_REPEAT" << std::endl; 260 | } 261 | else if(wrap_option == 1) { 262 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 263 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 264 | std::cout << "Using GL_CLAMP_TO_EDGE" << std::endl; 265 | } 266 | else if(wrap_option == 2) { 267 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 268 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); 269 | std::cout << "Using GL_CLAMP_TO_BORDER" << std::endl; 270 | } 271 | else if(wrap_option == 3) { 272 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); 273 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); 274 | std::cout << "Using GL_MIRRORED_REPEAT" << std::endl; 275 | } 276 | } 277 | 278 | 279 | // Called when the window is resized 280 | void GLFWCALL window_resized(int width, int height) { 281 | // Use red to clear the screen 282 | //glClearColor(1, 0, 0, 1); 283 | 284 | // Set the viewport 285 | glViewport(0, 0, width, height); 286 | 287 | glClear(GL_COLOR_BUFFER_BIT); 288 | glfwSwapBuffers(); 289 | } 290 | 291 | // Called for keyboard events 292 | void keyboard(int key, int action) { 293 | if(key == 'Q' && action == GLFW_PRESS) { 294 | glfwTerminate(); 295 | exit(0); 296 | } 297 | if(key == 'W' && action == GLFW_PRESS) { 298 | wrap_option++; 299 | // Cycle through values from 0 to 3 300 | if(wrap_option > 3) { 301 | wrap_option = 0; 302 | } 303 | wrap_tests(); 304 | } 305 | } 306 | 307 | // Read a shader source from a file 308 | // store the shader source in a std::vector 309 | void read_shader_src(const char *fname, std::vector &buffer) { 310 | std::ifstream in; 311 | in.open(fname, std::ios::binary); 312 | 313 | if(in.is_open()) { 314 | // Get the number of bytes stored in this file 315 | in.seekg(0, std::ios::end); 316 | size_t length = (size_t)in.tellg(); 317 | 318 | // Go to start of the file 319 | in.seekg(0, std::ios::beg); 320 | 321 | // Read the content of the file in a buffer 322 | buffer.resize(length + 1); 323 | in.read(&buffer[0], length); 324 | in.close(); 325 | // Add a valid C - string end 326 | buffer[length] = '\0'; 327 | } 328 | else { 329 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 330 | exit(-1); 331 | } 332 | } 333 | 334 | // Compile a shader 335 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 336 | // Load a shader from an external file 337 | std::vector buffer; 338 | read_shader_src(fname, buffer); 339 | const char *src = &buffer[0]; 340 | 341 | // Compile the shader 342 | GLuint shader = glCreateShader(shaderType); 343 | glShaderSource(shader, 1, &src, NULL); 344 | glCompileShader(shader); 345 | // Check the result of the compilation 346 | GLint test; 347 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 348 | if(!test) { 349 | std::cerr << "Shader compilation failed with this message:" << std::endl; 350 | std::vector compilation_log(512); 351 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 352 | std::cerr << &compilation_log[0] << std::endl; 353 | glfwTerminate(); 354 | exit(-1); 355 | } 356 | return shader; 357 | } 358 | 359 | // Create a program from two shaders 360 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 361 | // Load and compile the vertex and fragment shaders 362 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 363 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 364 | 365 | // Attach the above shader to a program 366 | GLuint shaderProgram = glCreateProgram(); 367 | glAttachShader(shaderProgram, vertexShader); 368 | glAttachShader(shaderProgram, fragmentShader); 369 | 370 | // Flag the shaders for deletion 371 | glDeleteShader(vertexShader); 372 | glDeleteShader(fragmentShader); 373 | 374 | // Link and use the program 375 | glLinkProgram(shaderProgram); 376 | glUseProgram(shaderProgram); 377 | 378 | return shaderProgram; 379 | } 380 | 381 | -------------------------------------------------------------------------------- /Textures/ex_12/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec2 texture_coord_from_vshader; 4 | out vec4 out_color; 5 | 6 | uniform sampler2D texture_sampler; 7 | 8 | void main() { 9 | out_color = texture(texture_sampler, texture_coord_from_vshader); 10 | } 11 | -------------------------------------------------------------------------------- /Textures/ex_12/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | in vec2 texture_coord; 5 | out vec2 texture_coord_from_vshader; 6 | 7 | void main() { 8 | gl_Position = position; 9 | texture_coord_from_vshader = texture_coord; 10 | } 11 | -------------------------------------------------------------------------------- /Textures/ex_12/squirrel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sol-prog/OpenGL-101/ac06e83509d8c16deb3c7a154a06c252ebb946c8/Textures/ex_12/squirrel.jpg -------------------------------------------------------------------------------- /Transformations/ex_14.cpp: -------------------------------------------------------------------------------- 1 | // Draw four triangles on a red background 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | //#include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | // Read a shader source from a file 16 | // store the shader source in a std::vector 17 | void read_shader_src(const char *fname, std::vector &buffer); 18 | 19 | // Compile a shader 20 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType); 21 | 22 | // Create a program from two shaders 23 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader); 24 | 25 | // Called when the window is resized 26 | void GLFWCALL window_resized(int width, int height); 27 | 28 | // Called for keyboard events 29 | void keyboard(int key, int action); 30 | 31 | // Render scene 32 | void display(GLuint &vao); 33 | 34 | // Initialize the data to be rendered 35 | void initialize(GLuint &vao); 36 | 37 | // Load an image from the disk with FreeImage 38 | void load_image(const char *fname); 39 | 40 | int main () { 41 | // Initialize GLFW 42 | if ( !glfwInit()) { 43 | std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl; 44 | exit(-1); 45 | } 46 | 47 | // Use OpenGL 3.2 core profile 48 | glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 49 | glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 50 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 51 | glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); 52 | 53 | // Open a window and attach an OpenGL rendering context to the window surface 54 | if( !glfwOpenWindow(800, 600, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)) { 55 | std::cerr << "Failed to open a window! I'm out!" << std::endl; 56 | glfwTerminate(); 57 | exit(-1); 58 | } 59 | 60 | // Register a callback function for window resize events 61 | glfwSetWindowSizeCallback( window_resized ); 62 | 63 | // Register a callback function for keyboard pressed events 64 | glfwSetKeyCallback(keyboard); 65 | 66 | // Print the OpenGL version 67 | int major, minor, rev; 68 | glfwGetGLVersion(&major, &minor, &rev); 69 | std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl; 70 | 71 | // Initialize GLEW 72 | glewExperimental = GL_TRUE; 73 | if(glewInit() != GLEW_OK) { 74 | std::cerr << "Failed to initialize GLEW! I'm out!" << std::endl; 75 | glfwTerminate(); 76 | exit(-1); 77 | } 78 | 79 | // Create a vertex array object 80 | GLuint vao; 81 | 82 | // Initialize the data to be rendered 83 | initialize(vao); 84 | 85 | // Create a rendering loop 86 | int running = GL_TRUE; 87 | 88 | while(running) { 89 | // Display scene 90 | display(vao); 91 | 92 | // Pool for events 93 | glfwPollEvents(); 94 | // Check if the window was closed 95 | running = glfwGetWindowParam(GLFW_OPENED); 96 | } 97 | 98 | // Terminate GLFW 99 | glfwTerminate(); 100 | 101 | return 0; 102 | } 103 | 104 | // Render scene 105 | void display(GLuint &vao) { 106 | glClear(GL_COLOR_BUFFER_BIT); 107 | 108 | glBindVertexArray(vao); 109 | glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 110 | 111 | // Swap front and back buffers 112 | glfwSwapBuffers(); 113 | } 114 | 115 | void initialize(GLuint &vao) { 116 | // Use a Vertex Array Object 117 | glGenVertexArrays(1, &vao); 118 | glBindVertexArray(vao); 119 | 120 | // 1 square (made by 2 triangles) to be rendered 121 | GLfloat vertices_position[12] = { 122 | -0.5, -0.5, 0, 123 | 0.5, -0.5, 0, 124 | 0.5, 0.5, 0, 125 | -0.5, 0.5, 0 126 | }; 127 | 128 | GLfloat texture_coord[8] = { 129 | 0.0, 0.0, 130 | 1.0, 0.0, 131 | 1.0, 1.0, 132 | 0.0, 1.0, 133 | }; 134 | 135 | GLuint indices[6] = { 136 | 0, 1, 2, 137 | 2, 3, 0 138 | }; 139 | 140 | // Initialize the model, view and projection matrices 141 | glm::mat4 Model, View, Projection; 142 | 143 | // Set the projection matrix 144 | Projection = glm::ortho(-4.0f/3.0f, 4.0f/3.0f, -1.0f, 1.0f, -1.0f, 1.0f); 145 | 146 | // Translation 147 | Model = glm::translate(Model, glm::vec3(0.1f, 0.2f, 0.5f)); 148 | 149 | // Rotation around Oz with 45 degrees 150 | Model = glm::rotate(Model, 45.0f, glm::vec3(0.0f, 0.0f, 1.0f)); 151 | 152 | 153 | 154 | // Create a Vector Buffer Object that will store the vertices on video memory 155 | GLuint vbo; 156 | glGenBuffers(1, &vbo); 157 | 158 | // Allocate space for vertex positions and texture coordinates 159 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 160 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position) + sizeof(texture_coord), NULL, GL_STATIC_DRAW); 161 | 162 | // Transfer the vertex positions: 163 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices_position), vertices_position); 164 | 165 | // Transfer the texture coordinates: 166 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices_position), sizeof(texture_coord), texture_coord); 167 | 168 | // Create an Element Array Buffer that will store the indices array: 169 | GLuint eab; 170 | glGenBuffers(1, &eab); 171 | 172 | // Transfer the data from indices to eab 173 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab); 174 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 175 | 176 | // Create a texture 177 | GLuint texture; 178 | glGenTextures(1, &texture); 179 | 180 | // Specify that we work with a 2D texture 181 | glBindTexture(GL_TEXTURE_2D, texture); 182 | 183 | load_image("squirrel.jpg"); 184 | 185 | GLuint shaderProgram = create_program("shaders/vert.shader", "shaders/frag.shader"); 186 | 187 | // Get the location of the attributes that enters in the vertex shader 188 | GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); 189 | 190 | // Specify how the data for position can be accessed 191 | glVertexAttribPointer(position_attribute, 3, GL_FLOAT, GL_FALSE, 0, 0); 192 | 193 | // Enable the attribute 194 | glEnableVertexAttribArray(position_attribute); 195 | 196 | // Texture coord attribute 197 | GLint texture_coord_attribute = glGetAttribLocation(shaderProgram, "texture_coord"); 198 | glVertexAttribPointer(texture_coord_attribute, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid *)sizeof(vertices_position)); 199 | glEnableVertexAttribArray(texture_coord_attribute); 200 | 201 | // Transfer the transformation matrices to the shader program 202 | GLint model = glGetUniformLocation( shaderProgram, "Model" ); 203 | glUniformMatrix4fv(model, 1, GL_FALSE, glm::value_ptr(Model)); 204 | 205 | GLint view = glGetUniformLocation( shaderProgram, "View" ); 206 | glUniformMatrix4fv(view, 1, GL_FALSE, glm::value_ptr(View)); 207 | 208 | GLint projection = glGetUniformLocation( shaderProgram, "Projection" ); 209 | glUniformMatrix4fv(projection, 1, GL_FALSE, glm::value_ptr(Projection)); 210 | 211 | } 212 | 213 | void load_image(const char *fname) { 214 | 215 | // active only for static linking 216 | #ifdef FREEIMAGE_LIB 217 | FreeImage_Initialise(); 218 | #endif 219 | 220 | FIBITMAP *bitmap; 221 | // Get the format of the image file 222 | FREE_IMAGE_FORMAT fif =FreeImage_GetFileType(fname, 0); 223 | 224 | // If the format can't be determined, try to guess the format from the file name 225 | if(fif == FIF_UNKNOWN) { 226 | fif = FreeImage_GetFIFFromFilename(fname); 227 | } 228 | 229 | // Load the data in bitmap if possible 230 | if(fif != FIF_UNKNOWN && FreeImage_FIFSupportsReading(fif)) { 231 | bitmap = FreeImage_Load(fif, fname); 232 | } 233 | else { 234 | bitmap = NULL; 235 | } 236 | 237 | // PROCESS IMAGE if bitmap was successfully initialized 238 | if(bitmap) { 239 | unsigned int w = FreeImage_GetWidth(bitmap); 240 | unsigned int h = FreeImage_GetHeight(bitmap); 241 | unsigned pixel_size = FreeImage_GetBPP(bitmap); 242 | 243 | // Get a pointer to the pixel data 244 | BYTE *data = (BYTE*)FreeImage_GetBits(bitmap); 245 | 246 | // Process only RGB and RGBA images 247 | if(pixel_size == 24) { 248 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, (GLvoid*)data); 249 | } 250 | else if (pixel_size == 32) { 251 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)data); 252 | } 253 | else { 254 | std::cerr << "pixel size = " << pixel_size << " don't know how to process this case. I'm out!" << std::endl; 255 | exit(-1); 256 | } 257 | 258 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 259 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 260 | } 261 | else { 262 | std::cerr << "Unable to load the image file " << fname << " I'm out!" << std::endl; 263 | exit(-1); 264 | } 265 | 266 | // Clean bitmap; 267 | FreeImage_Unload(bitmap); 268 | 269 | // active only for static linking 270 | #ifdef FREEIMAGE_LIB 271 | FreeImage_DeInitialise(); 272 | #endif 273 | } 274 | 275 | 276 | // Called when the window is resized 277 | void GLFWCALL window_resized(int width, int height) { 278 | // Use red to clear the screen 279 | //glClearColor(1, 0, 0, 1); 280 | 281 | // Set the viewport 282 | glViewport(0, 0, width, height); 283 | 284 | glClear(GL_COLOR_BUFFER_BIT); 285 | glfwSwapBuffers(); 286 | } 287 | 288 | // Called for keyboard events 289 | void keyboard(int key, int action) { 290 | if(key == 'Q' && action == GLFW_PRESS) { 291 | glfwTerminate(); 292 | exit(0); 293 | } 294 | } 295 | 296 | // Read a shader source from a file 297 | // store the shader source in a std::vector 298 | void read_shader_src(const char *fname, std::vector &buffer) { 299 | std::ifstream in; 300 | in.open(fname, std::ios::binary); 301 | 302 | if(in.is_open()) { 303 | // Get the number of bytes stored in this file 304 | in.seekg(0, std::ios::end); 305 | size_t length = (size_t)in.tellg(); 306 | 307 | // Go to start of the file 308 | in.seekg(0, std::ios::beg); 309 | 310 | // Read the content of the file in a buffer 311 | buffer.resize(length + 1); 312 | in.read(&buffer[0], length); 313 | in.close(); 314 | // Add a valid C - string end 315 | buffer[length] = '\0'; 316 | } 317 | else { 318 | std::cerr << "Unable to open " << fname << " I'm out!" << std::endl; 319 | exit(-1); 320 | } 321 | } 322 | 323 | // Compile a shader 324 | GLuint load_and_compile_shader(const char *fname, GLenum shaderType) { 325 | // Load a shader from an external file 326 | std::vector buffer; 327 | read_shader_src(fname, buffer); 328 | const char *src = &buffer[0]; 329 | 330 | // Compile the shader 331 | GLuint shader = glCreateShader(shaderType); 332 | glShaderSource(shader, 1, &src, NULL); 333 | glCompileShader(shader); 334 | // Check the result of the compilation 335 | GLint test; 336 | glGetShaderiv(shader, GL_COMPILE_STATUS, &test); 337 | if(!test) { 338 | std::cerr << "Shader compilation failed with this message:" << std::endl; 339 | std::vector compilation_log(512); 340 | glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]); 341 | std::cerr << &compilation_log[0] << std::endl; 342 | glfwTerminate(); 343 | exit(-1); 344 | } 345 | return shader; 346 | } 347 | 348 | // Create a program from two shaders 349 | GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) { 350 | // Load and compile the vertex and fragment shaders 351 | GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER); 352 | GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER); 353 | 354 | // Attach the above shader to a program 355 | GLuint shaderProgram = glCreateProgram(); 356 | glAttachShader(shaderProgram, vertexShader); 357 | glAttachShader(shaderProgram, fragmentShader); 358 | 359 | // Flag the shaders for deletion 360 | glDeleteShader(vertexShader); 361 | glDeleteShader(fragmentShader); 362 | 363 | // Link and use the program 364 | glLinkProgram(shaderProgram); 365 | glUseProgram(shaderProgram); 366 | 367 | return shaderProgram; 368 | } 369 | 370 | -------------------------------------------------------------------------------- /Transformations/shaders/frag.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec2 texture_coord_from_vshader; 4 | out vec4 out_color; 5 | 6 | uniform sampler2D texture_sampler; 7 | 8 | void main() { 9 | out_color = texture(texture_sampler, texture_coord_from_vshader); 10 | } 11 | -------------------------------------------------------------------------------- /Transformations/shaders/vert.shader: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 position; 4 | in vec2 texture_coord; 5 | out vec2 texture_coord_from_vshader; 6 | 7 | uniform mat4 Model; 8 | uniform mat4 View; 9 | uniform mat4 Projection; 10 | 11 | void main() { 12 | gl_Position = Projection * View * Model * position; 13 | texture_coord_from_vshader = texture_coord; 14 | } 15 | -------------------------------------------------------------------------------- /Transformations/squirrel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sol-prog/OpenGL-101/ac06e83509d8c16deb3c7a154a06c252ebb946c8/Transformations/squirrel.jpg --------------------------------------------------------------------------------