Testing textures and events

This commit is contained in:
2022-10-20 15:49:25 -07:00
parent 375b25ff59
commit 80d6cba854
27 changed files with 513 additions and 26 deletions

View File

@ -21,8 +21,11 @@ float_t BackBufferRenderTarget::getHeight() {
}
void BackBufferRenderTarget::setSize(float_t width, float_t height) {
if(this->width == width && this->height == height) return;
this->width = width;
this->height = height;
this->eventRenderTargetResized.invoke(*this, width, height);
}
void BackBufferRenderTarget::setClearColor(struct Color color) {

View File

@ -9,6 +9,7 @@ target_sources(${DAWN_TARGET_NAME}
RenderManager.cpp
BackBufferRenderTarget.cpp
StandardRenderPipeline.cpp
Texture.cpp
)
# Subdirs

View File

@ -0,0 +1,76 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Texture.hpp"
using namespace Dawn;
void Texture::bind(textureslot_t slot) {
if(this->id == -1) throw "Texture has not been initialized, cannot bind.";
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, this->id);
}
int32_t Texture::getWidth() {
return this->width;
}
int32_t Texture::getHeight() {
return this->height;
}
void Texture::setSize(int32_t width, int32_t height) {
if(this->id != -1) glDeleteTextures(1, &this->id);
this->width = width;
this->height = height;
glGenTextures(1, &this->id);
if(this->id <= 0) throw "Texture generation failed!";
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->id);
// Setup our preferred texture params, later this will be configurable.
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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Initialize the texture to blank
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA,
width, height,
0, GL_RGBA, GL_FLOAT, NULL
);
}
void Texture::fill(struct Color color) {
struct Color *pixels = (struct Color *)memoryAllocate(
sizeof(struct Color) * this->width * this->height
);
this->buffer(pixels);
memoryFree(pixels);
}
bool_t Texture::isReady() {
return this->id != -1;
}
void Texture::buffer(struct Color pixels[]) {
glBindTexture(GL_TEXTURE_2D, this->id);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA,
this->width, this->height,
0, GL_RGBA, GL_FLOAT, (void *)pixels
);
}
Texture::~Texture() {
if(this->id != -1) glDeleteTextures(1, &this->id);
}

View File

@ -0,0 +1,32 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/_Texture.hpp"
#include "util/memory.hpp"
namespace Dawn {
typedef GLuint textureslot_t;
class Texture : public ITexture {
private:
int32_t width = -1;
int32_t height = -1;
GLuint id = -1;
public:
void bind(textureslot_t slot);
int32_t getWidth() override;
int32_t getHeight() override;
void setSize(int32_t width, int32_t height) override;
void fill(struct Color) override;
bool_t isReady() override;
void buffer(struct Color pixels[]) override;
~Texture();
};
}

View File

@ -7,5 +7,4 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Mesh.cpp
TriangleMesh.cpp
)

View File

@ -80,10 +80,15 @@ namespace Dawn {
int32_t position,
std::array<glm::vec2, N> coordinates
) {
auto offsetCoordinates = (
(sizeof(glm::vec3) * this->verticeCount) +
(sizeof(glm::vec2) * position)
);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
sizeof(glm::vec2) * position,
offsetCoordinates,
sizeof(coordinates),
(void *)coordinates.data()
);

View File

@ -1,26 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/mesh/Mesh.hpp"
#include "display/mesh/TriangleMesh.hpp"
using namespace Dawn;
void TriangleMesh::createTriangleMesh(Mesh &mesh) {
mesh.createBuffers(3, 3);
mesh.bufferPositions(0, std::array<glm::vec3, 3>{{
glm::vec3(-1, 0, 0),
glm::vec3(0, 1, 0),
glm::vec3(1, 0, 0)
}});
mesh.bufferCoordinates(0, std::array<glm::vec2, 3>{{
glm::vec2(0, 0),
glm::vec2(0, 1),
glm::vec2(1, 0)
}});
mesh.bufferIndices(0, std::array<meshindice_t,3>{{
0, 1, 2
}});
}

View File

@ -61,6 +61,10 @@ void Shader::compileShader(
// Now parse out the variables.
}
void Shader::setTextureSlot(shaderparameter_t param, textureslot_t slot) {
glUniform1i(param, slot);
}
shaderparameter_t Shader::getParameterByName(std::string name) {
return glGetUniformLocation(this->shaderProgram, name.c_str());
}
@ -81,6 +85,13 @@ void Shader::setVector3(shaderparameter_t uniform, glm::vec3 vector) {
glUniform3f(uniform, vector.x, vector.y, vector.z);
}
void Shader::setTexture(
shaderparameter_t param,
std::shared_ptr<Texture> texture
) {
this->bindTexture(param, texture);
}
void Shader::bind() {
if(this->shaderProgram == -1) throw "Shader has not yet been compiled";
glUseProgram(this->shaderProgram);

View File

@ -32,6 +32,29 @@ namespace Dawn {
*/
void compileShader(std::string vertexShader, std::string fragmentShader);
/**
* Bind a specific texture slot (of an already bound texture) to a shader
* parameter.
*
* @param param Parameter to bind the texture slot for.
* @param slot Slot to bind.
*/
void setTextureSlot(shaderparameter_t param, textureslot_t slot);
/**
* Method designed to be overwritten by child shaders on how to handle a
* bind texture request. This is left to the discretion of the child
* shader for which slot(s) to use, how to handle nullptr textures, etc.
*
* @param param Parameter to bind the requested texture to.
* @param texture Texture trying to be bound, may be nullptr.
*/
virtual void bindTexture(
shaderparameter_t param,
std::shared_ptr<Texture> texture
) = 0;
public:
/**
* Locate a shader parameter by its name.
@ -52,6 +75,10 @@ namespace Dawn {
void setBoolean(shaderparameter_t parameter, bool_t value) override;
void setColor(shaderparameter_t parameter, struct Color color) override;
void setVector3(shaderparameter_t parameter, glm::vec3 vector) override;
void setTexture(
shaderparameter_t parameter,
std::shared_ptr<Texture> texture
) override;
/**
* Destroys and deletes the shader from the GPU.

View File

@ -24,7 +24,7 @@ namespace Dawn {
ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR;
ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN;
// ps[paramTexture] SHADER_PARAMETER_TYPE_TEXTURE;
ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE;
return ps;
}
@ -42,6 +42,19 @@ namespace Dawn {
this->setMatrix(this->paramModel, transform);
}
void bindTexture(
shaderparameter_t param,
std::shared_ptr<Texture> texture
) override {
if(texture == nullptr) {
this->setBoolean(this->paramHasTexture, false);
} else {
this->setBoolean(this->paramHasTexture, true);
this->setTextureSlot(param, 0x00);
texture->bind(0x00);
}
}
void compile() override {
this->compileShader(
// Vertex Shader