Skip to content

Commit

Permalink
OBJ loader speed up, texture with alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
mtiapko committed Feb 6, 2019
1 parent e15eb31 commit d19fa8e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/res/audio/
/res/model/
/res/skybox/
/res/tex/
/obj/
/dk
2 changes: 1 addition & 1 deletion include/util/Perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace std
{
template<> struct hash<dk::util::PerfEntityLocation> {
std::size_t operator()(const dk::util::PerfEntityLocation &loc) const {
return std::hash<dk::StringView>{}(loc.func);
return std::hash<dk::StringView>{}(loc.func) + loc.line;
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion res/shader/vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void main()

mat4 proj = proj_mat;
mat4 view = view_mat;
mat4 model = model_mat;

gl_Position = vec4(attr_vert_pos, 1.0) * view * proj;
gl_Position = vec4(attr_vert_pos, 1.0) * model * view * proj;
}
12 changes: 6 additions & 6 deletions src/graph/CubeMapTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Status CubeMapTexture::create(const TextureData& right, const TextureData& left,
GL_CALL(glGenTextures(1, &m_id));
GL_CALL(glBindTexture(GL_TEXTURE_CUBE_MAP, m_id));

GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, Texture::to_gl_fmt(right), right.width(), right.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, right.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, Texture::to_gl_fmt(left), left.width(), left.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, left.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, Texture::to_gl_fmt(top), top.width(), top.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, top.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, Texture::to_gl_fmt(bottom), bottom.width(), bottom.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, bottom.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, Texture::to_gl_fmt(front), front.width(), front.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, front.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, Texture::to_gl_fmt(back), back.width(), back.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, back.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, right.width(), right.height(), 0, Texture::to_gl_fmt(right), GL_UNSIGNED_BYTE, right.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, left.width(), left.height(), 0, Texture::to_gl_fmt(left), GL_UNSIGNED_BYTE, left.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, top.width(), top.height(), 0, Texture::to_gl_fmt(top), GL_UNSIGNED_BYTE, top.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, bottom.width(), bottom.height(), 0, Texture::to_gl_fmt(bottom), GL_UNSIGNED_BYTE, bottom.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, front.width(), front.height(), 0, Texture::to_gl_fmt(front), GL_UNSIGNED_BYTE, front.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, back.width(), back.height(), 0, Texture::to_gl_fmt(back), GL_UNSIGNED_BYTE, back.data()));

GL_CALL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
Expand Down
2 changes: 1 addition & 1 deletion src/graph/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Status Texture::create(const TextureData& data) noexcept
GLenum format = to_gl_fmt(data);
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, format, data.width(), data.height(), 0, format, GL_UNSIGNED_BYTE, data.data()));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data.width(), data.height(), 0, format, GL_UNSIGNED_BYTE, data.data()));
GL_CALL(glGenerateMipmap(GL_TEXTURE_2D));
this->disable();

Expand Down
20 changes: 13 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ class test_app final: public Application, sys::EventListener<sys::WindowCloseEve
graph::Camera m_camera;

graph::ShaderProgram m_shader;
graph::UniformLocation m_view_loc;
graph::UniformLocation m_proj_loc;
graph::UniformLocation m_view_loc;
graph::UniformLocation m_model_loc;

graph::CubeMap m_cube_map;
graph::CubeMapTexture m_cube_map_tex;
graph::ShaderProgram m_cube_map_shader;
graph::UniformLocation m_cube_map_view_loc;
graph::UniformLocation m_cube_map_proj_loc;

float m_val = 0.0f;
float m_val = 1.0f;

