Changeset 07ed460 in opengl-game
- Timestamp:
- Apr 26, 2018, 3:37:36 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- d9f99b2
- Parents:
- 05e43cf
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r05e43cf r07ed460 43 43 mat4 model_mat; 44 44 GLuint shader_program; 45 GLvoid* vbo_offset;46 45 unsigned int num_points; 46 GLvoid* vertex_vbo_offset; 47 GLvoid* texture_vbo_offset; 48 vector<GLfloat> points; 49 vector<GLfloat> colors; 50 vector<GLfloat> texcoords; 51 vector<GLfloat> selected_colors; 47 52 }; 48 53 … … 208 213 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 209 214 210 GLfloat points[] = { 215 mat4 T_model, R_model; 216 217 // triangle 218 objects.push_back(SceneObject()); 219 objects[0].shader_program = 0; 220 objects[0].vertex_vbo_offset = (GLvoid*) (0 * sizeof(float) * 3); 221 objects[0].texture_vbo_offset = (GLvoid*)(0 * sizeof(float) * 2); 222 objects[0].points = { 211 223 0.0f, 0.5f, 0.0f, 212 224 -0.5f, -0.5f, 0.0f, … … 216 228 0.0f, 0.5f, 0.0f, 217 229 }; 218 219 GLfloat colors[] = { 220 1.0, 0.0, 0.0, 221 0.0, 0.0, 1.0, 222 0.0, 1.0, 0.0, 223 0.0, 1.0, 0.0, 224 0.0, 0.0, 1.0, 225 1.0, 0.0, 0.0, 226 }; 227 228 GLfloat colors_new[] = { 229 0.0, 1.0, 0.0, 230 0.0, 1.0, 0.0, 231 0.0, 1.0, 0.0, 232 0.0, 1.0, 0.0, 233 0.0, 1.0, 0.0, 234 0.0, 1.0, 0.0, 235 }; 236 237 GLfloat points2[] = { 230 objects[0].colors = { 231 1.0f, 0.0f, 0.0f, 232 0.0f, 0.0f, 1.0f, 233 0.0f, 1.0f, 0.0f, 234 0.0f, 1.0f, 0.0f, 235 0.0f, 0.0f, 1.0f, 236 1.0f, 0.0f, 0.0f, 237 }; 238 objects[0].texcoords = { 239 1.0f, 1.0f, 240 0.0f, 1.0f, 241 0.0f, 0.0f, 242 1.0f, 1.0f, 243 0.0f, 0.0f, 244 1.0f, 0.0f 245 }; 246 objects[0].selected_colors = { 247 0.0f, 1.0f, 0.0f, 248 0.0f, 1.0f, 0.0f, 249 0.0f, 1.0f, 0.0f, 250 0.0f, 1.0f, 0.0f, 251 0.0f, 1.0f, 0.0f, 252 0.0f, 1.0f, 0.0f, 253 }; 254 objects[0].num_points = objects[0].points.size() / 3; 255 256 T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f)); 257 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 258 objects[0].model_mat = T_model*R_model; 259 260 // square 261 objects.push_back(SceneObject()); 262 objects[1].shader_program = 0; 263 objects[1].vertex_vbo_offset = (GLvoid*) (6 * sizeof(float) * 3); 264 objects[1].texture_vbo_offset = (GLvoid*)(6 * sizeof(float) * 2); 265 objects[1].points = { 238 266 0.5f, 0.5f, 0.0f, 239 267 -0.5f, 0.5f, 0.0f, … … 243 271 0.5f, -0.5f, 0.0f, 244 272 }; 245 246 GLfloat colors2[] = { 247 0.0, 0.9, 0.9, 248 0.0, 0.9, 0.9, 249 0.0, 0.9, 0.9, 250 0.0, 0.9, 0.9, 251 0.0, 0.9, 0.9, 252 0.0, 0.9, 0.9, 253 }; 254 255 GLfloat texcoords[] = { 273 objects[1].colors = { 274 1.0f, 0.0f, 0.0f, 275 0.0f, 0.0f, 1.0f, 276 0.0f, 1.0f, 0.0f, 277 0.0f, 1.0f, 0.0f, 278 0.0f, 0.0f, 1.0f, 279 1.0f, 0.0f, 0.0f, 280 }; 281 objects[1].texcoords = { 256 282 1.0f, 1.0f, 257 283 0.0f, 1.0f, 258 0.0, 0.0, 259 1.0, 1.0, 260 0.0, 0.0, 261 1.0, 0.0 262 }; 263 264 mat4 T_model, R_model; 265 266 // triangle 267 objects.push_back(SceneObject()); 268 objects[0].shader_program = 0; 269 objects[0].vbo_offset = (GLvoid*) (0 * sizeof(float) * 3); 270 objects[0].num_points = 6; 271 272 T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f)); 273 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 274 objects[0].model_mat = T_model*R_model; 275 276 faces.push_back(ObjectFace()); 277 faces[0].object_id = 0; 278 faces[0].points = { 279 vec3(points[0], points[1], points[2]), 280 vec3(points[3], points[4], points[5]), 281 vec3(points[6], points[7], points[8]), 282 }; 283 284 // square 285 objects.push_back(SceneObject()); 286 objects[1].shader_program = 0; 287 objects[1].vbo_offset = (GLvoid*) (6 * sizeof(float) * 3); 288 objects[1].num_points = 6; 284 0.0f, 0.0f, 285 1.0f, 1.0f, 286 0.0f, 0.0f, 287 1.0f, 0.0f 288 }; 289 objects[1].selected_colors = { 290 0.0f, 0.9f, 0.9f, 291 0.0f, 0.9f, 0.9f, 292 0.0f, 0.9f, 0.9f, 293 0.0f, 0.9f, 0.9f, 294 0.0f, 0.9f, 0.9f, 295 0.0f, 0.9f, 0.9f, 296 }; 297 objects[1].num_points = objects[1].points.size() / 3; 289 298 290 299 T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f)); … … 292 301 objects[1].model_mat = T_model*R_model; 293 302 294 faces.push_back(ObjectFace()); 295 faces[1].object_id = 1; 296 faces[1].points = { 297 vec3(points2[0], points2[1], points2[2]), 298 vec3(points2[3], points2[4], points2[5]), 299 vec3(points2[6], points2[7], points2[8]), 300 }; 301 302 faces.push_back(ObjectFace()); 303 faces[2].object_id = 1; 304 faces[2].points = { 305 vec3(points2[9], points2[10], points2[11]), 306 vec3(points2[12], points2[13], points2[14]), 307 vec3(points2[15], points2[16], points2[17]), 308 }; 303 vector<SceneObject>::iterator obj_it; 304 GLsizeiptr offset; 305 306 GLsizeiptr points_buffer_size = 0; 307 GLsizeiptr textures_buffer_size = 0; 308 309 faces.clear(); 310 311 unsigned int object_id = 0; 312 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 313 points_buffer_size += obj_it->points.size() * sizeof(GLfloat); 314 textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat); 315 316 for (int p_idx = 0; p_idx < obj_it->points.size(); p_idx += 9) { 317 faces.push_back(ObjectFace()); 318 faces.back().object_id = object_id; 319 faces.back().points = { 320 vec3(obj_it->points[p_idx], obj_it->points[p_idx + 1], obj_it->points[p_idx + 2]), 321 vec3(obj_it->points[p_idx + 3], obj_it->points[p_idx + 4], obj_it->points[p_idx + 5]), 322 vec3(obj_it->points[p_idx + 6], obj_it->points[p_idx + 7], obj_it->points[p_idx + 8]), 323 }; 324 } 325 326 object_id++; 327 } 309 328 310 329 GLuint points_vbo = 0; 311 330 glGenBuffers(1, &points_vbo); 312 331 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 313 glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(points2), NULL, GL_DYNAMIC_DRAW); 314 315 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points); 316 glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(points2), points2); 332 glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW); 333 334 offset = 0; 335 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 336 glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->points.size() * sizeof(GLfloat), &obj_it->points[0]); 337 offset += obj_it->points.size() * sizeof(GLfloat); 338 } 317 339 318 340 GLuint colors_vbo = 0; 319 341 glGenBuffers(1, &colors_vbo); 320 342 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 321 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); 343 glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW); 344 345 offset = 0; 346 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 347 glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->colors.size() * sizeof(GLfloat), &obj_it->colors[0]); 348 offset += obj_it->colors.size() * sizeof(GLfloat); 349 } 350 351 GLuint selected_colors_vbo = 0; 352 glGenBuffers(1, &selected_colors_vbo); 353 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 354 glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW); 355 356 offset = 0; 357 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 358 glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->selected_colors.size() * sizeof(GLfloat), &obj_it->selected_colors[0]); 359 offset += obj_it->selected_colors.size() * sizeof(GLfloat); 360 } 361 362 GLuint texcoords_vbo = 0; 363 glGenBuffers(1, &texcoords_vbo); 364 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 365 glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW); 366 367 offset = 0; 368 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 369 glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->texcoords.size() * sizeof(GLfloat), &obj_it->texcoords[0]); 370 offset += obj_it->texcoords.size() * sizeof(GLfloat); 371 } 322 372 323 373 GLuint vao = 0; … … 327 377 glEnableVertexAttribArray(0); 328 378 glEnableVertexAttribArray(1); 329 330 GLuint colors2_vbo = 0;331 glGenBuffers(1, &colors2_vbo);332 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);333 glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);334 335 GLuint vt_vbo;336 glGenBuffers(1, &vt_vbo);337 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);338 glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);339 379 340 380 GLuint vao2 = 0; … … 504 544 505 545 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 506 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].v bo_offset);546 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 507 547 508 548 if (selectedObject == &objects[*it]) { 509 if (*it == 1) { 510 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo); 511 } else { 512 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 513 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW); 514 } 549 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 515 550 } else { 516 551 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 517 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);518 552 } 519 520 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 553 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 521 554 522 555 glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points); … … 530 563 531 564 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 532 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].v bo_offset);533 534 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);535 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);565 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 566 567 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 568 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, objects[*it].texture_vbo_offset); 536 569 537 570 glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
Note:
See TracChangeset
for help on using the changeset viewer.