forked from srinchiera/cloth-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderstates.cpp
More file actions
executable file
·154 lines (129 loc) · 3.55 KB
/
renderstates.cpp
File metadata and controls
executable file
·154 lines (129 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdexcept>
#include "glsupport.h"
#include "renderstates.h"
using namespace std;
static const unsigned int kBlendBit = 1,
kCullFaceBit = 2;
RenderStates::RenderStates()
: glFront(GL_FILL)
, glBack(GL_FILL)
, glBlendSrcFactor(GL_ONE)
, glBlendDstFactor(GL_ZERO)
, glCullFaceMode(GL_BACK)
, flags(kCullFaceBit) {}
RenderStates& RenderStates::polygonMode(GLenum face, GLenum mode) {
switch (mode) {
case GL_POINT:
case GL_LINE:
case GL_FILL:
switch (face) {
case GL_FRONT_AND_BACK:
glFront = glBack = mode;
return *this;
case GL_FRONT:
glFront = mode;
return *this;
case GL_BACK:
glBack = mode;
return *this;
default:
;
}
default:
;
}
throw invalid_argument("RenderStates::glPolygonMode: invalid argument");
}
RenderStates& RenderStates::blendFunc(GLenum sfactor, GLenum dfactor) {
// future TODO: should check if sfactor and dfactor are valid enums
glBlendSrcFactor = sfactor;
glBlendDstFactor = dfactor;
return *this;
}
RenderStates& RenderStates::cullFace(GLenum mode) {
glCullFaceMode = mode;
return *this;
}
RenderStates& RenderStates::enable(GLenum target) {
switch (target) {
case GL_BLEND:
flags |= kBlendBit;
return *this;
case GL_CULL_FACE:
flags |= kCullFaceBit;
return *this;
default:
;
}
throw invalid_argument("RenderStates::glEnable: unsupported target");
}
RenderStates& RenderStates::disable(GLenum target) {
switch (target) {
case GL_BLEND:
flags &= ~kBlendBit;
return *this;
case GL_CULL_FACE:
flags &= ~kCullFaceBit;
return *this;
default:
;
}
throw invalid_argument("RenderStates::glEnable: unsupported target");
}
void RenderStates::apply() const {
static bool firstRun = false;
static RenderStates currentRs;
if (firstRun) {
currentRs.captureFromGl();
firstRun = false;
}
if (glFront != currentRs.glFront) {
::glPolygonMode(GL_FRONT, glFront);
currentRs.glFront = glFront;
}
if (glBack != currentRs.glBack) {
::glPolygonMode(GL_BACK, glFront);
currentRs.glBack = glBack;
}
if (glBlendSrcFactor != currentRs.glBlendSrcFactor || glBlendDstFactor != currentRs.glBlendDstFactor) {
::glBlendFunc(glBlendSrcFactor, glBlendDstFactor);
currentRs.glBlendSrcFactor = glBlendSrcFactor;
currentRs.glBlendDstFactor = glBlendDstFactor;
}
if (glCullFaceMode != currentRs.glCullFaceMode) {
::glCullFace(glCullFaceMode);
currentRs.glCullFaceMode = glCullFaceMode;
}
if ((flags & kBlendBit) != (currentRs.flags & kBlendBit)) {
if (flags & kBlendBit)
::glEnable(GL_BLEND);
else
::glDisable(GL_BLEND);
currentRs.flags = (currentRs.flags & (~kBlendBit)) | (flags & kBlendBit);
}
if ((flags & kCullFaceBit) != (currentRs.flags & kCullFaceBit)) {
if (flags & kCullFaceBit)
::glEnable(GL_CULL_FACE);
else
::glDisable(GL_CULL_FACE);
currentRs.flags = (currentRs.flags & (~kCullFaceBit)) | (flags & kCullFaceBit);
}
}
void RenderStates::captureFromGl() {
GLint values[2];
::glGetIntegerv(GL_POLYGON_MODE, values);
glFront = values[0];
glBack = values[1];
::glGetIntegerv(GL_BLEND_SRC_RGB, values); // Ignore glBlendFuncSeparate fo rnow
glBlendSrcFactor = values[0];
::glGetIntegerv(GL_BLEND_DST_RGB, values);
glBlendDstFactor = values[0];
::glGetIntegerv(GL_CULL_FACE_MODE, values);
glCullFaceMode = values[0];
flags = 0;
if (::glIsEnabled(GL_BLEND))
flags |= kBlendBit;
if (::glIsEnabled(GL_CULL_FACE))
flags |= kCullFaceBit;
checkGlErrors();
}