Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//OpenGL state management: Lights, matrices, viewport, bound objects etc
#include "Headers/GLWrapper.h"
#include "Headers/glRenderStateBlock.h"

#include "Core/Headers/ParamHandler.h"
#include "Managers/Headers/ShaderManager.h"
#include "Rendering/Headers/Frustum.h"
#include "Rendering/Lighting/Headers/Light.h"
#include "Hardware/Video/Headers/GFXDevice.h"
#include "Hardware/Video/OpenGL/Buffers/VertexBufferObject/Headers/glVertexArrayObject.h"
#include "Hardware/Video/OpenGL/Shaders/Headers/glUniformBufferObject.h"
#include <gtc/type_ptr.hpp>
#include <gtc/matrix_transform.hpp>

glShaderProgram* GL_API::_activeShaderProgram = NULL;
GLuint GL_API::_activeVAOId = 0;
GLuint GL_API::_activeTextureUnit = 0;
vec4<GLfloat> GL_API::_prevClearColor = DIVIDE_BLUE();

void GL_API::clearStates(const bool skipShader,const bool skipTextures,const bool skipBuffers, const bool forceAll){
    if(!skipShader || forceAll) {
        setActiveProgram(NULL,forceAll);
    }

    if(!skipTextures || forceAll){
        setActiveTextureUnit(0,forceAll);
    }

    if(!skipBuffers || forceAll){
        setActiveVAO(0,forceAll);
		GLCheck(glBindBuffer(GL_ARRAY_BUFFER, 0));
		GLCheck(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
	}

	GL_API::clearColor(DIVIDE_BLUE());
}

void GL_API::updateStateInternal(RenderStateBlock* block, bool force){
	assert(block != NULL);

   glRenderStateBlock* glBlock = static_cast<glRenderStateBlock*>(block);
   glRenderStateBlock* glCurrent = NULL;
   if(!force){
      glCurrent = static_cast<glRenderStateBlock*>(GFX_DEVICE._currentStateBlock);
   }
   glBlock->activate(glCurrent);
   _currentGLRenderStateBlock = glBlock;
}

void GL_API::getMatrix(const MATRIX_MODE& mode, mat4<GLfloat>& mat){
    Divide::GL::_queryMatrix(mode, mat);
}

void GL_API::getMatrix(const EXTENDED_MATRIX& mode, mat4<GLfloat>& mat){
    assert(mode != NORMAL_MATRIX /* || mode != ...*/);
    Divide::GL::_queryMatrix(mode, mat);
}

void GL_API::getMatrix(const EXTENDED_MATRIX& mode, mat3<GLfloat>& mat){
    assert(mode == NORMAL_MATRIX /* || mode = ...*/);
    Divide::GL::_queryMatrix(NORMAL_MATRIX,mat);
}

void GL_API::toggle2D(bool state){
	if(state == _2DRendering) return;

	_2DRendering = state;

	if(state){ //2D
		SET_STATE_BLOCK(_state2DRendering);
        Divide::GL::_matrixMode(PROJECTION_MATRIX);
        Divide::GL::_pushMatrix(); //1
        Divide::GL::_ortho(0,_cachedResolution.width,0,_cachedResolution.height,-1,1);
		Divide::GL::_matrixMode(VIEW_MATRIX);
		Divide::GL::_pushMatrix(); //2
        Divide::GL::_loadIdentity();
	}else{ //3D
        Divide::GL::_matrixMode(VIEW_MATRIX);
        Divide::GL::_popMatrix(); //2
        Divide::GL::_matrixMode(PROJECTION_MATRIX);
		Divide::GL::_popMatrix(); //1
		SET_PREVIOUS_STATE_BLOCK();
	}
}

///Update OpenGL light state
void GL_API::setLight(Light* const light){
	assert(light != NULL);
	GLubyte slot = light->getSlot();

    if(!light->getEnabled()){
      return;
    }
	
	U32 offset = slot * sizeof(light->getProperties());
	_uniformBufferObjects[Lights_UBO]->ChangeSubData(offset, 
												     sizeof(light->getProperties()),
													 (const GLvoid*)(&(light->getProperties())));

	LightType type = light->getLightType();

	glm::vec4 position(light->getPosition().x, 
					   light->getPosition().y, 
					   light->getPosition().z,
					   (type == LIGHT_TYPE_DIRECTIONAL) ? 0.0f : 1.0f);

	GLCheck(glLightfv(GL_LIGHT0+slot, GL_POSITION, &position.x));

	GLCheck(glLightfv(GL_LIGHT0+slot, GL_AMBIENT,  light->getVProperty(LIGHT_PROPERTY_AMBIENT)));
	GLCheck(glLightfv(GL_LIGHT0+slot, GL_DIFFUSE,  light->getVProperty(LIGHT_PROPERTY_DIFFUSE)));
	GLCheck(glLightfv(GL_LIGHT0+slot, GL_SPECULAR, light->getVProperty(LIGHT_PROPERTY_SPECULAR)));
	if(type == LIGHT_TYPE_SPOT){
		GLCheck(glLightfv(GL_LIGHT0+slot, GL_SPOT_DIRECTION, light->getDirection()));
		GLCheck(glLightf(GL_LIGHT0+slot, GL_SPOT_EXPONENT,light->getFProperty(LIGHT_PROPERTY_SPOT_EXPONENT)));
		GLCheck(glLightf(GL_LIGHT0+slot, GL_SPOT_CUTOFF, light->getFProperty(LIGHT_PROPERTY_SPOT_CUTOFF)));
	}
	if(type != LIGHT_TYPE_DIRECTIONAL){
		GLCheck(glLightf(GL_LIGHT0+slot, GL_CONSTANT_ATTENUATION,light->getFProperty(LIGHT_PROPERTY_CONST_ATT)));
		GLCheck(glLightf(GL_LIGHT0+slot, GL_LINEAR_ATTENUATION,light->getFProperty(LIGHT_PROPERTY_LIN_ATT)));
		GLCheck(glLightf(GL_LIGHT0+slot, GL_QUADRATIC_ATTENUATION,light->getFProperty(LIGHT_PROPERTY_QUAD_ATT)));
	}

}

void GL_API::updateProjectionMatrix(){
	if(!Divide::GL::_contextAvailable) return;
	size_t mat4Size = 16 * sizeof(F32);
	//Not using sizeof(glm::mat4) to make it easier to drop GLM if needed (not that I would) and use my own mat4 matrices for this
	_uniformBufferObjects[Matrices_UBO]->ChangeSubData(0, mat4Size, glm::value_ptr(Divide::GL::_projectionMatrix.top()));
}

void GL_API::updateViewMatrix(){
	if(!Divide::GL::_contextAvailable) return;
	size_t mat4Size = 16 * sizeof(F32);
	_uniformBufferObjects[Matrices_UBO]->ChangeSubData(mat4Size, mat4Size, glm::value_ptr(Divide::GL::_viewMatrix.top()));
}

//Setting _LookAt here for camera's or shadow projection
// -set the current matrix to GL_MODELVIEW
// -reset it to identity

void GL_API::lookAt(const vec3<F32>& eye, const vec3<F32>& target, const vec3<F32>& up){
	vec3<F32> viewDirection(eye-target);

	Divide::GL::_LookAt(glm::value_ptr(glm::lookAt(glm::vec3(eye.x, eye.y, eye.z),
		                                           glm::vec3(target.x, target.y, target.z),
												   glm::vec3(up.x, up.y, up.z))),
		                normalize(viewDirection));
}

void GL_API::lockMatrices(const MATRIX_MODE& setCurrentMatrix, bool lockView, bool lockProjection){
    if(lockProjection){
        Divide::GL::_matrixMode(PROJECTION_MATRIX);
        Divide::GL::_pushMatrix();
    }

    if(lockView){
        Divide::GL::_matrixMode(VIEW_MATRIX);
	    Divide::GL::_pushMatrix();
    }

    //This is such a cheap operation that no other checks are needed (even if we double set the VIEW_MATRIX)
    Divide::GL::_matrixMode(setCurrentMatrix);
}

void GL_API::releaseMatrices( const MATRIX_MODE& setCurrentMatrix, bool releaseView, bool releaseProjection){
    if(releaseProjection){
        Divide::GL::_matrixMode(PROJECTION_MATRIX);
        Divide::GL::_popMatrix();
    }

    if(releaseView){
        Divide::GL::_matrixMode(VIEW_MATRIX);
        Divide::GL::_popMatrix();
    }

    //This is such a cheap operation that no other checks are needed (even if we double set the VIEW_MATRIX)
    Divide::GL::_matrixMode(setCurrentMatrix);
}

//Setting ortho projection:
// -sets the current matrix to GL_PROJECTION
// -resets it to identity
// -sets an ortho perspective based on the input rect and limits
// -and sets the matrix mode back to GL_MODELVIEW
void GL_API::setOrthoProjection(const vec4<GLfloat>& rect, const vec2<GLfloat>& planes){
    Divide::GL::_ortho(rect.x, rect.y, rect.z, rect.w, planes.x, planes.y);
	Divide::GL::_matrixMode(VIEW_MATRIX);
}

//Setting perspective projection:
// -sets the current matrix to GL_PROJECTION
// -resets it to identity
// -sets a perspective projection based on the input FoV, aspect ration and limits
// -and sets the matrix mode back to GL_MODELVIEW
void GL_API::setPerspectiveProjection(GLfloat FoV,GLfloat aspectRatio, const vec2<GLfloat>& planes){
	Divide::GL::_perspective(FoV, aspectRatio, planes.x, planes.y);
	Divide::GL::_matrixMode(VIEW_MATRIX);
}

void GL_API::setAnaglyphFrustum(F32 camIOD, bool rightFrustum){
	ParamHandler& par = ParamHandler::getInstance();
	const vec2<GLfloat>& zPlanes = Frustum::getInstance().getZPlanes();
	GLfloat fov    = par.getParam<GLfloat>("runtime.verticalFOV");
	GLfloat ratio  = par.getParam<GLfloat>("runtime.aspectRatio");
	Divide::GL::_anaglyph(camIOD,(GLdouble)zPlanes.x, (GLdouble)zPlanes.y,ratio,fov,rightFrustum);
}

void GL_API::updateClipPlanes(){
	//ToDo: Hack because clearly, HW clipping is too complicated for some ATI cards ... -Ionut
	if(getGPUVendor() == GPU_VENDOR_AMD) 
		return;

	const PlaneList& list = GFX_DEVICE.getClippingPlanes();
	bool clipPlaneState = false;
	for(GLuint clipPlaneIndex = 0; clipPlaneIndex < list.size(); clipPlaneIndex++){
		clipPlaneState = list[clipPlaneIndex].active();
		if(clipPlaneState != _activeClipPlanes[clipPlaneIndex]){
			_activeClipPlanes[clipPlaneIndex] = clipPlaneState;
			if(clipPlaneState){
				GLCheck(glEnable(GL_CLIP_DISTANCE0 + clipPlaneIndex));
			}else{
				GLCheck(glDisable(GL_CLIP_DISTANCE0 + clipPlaneIndex));
			}
		}
	}

}

void GL_API::setActiveTextureUnit(GLuint unit,const bool force){
	if(_activeTextureUnit == unit && !force) 
		return; //< prevent double bind

    _activeTextureUnit = unit;
	GLCheck(glActiveTexture(GL_TEXTURE0 + unit));
}

void GL_API::setActiveVAO(GLuint id,const bool force){
	if(_activeVAOId == id && !force) 
		return; //<prevent double bind

    _activeVAOId = id;
    GLCheck(glBindVertexArray(id));
}

void GL_API::setActiveProgram(glShaderProgram* const program,const bool force){
	U32 newProgramId = (program != NULL) ? program->getId() : 0;
	U32 oldProgramId = (_activeShaderProgram != NULL) ? _activeShaderProgram->getId() : 0;
	if(oldProgramId == newProgramId && !force) 
		return; //<prevent double bind

	if(_activeShaderProgram != NULL) _activeShaderProgram->unbind(false);

	_activeShaderProgram = program;

	GLCheck(glUseProgram(newProgramId));
}

void GL_API::clearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a, bool force){
	if(!FLOAT_COMPARE(_prevClearColor.r,r ) ||
	   !FLOAT_COMPARE(_prevClearColor.g,g ) ||
	   !FLOAT_COMPARE(_prevClearColor.b,b ) ||
	   !FLOAT_COMPARE(_prevClearColor.a,a )){
		   _prevClearColor.set(r,g,b,a);
			GLCheck(glClearColor(r,g,b,a));
	}
}

