-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
textures + mesh loader (works, but looks bad)
- Loading branch information
Showing
11 changed files
with
1,870 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef __DK_TEXTURE_H__ | ||
#define __DK_TEXTURE_H__ | ||
|
||
#include <SOIL2.h> | ||
#include "log.h" | ||
#include "status.h" | ||
#include "graphic/gl.h" | ||
#include "containers/string_view.h" | ||
|
||
namespace dk | ||
{ | ||
|
||
class texture | ||
{ | ||
private: | ||
int32_t m_height; | ||
int32_t m_width; | ||
uint8_t* m_data; | ||
GLuint m_tex; | ||
|
||
public: | ||
void enable() const { GL_CALL(glBindTexture(GL_TEXTURE_2D, m_tex)); } | ||
void disable() const { GL_CALL(glBindTexture(GL_TEXTURE_2D, 0)); } | ||
|
||
status create(std::string_view file_path) | ||
{ | ||
auto beg = std::chrono::high_resolution_clock::now(); | ||
m_data = SOIL_load_image(file_path.data(), &m_width, &m_height, nullptr, SOIL_LOAD_RGB); | ||
if (m_data == nullptr) { | ||
DK_LOG_ERROR("Failed to load texture from file '", file_path, "': ", SOIL_last_result()); | ||
return status::ERROR; | ||
} | ||
|
||
GL_CALL(glGenTextures(1, &m_tex)); | ||
this->enable(); | ||
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, m_data)); | ||
GL_CALL(glGenerateMipmap(GL_TEXTURE_2D)); | ||
this->disable(); | ||
|
||
SOIL_free_image_data(m_data); | ||
auto end = std::chrono::high_resolution_clock::now(); | ||
auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - beg); | ||
DK_LOG_OK("Texture '", file_path, "' loaded (", dur.count() / 1000.0f, " ms): ", m_width, "x", m_height); | ||
return status::OK; | ||
} | ||
}; | ||
|
||
} | ||
|
||
#endif // !__DK_TEXTURE_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "containers/string_view.h" | ||
|
||
dk::string_view vert_src{ R"glsl( | ||
#version 330 core | ||
layout(location = 0) in vec3 vertexPosition_modelspace; | ||
layout(location = 1) in vec3 norm; | ||
layout(location = 2) in vec3 in_tex_coord; | ||
uniform mat4 mat; | ||
varying vec3 my_norm; | ||
varying vec2 tex_coord; | ||
void main(){ | ||
my_norm = norm; | ||
tex_coord = in_tex_coord.xy; | ||
gl_Position = vec4(vertexPosition_modelspace, 1) * mat; | ||
} | ||
)glsl" }; | ||
|
||
dk::string_view frag_src{ R"glsl( | ||
#version 330 core | ||
uniform sampler2D tex; | ||
out vec3 color; | ||
varying vec3 my_norm; | ||
varying vec2 tex_coord; | ||
void main(){ | ||
//color = vec3(gl_FragCoord.z); | ||
color = texture(tex, vec2(tex_coord.x, 1.0 - tex_coord.y)).rgb; | ||
} | ||
)glsl" }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.