public:
~test_app() noexcept override
Expand Down Expand Up @@ -70,19 +71,21 @@ class test_app final: public Application, sys::EventListener<sys::WindowCloseEve
{
m_wnd->clear();
auto view = m_camera.view();
auto model = math::Mat4f(m_val);

m_cube_map_shader.enable();
m_shader.set_uniform(m_cube_map_view_loc, view);
m_cube_map_shader.set_uniform(m_cube_map_view_loc, view);
m_cube_map.render();

m_shader.enable();
m_shader.set_uniform(m_view_loc, view);
m_shader.set_uniform(m_model_loc, model);
m_texture.enable();
m_stall.render();

/* GUI */
ImGui::Begin("Matrix");
ImGui::SliderFloat("Rot Y", &m_val, 0.0f, 360.0f);
ImGui::SliderFloat("Rot Y", &m_val, 0.01f, 5.0f);
ImGui::End();

graph::GUI::render();
Expand All @@ -102,13 +105,13 @@ class test_app final: public Application, sys::EventListener<sys::WindowCloseEve
if (auto ret = m_res_mgr.load(m_sint, "res/audio/sint.wav"); !ret)
return ret;

if (auto ret = m_res_mgr.load(m_stall, "res/model/stall.obj"); !ret)
if (auto ret = m_res_mgr.load(m_stall, "res/model/lost_empire.obj"); !ret)
return ret;

if ((m_sint_2 = m_res_mgr.load<audio::Sound>("res/audio/sint.wav")) == nullptr)
return Status::ERROR;

if (auto ret = m_res_mgr.load(m_texture, "res/tex/stallTexture.png"); !ret)
if (auto ret = m_res_mgr.load(m_texture, "res/tex/lost_empire-RGBA.png"); !ret)
return ret;

/* basic shader */
Expand All @@ -126,8 +129,9 @@ class test_app final: public Application, sys::EventListener<sys::WindowCloseEve

auto proj = math::Mat4f::get_perspective(90.0f, m_wnd->ratio(), 0.1f, 100.0f);
m_shader.enable();
m_shader.uniform_location("view_mat", m_view_loc);
m_shader.uniform_location("proj_mat", m_proj_loc);
m_shader.uniform_location("view_mat", m_view_loc);
m_shader.uniform_location("model_mat", m_model_loc);
m_shader.set_uniform(m_proj_loc, proj);

/* cube map shader */
Expand Down Expand Up @@ -173,6 +177,8 @@ class test_app final: public Application, sys::EventListener<sys::WindowCloseEve
sys::EventManager<sys::WindowCloseEvent>::get().subscribe(this);
sys::Mouse::record_input(true);
sys::Keyboard::record_input(true);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return Status::OK;
}

Expand Down
2 changes: 1 addition & 1 deletion src/math/SIMD_Mat4f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SIMD_Mat4f::SIMD_Mat4f(float e) noexcept
row[0] = _mm_set_ps(0.0f, 0.0f, 0.0f, e);
row[1] = _mm_set_ps(0.0f, 0.0f, e, 0.0f);
row[2] = _mm_set_ps(0.0f, e, 0.0f, 0.0f);
row[3] = _mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f);
row[3] = _mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f);
}

SIMD_Mat4f::SIMD_Mat4f(float e00, float e11, float e22, float e33 /* = 1.0f */) noexcept
Expand Down
8 changes: 5 additions & 3 deletions src/sys/loaders/OBJ_Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ Status OBJ_Loader::load(graph::ModelData& model_data, StringView file_path) noex
}
break;
case 'f':
model_data.indx.reserve(model_data.indx.capacity() + 3);
norm_indx.reserve(norm_indx.capacity() + 3);
uv_indx.reserve(uv_indx.capacity() + 3);
// TODO: remove. super slow, let it grow exponentially
//model_data.indx.reserve(model_data.indx.capacity() + 3);
//norm_indx.reserve(norm_indx.capacity() + 3);
//uv_indx.reserve(uv_indx.capacity() + 3);

skip_spaces(++file_data);
read(file_data, model_data.indx.emplace_back());
Expand Down Expand Up @@ -190,6 +191,7 @@ Status OBJ_Loader::load(graph::ModelData& model_data, StringView file_path) noex
return Status::ERROR;
}

DK_LOG_OK("Loaded. Compiling size: ", model_data.indx.size());
if (model_data.indx.size() % 3 != 0)
DK_LOG_WARNING(""); // XXX: is this possible? no, is this legal???

Expand Down

0 comments on commit d19fa8e

Please sign in to comment.