static bool popViewport = false;
void GL_API::restoreViewport(){
	if(popViewport)
		Divide::GL::_viewport.pop();
	const vec4<U32>& prevViewport = Divide::GL::_viewport.top();
	GLCheck(glViewport(prevViewport.x, prevViewport.y, prevViewport.z, prevViewport.w));
}

vec4<U32> GL_API::setViewport(const vec4<U32>& viewport, bool force){
	bool updateGL = !viewport.compare(Divide::GL::_viewport.top());
	popViewport = !force;
	if(force)
		Divide::GL::_viewport.top() = viewport;
	else
		Divide::GL::_viewport.push(viewport);

	if(updateGL)
		GLCheck(glViewport(viewport.x,viewport.y,viewport.z,viewport.w));

	return Divide::GL::_viewport.top();
}

Commits for Divide-Framework/trunk/Source Code/Hardware/Video/OpenGL/GLStates.cpp

Diff revisions: vs.
Revision Author Commited Message
168 Diff Diff k1ngp1n picture k1ngp1n Sat 26 Oct, 2013 19:03:21 +0000

- Reworked the Camera class[[BR]]
— Now fully quaternion based [[BR]]
— Basic camera types added but not used yet (third person, first person, orbit) [[BR]]
- Cleaned up Material and Texture handling [[BR]]
- Added clipping plane support [[BR]]
— Similar to OpenGL fixed-function clip planes but fully shader driven [[BR]]
— Added a new class, “Plane”, that helps define clip planes [[BR]]
- Optimized the Singleton class to allow faster “getInstance” calls without performance penalties [[BR]]
-- “createInstance” must be called for each singleton class before usage. Or “gerOrCreateInstance” can be used, which is basically the former “getInstance” implementation [[BR]]
- Improved console logging by changing some heap allocations to stack and removing dependencies on the std::string class [[BR]]
- Added a lot of performance optimizations related to coding standards and redundant calculations [[BR]]
— e.g. Frustum AABB check didn’t need to recompute the AABB points as they were calculated already [[BR]]
— e.g. A vector did not need to be set to 0 on initialization as that is already it’s default state on creation [[BR]]
— e.g. Faster Framerate and Timing calculations by using less member variables that are not needed outsied of calling functions [[BR]]
- The SceneState now contains the SceneRenderState and is passed on to the SceneGraph’s update calls [[BR]]
- Better material export/import to/from XML format [[BR]]
- More bug fixes and cleanups [[BR]]

