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

@ -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);
}
}