Textures Work!

MyBuiltinTexturedScene (number 7) displays a cube that loads its
faces from a hard-coded texture. Mipmaping is supported. Just use
the XXX_MIPMAP_XXX Min/Mag filters to use it. TODO next is
combining lighting and textures and then up after that is shadows.
I may just do lighting and shadows before lighting, shadows, and
textures.
This commit is contained in:
elipzer 2018-09-16 20:50:51 -04:00
parent c0d49da992
commit 0e3982591e
6 changed files with 194 additions and 4 deletions

View File

@ -172,6 +172,185 @@ namespace charcoal
}
}
template <typename VertexType, typename IndexType>
Mesh<VertexType, IndexType>* gen_cube_pt(const DrawMode& draw_mode, float width, float height, float depth)
{
Mesh<VertexType, IndexType>* mesh;
float x = width / 2.0f;
float y = height / 2.0f;
float z = depth / 2.0f;
Position pos0(-x, y, -z); // Front Left Top
Position pos1(-x, -y, -z); // Front Left Bottom
Position pos2(x, y, -z); // Front Right Top
Position pos3(x, -y, -z); // Front Right Bottom
Position pos4(-x, y, z); // Back Left Top
Position pos5(-x, -y, z); // Back Left Bottom
Position pos6(x, y, z); // Back Right Top
Position pos7(x, -y, z); // Back Right Bottom
Normal front(0.0f, 0.0f, -1.0f);
Normal right(1.0f, 0.0f, 0.0f);
Normal back(0.0f, 0.0f, 1.0f);
Normal left(-1.0f, 0.0f, 0.0f);
Normal top(0.0f, 1.0f, 0.0f);
Normal bottom(0.0f, -1.0f, 0.0f);
UV front_tr(0.0000000f, 1.0f);
UV front_br(0.0000000f, 0.0f);
UV front_tl(0.1666666f, 1.0f);
UV front_bl(0.1666666f, 0.0f);
UV right_tr(0.1666666f, 1.0f);
UV right_br(0.1666666f, 0.0f);
UV right_tl(0.3333333f, 1.0f);
UV right_bl(0.3333333f, 0.0f);
UV back_tr(0.3333333f, 1.0f);
UV back_br(0.3333333f, 0.0f);
UV back_tl(0.5000000f, 1.0f);
UV back_bl(0.5000000f, 0.0f);
UV left_tr(0.5000000f, 1.0f);
UV left_br(0.5000000f, 0.0f);
UV left_tl(0.6666666f, 1.0f);
UV left_bl(0.6666666f, 0.0f);
UV top_tr(0.6666666f, 1.0f);
UV top_br(0.6666666f, 0.0f);
UV top_tl(0.8333333f, 1.0f);
UV top_bl(0.8333333f, 0.0f);
UV bottom_tr(0.8333333f, 1.0f);
UV bottom_br(0.8333333f, 0.0f);
UV bottom_tl(1.0000000f, 1.0f);
UV bottom_bl(1.0000000f, 0.0f);
switch (draw_mode)
{
case DrawMode::DRAW_TRIANGLES:
mesh = MeshFactory<VertexType, IndexType>::create_mesh(24, 36);
mesh->vertices[0].set_uv(front_tr);
mesh->vertices[1].set_uv(front_br);
mesh->vertices[2].set_uv(front_tl);
mesh->vertices[3].set_uv(front_bl);
mesh->vertices[4].set_uv(right_tr);
mesh->vertices[5].set_uv(right_br);
mesh->vertices[6].set_uv(right_tl);
mesh->vertices[7].set_uv(right_bl);
mesh->vertices[8].set_uv(back_tr);
mesh->vertices[9].set_uv(back_br);
mesh->vertices[10].set_uv(back_tl);
mesh->vertices[11].set_uv(back_bl);
mesh->vertices[12].set_uv(left_tr);
mesh->vertices[13].set_uv(left_br);
mesh->vertices[14].set_uv(left_tl);
mesh->vertices[15].set_uv(left_bl);
mesh->vertices[16].set_uv(top_tr);
mesh->vertices[17].set_uv(top_br);
mesh->vertices[18].set_uv(top_tl);
mesh->vertices[19].set_uv(top_bl);
mesh->vertices[20].set_uv(bottom_tr);
mesh->vertices[21].set_uv(bottom_br);
mesh->vertices[22].set_uv(bottom_tl);
mesh->vertices[23].set_uv(bottom_bl);
mesh->vertices[0].set_position(pos0);
mesh->vertices[1].set_position(pos1);
mesh->vertices[2].set_position(pos2);
mesh->vertices[3].set_position(pos3);
mesh->vertices[4].set_position(pos2);
mesh->vertices[5].set_position(pos3);
mesh->vertices[6].set_position(pos6);
mesh->vertices[7].set_position(pos7);
mesh->vertices[8].set_position(pos6);
mesh->vertices[9].set_position(pos7);
mesh->vertices[10].set_position(pos4);
mesh->vertices[11].set_position(pos5);
mesh->vertices[12].set_position(pos4);
mesh->vertices[13].set_position(pos5);
mesh->vertices[14].set_position(pos0);
mesh->vertices[15].set_position(pos1);
mesh->vertices[16].set_position(pos4);
mesh->vertices[17].set_position(pos0);
mesh->vertices[18].set_position(pos6);
mesh->vertices[19].set_position(pos2);
mesh->vertices[20].set_position(pos1);
mesh->vertices[21].set_position(pos5);
mesh->vertices[22].set_position(pos3);
mesh->vertices[23].set_position(pos7);
// Counter Clockwise
// Front
mesh->indices[0] = 1;
mesh->indices[1] = 3;
mesh->indices[2] = 0;
mesh->indices[3] = 0;
mesh->indices[4] = 3;
mesh->indices[5] = 2;
// Right
mesh->indices[6] = 5;
mesh->indices[7] = 7;
mesh->indices[8] = 4;
mesh->indices[9] = 4;
mesh->indices[10] = 7;
mesh->indices[11] = 6;
// Back
mesh->indices[12] = 9;
mesh->indices[13] = 11;
mesh->indices[14] = 8;
mesh->indices[15] = 8;
mesh->indices[16] = 11;
mesh->indices[17] = 10;
// Left
mesh->indices[18] = 13;
mesh->indices[19] = 15;
mesh->indices[20] = 12;
mesh->indices[21] = 12;
mesh->indices[22] = 15;
mesh->indices[23] = 14;
// Top
mesh->indices[24] = 17;
mesh->indices[25] = 19;
mesh->indices[26] = 16;
mesh->indices[27] = 16;
mesh->indices[28] = 19;
mesh->indices[29] = 18;
// Bottom
mesh->indices[30] = 21;
mesh->indices[31] = 23;
mesh->indices[32] = 20;
mesh->indices[33] = 20;
mesh->indices[34] = 23;
mesh->indices[35] = 22;
return mesh;
case DrawMode::DRAW_POINTS:
case DrawMode::DRAW_LINES:
case DrawMode::DRAW_LINE_STRIP:
case DrawMode::DRAW_LINE_LOOP:
case DrawMode::DRAW_TRIANGLE_STRIP:
case DrawMode::DRAW_TRIANGLE_FAN:
case DrawMode::DRAW_LINE_STRIP_ADJACENCY:
case DrawMode::DRAW_LINES_ADJACENCY:
case DrawMode::DRAW_TRIANGLE_STRIP_ADJACENCY:
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
case DrawMode::DRAW_PATCHES:
default:
throw EXCEPTION("Unable to gen_cube for current draw mode: " + std::to_string(draw_mode));
}
}
// Creates a cube centered at the origin with specified width, height, and depth
template <typename VertexType, typename IndexType>
Mesh<VertexType, IndexType>* gen_cube_pn(const DrawMode& draw_mode, float width, float height, float depth)