163 Diff Diff k1ngp1n picture k1ngp1n Thu 18 Jul, 2013 12:24:26 +0000

[Ionut] [[BR]]
- Fixed a rendering bug with the previous terrain rendering system (number of strips was not calculated correctly. had to add the primitive restart indices to the list as well) [[BR]]
- Fixed a rendering bug with the previous double sided Quad3D patch (water planes should not be double sided. The first parameter of the bool mask of the resource descriptor for water planes now controls whether the Quad will be single or double sided) [[BR]]
- NavMesh debug draw now uses a caching system for rendering instead of re-submitting the same geometry every frame [[BR]]
— Fixed a bug that prevented GLIM primitives to be cached if they had “canZombify” set to false [[BR]]
- Navigation meshes are now saved and loaded from file on creation. Delete the “nm” file from the navMeshes subfolder to recreate a navigation mesh for a scene. [[BR]]
- LightManager is now a FrameListener. ShadowMap generation is called with the “FramePreRenderEnd” event [[BR]]
- Terrain NavMesh uses the lowest LoD possible for now to improve creation times until all bugs are resolved [[BR]]
- Geometry importer now uses the “replace” operation for the base texture of objects [[BR]]
- glUniformBufferObjects are now a little less dependent on the GLM library [[BR]]
- Primitive restart is now toggled on or off at each VAO draw call [[BR]]
- Some GFXDevice inlining [[BR]]

