generated from AfonsoCMSousa/CPP-Template
Experimental: Supersampling, (doesnt work)
This commit is contained in:
parent
a96657e40c
commit
f077daebcd
Binary file not shown.
BIN
build/bin/RASTER
BIN
build/bin/RASTER
Binary file not shown.
0
build/libraries/glfw/cmake_uninstall.cmake
Normal file → Executable file
0
build/libraries/glfw/cmake_uninstall.cmake
Normal file → Executable file
0
build/libraries/glfw/src/glfw3.pc
Normal file → Executable file
0
build/libraries/glfw/src/glfw3.pc
Normal file → Executable file
0
build/libraries/glfw/src/glfw3Config.cmake
Normal file → Executable file
0
build/libraries/glfw/src/glfw3Config.cmake
Normal file → Executable file
111
source/main.cpp
111
source/main.cpp
@ -44,8 +44,8 @@ GLuint compileShader(GLenum type, const std::string& source) {
|
||||
int success;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
|
||||
char infoLog[1024];
|
||||
glGetShaderInfoLog(shader, 1024, nullptr, infoLog);
|
||||
std::cerr << "Shader compilation failed:\n" << infoLog << std::endl;
|
||||
exit(10);
|
||||
}
|
||||
@ -67,8 +67,8 @@ GLuint createShaderProgram(const std::string& vertexPath, const std::string& fra
|
||||
int success;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetProgramInfoLog(program, 512, nullptr, infoLog);
|
||||
char infoLog[1024];
|
||||
glGetProgramInfoLog(program, 1024, nullptr, infoLog);
|
||||
std::cerr << "Shader linking failed:\n" << infoLog << std::endl;
|
||||
exit(11);
|
||||
}
|
||||
@ -89,8 +89,8 @@ GLuint createComputeProgram(const std::string& computePath) {
|
||||
int success;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetProgramInfoLog(program, 512, nullptr, infoLog);
|
||||
char infoLog[1024];
|
||||
glGetProgramInfoLog(program, 1024, nullptr, infoLog);
|
||||
std::cerr << "Compute shader linking failed:\n" << infoLog << std::endl;;
|
||||
}
|
||||
|
||||
@ -125,12 +125,7 @@ int main() {
|
||||
//DEBUG
|
||||
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
|
||||
|
||||
// ERROR: macOS does not support compute shaders in OpenGL < 4.3.
|
||||
// GLuint computeProgram = createComputeProgram(COMPUTE_FILE);
|
||||
|
||||
// ----- Vertex Data -----
|
||||
// Cube
|
||||
// Positions
|
||||
float vertices[] = {
|
||||
// positions
|
||||
-1.0f, -1.0f, 0.0f,
|
||||
@ -161,22 +156,11 @@ int main() {
|
||||
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// color - FRAGMENT SHADER
|
||||
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)(3*sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER,0);
|
||||
// glBindVertexArray(0);
|
||||
|
||||
// Load shaders
|
||||
GLuint shaderProgram = createShaderProgram(VERTEX_FILE, FRAGMENT_FILE);
|
||||
|
||||
// Check if shader program was created successfully
|
||||
if (shaderProgram == 0) {
|
||||
std::cerr << "Failed to create shader programs.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Caulculate model, view, projection matrices
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -1.0f));
|
||||
@ -191,21 +175,55 @@ int main() {
|
||||
glUniformMatrix4fv((int) viewLoc, 1, GL_FALSE, glm::value_ptr(view));
|
||||
glUniformMatrix4fv((int) projLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
||||
|
||||
|
||||
// Query important uniform locations (we'll keep them around)
|
||||
GLint resLoc = glGetUniformLocation(shaderProgram, "u_resolution");
|
||||
glUniform2f(resLoc, SCR_WIDTH, SCR_HEIGHT); // hardcode for now
|
||||
|
||||
GLint timeLoc = glGetUniformLocation(shaderProgram, "u_time");
|
||||
GLint scaleLoc = glGetUniformLocation(shaderProgram, "u_scale");
|
||||
GLint mouseLoc = glGetUniformLocation(shaderProgram, "u_mouse");
|
||||
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
// --- Supersampling FBO ---
|
||||
int scaleFactor = 2; // try 2 or 4 for smoother output
|
||||
int fbWidth = (int)SCR_WIDTH * scaleFactor;
|
||||
int fbHeight = (int)SCR_HEIGHT * scaleFactor;
|
||||
|
||||
unsigned int fbo, colorTex, rbo;
|
||||
glGenFramebuffers(1, &fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
|
||||
// Color texture
|
||||
glGenTextures(1, &colorTex);
|
||||
glBindTexture(GL_TEXTURE_2D, colorTex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fbWidth, fbHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
|
||||
// filtering + wrap
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTex, 0);
|
||||
|
||||
// Depth/stencil buffer
|
||||
glGenRenderbuffers(1, &rbo);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, fbWidth, fbHeight);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
std::cout << "ERROR: Framebuffer not complete!" << std::endl;
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
// --------------------------
|
||||
|
||||
// set the shader resolution to the FBO size (important!)
|
||||
glUseProgram(shaderProgram);
|
||||
glUniform2f(resLoc, SCR_WIDTH, SCR_HEIGHT);
|
||||
|
||||
float scale = 1.0f;
|
||||
int frameCount = 0;
|
||||
float mousePos[2] = {0.0f, 0.0f};
|
||||
|
||||
|
||||
// FPS tracking
|
||||
double lastTime = glfwGetTime(); // when we last printed FPS
|
||||
double fps = 0.0;
|
||||
@ -223,7 +241,7 @@ int main() {
|
||||
// Input
|
||||
glfwPollEvents();
|
||||
|
||||
|
||||
// Shader hot-reload
|
||||
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
|
||||
glDeleteProgram(shaderProgram);
|
||||
shaderProgram = createShaderProgram(VERTEX_FILE, FRAGMENT_FILE);
|
||||
@ -236,11 +254,14 @@ int main() {
|
||||
glUniformMatrix4fv((int) viewLoc, 1, GL_FALSE, glm::value_ptr(view));
|
||||
glUniformMatrix4fv((int) projLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
||||
|
||||
// Re-query uniforms and set resolution to FBO size (IMPORTANT)
|
||||
resLoc = glGetUniformLocation(shaderProgram, "u_resolution");
|
||||
glUniform2f(resLoc, SCR_WIDTH, SCR_HEIGHT); // hardcode for now
|
||||
|
||||
timeLoc = glGetUniformLocation(shaderProgram, "u_time");
|
||||
scaleLoc = glGetUniformLocation(shaderProgram, "u_scale");
|
||||
mouseLoc = glGetUniformLocation(shaderProgram, "u_mouse");
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
glUniform2f(resLoc, (float)fbWidth, (float)fbHeight);
|
||||
|
||||
std::cout << "Shaders recompiled!\n";
|
||||
}
|
||||
@ -256,10 +277,12 @@ int main() {
|
||||
if (scale < 0.1f) scale = 0.1f; // Min zoom
|
||||
}
|
||||
|
||||
// --- FPS Counter ---
|
||||
// increment frame count (for FPS)
|
||||
frameCount++;
|
||||
|
||||
// --- FPS Counter (print to console every second) ---
|
||||
double currentTime = glfwGetTime();
|
||||
double delta = currentTime - lastTime;
|
||||
|
||||
if (delta >= 1.0) { // print every ~1 second
|
||||
fps = double(frameCount) / delta;
|
||||
std::cout << "FPS: " << fps
|
||||
@ -272,18 +295,31 @@ int main() {
|
||||
lastTime = currentTime;
|
||||
}
|
||||
|
||||
// --- Render into high-res FBO ---
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
glViewport(0, 0, fbWidth, fbHeight);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Use shader & set uniforms
|
||||
glUseProgram(shaderProgram);
|
||||
glUniform1f(timeLoc, timeValue);
|
||||
glUniform1f(scaleLoc, scale);
|
||||
glUniform2f(mouseLoc, mousePos[0], mousePos[1]);
|
||||
// (u_resolution already set to fbWidth, fbHeight)
|
||||
|
||||
frameCount++;
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
// --- Blit back to screen ---
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glBlitFramebuffer(0, 0, fbWidth, fbHeight,
|
||||
0, 0, (int)SCR_WIDTH, (int)SCR_HEIGHT,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glViewport(0, 0, (int)SCR_WIDTH, (int)SCR_HEIGHT);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
@ -297,4 +333,3 @@ int main() {
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user