Added Texture class

This commit is contained in:
Robert 2021-01-21 12:07:43 +01:00
parent 1b2ed0cdf5
commit e2e15dd98f
13 changed files with 8005 additions and 35 deletions

View file

@ -14,4 +14,5 @@ target_link_libraries(debug PRIVATE
add_custom_command(TARGET debug POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:openglu> $<TARGET_FILE:glfw> $<TARGET_FILE_DIR:debug>
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/shaders $<TARGET_FILE_DIR:debug>/shaders
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/assets $<TARGET_FILE_DIR:debug>/assets
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

View file

@ -43,10 +43,10 @@ int main(int argc, char** argv)
// Create vertices for square
float vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f // top left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
@ -55,12 +55,13 @@ int main(int argc, char** argv)
};
oglu::VertexAttribute topology[] = {
{ 0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0 },
{ 1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)) }
{ 0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0 },
{ 1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)) },
{ 2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)) }
};
// Make a square
oglu::Object square = oglu::MakeObject(vertices, sizeof(vertices), indices, sizeof(indices), topology, sizeof(topology));
oglu::VertexArray square = oglu::MakeObject(vertices, sizeof(vertices), indices, sizeof(indices), topology, sizeof(topology));
// Create a shader
oglu::Shader shader;
@ -74,6 +75,9 @@ int main(int argc, char** argv)
return -1;
}
// Create a texture
oglu::Texture texture = oglu::MakeTexture("assets/crate.jpg");
// Window loop
while (!glfwWindowShouldClose(window))
{
@ -81,8 +85,8 @@ int main(int argc, char** argv)
oglu::ClearScreen(GL_COLOR_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f));
shader->SetUniform("ourColor", &oglu::Color::Transparent);
shader->Use();
texture->Bind();
square->BindAndDraw();
glfwSwapBuffers(window);

View file

@ -1,11 +1,12 @@
#version 330 core
in vec3 oCol;
in vec2 oUV;
out vec4 FragColor;
uniform vec4 ourColor;
uniform sampler2D ourTexture;
void main()
{
FragColor = vec4(oCol, 1.0f) + ourColor;
FragColor = texture(ourTexture, oUV) * vec4(oCol, 1.0);
}

View file

