Added PS Vita support
This commit is contained in:
@ -1,27 +1,29 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Libraries
|
||||
find_package(OpenGL REQUIRED)
|
||||
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${OPENGL_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${OPENGL_LIBRARIES}
|
||||
)
|
||||
|
||||
# Includes
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(display)
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
if(NOT DEFINED DAWN_OPENGL_EXCLUDE_LIBRARIES)
|
||||
# Libraries
|
||||
find_package(OpenGL REQUIRED)
|
||||
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${OPENGL_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${OPENGL_LIBRARIES}
|
||||
)
|
||||
endif()
|
||||
|
||||
# Includes
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(scene)
|
@ -1,101 +1,109 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "ShaderProgram.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void ShaderProgram::compileShader(
|
||||
std::string vertexShader,
|
||||
std::string fragmentShader
|
||||
) {
|
||||
GLint isSuccess;
|
||||
int32_t maxLength;
|
||||
char error[1024];
|
||||
|
||||
// Load the vertex shader first
|
||||
this->shaderVertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
auto vertShaderC = vertexShader.c_str();
|
||||
glShaderSource(this->shaderVertex, 1, &vertShaderC, 0);
|
||||
glCompileShader(this->shaderVertex);
|
||||
|
||||
// Validate
|
||||
glGetShaderiv(this->shaderVertex, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(this->shaderVertex, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetShaderInfoLog(this->shaderVertex, maxLength, &maxLength, error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now load the Frag shader
|
||||
this->shaderFrag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
auto fragShaderC = fragmentShader.c_str();
|
||||
glShaderSource(this->shaderFrag, 1, &fragShaderC, 0);
|
||||
glCompileShader(this->shaderFrag);
|
||||
glGetShaderiv(this->shaderFrag, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(this->shaderFrag, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetShaderInfoLog(this->shaderFrag, maxLength, &maxLength, error);
|
||||
glDeleteShader(this->shaderVertex);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now create the shader program.
|
||||
this->shaderProgram = glCreateProgram();
|
||||
glAttachShader(this->shaderProgram, this->shaderVertex);
|
||||
glAttachShader(this->shaderProgram, this->shaderFrag);
|
||||
|
||||
//Bind, Verify & Use the shader program
|
||||
glLinkProgram(this->shaderProgram);
|
||||
glGetProgramiv(this->shaderProgram, GL_LINK_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetProgramiv(this->shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetProgramInfoLog(this->shaderProgram, maxLength, &maxLength, error);
|
||||
glDeleteShader(this->shaderVertex);
|
||||
glDeleteShader(this->shaderFrag);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now parse out the variables.
|
||||
}
|
||||
|
||||
void ShaderProgram::setTexture(shaderparameter_t param, textureslot_t slot) {
|
||||
glUniform1i(param, slot);
|
||||
}
|
||||
|
||||
shaderparameter_t ShaderProgram::getParameterByName(std::string name) {
|
||||
return glGetUniformLocation(this->shaderProgram, name.c_str());
|
||||
}
|
||||
|
||||
void ShaderProgram::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) {
|
||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix));
|
||||
}
|
||||
|
||||
void ShaderProgram::setBoolean(shaderparameter_t uni, bool value) {
|
||||
glUniform1i(uni, value);
|
||||
}
|
||||
|
||||
void ShaderProgram::setColor(shaderparameter_t uniform, struct Color color) {
|
||||
glUniform4f(uniform, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
|
||||
void ShaderProgram::setVector3(shaderparameter_t uniform, glm::vec3 vector) {
|
||||
glUniform3f(uniform, vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
void ShaderProgram::setFloat(shaderparameter_t param, float_t value) {
|
||||
glUniform1f(param, value);
|
||||
}
|
||||
|
||||
void ShaderProgram::bind() {
|
||||
if(this->shaderProgram == -1) throw "Shader has not yet been compiled";
|
||||
glUseProgram(this->shaderProgram);
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram() {
|
||||
if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram);
|
||||
if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex);
|
||||
if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag);
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "ShaderProgram.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void ShaderProgram::compileShader(
|
||||
std::string vertexShader,
|
||||
std::string fragmentShader
|
||||
) {
|
||||
GLint isSuccess;
|
||||
int32_t maxLength;
|
||||
char error[1024];
|
||||
|
||||
// Load the vertex shader first
|
||||
this->shaderVertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
auto vertShaderC = vertexShader.c_str();
|
||||
glShaderSource(this->shaderVertex, 1, &vertShaderC, 0);
|
||||
glCompileShader(this->shaderVertex);
|
||||
|
||||
// Validate
|
||||
glGetShaderiv(this->shaderVertex, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(this->shaderVertex, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetShaderInfoLog(this->shaderVertex, maxLength, &maxLength, error);
|
||||
debugMessage("Error compiling vert shader");
|
||||
debugMessage(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now load the Frag shader
|
||||
this->shaderFrag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
auto fragShaderC = fragmentShader.c_str();
|
||||
glShaderSource(this->shaderFrag, 1, &fragShaderC, 0);
|
||||
glCompileShader(this->shaderFrag);
|
||||
glGetShaderiv(this->shaderFrag, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(this->shaderFrag, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetShaderInfoLog(this->shaderFrag, maxLength, &maxLength, error);
|
||||
glDeleteShader(this->shaderVertex);
|
||||
debugMessage("Error compiling frag shader");
|
||||
debugMessage(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now create the shader program.
|
||||
this->shaderProgram = glCreateProgram();
|
||||
glAttachShader(this->shaderProgram, this->shaderVertex);
|
||||
glAttachShader(this->shaderProgram, this->shaderFrag);
|
||||
|
||||
//Bind, Verify & Use the shader program
|
||||
glLinkProgram(this->shaderProgram);
|
||||
glGetProgramiv(this->shaderProgram, GL_LINK_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetProgramiv(this->shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetProgramInfoLog(this->shaderProgram, maxLength, &maxLength, error);
|
||||
glDeleteShader(this->shaderVertex);
|
||||
glDeleteShader(this->shaderFrag);
|
||||
debugMessage("Error compiling shader program");
|
||||
debugMessage(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now parse out the variables.
|
||||
glBindAttribLocation(this->shaderProgram, 0, "aPos");
|
||||
glBindAttribLocation(this->shaderProgram, 1, "aTexCoord");
|
||||
}
|
||||
|
||||
void ShaderProgram::setTexture(shaderparameter_t param, textureslot_t slot) {
|
||||
glUniform1i(param, slot);
|
||||
}
|
||||
|
||||
shaderparameter_t ShaderProgram::getParameterByName(std::string name) {
|
||||
return glGetUniformLocation(this->shaderProgram, name.c_str());
|
||||
}
|
||||
|
||||
void ShaderProgram::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) {
|
||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix));
|
||||
}
|
||||
|
||||
void ShaderProgram::setBoolean(shaderparameter_t uni, bool value) {
|
||||
glUniform1i(uni, value);
|
||||
}
|
||||
|
||||
void ShaderProgram::setColor(shaderparameter_t uniform, struct Color color) {
|
||||
glUniform4f(uniform, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
|
||||
void ShaderProgram::setVector3(shaderparameter_t uniform, glm::vec3 vector) {
|
||||
glUniform3f(uniform, vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
void ShaderProgram::setFloat(shaderparameter_t param, float_t value) {
|
||||
glUniform1f(param, value);
|
||||
}
|
||||
|
||||
void ShaderProgram::bind() {
|
||||
if(this->shaderProgram == -1) throw "Shader has not yet been compiled";
|
||||
glUseProgram(this->shaderProgram);
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram() {
|
||||
if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram);
|
||||
if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex);
|
||||
if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag);
|
||||
}
|
@ -1,64 +1,65 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "display/shader/_ShaderProgram.hpp"
|
||||
#include "dawnopengl.hpp"
|
||||
#include "display/Color.hpp"
|
||||
|
||||
typedef GLuint shaderparameter_t;
|
||||
|
||||
namespace Dawn {
|
||||
class ShaderProgram : public IShaderProgram<shaderparameter_t> {
|
||||
private:
|
||||
/** Pointer to an uploaded vertex shader program */
|
||||
GLuint shaderVertex = -1;
|
||||
|
||||
/** Pointer to an uploaded fragment shader program */
|
||||
GLuint shaderFrag = -1;
|
||||
|
||||
/** Pointer to an uploaded shader program linked */
|
||||
GLuint shaderProgram = -1;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Compiles a GLSL shader and stores it on the GPU, updates the underlying
|
||||
* pointers for you.
|
||||
*
|
||||
* @param vertexShader The string source of the vertex shader.
|
||||
* @param fragmentShader The string source of the fragment shader.
|
||||
*/
|
||||
void compileShader(std::string vertexShader, std::string fragmentShader);
|
||||
|
||||
|
||||
public:
|
||||
/**
|
||||
* Locate a shader parameter by its name.
|
||||
*
|
||||
* @param name Name of the parameter to get.
|
||||
* @return The shader parameter.
|
||||
*/
|
||||
shaderparameter_t getParameterByName(std::string name);
|
||||
|
||||
/**
|
||||
* Method to request that this shader be compiled and put on the GPU. This
|
||||
* method should call the protected compileShader method.
|
||||
*/
|
||||
virtual void compile() = 0;
|
||||
|
||||
void bind() override;
|
||||
void setMatrix(shaderparameter_t parameter, glm::mat4 matrix) override;
|
||||
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, textureslot_t texture) override;
|
||||
void setFloat(shaderparameter_t parameter, float_t value) override;
|
||||
|
||||
/**
|
||||
* Destroys and deletes the shader from the GPU.
|
||||
*/
|
||||
virtual ~ShaderProgram();
|
||||
};
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "display/shader/_ShaderProgram.hpp"
|
||||
#include "dawnopengl.hpp"
|
||||
#include "display/Color.hpp"
|
||||
#include "debug/debug.hpp"
|
||||
|
||||
typedef GLuint shaderparameter_t;
|
||||
|
||||
namespace Dawn {
|
||||
class ShaderProgram : public IShaderProgram<shaderparameter_t> {
|
||||
private:
|
||||
/** Pointer to an uploaded vertex shader program */
|
||||
GLuint shaderVertex = -1;
|
||||
|
||||
/** Pointer to an uploaded fragment shader program */
|
||||
GLuint shaderFrag = -1;
|
||||
|
||||
/** Pointer to an uploaded shader program linked */
|
||||
GLuint shaderProgram = -1;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Compiles a GLSL shader and stores it on the GPU, updates the underlying
|
||||
* pointers for you.
|
||||
*
|
||||
* @param vertexShader The string source of the vertex shader.
|
||||
* @param fragmentShader The string source of the fragment shader.
|
||||
*/
|
||||
void compileShader(std::string vertexShader, std::string fragmentShader);
|
||||
|
||||
|
||||
public:
|
||||
/**
|
||||
* Locate a shader parameter by its name.
|
||||
*
|
||||
* @param name Name of the parameter to get.
|
||||
* @return The shader parameter.
|
||||
*/
|
||||
shaderparameter_t getParameterByName(std::string name);
|
||||
|
||||
/**
|
||||
* Method to request that this shader be compiled and put on the GPU. This
|
||||
* method should call the protected compileShader method.
|
||||
*/
|
||||
virtual void compile() = 0;
|
||||
|
||||
void bind() override;
|
||||
void setMatrix(shaderparameter_t parameter, glm::mat4 matrix) override;
|
||||
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, textureslot_t texture) override;
|
||||
void setFloat(shaderparameter_t parameter, float_t value) override;
|
||||
|
||||
/**
|
||||
* Destroys and deletes the shader from the GPU.
|
||||
*/
|
||||
virtual ~ShaderProgram();
|
||||
};
|
||||
}
|
@ -17,38 +17,75 @@ namespace Dawn {
|
||||
shaderparameter_t paramHasTexture;
|
||||
|
||||
void compile() override {
|
||||
this->compileShader(
|
||||
// Vertex Shader
|
||||
"#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;\n"
|
||||
"layout (location = 1) in vec2 aTexCoord;\n"
|
||||
#if DAWN_OPENGL_GLSL
|
||||
this->compileShader(
|
||||
// Vertex Shader
|
||||
"#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;\n"
|
||||
"layout (location = 1) in vec2 aTexCoord;\n"
|
||||
|
||||
"uniform mat4 u_Proj;\n"
|
||||
"uniform mat4 u_View;\n"
|
||||
"uniform mat4 u_Model;\n"
|
||||
"uniform mat4 u_Proj;\n"
|
||||
"uniform mat4 u_View;\n"
|
||||
"uniform mat4 u_Model;\n"
|
||||
|
||||
"out vec2 o_TextCoord;\n"
|
||||
"void main() {\n"
|
||||
"gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
|
||||
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
|
||||
"}",
|
||||
"out vec2 o_TextCoord;\n"
|
||||
"void main() {\n"
|
||||
"gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
|
||||
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
|
||||
"}",
|
||||
|
||||
// Fragment Shader
|
||||
"#version 330 core\n"
|
||||
"in vec2 o_TextCoord;\n"
|
||||
"out vec4 o_Color;\n"
|
||||
"uniform vec4 u_Color;\n"
|
||||
"uniform bool u_HasTexture;\n"
|
||||
"uniform sampler2D u_Text;\n"
|
||||
// Fragment Shader
|
||||
"#version 330 core\n"
|
||||
"in vec2 o_TextCoord;\n"
|
||||
"out vec4 o_Color;\n"
|
||||
"uniform vec4 u_Color;\n"
|
||||
"uniform bool u_HasTexture;\n"
|
||||
"uniform sampler2D u_Text;\n"
|
||||
|
||||
"void main() {\n"
|
||||
"if(u_HasTexture) {\n"
|
||||
"o_Color = texture(u_Text, o_TextCoord) * u_Color;\n"
|
||||
"} else {\n"
|
||||
"o_Color = u_Color;"
|
||||
"void main() {\n"
|
||||
"if(u_HasTexture) {\n"
|
||||
"o_Color = texture(u_Text, o_TextCoord) * u_Color;\n"
|
||||
"} else {\n"
|
||||
"o_Color = u_Color;"
|
||||
"}\n"
|
||||
"}\n"
|
||||
"}\n"
|
||||
);
|
||||
);
|
||||
#elif DAWN_OPENGL_HLSL
|
||||
this->compileShader(
|
||||
// Vertex Shader
|
||||
"uniform float4x4 u_Proj;\n"
|
||||
"uniform float4x4 u_View;\n"
|
||||
"uniform float4x4 u_Model;\n"
|
||||
"void main("
|
||||
"float3 aPos,\n"
|
||||
// "float2 aTexCoord,\n"
|
||||
"float3 out o_TextCoord : TEXCOORD0,\n"
|
||||
"float4 out gl_Position : POSITION\n"
|
||||
") {\n"
|
||||
// "o_TextCoord = float3(aTexCoord.x, 0.0, 1.0);\n"
|
||||
"gl_Position = mul(mul(mul(float4(aPos, 1.0), u_Model), u_View), u_Proj);\n"
|
||||
"}",
|
||||
|
||||
// Fragment Shader
|
||||
"uniform float4 u_Color;\n"
|
||||
"uniform bool u_HasTexture;\n"
|
||||
"uniform sampler2D u_Text;\n"
|
||||
|
||||
"float4 main(\n"
|
||||
"float3 o_TextCoord : TEXCOORD0\n"
|
||||
") {\n"
|
||||
"float4 o_Color;\n"
|
||||
"if(u_HasTexture) {\n"
|
||||
// "o_Color = mul(tex2D(u_Text, o_TextCoord), u_Color);\n"
|
||||
"} else {\n"
|
||||
"o_Color = u_Color;\n"
|
||||
"}\n"
|
||||
"return o_Color;\n"
|
||||
"}\n"
|
||||
);
|
||||
#else
|
||||
#error Shader Type must be either GLSL or HLSL
|
||||
#endif
|
||||
|
||||
this->paramProjection = this->getParameterByName("u_Proj");
|
||||
this->paramView = this->getParameterByName("u_View");
|
||||
|
Reference in New Issue
Block a user