move gl stuff to Graphics class

This commit is contained in:
Furkan Mudanyali 2023-05-28 17:08:01 +03:00
parent 524dcab7be
commit 2a7c7b75b3
4 changed files with 72 additions and 45 deletions

View File

@ -29,6 +29,7 @@ ENDIF()
IF(CMAKE_BUILD_TYPE STREQUAL "Debug") IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
SET(LINK_FLAGS "${debug_linker}") SET(LINK_FLAGS "${debug_linker}")
SET(optimize_flags "-Og -g3 -ffunction-sections -fdata-sections") SET(optimize_flags "-Og -g3 -ffunction-sections -fdata-sections")
ADD_DEFINITIONS(-DDEBUG)
ELSE() ELSE()
SET(optimize_flags "-O3 -ffast-math") SET(optimize_flags "-O3 -ffast-math")
ENDIF() ENDIF()

View File

@ -13,6 +13,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#ifdef DEBUG
void GLAPIENTRY MessageCallback(GLenum source, void GLAPIENTRY MessageCallback(GLenum source,
GLenum type, GLenum type,
GLuint id, GLuint id,
@ -25,6 +26,7 @@ void GLAPIENTRY MessageCallback(GLenum source,
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), type, (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), type,
severity, message); severity, message);
} }
#endif
Graphics::Graphics(Game* game) { Graphics::Graphics(Game* game) {
this->pGame = game; this->pGame = game;
@ -40,8 +42,50 @@ Graphics::Graphics(Game* game) {
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
glewInit(); glewInit();
#ifdef DEBUG
glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, nullptr); glDebugMessageCallback(MessageCallback, nullptr);
#endif
this->program = glCreateProgram();
}
void Graphics::compileShader(const char* shaderSrc, int shaderType) const {
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &shaderSrc, nullptr);
glCompileShader(shader);
glAttachShader(this->program, shader);
#ifdef DEBUG
char msg[512];
glGetShaderInfoLog(shader, sizeof msg, nullptr, msg);
std::cout << "Vertex shader info: " << msg << "\n";
#endif
}
void Graphics::bindAttribLoc(int argIndex, const char* argName) const {
glBindAttribLocation(this->program, argIndex, argName);
}
void Graphics::useProgram() const {
glLinkProgram(program);
#ifdef DEBUG
char msg[512];
glGetProgramInfoLog(program, sizeof msg, nullptr, msg);
std::cout << "Program info: " << msg << "\n";
#endif
glUseProgram(program);
}
GLint Graphics::getUniformLoc(const char* name) const {
return glGetUniformLocation(this->program, name);
}
void Graphics::setUniformValue(GLint position, const GLfloat value[4]) {
glUniform4fv(position, 1, value);
}
void Graphics::setUniformMatrixValue(GLint position, const GLfloat value[16]) {
glUniformMatrix4fv(position, 1, GL_FALSE, value);
} }
void Graphics::storeVertexBufObj(GLuint& dest, GLsizeiptr size, int* target) { void Graphics::storeVertexBufObj(GLuint& dest, GLsizeiptr size, int* target) {

View File

@ -18,12 +18,19 @@ class Window;
class Graphics { class Graphics {
Game* pGame; Game* pGame;
SDL_GLContext context = nullptr; SDL_GLContext context = nullptr;
GLuint program;
public: public:
Graphics(Game*); Graphics(Game*);
~Graphics() = default; ~Graphics() = default;
void setGLContext(); void setGLContext();
void compileShader(const char* shaderSrc, int shaderType) const;
void bindAttribLoc(int, const char*) const;
void useProgram() const;
GLint getUniformLoc(const char* name) const;
static void setUniformValue(GLint, const GLfloat[4]);
static void setUniformMatrixValue(GLint, const GLfloat[16]);
void draw(); void draw();

View File

@ -56,49 +56,27 @@ static const char* fragmentShader = R"(
GearsScene::GearsScene(Game* pGame) { GearsScene::GearsScene(Game* pGame) {
this->pGame = pGame; this->pGame = pGame;
char msg[512];
const char* p;
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
GLuint vertex; this->pGame->pRenderer->compileShader(vertexShader, GL_VERTEX_SHADER);
GLuint fragment; this->pGame->pRenderer->compileShader(fragmentShader, GL_FRAGMENT_SHADER);
GLuint program; this->pGame->pRenderer->bindAttribLoc(0, "position");
this->pGame->pRenderer->bindAttribLoc(1, "normal");
p = vertexShader; this->pGame->pRenderer->useProgram();
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &p, nullptr);
glCompileShader(vertex);
glGetShaderInfoLog(vertex, sizeof msg, nullptr, msg);
std::cout << "Vertex shader info: " << msg << "\n";
p = fragmentShader;
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &p, nullptr);
glCompileShader(fragment);
glGetShaderInfoLog(fragment, sizeof msg, nullptr, msg);
std::cout << "Fragment shader info: " << msg << "\n";
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glBindAttribLocation(program, 0, "position");
glBindAttribLocation(program, 1, "normal");
glLinkProgram(program);
glGetProgramInfoLog(program, sizeof msg, nullptr, msg);
std::cout << "Program info: " << msg << "\n";
glUseProgram(program);
this->modelViewProjectionMatrixLoc = this->modelViewProjectionMatrixLoc =
glGetUniformLocation(program, "ModelViewProjectionMatrix"); this->pGame->pRenderer->getUniformLoc("ModelViewProjectionMatrix");
this->normalMatrixLoc = glGetUniformLocation(program, "NormalMatrix"); this->normalMatrixLoc =
this->lightSrcPosLoc = glGetUniformLocation(program, "LightSourcePosition"); this->pGame->pRenderer->getUniformLoc("NormalMatrix");
this->materialColorLoc = glGetUniformLocation(program, "MaterialColor"); this->lightSrcPosLoc =
this->pGame->pRenderer->getUniformLoc("LightSourcePosition");
this->materialColorLoc =
this->pGame->pRenderer->getUniformLoc("MaterialColor");
glUniform4fv((GLint)this->lightSrcPosLoc, 1, lightSourcePos); Graphics::setUniformValue((GLint)this->lightSrcPosLoc, lightSourcePos);
gears[0] = createGear(1.0, 4.0, 1.0, 20, 0.7); gears[0] = createGear(1.0, 4.0, 1.0, 20, 0.7);
gears[1] = createGear(0.5, 2.0, 2.0, 10, 0.7); gears[1] = createGear(0.5, 2.0, 2.0, 10, 0.7);
@ -266,11 +244,9 @@ Gear* GearsScene::createGear(GLfloat innerRad,
} }
gear->nVertices = (int)(vertex - gear->vertices); gear->nVertices = (int)(vertex - gear->vertices);
glGenBuffers(1, &gear->vertexBufObj); Graphics::storeVertexBufObj(
glBindBuffer(GL_ARRAY_BUFFER, gear->vertexBufObj); gear->vertexBufObj, (GLsizeiptr)(gear->nVertices * sizeof(GearVertex)),
glBufferData(GL_ARRAY_BUFFER, (int*)gear->vertices);
(GLsizeiptr)(gear->nVertices * sizeof(GearVertex)),
gear->vertices, GL_STATIC_DRAW);
return gear; return gear;
} }
@ -295,8 +271,7 @@ void GearsScene::drawGear(Gear* gear,
memcpy(modelViewProjection, this->projectionMatrix, memcpy(modelViewProjection, this->projectionMatrix,
sizeof(modelViewProjection)); sizeof(modelViewProjection));
Graphics::multiply4x4Matrices(modelViewProjection, modelView); Graphics::multiply4x4Matrices(modelViewProjection, modelView);
Graphics::setUniformMatrixValue((GLint)this->modelViewProjectionMatrixLoc,
glUniformMatrix4fv((GLint)this->modelViewProjectionMatrixLoc, 1, GL_FALSE,
modelViewProjection); modelViewProjection);
/* /*
@ -306,10 +281,10 @@ void GearsScene::drawGear(Gear* gear,
memcpy(normalMatrix, modelView, sizeof(normalMatrix)); memcpy(normalMatrix, modelView, sizeof(normalMatrix));
Graphics::invert4x4Matrix(normalMatrix); Graphics::invert4x4Matrix(normalMatrix);
Graphics::transpose4x4Matrix(normalMatrix); Graphics::transpose4x4Matrix(normalMatrix);
glUniformMatrix4fv((GLint)this->normalMatrixLoc, 1, GL_FALSE, normalMatrix); Graphics::setUniformMatrixValue((GLint)this->normalMatrixLoc, normalMatrix);
/* Set the gear color */ /* Set the gear color */
glUniform4fv((GLint)this->materialColorLoc, 1, color); Graphics::setUniformValue((GLint)this->materialColorLoc, color);
/* Set the vertex buffer object to use */ /* Set the vertex buffer object to use */
glBindBuffer(GL_ARRAY_BUFFER, gear->vertexBufObj); glBindBuffer(GL_ARRAY_BUFFER, gear->vertexBufObj);