rms/src/opengl/renderer.cpp
2020-07-05 18:55:39 +02:00

88 lines
2.6 KiB
C++

#include "renderer.hpp"
#include <iostream>
void GLAPIENTRY MessageCallback( GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam )
{
std::cerr << "GL CALLBACK" <<
(type == GL_DEBUG_TYPE_ERROR ? "(ERROR):" : "(UNKNOWN):") <<
" type="<< std::showbase << std::hex << type <<
" severity="<< severity << " message=" << message << std::endl;
}
Renderer::Renderer(short width,short height): Width(width),Height(height), ClockStart(steady_clock::now()){
// Init OpenGl
glewInit();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable ( GL_DEBUG_OUTPUT );
glDebugMessageCallback( MessageCallback, 0 );
// Create screen quad
glGenVertexArrays(1,&VAO);
glBindVertexArray(VAO);
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
float square[]{
-1,1,0,
1,1,0,
-1,-1,0,
-1,-1,0,
1,1,0,
1,-1,0
};
glBufferData(GL_ARRAY_BUFFER, 6*3*sizeof(float), square, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)0);
glBindVertexArray(0);
LoadShader("square.glsl");
}
void Renderer::Render(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(RayMarchingShader);
MProjection=glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.f,100.0f);
MModel=glm::scale(glm::mat4(1.0f),glm::vec3(10,10,1));
glUniformMatrix4fv(UProjection, 1, GL_FALSE, &MProjection[0][0]);
glUniformMatrix4fv(UModel, 1, GL_FALSE, &MModel[0][0]);
ClockCurrent = std::chrono::steady_clock::now();
duration<double> time_span = duration_cast<duration<double>>(ClockCurrent - ClockStart);
glUniform1f(UTime, time_span.count());
glUniform2f(UResolution,Width,Height);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,6);
glBindVertexArray(0);
glUseProgram(0);
}
void Renderer::UpdateShader(std::string name){
glDeleteProgram(RayMarchingShader);
LoadShader(name);
}
void Renderer::LoadShader(std::string name){
RayMarchingShader = CompileShader("square.glsl");
RayMarchingShader = CompileShader("square.glsl");
UProjection = glGetUniformLocation(RayMarchingShader, "projection");
UModel = glGetUniformLocation(RayMarchingShader, "model");
UResolution = glGetUniformLocation(RayMarchingShader, "resolution");
UTime = glGetUniformLocation(RayMarchingShader, "time");
}
void Renderer::AjustViewport(short width,short height){
glViewport(0, 0, width,height);
this->Width=width;
this->Height=height;
}