Skip to content

Commit

Permalink
textures + mesh loader (works, but looks bad)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtiapko committed Dec 1, 2018
1 parent b6f9133 commit e32d625
Show file tree
Hide file tree
Showing 11 changed files with 1,870 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CC := g++
LD := g++

CC_FLAGS := -std=c++2a -MMD -MP -g
CC_FLAGS := -std=c++2a -MMD -MP -Ofast
CC_INCLUDES := -I./include
CC_DEFINES :=

LD_FLAGS :=
LD_LIBS := -lEGL -lX11 -lGLEW -lGL
LD_LIBS := -lEGL -lX11 -lGLEW -lGL -lsoil2

SRC_DIR := src src/graphic src/math
OBJ_DIR := obj
Expand Down
3 changes: 2 additions & 1 deletion include/graphic/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace dk
enum class shader_type
{
VERTEX_SHADER = 0,
FRAGMENT_SHADER
FRAGMENT_SHADER,
GEOMETRY_SHADER
};

class shader
Expand Down
50 changes: 50 additions & 0 deletions include/graphic/texture.h
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__
28 changes: 28 additions & 0 deletions include/tmp.h
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" };
29 changes: 16 additions & 13 deletions res/cube.obj
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ vn 1.0000 -0.0000 0.0000
vn 0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
vt 0 0 0
f 2/1/1 4/1/1 1/1/1
f 8/1/2 6/1/2 5/1/2
f 5/1/3 2/1/3 1/1/3
f 6/1/4 3/1/4 2/1/4
f 3/1/5 8/1/5 4/1/5
f 1/1/6 8/1/6 5/1/6
f 2/1/1 3/1/1 4/1/1
f 8/1/2 7/1/2 6/1/2
f 5/1/3 6/1/3 2/1/3
f 6/1/4 7/1/4 3/1/4
f 3/1/5 7/1/5 8/1/5
f 1/1/6 4/1/6 8/1/6
vt 0 0
vt 1 0
vt 0 1
vt 1 1
f 2/1/1 4/2/1 1/3/1
f 8/1/2 6/2/2 5/3/2
f 5/1/3 2/2/3 1/3/3
f 6/1/4 3/2/4 2/3/4
f 3/1/5 8/2/5 4/3/5
f 1/1/6 8/2/6 5/3/6
f 2/1/1 3/2/1 4/3/1
f 8/1/2 7/2/2 6/3/2
f 5/1/3 6/2/3 2/3/3
f 6/1/4 7/2/4 3/3/4
f 3/1/5 7/2/5 8/3/5
f 1/1/6 4/2/6 8/3/6
Loading

0 comments on commit e32d625

Please sign in to comment.