Browse Source

adding entity positioning and scaling

master
cinnaboot 8 years ago
parent
commit
ecb52a9374
  1. 269
      data/SPIDBOT1.dae
  2. 1
      data/default.fs
  3. 18
      src/entity.h
  4. 1
      src/gooey.cpp
  5. 22
      src/hexgame.cpp
  6. 3
      src/mesh.cpp
  7. 10
      src/render_group.cpp
  8. 13
      src/renderer.cpp

269
data/SPIDBOT1.dae

File diff suppressed because one or more lines are too long

1
data/default.fs

@ -22,6 +22,5 @@ void main()
brightness = clamp(brightness, 0, 1); brightness = clamp(brightness, 0, 1);
color = brightness * fragmentColor; color = brightness * fragmentColor;
//color = vec3(255, 255, 255);
} }

18
src/entity.h

@ -1,6 +1,9 @@
#pragma once #pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "mesh.h" #include "mesh.h"
#include "render_group.h" #include "render_group.h"
@ -9,9 +12,24 @@ struct render_group;
struct Entity struct Entity
{ {
glm::mat4 world_transform;
glm::vec3 scale;
glm::vec3 translation;
glm::vec4 rotation; // maybe use quaternion for this one
meMeshGroup mesh_group; meMeshGroup mesh_group;
render_group* ren_group; render_group* ren_group;
//TODO: more entity properties: sound, gameplay info //TODO: more entity properties: sound, gameplay info
}; };
inline void
entityTranslate(Entity* e, int x, int y, int z)
{
e->world_transform = glm::translate(e->world_transform, glm::vec3(x, y, z));
}
inline void
entityScale(Entity* e, float sx, float sy, float sz)
{
//mi->model_transform = glm::scale(glm::mat4(1), glm::vec3(100, 100, 100));
}

1
src/gooey.cpp

