Changeset 3d06b4e in opengl-game for new-game.cpp


Ignore:
Timestamp:
Jun 28, 2018, 3:14:22 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
58088c0
Parents:
dba67b2
Message:

Calculate the bounding box for each object

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rdba67b2 r3d06b4e  
    4747   vector<GLfloat> selected_colors;
    4848   bool deleted;
     49   vec3 bounding_center;
     50   GLfloat bounding_radius;
    4951};
    5052
     
    9496                  GLuint model_mat_idx_vbo);
    9597void removeObjectFromScene(int objectId, GLuint ubo);
     98
     99void calculateObjectBoundingBox(SceneObject& obj);
    96100
    97101void initializeBuffers(
     
    420424   obj.points = {
    421425      //back
    422       -0.5f,  0.3f,  0.0f,
    423       -0.5f,  0.0f,  0.0f,
    424        0.5f,  0.0f,  0.0f,
    425       -0.5f,  0.3f,  0.0f,
    426        0.5f,  0.0f,  0.0f,
    427        0.5f,  0.3f,  0.0f,
     426      -0.5f,    0.3f,    0.0f,
     427      -0.5f,    0.0f,    0.0f,
     428       0.5f,    0.0f,    0.0f,
     429      -0.5f,    0.3f,    0.0f,
     430       0.5f,    0.0f,    0.0f,
     431       0.5f,    0.3f,    0.0f,
    428432
    429433      // left back
    430       -0.5f,  0.3f, -2.0f,
    431       -0.5f,  0.0f, -2.0f,
    432       -0.5f,  0.0f,  0.0f,
    433       -0.5f,  0.3f, -2.0f,
    434       -0.5f,  0.0f,  0.0f,
    435       -0.5f,  0.3f,  0.0f,
     434      -0.5f,    0.3f,  -2.0f,
     435      -0.5f,    0.0f,  -2.0f,
     436      -0.5f,    0.0f,    0.0f,
     437      -0.5f,    0.3f,  -2.0f,
     438      -0.5f,    0.0f,    0.0f,
     439      -0.5f,    0.3f,    0.0f,
    436440
    437441      // right back
    438        0.5f,  0.3f,  0.0f,
    439        0.5f,  0.0f,  0.0f,
    440        0.5f,  0.0f, -2.0f,
    441        0.5f,  0.3f,  0.0f,
    442        0.5f,  0.0f, -2.0f,
    443        0.5f,  0.3f, -2.0f,
     442       0.5f,    0.3f,    0.0f,
     443       0.5f,    0.0f,    0.0f,
     444       0.5f,    0.0f,  -2.0f,
     445       0.5f,    0.3f,    0.0f,
     446       0.5f,    0.0f,  -2.0f,
     447       0.5f,    0.3f,  -2.0f,
    444448
    445449      // left mid
    446       -0.25f,  0.3f, -3.0f,
    447       -0.25f,  0.0f, -3.0f,
    448       -0.5f,  0.0f, -2.0f,
    449       -0.25f,  0.3f, -3.0f,
    450       -0.5f,  0.0f, -2.0f,
    451       -0.5f,  0.3f, -2.0f,
     450      -0.25f,   0.3f,  -3.0f,
     451      -0.25f,   0.0f,  -3.0f,
     452      -0.5f,    0.0f,  -2.0f,
     453      -0.25f,   0.3f,  -3.0f,
     454      -0.5f,    0.0f,  -2.0f,
     455      -0.5f,    0.3f,  -2.0f,
    452456
    453457      // right mid
    454        0.5f,  0.3f, -2.0f,
    455        0.5f,  0.0f, -2.0f,
    456        0.25f,  0.0f, -3.0f,
    457        0.5f,  0.3f, -2.0f,
    458        0.25f,  0.0f, -3.0f,
    459        0.25f,  0.3f, -3.0f,
     458       0.5f,    0.3f,  -2.0f,
     459       0.5f,    0.0f,  -2.0f,
     460       0.25f,   0.0f,  -3.0f,
     461       0.5f,    0.3f,  -2.0f,
     462       0.25f,   0.0f,  -3.0f,
     463       0.25f,   0.3f,  -3.0f,
    460464
    461465      // left front
    462        0.0f,  0.0f, -3.5f,
    463       -0.25f,  0.0f, -3.0f,
    464       -0.25f,  0.3f, -3.0f,
     466       0.0f,    0.0f,  -3.5f,
     467      -0.25f,   0.0f,  -3.0f,
     468      -0.25f,   0.3f,  -3.0f,
    465469
    466470      // right front
    467        0.25f,  0.3f, -3.0f,
    468        0.25f,  0.0f, -3.0f,
    469        0.0f,  0.0f, -3.5f,
     471       0.25f,   0.3f,  -3.0f,
     472       0.25f,   0.0f,  -3.0f,
     473       0.0f,    0.0f,  -3.5f,
    470474
    471475      // top back
    472       -0.5f,  0.3f, -2.0f,
    473       -0.5f,  0.3f,  0.0f,
    474        0.5f,  0.3f,  0.0f,
    475       -0.5f,  0.3f, -2.0f,
    476        0.5f,  0.3f,  0.0f,
    477        0.5f,  0.3f, -2.0f,
     476      -0.5f,    0.3f,  -2.0f,
     477      -0.5f,    0.3f,    0.0f,
     478       0.5f,    0.3f,    0.0f,
     479      -0.5f,    0.3f,  -2.0f,
     480       0.5f,    0.3f,    0.0f,
     481       0.5f,    0.3f,  -2.0f,
    478482
    479483      // bottom back
    480       -0.5f,  0.0f,  0.0f,
    481       -0.5f,  0.0f, -2.0f,
    482        0.5f,  0.0f,  0.0f,
    483        0.5f,  0.0f,  0.0f,
    484       -0.5f,  0.0f, -2.0f,
    485        0.5f,  0.0f, -2.0f,
     484      -0.5f,    0.0f,    0.0f,
     485      -0.5f,    0.0f,  -2.0f,
     486       0.5f,    0.0f,    0.0f,
     487       0.5f,    0.0f,    0.0f,
     488      -0.5f,    0.0f,  -2.0f,
     489       0.5f,    0.0f,  -2.0f,
    486490
    487491      // top mid
    488       -0.25f,  0.3f, -3.0f,
    489       -0.5f,  0.3f, -2.0f,
    490        0.5f,  0.3f, -2.0f,
    491       -0.25f,  0.3f, -3.0f,
    492        0.5f,  0.3f, -2.0f,
    493        0.25f,  0.3f, -3.0f,
     492      -0.25f,   0.3f,  -3.0f,
     493      -0.5f,    0.3f,  -2.0f,
     494       0.5f,    0.3f,  -2.0f,
     495      -0.25f,   0.3f,  -3.0f,
     496       0.5f,    0.3f,  -2.0f,
     497       0.25f,   0.3f,  -3.0f,
    494498
    495499      // bottom mid
    496       -0.5f,  0.0f, -2.0f,
    497       -0.25f,  0.0f, -3.0f,
    498        0.5f,  0.0f, -2.0f,
    499        0.5f,  0.0f, -2.0f,
    500       -0.25f,  0.0f, -3.0f,
    501        0.25f,  0.0f, -3.0f,
     500      -0.5f,    0.0f,  -2.0f,
     501      -0.25f,   0.0f,  -3.0f,
     502       0.5f,    0.0f,  -2.0f,
     503       0.5f,    0.0f,  -2.0f,
     504      -0.25f,   0.0f,  -3.0f,
     505       0.25f,   0.0f,  -3.0f,
    502506
    503507      // top front
    504       -0.25f,  0.3f, -3.0f,
    505        0.25f,  0.3f, -3.0f,
    506        0.0f,  0.0f, -3.5f,
     508      -0.25f,   0.3f,  -3.0f,
     509       0.25f,   0.3f,  -3.0f,
     510       0.0f,    0.0f,  -3.5f,
    507511
    508512      // bottom front
    509        0.25f,  0.0f, -3.0f,
    510       -0.25f,  0.0f, -3.0f,
    511        0.0f,  0.0f, -3.5f,
     513       0.25f,   0.0f,  -3.0f,
     514      -0.25f,   0.0f,  -3.0f,
     515       0.0f,    0.0f,  -3.5f,
    512516
    513517      // left wing start back
    514       -1.5f,  0.3f,  0.0f,
    515       -1.5f,  0.0f,  0.0f,
    516       -0.5f,  0.0f,  0.0f,
    517       -1.5f,  0.3f,  0.0f,
    518       -0.5f,  0.0f,  0.0f,
    519       -0.5f,  0.3f,  0.0f,
     518      -1.5f,    0.3f,    0.0f,
     519      -1.5f,    0.0f,    0.0f,
     520      -0.5f,    0.0f,    0.0f,
     521      -1.5f,    0.3f,    0.0f,
     522      -0.5f,    0.0f,    0.0f,
     523      -0.5f,    0.3f,    0.0f,
    520524
    521525      // left wing start top
    522       -0.5f,  0.3f, -0.3f,
    523       -1.3f,  0.3f, -0.3f,
    524       -1.5f,  0.3f,  0.0f,
    525       -0.5f,  0.3f, -0.3f,
    526       -1.5f,  0.3f,  0.0f,
    527       -0.5f,  0.3f,  0.0f,
     526      -0.5f,    0.3f,  -0.3f,
     527      -1.3f,    0.3f,  -0.3f,
     528      -1.5f,    0.3f,    0.0f,
     529      -0.5f,    0.3f,  -0.3f,
     530      -1.5f,    0.3f,    0.0f,
     531      -0.5f,    0.3f,    0.0f,
    528532
    529533      // left wing start front
    530       -0.5f,  0.3f, -0.3f,
    531       -0.5f,  0.0f, -0.3f,
    532       -1.3f,  0.0f, -0.3f,
    533       -0.5f,  0.3f, -0.3f,
    534       -1.3f,  0.0f, -0.3f,
    535       -1.3f,  0.3f, -0.3f,
     534      -0.5f,    0.3f,  -0.3f,
     535      -0.5f,    0.0f,  -0.3f,
     536      -1.3f,    0.0f,  -0.3f,
     537      -0.5f,    0.3f,  -0.3f,
     538      -1.3f,    0.0f,  -0.3f,
     539      -1.3f,    0.3f,  -0.3f,
    536540
    537541      // left wing start bottom
    538       -0.5f, 0.0f, 0.0f,
    539       -1.5f, 0.0f, 0.0f,
    540       -1.3f, 0.0f, -0.3f,
    541       -0.5f, 0.0f, 0.0f,
    542       -1.3f, 0.0f, -0.3f,
    543       -0.5f, 0.0f, -0.3f,
     542      -0.5f,    0.0f,    0.0f,
     543      -1.5f,    0.0f,    0.0f,
     544      -1.3f,    0.0f,  -0.3f,
     545      -0.5f,    0.0f,    0.0f,
     546      -1.3f,    0.0f,  -0.3f,
     547      -0.5f,    0.0f,  -0.3f,
    544548
    545549      // left wing end outside
    546       -1.5f,  0.3f,  0.0f,
    547       -2.2f,  0.15f, -0.8f,
    548       -1.5f,  0.0f,  0.0f,
     550      -1.5f,    0.3f,    0.0f,
     551      -2.2f,    0.15f, -0.8f,
     552      -1.5f,    0.0f,    0.0f,
    549553
    550554      // left wing end top
    551       -1.3f,  0.3f, -0.3f,
    552       -2.2f,  0.15f, -0.8f,
    553       -1.5f,  0.3f,  0.0f,
     555      -1.3f,    0.3f,  -0.3f,
     556      -2.2f,    0.15f, -0.8f,
     557      -1.5f,    0.3f,    0.0f,
    554558
    555559      // left wing end front
    556       -1.3f, 0.0f, -0.3f,
    557       -2.2f, 0.15f, -0.8f,
    558       -1.3f, 0.3f, -0.3f,
     560      -1.3f,    0.0f,  -0.3f,
     561      -2.2f,    0.15f, -0.8f,
     562      -1.3f,    0.3f,  -0.3f,
    559563
    560564      // left wing end bottom
    561       -1.5f, 0.0f, 0.0f,
    562       -2.2f,  0.15f, -0.8f,
    563       -1.3f, 0.0f, -0.3f,
     565      -1.5f,    0.0f,    0.0f,
     566      -2.2f,    0.15f, -0.8f,
     567      -1.3f,    0.0f,  -0.3f,
    564568
    565569      // right wing start back
    566        1.5f, 0.0f, 0.0f,
    567        1.5f, 0.3f, 0.0f,
    568        0.5f, 0.0f, 0.0f,
    569        0.5f, 0.0f, 0.0f,
    570        1.5f, 0.3f, 0.0f,
    571        0.5f, 0.3f, 0.0f,
     570       1.5f,    0.0f,    0.0f,
     571       1.5f,    0.3f,    0.0f,
     572       0.5f,    0.0f,    0.0f,
     573       0.5f,    0.0f,    0.0f,
     574       1.5f,    0.3f,    0.0f,
     575       0.5f,    0.3f,    0.0f,
    572576
    573577      // right wing start top
    574        1.3f, 0.3f, -0.3f,
    575        0.5f, 0.3f, -0.3f,
    576        1.5f, 0.3f, 0.0f,
    577        1.5f, 0.3f, 0.0f,
    578        0.5f, 0.3f, -0.3f,
    579        0.5f, 0.3f, 0.0f,
     578       1.3f,    0.3f,  -0.3f,
     579       0.5f,    0.3f,  -0.3f,
     580       1.5f,    0.3f,    0.0f,
     581       1.5f,    0.3f,    0.0f,
     582       0.5f,    0.3f,  -0.3f,
     583       0.5f,    0.3f,    0.0f,
    580584
    581585      // right wing start front
    582        0.5f,  0.0f, -0.3f,
    583        0.5f,  0.3f, -0.3f,
    584        1.3f,  0.0f, -0.3f,
    585        1.3f,  0.0f, -0.3f,
    586        0.5f,  0.3f, -0.3f,
    587        1.3f,  0.3f, -0.3f,
     586       0.5f,    0.0f,  -0.3f,
     587       0.5f,    0.3f,  -0.3f,
     588       1.3f,    0.0f,  -0.3f,
     589       1.3f,    0.0f,  -0.3f,
     590       0.5f,    0.3f,  -0.3f,
     591       1.3f,    0.3f,  -0.3f,
    588592
    589593      // right wing start bottom
    590        1.5f, 0.0f, 0.0f,
    591        0.5f, 0.0f, 0.0f,
    592        1.3f, 0.0f, -0.3f,
    593        1.3f, 0.0f, -0.3f,
    594        0.5f, 0.0f, 0.0f,
    595        0.5f, 0.0f, -0.3f,
     594       1.5f,    0.0f,    0.0f,
     595       0.5f,    0.0f,    0.0f,
     596       1.3f,    0.0f,  -0.3f,
     597       1.3f,    0.0f,  -0.3f,
     598       0.5f,    0.0f,    0.0f,
     599       0.5f,    0.0f,  -0.3f,
    596600
    597601      // right wing end outside
    598        2.2f,  0.15f, -0.8f,
    599        1.5f,  0.3f,  0.0f,
    600        1.5f,  0.0f,  0.0f,
     602       2.2f,    0.15f, -0.8f,
     603       1.5f,    0.3f,    0.0f,
     604       1.5f,    0.0f,    0.0f,
    601605
    602606      // right wing end top
    603        2.2f,  0.15f, -0.8f,
    604        1.3f,  0.3f, -0.3f,
    605        1.5f,  0.3f,  0.0f,
     607       2.2f,    0.15f, -0.8f,
     608       1.3f,    0.3f,  -0.3f,
     609       1.5f,    0.3f,    0.0f,
    606610
    607611      // right wing end front
    608        2.2f, 0.15f, -0.8f,
    609        1.3f, 0.0f, -0.3f,
    610        1.3f, 0.3f, -0.3f,
     612       2.2f,    0.15f, -0.8f,
     613       1.3f,    0.0f,  -0.3f,
     614       1.3f,    0.3f,  -0.3f,
    611615
    612616      // right wing end bottom
    613        2.2f,  0.15f, -0.8f,
    614        1.5f, 0.0f, 0.0f,
    615        1.3f, 0.0f, -0.3f,
     617       2.2f,    0.15f, -0.8f,
     618       1.5f,    0.0f,    0.0f,
     619       1.3f,    0.0f,  -0.3f,
    616620   };
    617621   obj.colors = {
     
    816820      ubo,
    817821      model_mat_idx_vbo);
    818 
    819    /*
    820    spawnAsteroid(vec3(0.0f, -1.2f, -21.5f), color_sp,
    821       shaderBufferInfo,
    822       points_vbo,
    823       colors_vbo,
    824       selected_colors_vbo,
    825       texcoords_vbo,
    826       normals_vbo,
    827       ubo,
    828       model_mat_idx_vbo);
    829    spawnAsteroid(vec3(1.0f, -1.2f, -21.5f), color_sp,
    830       shaderBufferInfo,
    831       points_vbo,
    832       colors_vbo,
    833       selected_colors_vbo,
    834       texcoords_vbo,
    835       normals_vbo,
    836       ubo,
    837       model_mat_idx_vbo);
    838    spawnAsteroid(vec3(-0.5f, -1.2f, -20.8f), color_sp,
    839       shaderBufferInfo,
    840       points_vbo,
    841       colors_vbo,
    842       selected_colors_vbo,
    843       texcoords_vbo,
    844       normals_vbo,
    845       ubo,
    846       model_mat_idx_vbo);
    847    spawnAsteroid(vec3(-0.3f, -1.2f, -20.8f), color_sp,
    848       shaderBufferInfo,
    849       points_vbo,
    850       colors_vbo,
    851       selected_colors_vbo,
    852       texcoords_vbo,
    853       normals_vbo,
    854       ubo,
    855       model_mat_idx_vbo);
    856    spawnAsteroid(vec3(-0.1f, -1.2f, -20.8f), color_sp,
    857       shaderBufferInfo,
    858       points_vbo,
    859       colors_vbo,
    860       selected_colors_vbo,
    861       texcoords_vbo,
    862       normals_vbo,
    863       ubo,
    864       model_mat_idx_vbo);
    865    */
    866822
    867823   GLuint vao = 0;
     
    13731329
    13741330void addObjectToSceneDuringInit(SceneObject& obj) {
     1331   // Each objects must have at least 3 points, so the size of
     1332   // the points array must be a positive multiple of 9
     1333   if (obj.points.size() == 0 || (obj.points.size() % 9) != 0) {
     1334      return;
     1335   }
     1336
    13751337   obj.id = objects.size(); // currently unused
    13761338   obj.num_points = obj.points.size() / 3;
     
    13931355      }
    13941356   }
     1357
     1358   calculateObjectBoundingBox(obj);
    13951359
    13961360   objects.push_back(obj);
     
    14101374   BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program];
    14111375
    1412    // Check if the buffers aren't large enough to fit the new object and, if so, print an error and quit.
    1413    // This is a temporary sanity check to make sure things are working as expected
     1376   // Check if the buffers aren't large enough to fit the new object and, if so, call
     1377   // populateBuffers() to resize and repopupulate them
    14141378   if (bufferInfo->vbo_capacity < (bufferInfo->ubo_offset + obj.num_points) ||
    14151379      bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) {
     
    14451409}
    14461410
     1411void calculateObjectBoundingBox(SceneObject& obj) {
     1412   GLfloat min_x = obj.points[0];
     1413   GLfloat max_x = obj.points[0];
     1414   GLfloat min_y = obj.points[1];
     1415   GLfloat max_y = obj.points[1];
     1416   GLfloat min_z = obj.points[2];
     1417   GLfloat max_z = obj.points[2];
     1418
     1419   // start from the second point
     1420   for (int i = 3; i < obj.points.size(); i += 3) {
     1421      if (min_x > obj.points[i]) {
     1422         min_x = obj.points[i];
     1423      }
     1424      else if (max_x < obj.points[i]) {
     1425         max_x = obj.points[i];
     1426      }
     1427
     1428      if (min_y > obj.points[i + 1]) {
     1429         min_y = obj.points[i + 1];
     1430      }
     1431      else if (max_y < obj.points[i + 1]) {
     1432         max_y = obj.points[i + 1];
     1433      }
     1434
     1435      if (min_z > obj.points[i + 2]) {
     1436         min_z = obj.points[i + 2];
     1437      }
     1438      else if (max_z < obj.points[i + 2]) {
     1439         max_z = obj.points[i + 2];
     1440      }
     1441   }
     1442
     1443   obj.bounding_center = vec3((min_x + max_x) / 2.0f, (min_y + max_y) / 2.0f, (min_z + max_z) / 2.0f);
     1444
     1445   GLfloat radius_x = max_x - obj.bounding_center.x;
     1446   GLfloat radius_y = max_y - obj.bounding_center.y;
     1447   GLfloat radius_z = max_z - obj.bounding_center.z;
     1448
     1449   obj.bounding_radius = radius_x;
     1450   if (obj.bounding_radius < radius_y)
     1451      obj.bounding_radius = radius_y;
     1452   if (obj.bounding_radius < radius_z)
     1453      obj.bounding_radius = radius_z;
     1454
     1455   for (int i = 0; i < obj.points.size(); i += 3) {
     1456      obj.points[i] -= obj.bounding_center.x;
     1457      obj.points[i + 1] -= obj.bounding_center.y;
     1458      obj.points[i + 2] -= obj.bounding_center.z;
     1459   }
     1460
     1461   obj.bounding_center = vec3(0.0f, 0.0f, 0.0f);
     1462}
     1463
    14471464void initializeBuffers(
    14481465                  GLuint* points_vbo,
     
    16261643   }
    16271644
    1628    obj.model_mat = obj.model_base * obj.model_transform;
     1645   obj.model_mat = obj.model_transform * obj.model_base;
    16291646   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    16301647   glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
     
    16351652
    16361653void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo) {
    1637    obj.model_transform = obj.model_transform * transform;
     1654   obj.model_transform = transform * obj.model_transform;
    16381655   obj.model_mat = obj.model_transform * obj.model_base;
    16391656
Note: See TracChangeset for help on using the changeset viewer.