Changeset 485424b in opengl-game
- Timestamp:
- Aug 22, 2017, 2:36:33 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- c62eee6
- Parents:
- 8e232ce
- Files:
-
- 5 added
- 2 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
color.frag
r8e232ce r485424b 2 2 3 3 in vec3 color; 4 4 5 out vec4 frag_color; 5 6 -
makefile
r8e232ce r485424b 13 13 # as this well prevent regenerating .o files for unchanged .cpp files 14 14 15 newgame: new-game.cpp logger.cpp 15 newgame: new-game.cpp logger.cpp stb_image.cpp 16 16 $(CC) $^ $(DEP) $(CFLAGS) -o $@ 17 17 -
new-game.cpp
r8e232ce r485424b 1 // NEXT STEP; Modify the vertex shader2 3 1 #include "logger.h" 2 3 #include "stb_image.h" 4 4 5 5 #include <glm/mat4x4.hpp> // glm::mat4 … … 21 21 22 22 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 23 const bool FULLSCREEN = false; 23 24 24 25 GLuint loadShader(GLenum type, string file); 25 26 const bool FULLSCREEN = false;26 GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath); 27 unsigned char* loadImage(string file_name, int* x, int* y); 27 28 28 29 void glfw_error_callback(int error, const char* description) { … … 92 93 // glFrontFace(GL_CW); 93 94 95 int x, y; 96 unsigned char* texImage = loadImage("test.png", &x, &y); 97 if (texImage) { 98 cout << "Yay, I loaded an image!" << endl; 99 cout << x << endl; 100 cout << y << endl; 101 printf ("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]); 102 } 103 104 GLuint tex = 0; 105 glGenTextures(1, &tex); 106 glActiveTexture(GL_TEXTURE0); 107 glBindTexture(GL_TEXTURE_2D, tex); 108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage); 109 110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 112 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 113 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 114 94 115 GLfloat points[] = { 95 116 0.0f, 0.5f, 0.0f, … … 110 131 }; 111 132 133 // Each point is made of 3 floats 134 int numPoints = (sizeof(points) / sizeof(float)) / 3; 135 136 GLfloat points2[] = { 137 0.5f, 0.5f, 0.0f, 138 -0.5f, 0.5f, 0.0f, 139 -0.5f, -0.5f, 0.0f, 140 0.5f, 0.5f, 0.0f, 141 -0.5f, -0.5f, 0.0f, 142 0.5f, -0.5f, 0.0f, 143 }; 144 145 /* 146 GLfloat colors2[] = { 147 0.0, 0.9, 0.9, 148 0.0, 0.9, 0.9, 149 0.0, 0.9, 0.9, 150 0.0, 0.9, 0.9, 151 0.0, 0.9, 0.9, 152 0.0, 0.9, 0.9, 153 }; 154 */ 155 156 GLfloat texcoords[] = { 157 1.0f, 1.0f, 158 0.0f, 1.0f, 159 0.0, 0.0, 160 1.0, 1.0, 161 0.0, 0.0, 162 1.0, 0.0 163 }; 164 165 // Each point is made of 3 floats 166 int numPoints2 = (sizeof(points2) / sizeof(float)) / 3; 167 112 168 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f)); 113 169 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f)); 114 170 mat4 model_mat = T_model*R_model; 171 172 mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f)); 173 mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 174 mat4 model_mat2 = T_model2*R_model2; 115 175 116 176 GLuint points_vbo = 0; … … 135 195 glEnableVertexAttribArray(1); 136 196 137 GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert"); 138 GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag"); 139 140 GLuint shader_program = glCreateProgram(); 141 glAttachShader(shader_program, vs); 142 glAttachShader(shader_program, fs); 143 144 glLinkProgram(shader_program); 197 GLuint points2_vbo = 0; 198 glGenBuffers(1, &points2_vbo); 199 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo); 200 glBufferData(GL_ARRAY_BUFFER, sizeof(points2), points2, GL_STATIC_DRAW); 201 202 /* 203 GLuint colors2_vbo = 0; 204 glGenBuffers(1, &colors2_vbo); 205 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo); 206 glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW); 207 */ 208 209 GLuint vt_vbo; 210 glGenBuffers(1, &vt_vbo); 211 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo); 212 glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW); 213 214 GLuint vao2 = 0; 215 glGenVertexArrays(1, &vao2); 216 glBindVertexArray(vao2); 217 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo); 218 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 219 // glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo); 220 // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 221 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo); 222 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL); 223 224 glEnableVertexAttribArray(0); 225 glEnableVertexAttribArray(1); 226 227 GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag"); 228 GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag"); 145 229 146 230 float speed = 1.0f; … … 175 259 }; 176 260 177 GLint model_mat_loc = glGetUniformLocation(shader_program, "model"); 178 GLint view_mat_loc = glGetUniformLocation(shader_program, "view"); 179 GLint proj_mat_loc = glGetUniformLocation(shader_program, "proj"); 261 GLint model_mat_loc = glGetUniformLocation(shader_program2, "model"); 262 GLint view_mat_loc = glGetUniformLocation(shader_program2, "view"); 263 GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj"); 264 265 GLint model_test_loc = glGetUniformLocation(shader_program, "model"); 266 GLint view_test_loc = glGetUniformLocation(shader_program, "view"); 267 GLint proj_test_loc = glGetUniformLocation(shader_program, "proj"); 180 268 181 269 glUseProgram(shader_program); 182 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat)); 183 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 270 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat)); 271 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, proj_mat); 272 273 glUseProgram(shader_program2); 274 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2)); 184 275 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat); 276 277 // glUniform1i(tex_loc, 0); 185 278 186 279 bool cam_moved = false; … … 202 295 203 296 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 297 298 glUseProgram(shader_program); 299 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat)); 300 204 301 glBindVertexArray(vao); 205 302 206 // Each point is made of 3 floats207 int numPoints = (sizeof(points) / sizeof(float)) / 3;208 303 glDrawArrays(GL_TRIANGLES, 0, numPoints); 304 305 glUseProgram(shader_program2); 306 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 307 308 glBindVertexArray(vao2); 309 310 glDrawArrays(GL_TRIANGLES, 0, numPoints2); 209 311 210 312 glfwPollEvents(); … … 284 386 return shaderId; 285 387 } 388 389 GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) { 390 GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath); 391 GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath); 392 393 GLuint shader_program = glCreateProgram(); 394 glAttachShader(shader_program, vs); 395 glAttachShader(shader_program, fs); 396 397 glLinkProgram(shader_program); 398 399 return shader_program; 400 } 401 402 unsigned char* loadImage(string file_name, int* x, int* y) { 403 int n; 404 int force_channels = 4; 405 unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels); 406 if (!image_data) { 407 fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str()); 408 } 409 return image_data; 410 }
Note:
See TracChangeset
for help on using the changeset viewer.