Cogs.Core
OpenGLState.h
1#pragma once
2
3#include "CommonGL20.h"
4
5#include "Foundation/Logging/Logger.h"
6
7namespace
8{
9 static Cogs::Logging::Log stateLogger = Cogs::Logging::getLogger("OpenGLState");
10}
11
12namespace Cogs
13{
15 {
16 StateVariable(GLenum name) :
17 name(name)
18 {
19 originalValue = glIsEnabled(name);
20 }
22 {
23 originalValue ? glEnable(name) : glDisable(name);
24 }
25 GLenum name;
26 GLboolean originalValue;
27 };
28
30 {
31 StateBufferBinding(GLenum name, GLenum bindingName) :
32 name(name)
33 {
34 glGetIntegerv(bindingName, &binding);
35 }
37 {
38 glBindBuffer(name, binding);
39 }
40 GLenum name;
41 GLint binding;
42 };
43
45 {
46 TextureBinding(GLenum name, GLenum bindingName, GLint slot) :
47 name(name),
48 slot(slot)
49 {
50 glActiveTexture(GL_TEXTURE0 + slot);
51 glGetIntegerv(bindingName, &binding);
52 }
54 {
55 glActiveTexture(GL_TEXTURE0 + slot);
56 glBindTexture(name, binding);
57 }
58 GLenum name;
59 GLint slot;
60 GLint binding;
61 };
62
64 {
65 GLenum frontFace;
66 GLenum cullFace;
67
68 std::vector<StateVariable> enabledStates;
69 std::vector<StateBufferBinding> bindings;
70 std::vector<TextureBinding> textures;
71
72 float viewPort[4];
73
74 GLint activeProgram;
75 GLint activeTexture;
76
77 void push()
78 {
79 glGetIntegerv(GL_CULL_FACE_MODE, (GLint *)&cullFace);
80 glGetIntegerv(GL_FRONT_FACE, (GLint *)&frontFace);
81
82 glGetFloatv(GL_VIEWPORT, viewPort);
83
84 enabledStates.push_back(StateVariable(GL_BLEND));
85 enabledStates.push_back(StateVariable(GL_DEPTH_TEST));
86 enabledStates.push_back(StateVariable(GL_CULL_FACE));
87 enabledStates.push_back(StateVariable(GL_POLYGON_OFFSET_FILL));
88
89 bindings.push_back(StateBufferBinding(GL_ARRAY_BUFFER, GL_ARRAY_BUFFER_BINDING));
90 bindings.push_back(StateBufferBinding(GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER_BINDING));
91 bindings.push_back(StateBufferBinding(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER_BINDING));
92 bindings.push_back(StateBufferBinding(GL_PIXEL_PACK_BUFFER, GL_PIXEL_PACK_BUFFER_BINDING));
93 bindings.push_back(StateBufferBinding(GL_PIXEL_UNPACK_BUFFER, GL_PIXEL_UNPACK_BUFFER_BINDING));
94
95 // GLint maxTextureUnits;
96 // glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
97 // for (int i = 0; i < maxTextureUnits; ++i) {
98 // textures.push_back(TextureBinding(GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, i));
99 // }
100
101 glGetIntegerv(GL_CURRENT_PROGRAM, &activeProgram);
102 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTexture);
103 }
104
105 void pop()
106 {
107 glFrontFace(frontFace);
108 glCullFace(cullFace);
109
110 glViewport(static_cast<GLint>(viewPort[0]), static_cast<GLint>(viewPort[1]), static_cast<GLsizei>(viewPort[2]), static_cast<GLsizei>(viewPort[3]));
111
112 enabledStates.clear();
113 bindings.clear();
114 textures.clear();
115
116 glBindFramebuffer(GL_FRAMEBUFFER, 0);
117 glUseProgram(activeProgram);
118 glActiveTexture(activeTexture);
119 }
120 };
121}
Log implementation class.
Definition: LogManager.h:139
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
Contains all Cogs related functionality.
Definition: FieldSetter.h:23