@ -84,6 +84,7 @@ renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
ImGui::Text("is_selecting"); ImGui::Text("is_selecting");
ImGui::SameLine(); ImGui::TextUnformatted(is_selecting ? "true" : "false"); ImGui::SameLine(); ImGui::TextUnformatted(is_selecting ? "true" : "false");
ImGui::SetNextTreeNodeOpen(true);
if (ImGui::CollapsingHeader("Camera Position")) { if (ImGui::CollapsingHeader("Camera Position")) {
ImGui::Text("x: %f", camera_pos.x); ImGui::Text("x: %f", camera_pos.x);
ImGui::Text("y: %f", camera_pos.y); ImGui::Text("y: %f", camera_pos.y);

22
src/hexgame.cpp

@ -6,12 +6,13 @@
// - lighting // - lighting
// - add light struct // - add light struct
// - pass all lights to render groups/shaders every frame // - pass all lights to render groups/shaders every frame
// - maybe use more than one model in a scene
// - map generation // - map generation
// - pathfinding // - pathfinding
// - assimp animation // - assimp animation
// - replace aixlog with custom logging iostream? // - replace aixlog with custom logging iostream?
// - fix vector normalization in renderer.cpp when moving in 2 directions // - fix vector normalization in renderer.cpp when moving in 2 directions
// - may be fixed?? need to test by examining camera position between frames
// and compare the distance
// - add prefix to interface function names for eg) gooey.h, renderer.h // - add prefix to interface function names for eg) gooey.h, renderer.h
// - move hex logic to new file to leave only init glue in a main.cpp // - move hex logic to new file to leave only init glue in a main.cpp
// - move SDL input callbacks somewhere too? // - move SDL input callbacks somewhere too?
@ -19,7 +20,6 @@
// - actually don't need to save the vertex/normal buffers after passing // - actually don't need to save the vertex/normal buffers after passing
// to opengl // to opengl
// - replace calls to malloc/free with safe(r) function in util.h // - replace calls to malloc/free with safe(r) function in util.h
// - also need to make a macro that works with malloc void * and c++
// - add cpu perforcmace counters in render loop // - add cpu perforcmace counters in render loop
// - check for memory leaks w/ valgrind // - check for memory leaks w/ valgrind
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -550,12 +550,15 @@ int main(int argc, char* argv[])
game_state* g = g_game_state; game_state* g = g_game_state;
g->entities = (Entity*) std::calloc(1000, sizeof(Entity)); g->entities = (Entity*) std::calloc(1000, sizeof(Entity));
const char* test_file1 = "../data/animated.block.dae";
const char* test_file2 = "../data/catepillar.dae";
meMeshGroup mg1, mg2; meMeshGroup mg1, mg2;
if (meLoadFromFile(test_file1, mg1)) { // TODO: store these meMeshGroups serperately from entities to re-use them,
g->entities[g->entity_count].mesh_group = mg1; // and to make initializing easier, eg) appling translation has to happen after
// rgInitEntity() called from renderer createScene()
if (meLoadFromFile("../data/animated.block.dae", mg1)) {
Entity& e = g->entities[0];
e.mesh_group = mg1;
e.scale = glm::vec3(100,100,100);
g->entity_count++; g->entity_count++;
} }
else { else {
@ -563,8 +566,11 @@ int main(int argc, char* argv[])
return 1; return 1;
} }
if (meLoadFromFile(test_file2, mg2)) { if (meLoadFromFile("../data/catepillar.dae", mg2)) {
g->entities[g->entity_count].mesh_group = mg2; Entity& e = g->entities[1];
e.mesh_group = mg2;
e.scale = glm::vec3(10,10,10);
e.translation = glm::vec3(640, 500, 0);
g->entity_count++; g->entity_count++;
} }
else { else {

3
src/mesh.cpp

@ -81,8 +81,7 @@ meMeshInfo*
copyMeshInfo(const aiScene* scene, aiMesh* mesh) copyMeshInfo(const aiScene* scene, aiMesh* mesh)
{ {
meMeshInfo* mi = (meMeshInfo*) std::calloc(1, sizeof(meMeshInfo)); meMeshInfo* mi = (meMeshInfo*) std::calloc(1, sizeof(meMeshInfo));
// TODO: using hard coded model transform for now mi->model_transform = glm::mat4(1);
mi->model_transform = glm::scale(glm::mat4(1), glm::vec3(100, 100, 100));
// allocate buffers for vertex and index data from mesh // allocate buffers for vertex and index data from mesh
mi->num_vertices = mesh->mNumVertices; mi->num_vertices = mesh->mNumVertices;

10
src/render_group.cpp

@ -115,6 +115,11 @@ rgInitEntity(Entity* e)
rg->use_normals = e->mesh_group.use_normals; rg->use_normals = e->mesh_group.use_normals;
uint num_meshes = rg->num_objects = e->mesh_group.num_meshes; uint num_meshes = rg->num_objects = e->mesh_group.num_meshes;
// apply translation/rotation/scaling
e->world_transform = glm::mat4(1.0);
e->world_transform = glm::translate(e->world_transform, e->translation);
e->world_transform = glm::scale(e->world_transform, e->scale);
rg->render_objects = (render_object**) std::calloc(num_meshes, sizeof(render_object*)); rg->render_objects = (render_object**) std::calloc(num_meshes, sizeof(render_object*));
for (uint i = 0; i < num_meshes; i++) for (uint i = 0; i < num_meshes; i++)
@ -325,9 +330,12 @@ drawRenderObject(render_group* rg, render_object* ro,
// draw // draw
if (rg->draw_indexed) { if (rg->draw_indexed) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro->index_buffer.buffer_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro->index_buffer.buffer_id);
// TODO: find out why rg->draw_mode isn't being preserved as GL_TRIANGLES
//glDrawElements(rg->draw_mode, ro->index_buffer.buffer_len, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, ro->index_buffer.buffer_len, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, ro->index_buffer.buffer_len, GL_UNSIGNED_INT, 0);
} else { } else {
glDrawArrays(rg->draw_mode, 0, ro->vertex_buffer.buffer_len / 3); //glDrawArrays(rg->draw_mode, 0, ro->vertex_buffer.buffer_len / 3);
glDrawArrays(GL_TRIANGLES, 0, ro->vertex_buffer.buffer_len / 3);
} }
// cleanup // cleanup

13
src/renderer.cpp

@ -105,7 +105,7 @@ addTexture(SDL_Handles &handles, const char * path)
if (!image) if (!image)
{ {
LOG(ERROR) << "IMG_Load: " << IMG_GetError() << "\n"; LOG(ERROR) << "IMG_Load: " << IMG_GetError() << "\n";
return 1; return false;
} }
GLuint tex_id; GLuint tex_id;
@ -451,6 +451,9 @@ createScene(std::vector<hex_info>* hexes, Entity* entities, uint32 entity_count)
rgInitEntity(&entities[i]); rgInitEntity(&entities[i]);
entities[i].ren_group->shader = sp; entities[i].ren_group->shader = sp;
} }
//mi->model_transform = glm::scale(glm::mat4(1), glm::vec3(100, 100, 100));
//entityTranslate(&g_game_state->entities[1], 640, 500, 0);
#else #else
if (!rgInitEntity(&entities[0])) { if (!rgInitEntity(&entities[0])) {
LOG(ERROR) << "Error initialzing entity\n"; LOG(ERROR) << "Error initialzing entity\n";
@ -464,7 +467,7 @@ createScene(std::vector<hex_info>* hexes, Entity* entities, uint32 entity_count)
//g_test_light.light_ID = glGetUniformLocation(g_entity_render_group.program_id, "light_position"); //g_test_light.light_ID = glGetUniformLocation(g_entity_render_group.program_id, "light_position");
if (entities[0].ren_group != nullptr) { if (entities[0].ren_group != nullptr) {
g_test_light.light_ID = glGetUniformLocation(entities[0].ren_group->shader.program_id, "light_position"); g_test_light.light_ID = glGetUniformLocation(entities[0].ren_group->shader.program_id, "light_position");
g_test_light.position = glm::vec3(640, 500, 400); // above center of hexgrid g_test_light.position = glm::vec3(800, 300, 400); // above center of hexgrid
g_test_light.direction = glm::vec3(0, 0, 0) - g_test_light.position; // back towards test entity g_test_light.direction = glm::vec3(0, 0, 0) - g_test_light.position; // back towards test entity
g_test_light.color = glm::vec3(1.f, 1.f, 1.f); g_test_light.color = glm::vec3(1.f, 1.f, 1.f);
g_test_light.intensity = 1.f; g_test_light.intensity = 1.f;
@ -585,12 +588,8 @@ renderFrame(std::vector<hex_info> *hexes, Entity* entities, uint32 entity_count)
// entities // entities
for (uint i = 0; i < entity_count; i++) { for (uint i = 0; i < entity_count; i++) {
// TODO: find a way to store model xform on the meMeshGroup object
glm::mat4 model_transform = entities[i].mesh_group.meshes[0]->model_transform;
rgDraw( rgDraw(
entities[i].ren_group, model_transform, m_view, m_projection, entities[i].ren_group, entities[i].world_transform , m_view, m_projection,
g_test_light.position, g_test_light.light_ID g_test_light.position, g_test_light.light_ID
); );
} }

Loading…
Cancel
Save