move gl stuff to Graphics class
This commit is contained in:
parent
524dcab7be
commit
2a7c7b75b3
@ -29,6 +29,7 @@ ENDIF()
|
||||
IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
SET(LINK_FLAGS "${debug_linker}")
|
||||
SET(optimize_flags "-Og -g3 -ffunction-sections -fdata-sections")
|
||||
ADD_DEFINITIONS(-DDEBUG)
|
||||
ELSE()
|
||||
SET(optimize_flags "-O3 -ffast-math")
|
||||
ENDIF()
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#ifdef DEBUG
|
||||
void GLAPIENTRY MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
@ -25,6 +26,7 @@ void GLAPIENTRY MessageCallback(GLenum source,
|
||||
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), type,
|
||||
severity, message);
|
||||
}
|
||||
#endif
|
||||
|
||||
Graphics::Graphics(Game* game) {
|
||||
this->pGame = game;
|
||||
@ -40,8 +42,50 @@ Graphics::Graphics(Game* game) {
|
||||
glewExperimental = GL_TRUE;
|
||||
glewInit();
|
||||
|
||||
#ifdef DEBUG
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
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) {
|
||||
|
@ -18,12 +18,19 @@ class Window;
|
||||
class Graphics {
|
||||
Game* pGame;
|
||||
SDL_GLContext context = nullptr;
|
||||
GLuint program;
|
||||
|
||||
public:
|
||||
Graphics(Game*);
|
||||
~Graphics() = default;
|
||||
|
||||
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();
|
||||
|
||||
|
@ -56,49 +56,27 @@ static const char* fragmentShader = R"(
|
||||
|
||||
GearsScene::GearsScene(Game* pGame) {
|
||||
this->pGame = pGame;
|
||||
char msg[512];
|
||||
const char* p;
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
GLuint vertex;
|
||||
GLuint fragment;
|
||||
GLuint program;
|
||||
this->pGame->pRenderer->compileShader(vertexShader, GL_VERTEX_SHADER);
|
||||
this->pGame->pRenderer->compileShader(fragmentShader, GL_FRAGMENT_SHADER);
|
||||
this->pGame->pRenderer->bindAttribLoc(0, "position");
|
||||
this->pGame->pRenderer->bindAttribLoc(1, "normal");
|
||||
|
||||
p = vertexShader;
|
||||
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->pGame->pRenderer->useProgram();
|
||||
|
||||
this->modelViewProjectionMatrixLoc =
|
||||
glGetUniformLocation(program, "ModelViewProjectionMatrix");
|
||||
this->normalMatrixLoc = glGetUniformLocation(program, "NormalMatrix");
|
||||
this->lightSrcPosLoc = glGetUniformLocation(program, "LightSourcePosition");
|
||||
this->materialColorLoc = glGetUniformLocation(program, "MaterialColor");
|
||||
this->pGame->pRenderer->getUniformLoc("ModelViewProjectionMatrix");
|
||||
this->normalMatrixLoc =
|
||||
this->pGame->pRenderer->getUniformLoc("NormalMatrix");
|
||||
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[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);
|
||||
glGenBuffers(1, &gear->vertexBufObj);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, gear->vertexBufObj);
|
||||
glBufferData(GL_ARRAY_BUFFER,
|
||||
(GLsizeiptr)(gear->nVertices * sizeof(GearVertex)),
|
||||
gear->vertices, GL_STATIC_DRAW);
|
||||
Graphics::storeVertexBufObj(
|
||||
gear->vertexBufObj, (GLsizeiptr)(gear->nVertices * sizeof(GearVertex)),
|
||||
(int*)gear->vertices);
|
||||
|
||||
return gear;
|
||||
}
|
||||
@ -295,8 +271,7 @@ void GearsScene::drawGear(Gear* gear,
|
||||
memcpy(modelViewProjection, this->projectionMatrix,
|
||||
sizeof(modelViewProjection));
|
||||
Graphics::multiply4x4Matrices(modelViewProjection, modelView);
|
||||
|
||||
glUniformMatrix4fv((GLint)this->modelViewProjectionMatrixLoc, 1, GL_FALSE,
|
||||
Graphics::setUniformMatrixValue((GLint)this->modelViewProjectionMatrixLoc,
|
||||
modelViewProjection);
|
||||
|
||||
/*
|
||||
@ -306,10 +281,10 @@ void GearsScene::drawGear(Gear* gear,
|
||||
memcpy(normalMatrix, modelView, sizeof(normalMatrix));
|
||||
Graphics::invert4x4Matrix(normalMatrix);
|
||||
Graphics::transpose4x4Matrix(normalMatrix);
|
||||
glUniformMatrix4fv((GLint)this->normalMatrixLoc, 1, GL_FALSE, normalMatrix);
|
||||
Graphics::setUniformMatrixValue((GLint)this->normalMatrixLoc, normalMatrix);
|
||||
|
||||
/* Set the gear color */
|
||||
glUniform4fv((GLint)this->materialColorLoc, 1, color);
|
||||
Graphics::setUniformValue((GLint)this->materialColorLoc, color);
|
||||
|
||||
/* Set the vertex buffer object to use */
|
||||
glBindBuffer(GL_ARRAY_BUFFER, gear->vertexBufObj);
|
||||
|
Reference in New Issue
Block a user