Fixed and added some minor color correction

This commit is contained in:
2025-10-01 22:13:58 +01:00
parent 0c861f6855
commit 9107273a0c
10 changed files with 127 additions and 112 deletions
View File
View File
+9 -85
View File
@@ -1,3 +1,9 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
// glad and GLFW
#include <glad/glad.h>
#include <GLFW/glfw3.h>
@@ -7,11 +13,8 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
// HPP files
#include "shader.h"
#define VERTEX_FILE "./shaders/vertex.glsl"
#define FRAGMENT_FILE "./shaders/fragment.glsl"
@@ -20,85 +23,7 @@
const float SCR_WIDTH = 1200.0f;
const float SCR_HEIGHT = 1200.0f;
// Shader loader helper
std::string loadShaderSource(const std::string& filepath) {
std::ifstream file(filepath);
if (!file.is_open()) {
std::cerr << "Failed to open shader file: " << filepath << std::endl;
return "";
}
std::stringstream buffer;
std::cout << "Loaded " << filepath << " successfully!\n";
buffer << file.rdbuf();
return buffer.str();
}
GLuint compileShader(GLenum type, const std::string& source) {
GLuint shader = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char infoLog[512];
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
std::cerr << "Shader compilation failed:\n" << infoLog << std::endl;
exit(10);
}
return shader;
}
GLuint createShaderProgram(const std::string& vertexPath, const std::string& fragmentPath) {
std::string vertexSrc = loadShaderSource(vertexPath);
std::string fragmentSrc = loadShaderSource(fragmentPath);
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexSrc);
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentSrc);
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
glGetProgramInfoLog(program, 512, nullptr, infoLog);
std::cerr << "Shader linking failed:\n" << infoLog << std::endl;
exit(11);
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return program;
}
GLuint createComputeProgram(const std::string& computePath) {
std::string computeSrc = loadShaderSource(computePath);
GLuint computeShader = compileShader(GL_COMPUTE_SHADER, computeSrc);
GLuint program = glCreateProgram();
glAttachShader(program, computeShader);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
glGetProgramInfoLog(program, 512, nullptr, infoLog);
std::cerr << "Compute shader linking failed:\n" << infoLog << std::endl;;
}
glDeleteShader(computeShader);
return program;
}
int main() {
int main(void) {
// Initialize GLFW
if (!glfwInit()) return -1;
@@ -205,7 +130,6 @@ int main() {
int frameCount = 0;
float mousePos[2] = {0.0f, 0.0f};
// FPS tracking
double lastTime = glfwGetTime(); // when we last printed FPS
double fps = 0.0;
View File
View File
+80
View File
@@ -0,0 +1,80 @@
#include "shader.h"
// Shader loader helper
std::string loadShaderSource(const std::string& filepath) {
std::ifstream file(filepath);
if (!file.is_open()) {
std::cerr << "Failed to open shader file: " << filepath << std::endl;
return "";
}
std::stringstream buffer;
std::cout << "Loaded " << filepath << " successfully!\n";
buffer << file.rdbuf();
return buffer.str();
}
GLuint compileShader(GLenum type, const std::string& source) {
GLuint shader = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char infoLog[512];
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
std::cerr << "Shader compilation failed:\n" << infoLog << std::endl;
exit(10);
}
return shader;
}
GLuint createShaderProgram(const std::string& vertexPath, const std::string& fragmentPath) {
std::string vertexSrc = loadShaderSource(vertexPath);
std::string fragmentSrc = loadShaderSource(fragmentPath);
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexSrc);
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentSrc);
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
glGetProgramInfoLog(program, 512, nullptr, infoLog);
std::cerr << "Shader linking failed:\n" << infoLog << std::endl;
exit(11);
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return program;
}
/* GLuint createComputeProgram(const std::string& computePath) {
std::string computeSrc = loadShaderSource(computePath);
GLuint computeShader = compileShader(GL_COMPUTE_SHADER, computeSrc);
GLuint program = glCreateProgram();
glAttachShader(program, computeShader);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
glGetProgramInfoLog(program, 512, nullptr, infoLog);
std::cerr << "Compute shader linking failed:\n" << infoLog << std::endl;;
}
glDeleteShader(computeShader);
return program;
} */
+31
View File
@@ -0,0 +1,31 @@
#ifndef SHADER_HPP
#define SHADER_HPP
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <GLFW/glfw3.h>
// Shader loader helper
std::string loadShaderSource(const std::string& filepath);
// Compile a shader of given type from source code
GLuint compileShader(GLenum type, const std::string& source);
// Create a shader program from vertex and fragment shader file paths
GLuint createShaderProgram(const std::string& vertexPath, const std::string& fragmentPath);
// Create a compute shader program from compute shader file path
GLuint createComputeProgram(const std::string& computePath);
#endif // SHADER_HPP