Browse Source

Basic gl state tracking

testing
= 6 years ago
parent
commit
8c47ebbd47
  1. 45
      \
  2. 24
      include/graphics/glstate.h
  3. 3
      src/game.cpp
  4. 47
      src/graphics/glstate.cpp
  5. 9
      src/graphics/skybox.cpp
  6. 2
      src/scenes/testscene.cpp

45
\

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
#include <vector>
#include <utility>
#include "graphics/glstate.h"
#include "graphics/shader.h"
namespace vtk::gls {
static GLuint activeShader(0);
static bool trackingEnabled(false);
static std::vector<std::pair<GLenum, bool> > flags;
int setShader(const GLuint& shaderID) {
if (activeShader == shaderID) return 0;
glUseProgram(shaderID);
activeShader = shaderID;
}
void setTracking(const bool& flag) {
trackingEnabled = flag;
}
void enableFlag(const GLenum& flag) {
if (trackingEnabled) {
GLboolean state;
glGetBooleanv(flag, &state);
if (!(bool)state) {
glEnable(flag);
if (trackingEnabled) {
flags.push_back(std::make_pair(flag,true));
}
}
} else {
glEnable(flag);
}
}
void restoreState() {
for(auto& i : flags) {
if(i.second) glDisable(i.first);
else glDisable(i.first);
}
flags.clear();
}
}

24
include/graphics/glstate.h

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
/*
* Functions for dealing with OGL's PITA state management
*/
#pragma once
#include <memory>
#include <GL/glew.h>
#include <GL/gl.h>
// forward declarations
namespace vtk {
class Shader;
}
namespace vtk::gls {
int setShader(const GLuint& shaderID);
void setTracking(const bool& flag);
void setFlag(const GLenum& flag, const bool& newState);
void restoreState();
}

3
src/game.cpp

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
#include "game.h"
#include "loadShader.h"
#include "graphics/camera.h"
#include "graphics/glstate.h"
#include "scene.h"
//SPAGHETTI INCLUDES
@ -61,7 +62,7 @@ void Game::init() { @@ -61,7 +62,7 @@ void Game::init() {
glFrontFace(GL_CW);
running = false;
gls::setTracking(true);
}
void Game::start() {

47
src/graphics/glstate.cpp

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
#include <vector>
#include <utility>
#include <iostream>
#include "graphics/glstate.h"
#include "graphics/shader.h"
namespace vtk::gls {
static GLuint activeShader(0);
static bool trackingEnabled(true);
static std::vector<std::pair<GLenum, bool> > flags;
int setShader(const GLuint& shaderID) {
if (activeShader == shaderID) return 0;
glUseProgram(shaderID);
activeShader = shaderID;
}
void setTracking(const bool& flag) {
trackingEnabled = flag;
}
void setFlag(const GLenum& flag, const bool& newState) {
if (trackingEnabled) {
GLboolean state;
glGetBooleanv(flag, &state);
if ((bool)state != newState) {
if (newState) glEnable(flag);
else glDisable(flag);
flags.push_back(std::make_pair(flag,newState));
}
} else {
if (newState) glEnable(flag);
else glDisable(flag);
}
}
void restoreState() {
for(auto& i : flags) {
if(i.second) glDisable(i.first);
else glEnable(i.first);
}
flags.clear();
}
}

9
src/graphics/skybox.cpp

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include "loadShader.h"
#include "graphics/skybox.h"
#include "graphics/glstate.h"
namespace vtk {
@ -81,15 +82,15 @@ namespace vtk { @@ -81,15 +82,15 @@ namespace vtk {
auto viewMat = camera.getAngleMatrix();
auto pos = camera.getPosition();
//gls::setShader(mShader);
glUseProgram(mShader);
glUniformMatrix4fv(mViewMat, 1, GL_FALSE, glm::value_ptr(viewMat));
glUniformMatrix4fv(mProjMat, 1, GL_FALSE, glm::value_ptr(proj));
glDepthMask(GL_FALSE);
gls::setFlag(GL_DEPTH_TEST, false);
glBindVertexArray(mVao);
glDrawArrays(GL_TRIANGLES, 0, 36);
glDepthMask(GL_TRUE);
gls::restoreState();
}
}

2
src/scenes/testscene.cpp

@ -185,7 +185,6 @@ void TestScene::update(const float& dTime) { @@ -185,7 +185,6 @@ void TestScene::update(const float& dTime) {
void TestScene::draw() {
mSkybox.draw(camera, linkedGame->window.getProjectionMatrix());
glEnable(GL_DEPTH_TEST);
glUseProgram(shaders);
glUniformMatrix4fv(viewMatUni, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix()));
world.draw();
@ -194,6 +193,7 @@ void TestScene::draw() { @@ -194,6 +193,7 @@ void TestScene::draw() {
glUseProgram(cursorShaders);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
glEnable(GL_DEPTH_TEST);
}
void TestScene::look() {

Loading…
Cancel
Save