added shader

This commit is contained in:
djaidjai committed 2025-01-05 22:48:09 +01:00
1 parent 126ef4ace8
commit edf4d9fd60
17 files changed
+257 -153

No files matched your search

+16 -11
View File
@@ -3,9 +3,11 @@
#include <iostream>
#include <memory>
#include "Shader.h"
Window::Window(unsigned int w, unsigned int h) {
Window::w = w;
Window::h = h;
this->w = w;
this->h = h;
}
void framebuffer_size_callback(GLFWwindow* window, int w, int h) {
@@ -16,7 +18,7 @@ void framebuffer_size_callback(GLFWwindow* window, int w, int h) {
}
int Window::init() {
int Window::init(float* data) {
// init glfw
glfwInit();
// requesting context version 1.0 makes glfw try to provide the latest version if possible
@@ -25,7 +27,7 @@ int Window::init() {
this->window = glfwCreateWindow(this->w, this->h, "CUDA ray tracing", NULL, NULL);
//hide cursor
//hide cursor // TODO: switch from this style input to something more resembling an actual gui
glfwSetInputMode(this->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetWindowUserPointer(this->window, reinterpret_cast<void*>(this));
@@ -37,20 +39,17 @@ int Window::init() {
glfwMakeContextCurrent(this->window);
// init glad(opengl)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD\n";
return -1;
}
// init framebuffer
glViewport(0, 0, this->w, this->h);
if (glfwSetFramebufferSizeCallback(this->window, framebuffer_size_callback) != 0) return -1;
if (init_quad()) return -1;
if (init_quad(data)) return -1;
this->last_frame = std::chrono::steady_clock::now();
while (!glfwWindowShouldClose(window)) {
@@ -62,11 +61,14 @@ int Window::init() {
}
int Window::init_quad() {
int Window::init_quad(float* data) {
this->current_quad = std::make_unique<Quad>(this->w, this->h);
this->current_quad->cuda_init();
this->current_quad->cuda_init(data);
this->current_quad->make_fbo();
this->shader = std::make_unique<Shader>("./shaders/vertshader.glsl", "./shaders/fragshader.glsl");
this->shader->use();
// TODO: default shaders
return 0;
}
@@ -107,6 +109,7 @@ void Window::tick() {
// render frame
glBindFramebuffer(GL_FRAMEBUFFER, this->current_quad->fb);
this->current_quad->render();
this->shader->use();
glBindVertexArray(this->current_quad->VAO);
glBindTexture(GL_TEXTURE_2D, this->current_quad->tex);
glDrawArrays(GL_TRIANGLES, 0, 6); // draw current frame to texture
@@ -115,6 +118,8 @@ void Window::tick() {
// + swap buffers; TODO: check if necessary?
glfwSwapBuffers(this->window);
glfwPollEvents();
std::cout << "done ticking";
}
+5 -3
View File
@@ -2,6 +2,7 @@
#define MAINWINDOW_H
#include "Quad.h"
#include "Shader.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <chrono>
@@ -11,10 +12,11 @@ class Window {
public:
unsigned int w;
unsigned int h;
float* data; //TODO: dynamic data loading
Window(unsigned int w, unsigned int h);
int init();
int init(float* data);
void free();
void resize(unsigned int w, unsigned int h);
@@ -25,8 +27,8 @@ private:
std::chrono::steady_clock::time_point last_frame;
void tick();
int init_quad();
int init_quad(float* data);
// std::unique_ptr<Shader> shader;
std::unique_ptr<Shader> shader;
};
#endif // MAINWINDOW_H
+6 -5
View File
@@ -61,13 +61,14 @@ Quad::Quad(unsigned int w, unsigned int h) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
// register the FBO
};
void Quad::make_fbo(){
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->tex, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
};
}
Quad::~Quad() {
int res = cudaGraphicsUnregisterResource(CGR);
@@ -79,14 +80,14 @@ Quad::~Quad() {
};
void Quad::cuda_init() {
void Quad::cuda_init(float* data) {
int res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone);
if (res) {
std::cout << "CUDA error while registering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
this->renderer = std::make_unique<Raycaster>(this->CGR, this->w, this->h);
this->renderer = std::make_unique<Raycaster>(this->CGR, this->w, this->h, data);
};
+2 -1
View File
@@ -30,7 +30,8 @@ public:
void render();
void resize(unsigned int w, unsigned int h);
void cuda_init();
void cuda_init(float* data);
void make_fbo();
};
#endif // QUAD_H
+126
View File
@@ -0,0 +1,126 @@
// Shader class nicked from learnOpengl here: https://learnopengl.com/Getting-started/Shaders
// which is published under the CC BY-NC 4.0 license (and thus fine to use here)
#ifndef SHADER_H
#define SHADER_H
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader
{
public:
unsigned int ID;
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
Shader(const char* vertexPath, const char* fragmentPath)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure& e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
// 2. compile shaders
unsigned int vertex, fragment;
// vertex shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
// fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
// shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
checkCompileErrors(ID, "PROGRAM");
// delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex);
glDeleteShader(fragment);
}
// activate the shader
// ------------------------------------------------------------------------
void use()
{
glUseProgram(ID);
}
// utility uniform functions
// ------------------------------------------------------------------------
void setBool(const std::string &name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
}
// ------------------------------------------------------------------------
void setInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
// ------------------------------------------------------------------------
void setFloat(const std::string &name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
private:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------
void checkCompileErrors(unsigned int shader, std::string type)
{
int success;
char infoLog[1024];
if (type != "PROGRAM")
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
else
{
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
};
#endif
File renamed without changes.
@@ -1,19 +0,0 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D currentFrameTex;
uniform sampler2D lastFrameTex;
uniform int frameCount;
#define MAX_FRAMES 500.0f
void main()
{
vec3 col = texture(currentFrameTex, TexCoords).rgb;
vec3 col2 = texture(lastFrameTex, TexCoords).rgb;
col = mix(col, col2, min(frameCount/MAX_FRAMES,1.0f));
FragColor = vec4(col, 1.0);
}
-11
View File
@@ -1,11 +0,0 @@
#version 330 core
layout(location = 0) in vec2 aPos;
layout(location = 1) in vec2 aTexCoords;
out vec2 TexCoords;
void main()
{
TexCoords = aTexCoords;
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}
File renamed without changes.
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.