Getting Closer to Textures

This commit is contained in:
elipzer 2018-09-15 20:43:29 -04:00
parent ae7fcc9011
commit c0d49da992
16 changed files with 209 additions and 21 deletions

View File

@ -76,8 +76,11 @@ namespace charcoal
update_element_buffers();
}
virtual void preprender() const {}
void render() const
{
preprender();
glBindVertexArray(m_vao);
glDrawElementsInstanced(
m_p_renderable->get_draw_mode(),
@ -111,7 +114,6 @@ namespace charcoal
GLuint m_vertex_vbo;
std::vector<GLuint> m_element_buffers;
private:
GLuint m_vao;
GLuint m_index_vbo;
GLenum m_gl_index_type;

View File

@ -4,6 +4,7 @@
#include "Renderable.h"
#include "PoseableBatch.h"
#include "VertexFragmentShaderProgram.h"
#include "TextureRenderable.h"
namespace charcoal
{
@ -111,6 +112,6 @@ namespace charcoal
typedef PTVertex TexturedVertex;
typedef Index TexturedIndex;
typedef Renderable<TexturedVertex, TexturedIndex> TexturedRenderable;
typedef TextureRenderable<TexturedVertex, TexturedIndex> TexturedRenderable;
}
}

View File

@ -1,12 +1,18 @@
#include "MyBuiltinTexturedScene.h"
#include "MeshGenerator.h"
#include "TextureGenerator.h"
#include "constants.h"
MyBuiltinTexturedScene::MyBuiltinTexturedScene(Application& application)
: TexturedScene(application),
m_shape(meshgenerator::gen_cube_p<TexturedVertex, TexturedIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), DrawMode::DRAW_TRIANGLES),
m_shape(
meshgenerator::gen_cube_p<TexturedVertex, TexturedIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f),
texturegenerator::gen_quick_cube_texture(),
texturegenerator::gen_quick_sampler(),
DrawMode::DRAW_TRIANGLES
),
m_camera((float)TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f)),
m_batch(add_batch(&m_shape, 1))
{

View File

@ -176,6 +176,7 @@
<ClCompile Include="Poseable.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="ShaderProgram.cpp" />
<ClCompile Include="Sampler.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
@ -211,7 +212,12 @@
<ClInclude Include="MyBuiltinTexturedScene.h" />
<ClInclude Include="PoseableBatch.h" />
<ClInclude Include="Prerenderable.h" />
<ClInclude Include="Sampler.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="TexturedBatch.h" />
<ClInclude Include="TextureFactory.h" />
<ClInclude Include="TextureGenerator.h" />
<ClInclude Include="TextureRenderable.h" />
<ClInclude Include="TexturedScene.h" />
<ClInclude Include="TexturedShaderProgram.h" />
<ClInclude Include="VertexFragmentShaderProgram.h" />

View File

@ -171,6 +171,9 @@
<ClCompile Include="MyBuiltinTexturedScene.cpp">
<Filter>Source Files\Example\Application</Filter>
</ClCompile>
<ClCompile Include="Sampler.cpp">
<Filter>Source Files\Engine</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Exception.h">
@ -203,9 +206,6 @@
<ClInclude Include="ShaderProgram.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="Shader.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="Camera.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
@ -326,6 +326,24 @@
<ClInclude Include="MyBuiltinTexturedScene.h">
<Filter>Header Files\Example\Application</Filter>
</ClInclude>
<ClInclude Include="Shader.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="Texture.h">
<Filter>Header Files\Engine</Filter>
</ClInclude>
<ClInclude Include="Sampler.h">
<Filter>Header Files\Engine</Filter>
</ClInclude>
<ClInclude Include="TextureRenderable.h">
<Filter>Header Files\Engine</Filter>
</ClInclude>
<ClInclude Include="TextureFactory.h">
<Filter>Header Files\Engine</Filter>
</ClInclude>
<ClInclude Include="TextureGenerator.h">
<Filter>Header Files\Engine\builtin</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="MySimpleVS.glsl">

View File

@ -11,7 +11,7 @@
namespace charcoal
{
template <typename VertexType, typename IndexType>
class Renderable final
class Renderable
{
public:
typedef VertexType VertexType;

View File

@ -6,7 +6,10 @@ namespace charcoal
{
glGenSamplers(1, &m_sampler);
glSamplerParameteri(m_sampler, GL_WRAP_S, wrap_s);
glSamplerParameteri(m_sampler, GL_TEXTURE_WRAP_S, (GLenum)wrap_s);
glSamplerParameteri(m_sampler, GL_TEXTURE_WRAP_T, (GLenum)wrap_t);
glSamplerParameteri(m_sampler, GL_TEXTURE_MAG_FILTER, (GLenum)magnification_filter);
glSamplerParameteri(m_sampler, GL_TEXTURE_MIN_FILTER, (GLenum)minification_filter);
}
Sampler::~Sampler()

View File

@ -7,7 +7,7 @@ namespace charcoal
class Sampler
{
public:
enum MinFilter : GLenum
enum class MinFilter : GLenum
{
NEAREST = GL_NEAREST,
LINEAR = GL_LINEAR,
@ -17,13 +17,13 @@ namespace charcoal
LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR,
};
enum MagFilter : GLenum
enum class MagFilter : GLenum
{
NEAREST = GL_NEAREST,
LINEAR = GL_LINEAR,
};
enum Wrap : GLenum
enum class Wrap : GLenum
{
CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
MIRRORED_REPEAT = GL_MIRRORED_REPEAT,
@ -42,6 +42,8 @@ namespace charcoal
void bind(GLuint texture_unit);
GLuint get_sampler() const { return m_sampler; }
private:
GLuint m_sampler = 0;
};

View File

@ -11,7 +11,7 @@ namespace charcoal
class Texture
{
public:
enum Target : GLenum
enum class Target : GLenum
{
TEXTURE_2D = GL_TEXTURE_2D,
PROXY_TEXTURE_2D = GL_PROXY_TEXTURE_2D,
@ -28,7 +28,7 @@ namespace charcoal
PROXY_TEXTURE_CUBE_MAP = GL_PROXY_TEXTURE_CUBE_MAP,
};
enum InternalFormat : GLint
enum class InternalFormat : GLint
{
// Base Internal Formats
DEPTH_COMPONENT = GL_DEPTH_COMPONENT,
@ -116,7 +116,7 @@ namespace charcoal
COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT = GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT,
};
enum Format : GLenum
enum class Format : GLenum
{
RED = GL_RED,
RG = GL_RG,
@ -135,7 +135,7 @@ namespace charcoal
DEPTH_STENCIL = GL_DEPTH_STENCIL,
};
enum Type : GLenum
enum class Type : GLenum
{
UNSIGNED_BYTE = GL_UNSIGNED_BYTE,
BYTE = GL_BYTE,
@ -167,22 +167,22 @@ namespace charcoal
const std::vector<T>& data,
InternalFormat internal_format,
unsigned short mipmap_level = 0,
Target texture_target = TEXTURE_2D
Target texture_target = Target::TEXTURE_2D
)
: width(width), height(height)
{
if (texture_target != TEXTURE_2D)
if (texture_target != Target::TEXTURE_2D)
throw EXCEPTION("Non 2D textures are not supported yet.");
glGenTextures(1, &m_texture);
glBindTexture(texture_target, m_texture);
glBindTexture((GLenum)texture_target, m_texture);
glTexImage2D(texture_target, mipmap_level, internal_format, width, height, 0, data_format, data_type, data);
glTexImage2D((GLenum)texture_target, mipmap_level, (GLint)internal_format, width, height, 0, (GLenum)data_format, (GLenum)data_type, &data[0]);
glGenerateMipmap(texture_target);
glGenerateMipmap((GLenum)texture_target);
glBindTexture(texture_target, NULL);
glBindTexture((GLenum)texture_target, NULL);
}
~Texture()
@ -190,6 +190,7 @@ namespace charcoal
glDeleteTextures(1, &m_texture);
}
GLuint get_texture() const { return m_texture; }
private:
GLuint m_texture = 0;

View File

@ -0,0 +1,47 @@
#pragma once
#include <vector>
#include "Texture.h"
#include "Sampler.h"
namespace charcoal
{
class TextureFactory
{
private:
TextureFactory() {}
public:
template <typename T>
static Texture* gen_texture(
Texture::Format data_format,
Texture::Type data_type,
unsigned long width,
unsigned long height,
const std::vector<T>& data,
Texture::InternalFormat internal_format,
unsigned short mipmap_level = 0,
Texture::Target texture_target = Texture::Target::TEXTURE_2D
)
{
m_textures.emplace_back(data_format, data_type, width, height, data, internal_format, mipmap_level, texture_target);
return &m_textures.back();
}
static Sampler* gen_sampler(
Sampler::Wrap wrap_s,
Sampler::Wrap wrap_t,
Sampler::MagFilter magnification_filter,
Sampler::MinFilter minification_filter
)
{
m_samplers.emplace_back(wrap_s, wrap_t, magnification_filter, minification_filter);
return &m_samplers.back();
}
private:
static std::vector<Texture> m_textures;
static std::vector<Sampler> m_samplers;
};
}

View File

@ -0,0 +1,64 @@
#pragma once
#include <vector>
#include "Texture.h"
#include "Sampler.h"
#include "TextureFactory.h"
namespace charcoal
{
namespace builtin
{
namespace texturegenerator
{
Texture* gen_quick_cube_texture()
{
std::vector<unsigned char> values;
// Front
values.emplace_back(255);
values.emplace_back(255);
values.emplace_back(255);
values.emplace_back(255);
// Right
values.emplace_back(255);
values.emplace_back(255);
values.emplace_back(0);
values.emplace_back(255);
// Back
values.emplace_back(255);
values.emplace_back(0);
values.emplace_back(255);
values.emplace_back(255);
// Left
values.emplace_back(0);
values.emplace_back(255);
values.emplace_back(255);
values.emplace_back(255);
// Top
values.emplace_back(255);
values.emplace_back(0);
values.emplace_back(0);
values.emplace_back(255);
// Bottom
values.emplace_back(0);
values.emplace_back(255);
values.emplace_back(0);
values.emplace_back(255);
return TextureFactory::gen_texture(Texture::Format::RGBA, Texture::Type::UNSIGNED_BYTE, 6, 1, values, Texture::InternalFormat::RGBA);
}
Sampler* gen_quick_sampler()
{
return TextureFactory::gen_sampler(Sampler::Wrap::REPEAT, Sampler::Wrap::REPEAT, Sampler::MagFilter::LINEAR, Sampler::MinFilter::LINEAR_MIPMAP_LINEAR);
}
}
}
}

View File

@ -0,0 +1,25 @@
#pragma once
#include "Renderable.h"
#include "Texture.h"
#include "Sampler.h"
namespace charcoal
{
template <typename VertexType, typename IndexType>
class TextureRenderable : public Renderable<VertexType, IndexType>
{
public:
TextureRenderable(const Renderable<VertexType, IndexType>::MeshType* mesh, Texture* texture, Sampler* sampler, const DrawMode& draw_mode)
: Renderable<VertexType, IndexType>(mesh, draw_mode), m_p_texture(texture), m_p_sampler(sampler)
{}
const Texture* get_texture() const { return m_p_texture; }
const Sampler* get_sampler() const { return m_p_sampler; }
private:
Texture* m_p_texture;
Sampler* m_p_sampler;
};
}

View File

@ -4,6 +4,13 @@ namespace charcoal
{
namespace builtin
{
void TexturedBatch::preprender() const
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_p_renderable->get_texture()->get_texture());
glBindSampler(0, m_p_renderable->get_sampler()->get_sampler());
}
void TexturedBatch::setup_vao()
{
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);

View File

@ -23,6 +23,8 @@ namespace charcoal
) : PoseableBatch<TexturedVertex, TexturedIndex, TexturedRenderable>(renderable, element_count, element_render_count)
{}
void preprender() const override;
protected:
void setup_vao() override;
};

View File

@ -1,5 +1,8 @@
#version 430
out vec4 frag_color;
layout(location = 4) uniform sampler2D tex;
void main()
{
frag_color = vec4(1.0, 1.0, 1.0, 1.0);

View File

@ -36,6 +36,7 @@ namespace charcoal
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
glutil::uniform_int(4, 0); // The textured batch uses GL_TEXTURE0
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
iter->render();