@ -1,11 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aCol;
layout (location = 2) in vec2 aUV;
out vec3 oCol;
out vec2 oUV;
void main()
{
oCol = aCol;
oUV = aUV;
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

View file

@ -2,6 +2,7 @@
#define CORE_HPP
#include <memory>
#include <glad/glad.h>
#ifdef OGLU_WIN32
#ifdef OGLU_BUILD_DLL

View file

@ -2,7 +2,6 @@
#define DRAWABLE_HPP
#include <core.hpp>
#include <glad/glad.h>
namespace oglu
{
@ -15,13 +14,13 @@ namespace oglu
const GLvoid* pointer;
} VertexAttribute;
class OGLU_API AbstractObject
class OGLU_API AbstractVertexArray
{
public:
AbstractObject(const AbstractObject& other);
~AbstractObject();
AbstractVertexArray(const AbstractVertexArray& other);
~AbstractVertexArray();
friend std::shared_ptr<AbstractObject> OGLU_API MakeObject(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
friend std::shared_ptr<AbstractVertexArray> OGLU_API MakeObject(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
GLuint GetVAO() { return VAO; }
void Bind();
@ -31,7 +30,7 @@ namespace oglu
void BindAndDraw();
private:
AbstractObject(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
AbstractVertexArray(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
inline void RegisterVertexAttribPointer(GLuint index, const VertexAttribute& topology);
@ -39,7 +38,7 @@ namespace oglu
GLsizei count;
};
typedef std::shared_ptr<AbstractObject> Object;
typedef std::shared_ptr<AbstractVertexArray> VertexArray;
}
#endif

View file

@ -1,12 +1,10 @@
#ifndef OPENGLU_HPP
#define OPENGLU_HPP
#include "core.hpp"
#include <glad/glad.h>
#include "color.hpp"
#include "object.hpp"
#include "shader.hpp"
#include <color.hpp>
#include <object.hpp>
#include <shader.hpp>
#include <texture.hpp>
namespace oglu
{

View file

@ -2,7 +2,6 @@
#define SHADER_HPP
#include <core.hpp>
#include <glad/glad.h>
namespace oglu
{

32
include/texture.hpp Normal file
View file

@ -0,0 +1,32 @@
#ifndef TEXTURE_HPP
#define TEXTURE_HPP
#include <core.hpp>
namespace oglu
{
void ActiveTexture(GLubyte index);
class OGLU_API AbstractTexture
{
public:
AbstractTexture(const AbstractTexture& other);
~AbstractTexture();
friend std::shared_ptr<AbstractTexture> OGLU_API MakeTexture(const char* filename);
void Bind();
void Unbind();
private:
AbstractTexture(const char* filename);
private:
int width, height, nrChannels;
GLuint texture;
};
typedef std::shared_ptr<AbstractTexture> Texture;
}
#endif

View file

@ -2,13 +2,13 @@
namespace oglu
{
AbstractObject::AbstractObject(const AbstractObject& other) :
AbstractVertexArray::AbstractVertexArray(const AbstractVertexArray& other) :
VAO(other.VAO), VBO(other.VBO), EBO(other.EBO), count(other.count)
{
}
AbstractObject::~AbstractObject()
AbstractVertexArray::~AbstractVertexArray()
{
glBindVertexArray(0);
glDeleteVertexArrays(1, &VAO);
@ -16,13 +16,13 @@ namespace oglu
glDeleteBuffers(1, &EBO);
}
Object MakeObject(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize)
VertexArray MakeObject(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize)
{
AbstractObject* obj = new AbstractObject(vertices, verticesSize, indices, indicesSize, topology, topologySize);
return Object(obj);
AbstractVertexArray* obj = new AbstractVertexArray(vertices, verticesSize, indices, indicesSize, topology, topologySize);
return VertexArray(obj);
}
AbstractObject::AbstractObject(const GLfloat* vertices, size_t verticesSize,
AbstractVertexArray::AbstractVertexArray(const GLfloat* vertices, size_t verticesSize,
const GLuint* indices, size_t indicesSize,
const VertexAttribute* topology, size_t topologySize) :
VAO(0), VBO(0), EBO(0), count(0)
@ -51,29 +51,29 @@ namespace oglu
count = indicesSize / sizeof(GLuint);
}
void AbstractObject::Bind()
void AbstractVertexArray::Bind()
{
glBindVertexArray(VAO);
}
void AbstractObject::Unbind()
void AbstractVertexArray::Unbind()
{
glBindVertexArray(0);
}
void AbstractObject::Draw()
void AbstractVertexArray::Draw()
{
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (GLvoid*)0);
}
void AbstractObject::BindAndDraw()
void AbstractVertexArray::BindAndDraw()
{
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (GLvoid*)0);
glBindVertexArray(0);
}
void AbstractObject::RegisterVertexAttribPointer(GLuint index, const VertexAttribute& topology)
void AbstractVertexArray::RegisterVertexAttribPointer(GLuint index, const VertexAttribute& topology)
{
glVertexAttribPointer(topology.index, topology.size, topology.type, topology.normalized, topology.stride, topology.pointer);
glEnableVertexAttribArray(index);

61
src/texture.cpp Normal file
View file

@ -0,0 +1,61 @@
#include "texture.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
namespace oglu
{
void ActiveTexture(GLubyte index)
{
glActiveTexture(GL_TEXTURE0 + index);
}
AbstractTexture::AbstractTexture(const AbstractTexture& other) :
width(other.width), height(other.height), nrChannels(other.nrChannels), texture(other.texture)
{
}
AbstractTexture::~AbstractTexture()
{
glDeleteBuffers(1, &texture);
}
AbstractTexture::AbstractTexture(const char* filename)
{
stbi_set_flip_vertically_on_load(true);
stbi_uc* data = stbi_load(filename, &width, &height, &nrChannels, 0);
if (data == nullptr)
{
throw stbi_failure_reason();
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
Texture MakeTexture(const char* filename)
{
return std::shared_ptr<AbstractTexture>(new AbstractTexture(filename));
}
void AbstractTexture::Bind()
{
glBindTexture(GL_TEXTURE_2D, texture);
}
void AbstractTexture::Unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);
}
}

7871
vendor/include/stb/stb_image.h vendored Normal file

File diff suppressed because it is too large Load diff