155 Diff Diff k1ngp1n picture k1ngp1n Thu 27 Jun, 2013 15:02:43 +0000

[Ionut] [[BR]]
- Small matrix stack optimization [[BR]]

154 Diff Diff k1ngp1n picture k1ngp1n Sun 23 Jun, 2013 23:39:07 +0000

[Ionut] [[BR]]
- Changed math computation for: Matrix multiplication, Normal and Tangent calculation, Quaternion to Matrix conversion and more [[BR]]
- Removed useless UBO classes and extended the functionality of the base UBO class [[BR]]
- Changed lighting shaders to be more streamlined [[BR]]
- Added full shader based fog [[BR]]
— Added per-scene optional fog settings [[BR]]
- Re-enabled sorting by shader program for Mesh Delegate Bin (was set to Front to Back for debugging purposes and forgotten there) [[BR]]

152 k1ngp1n picture k1ngp1n Thu 20 Jun, 2013 14:23:31 +0000

[Ionut] [[BR]]
- New rendering pipeline (experimental and unstable): [[BR]]
— OpenGL 3.x clean context (glLight commands are still used for now, but UBO support is there, but not used yet) [[BR]]
— Custom matrix stacks and UBO based shader data (UBOs cause some link errors on some nVidia cards)[[BR]]
— Multi-threaded texture loading and shader compilation (mt shader compilation disabled for now. needs more tests) [[BR]]
— VAO rendering is self contained (no more enable/disable calls before a draw request) [[BR]]
— Render instance class used for rendering information [[BR]]
[[BR]]
- New unique ID generation base class: GUIDWrapper. Each class that derives from this, gets an application wide unique id on construction [[BR]]
- Added base code for an in-game editor using CEGUI [[BR]]
- Replaced some mutex locks with lockless data structures or atomic data [[BR]]
- CEGUI 0.8.2, FTGL-GL3 and GLIM 0.4 modified to support GLEW_MX if defined [[BR]]
- Dropped VS2008 support [[BR]]
- Networking library, server framework and ReCast files are now projects inside the Divide Framework solution [[BR]]
- New console command “setFov” that modifies the horizontal FoV value in realtime [[BR]]
- New console command “recompileShader” that reparses shader files, recompiles shaders and relinks shader programs who’s names match or contain the argument string [[BR]]
- New debug utility: axis angle drawn in corner of the screen if needed [[BR]]
- More ‘const’-s pasted around the code, more loop performance tweaks (allocations before a loop, tighter loops, etc), more utility member functions added to various classes [[BR]]