View File

@ -8,7 +8,7 @@
MyBuiltinTexturedScene::MyBuiltinTexturedScene(Application& application)
: TexturedScene(application),
m_shape(
meshgenerator::gen_cube_p<TexturedVertex, TexturedIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f),
meshgenerator::gen_cube_pt<TexturedVertex, TexturedIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f),
texturegenerator::gen_quick_cube_texture(),
texturegenerator::gen_quick_sampler(),
DrawMode::DRAW_TRIANGLES
@ -38,7 +38,7 @@ void MyBuiltinTexturedScene::update(float delta_time, clock_t clock)
{
Poseable& pose = m_batch.get_pose(0);
pose.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_2 * delta_time);
pose.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_4 * delta_time);
pose.update_position(vec3(3 * (float)cos(radians), 0.0f, 0.0f));
}

View File

@ -11,6 +11,7 @@ namespace charcoal
{
private:
TextureFactory() {}
virtual ~TextureFactory() {}
public:
template <typename T>
@ -44,4 +45,7 @@ namespace charcoal
static std::vector<Texture> m_textures;
static std::vector<Sampler> m_samplers;
};
std::vector<Texture> TextureFactory::m_textures;
std::vector<Sampler> TextureFactory::m_samplers;
}

View File

@ -57,7 +57,7 @@ namespace charcoal
Sampler* gen_quick_sampler()
{
return TextureFactory::gen_sampler(Sampler::Wrap::REPEAT, Sampler::Wrap::REPEAT, Sampler::MagFilter::LINEAR, Sampler::MinFilter::LINEAR_MIPMAP_LINEAR);
return TextureFactory::gen_sampler(Sampler::Wrap::REPEAT, Sampler::Wrap::REPEAT, Sampler::MagFilter::NEAREST, Sampler::MinFilter::NEAREST);
}
}
}

View File

@ -1,9 +1,13 @@
#version 430
in vec2 fragment_uv;
out vec4 frag_color;
layout(location = 4) uniform sampler2D tex;
void main()
{
frag_color = vec4(1.0, 1.0, 1.0, 1.0);
vec3 base = vec3(0.1, 0.1, 0.1);
vec4 sampled = texture(tex, fragment_uv);
frag_color = vec4(base + sampled.xyz, 1.0);
}

View File

@ -5,7 +5,10 @@ layout(location = 2) in mat4 model_to_world;
layout(location = 0) uniform mat4 world_to_projection;
out vec2 fragment_uv;
void main()
{
fragment_uv = vertex_uv;
gl_Position = world_to_projection * model_to_world * vec4(vertex_position, 1.0);
}