Skip to content

Commit

Permalink
Methods for shaders added
Browse files Browse the repository at this point in the history
  • Loading branch information
Prtm2110 committed Jan 10, 2025
1 parent dd473c5 commit a8f2ef8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 12 deletions.
43 changes: 35 additions & 8 deletions Spraky-core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,46 @@ int main() {

// Create a window
Window window("Sparky!!", 1280, 800);
glClearColor(0.2f, 0.3f, 0.8f, 0.9f);
glClearColor(0.2f, 1.3f, 0.8f, 0.9f);

// Initialize OpenGL VAO
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

Shader shader("x64/src/shaders/basic.vert", "x64/src/shaders/basic.frag");
//std::cout << position << std::endl;

GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f
};

GLuint indices[] = {
0, 1, 2,
2, 3, 0
};

GLuint vbo, ibo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

mat4 ortho = mat4::orthographic(0.0f, 16.0f, 0.0f, 9.0f, -1.0f, 1.0f);

Shader shader("x64/src/shaders/basic.vert", "x64/src/shaders/basic.frag");
shader.enable();
shader.setUniformMat4("pr_matrix", ortho);
shader.setUniformMat4("ml_matrix", mat4::tranlation(vec3(8, 4.5, 0)));
shader.setUniform2f("light_pos", vec2(8.0f, 4.5f)); // Center of the square
shader.setUniform4f("colour", vec4(0.0f, 0.0f, 0.0f, 1.0f )); // Black color for the glow

while (!window.closed()) {
window.clear();


glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
window.update();
}
return 0;
Expand Down
25 changes: 25 additions & 0 deletions Spraky-core/x64/src/graphics/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ namespace sparky {
return program;
}

GLint Shader::getUniformLocation(const GLchar* name)
{
return glGetUniformLocation(m_ShaderID, name);
}

void Shader::setUniform1f(const GLchar* name, float value) {
glUniform1f(getUniformLocation(name), value);
}
void Shader::setUniform1i(const GLchar* name, int value) {
glUniform1i(getUniformLocation(name), value);
}
void Shader::setUniform2f(const GLchar* name, const maths::vec2& vector) {
glUniform2f(getUniformLocation(name), vector.x, vector.y);
}
void Shader::setUniform3f(const GLchar* name, const maths::vec3& vector) {
glUniform3f(getUniformLocation(name), vector.x, vector.y, vector.z);
}
void Shader::setUniform4f(const GLchar* name, const maths::vec4& vector) {
glUniform4f(getUniformLocation(name), vector.x, vector.y, vector.z, vector.w);
}
void Shader::setUniformMat4(const GLchar* name, const maths::mat4& matrix) {
glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, matrix.elements);
}


void Shader::enable() const
{
glUseProgram(m_ShaderID);
Expand Down
13 changes: 11 additions & 2 deletions Spraky-core/x64/src/graphics/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
#include <vector>
#include <GL/glew.h>
#include "../utils/fileutils.h"

#include "../maths/math.h"
namespace sparky {
namespace graphics {

class Shader {
private:
GLuint m_ShaderID;
const char *m_VertPath, *m_FragPath; // Updated variable names
const char *m_VertPath, *m_FragPath;
public:
Shader(const char* vertPath, const char* fragPath);
~Shader();

void setUniform1f(const GLchar* name, float value);
void setUniform1i(const GLchar* name, int value);
void setUniform2f(const GLchar* name, const maths::vec2& vector);
void setUniform3f(const GLchar* name, const maths::vec3& vector);
void setUniform4f(const GLchar* name, const maths::vec4& vector);
void setUniformMat4(const GLchar* name, const maths::mat4& matrix);


void enable() const;
void disable() const;
private:
Expand Down
9 changes: 8 additions & 1 deletion Spraky-core/x64/src/shaders/basic.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ in vec4 position; // Remove location qualifier

out vec4 color;

uniform vec4 colour;
uniform vec2 ligh_Pos;

in vec4 pos;

void main()
{
color = vec4(1.0, 0.0, 0.0, 1.0); // Set the fragment color to red
float intensity = 1.0 / length(pos.xy - pos.xy);
color = colour * intensity;

}

6 changes: 5 additions & 1 deletion Spraky-core/x64/src/shaders/basic.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ uniform mat4 pr_matrix;
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 ml_matrix = mat4(1.0);

out vec2 lightPos;
out vec4 pos;

void main()
{
gl_Position = position;
gl_Position = pr_matrix * vw_matrix * ml_matrix * position;
pos = position;
}

0 comments on commit a8f2ef8

Please sign in to comment.