VPS
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
#include "Headers/Water.h"

#include "Core/Headers/ParamHandler.h"
#include "Core/Math/Headers/Transform.h"
#include "Managers/Headers/SceneManager.h"
#include "Hardware/Video/Headers/RenderStateBlock.h"
#include "Geometry/Material/Headers/Material.h"

namespace Divide {

WaterPlane::WaterPlane() : SceneNode(TYPE_WATER), 
                           Reflector(TYPE_WATER_SURFACE,vec2<U16>(1024,1024)),
                           _plane(nullptr), 
                           _waterLevel(0), 
                           _waterDepth(0),
                           _refractionPlaneID(ClipPlaneIndex_PLACEHOLDER), 
                           _reflectionPlaneID(ClipPlaneIndex_PLACEHOLDER), 
                           _reflectionRendering(false), 
                           _refractionRendering(false), 
                           _refractionTexture(nullptr), 
                           _dirty(true), 
                           _paramsDirty(true), 
                           _cameraUnderWater(false), 
                           _cameraMgr(Application::getInstance().getKernel()->getCameraMgr())
{
    //Set water plane to be single-sided
    P32 quadMask;
    quadMask.i = 0;
    quadMask.b.b0 = true;

    setParams(50.0f, vec2<F32>(10.0f, 10.0f), vec2<F32>(0.1f, 0.1f), 0.34f);

    ResourceDescriptor waterPlane("waterPlane");
    waterPlane.setFlag(true); //No default material
    waterPlane.setBoolMask(quadMask);
    _plane = CreateResource<Quad3D>(waterPlane);
    _farPlane = 2.0f * GET_ACTIVE_SCENE()->state().getRenderState().getCameraConst().getZPlanes().y;
    _plane->setCorner( Quad3D::TOP_LEFT, vec3<F32>( -_farPlane * 1.5f, 0, -_farPlane * 1.5f ) );
    _plane->setCorner( Quad3D::TOP_RIGHT, vec3<F32>( _farPlane * 1.5f, 0, -_farPlane * 1.5f ) );
    _plane->setCorner( Quad3D::BOTTOM_LEFT, vec3<F32>( -_farPlane * 1.5f, 0, _farPlane * 1.5f ) );
    _plane->setCorner( Quad3D::BOTTOM_RIGHT, vec3<F32>( _farPlane * 1.5f, 0, _farPlane * 1.5f ) );
    _plane->setNormal( Quad3D::CORNER_ALL, WORLD_Y_AXIS );
    _plane->renderState().setDrawState( false );
    //The water doesn't cast shadows, doesn't need ambient occlusion and doesn't have real "depth"
    renderState().addToDrawExclusionMask( SHADOW_STAGE );
    PRINT_FN( Locale::get( "REFRACTION_INIT_FB" ), _resolution.x, _resolution.y );
    SamplerDescriptor refractionSampler;
    refractionSampler.setWrapMode( TEXTURE_CLAMP_TO_EDGE );
    refractionSampler.toggleMipMaps( false );

    TextureDescriptor refractionDescriptor( TEXTURE_2D, RGBA8, UNSIGNED_BYTE ); //Less precision for reflections
    refractionDescriptor.setSampler( refractionSampler );

    _refractionTexture = GFX_DEVICE.newFB();
    _refractionTexture->AddAttachment( refractionDescriptor, TextureDescriptor::Color0 );
    _refractionTexture->toggleDepthBuffer( true );
    _refractionTexture->Create( _resolution.x, _resolution.y );
}

void WaterPlane::postLoad(SceneGraphNode* const sgn){
    sgn->addNode(_plane);
    SceneNode::postLoad(sgn);
}

bool WaterPlane::computeBoundingBox(SceneGraphNode* const sgn){
    BoundingBox& bb = sgn->getBoundingBox();

    if ( bb.isComputed() ) {
        return true;
    }
    SceneGraphNode* planeSGN = sgn->getChildren()[0];
    _waterLevel = GET_ACTIVE_SCENE()->state().getWaterLevel();
    _waterDepth = GET_ACTIVE_SCENE()->state().getWaterDepth();
    planeSGN->getComponent<PhysicsComponent>()->setPositionY(_waterLevel);
    bb.set(vec3<F32>(-_farPlane,_waterLevel - _waterDepth, -_farPlane), vec3<F32>(_farPlane, _waterLevel, _farPlane));
    planeSGN->getBoundingBox().Add(bb);
    PRINT_FN(Locale::get("WATER_CREATE_DETAILS_1"), bb.getMax().y)
    PRINT_FN(Locale::get("WATER_CREATE_DETAILS_2"), bb.getMin().y);
    _dirty = true;
    return SceneNode::computeBoundingBox(sgn);
}

bool WaterPlane::unload(){
    bool state = false;
    state = SceneNode::unload();
    return state;
}

void WaterPlane::setParams(F32 shininess, const vec2<F32>& noiseTile, const vec2<F32>& noiseFactor, F32 transparency){
    _shininess = shininess;
    _noiseTile = noiseTile;
    _noiseFactor = noiseFactor;
    _transparency = transparency;
    _paramsDirty = true;
}

void WaterPlane::sceneUpdate(const U64 deltaTime, SceneGraphNode* const sgn, SceneState& sceneState){
    _cameraUnderWater = isPointUnderWater(sceneState.getRenderState().getCamera().getEye());
    if(_dirty){
       sgn->getBoundingSphere().fromBoundingBox(sgn->getBoundingBoxConst());
       _dirty = false;
    }
    if (_paramsDirty){
        RenderingComponent* rComp = sgn->getComponent<RenderingComponent>();
        ShaderProgram* shader = rComp->getMaterialInstance()->getShaderInfo(FINAL_STAGE).getProgram();
        shader->Uniform("_waterShininess", _shininess);
        shader->Uniform("_noiseFactor", _noiseFactor);
        shader->Uniform("_noiseTile", _noiseTile);
        shader->Uniform("_transparencyBias", _transparency);
        _paramsDirty = false;
    }
}

bool WaterPlane::onDraw(SceneGraphNode* const sgn, const RenderStage& currentStage){
    const Quaternion<F32>& orientation = sgn->getComponent<PhysicsComponent>()->getOrientation();
    if ( !_orientation.compare( orientation )) {
        _orientation.set( orientation );
        updatePlaneEquation();
    }
    return true;
}

void WaterPlane::postDraw(SceneGraphNode* const sgn, const RenderStage& currentStage){
}

void WaterPlane::getDrawCommands(SceneGraphNode* const sgn, 
                                 const RenderStage& currentRenderStage, 
                                 SceneRenderState& sceneRenderState, 
                                 vectorImpl<GenericDrawCommand>& drawCommandsOut) {

    bool depthPass = GFX_DEVICE.isCurrentRenderStage(DEPTH_STAGE);
    RenderingComponent* const renderable = sgn->getComponent<RenderingComponent>();
    assert(renderable != nullptr);

    ShaderProgram* drawShader = renderable->getDrawShader(depthPass ? Z_PRE_PASS_STAGE : FINAL_STAGE);
    drawShader->Uniform("underwater", _cameraUnderWater);
    GenericDrawCommand cmd(TRIANGLE_STRIP, 0, 0);
    cmd.renderWireframe(renderable->renderWireframe());
    cmd.stateHash(renderable->getMaterialInstance()->getRenderStateBlock(FINAL_STAGE));
    cmd.shaderProgram(drawShader);
    cmd.sourceBuffer(_plane->getGeometryVB());
    drawCommandsOut.push_back(cmd);
}

void WaterPlane::render(SceneGraphNode* const sgn, 
                        const SceneRenderState& sceneRenderState, 
                        const RenderStage& currentRenderStage) {

    if (!_plane->onDraw(nullptr, currentRenderStage)) {
        return;
    }
    if (!GFX_DEVICE.isCurrentRenderStage(DEPTH_STAGE)){
        _reflectedTexture->Bind(1);
        if ( !_cameraUnderWater ) {
            _refractionTexture->Bind( 2 );
        }
    }

    GFX_DEVICE.submitRenderCommand(sgn->getComponent<RenderingComponent>()->getDrawCommands());
}

bool WaterPlane::getDrawState(const RenderStage& currentStage) {
    // Wait for the Reflector to update
    if (!_createdFB) {
        return false;
    }

    // Make sure we are not drawing our self unless this is desired
    if((currentStage == REFLECTION_STAGE || _reflectionRendering || _refractionRendering) && !_updateSelf) {
    	return false;
    }

    // Else, process normal exclusion
    return SceneNode::getDrawState(currentStage);
}

/// update water refraction
void WaterPlane::updateRefraction(){
	if (_cameraUnderWater) {
		return;
	}
    // Early out check for render callback
	if (!_renderCallback) {
		return;
	}
    _refractionRendering = true;
    // If we are above water, process the plane's reflection. If we are below, we render the scene normally
    RenderStage prevRenderStage = GFX_DEVICE.setRenderStage(FINAL_STAGE);
	GFX_DEVICE.toggleClipPlane(_refractionPlaneID, true);
    _cameraMgr.getActiveCamera()->renderLookAt();
    // bind the refractive texture
    _refractionTexture->Begin(Framebuffer::defaultPolicy());
        // render to the reflective texture
        _refractionCallback();
    _refractionTexture->End();

	GFX_DEVICE.toggleClipPlane(_refractionPlaneID, false);
    GFX_DEVICE.setRenderStage(prevRenderStage);

    _refractionRendering = false;
}

/// Update water reflections
void WaterPlane::updateReflection(){
    // Early out check for render callback
    if (_renderCallback){
        //ToDo: this will cause problems later with multiple reflectors. Fix it! -Ionut
        _reflectionRendering = true;
    
        RenderStage prevRenderStage = GFX_DEVICE.setRenderStage(_cameraUnderWater ? FINAL_STAGE : REFLECTION_STAGE);
		GFX_DEVICE.toggleClipPlane(_reflectionPlaneID, true);

        _cameraUnderWater ? _cameraMgr.getActiveCamera()->renderLookAt() :
                            _cameraMgr.getActiveCamera()->renderLookAtReflected(getReflectionPlane());

        _reflectedTexture->Begin(Framebuffer::defaultPolicy());
            _renderCallback(); //< render to the reflective texture
        _reflectedTexture->End();

		GFX_DEVICE.toggleClipPlane(_reflectionPlaneID, false);
        GFX_DEVICE.setRenderStage(prevRenderStage);
    
        _reflectionRendering = false;
    }
    updateRefraction();
}

void WaterPlane::updatePlaneEquation(){
    vec3<F32> reflectionNormal( _orientation * WORLD_Y_AXIS );
    reflectionNormal.normalize();
    vec3<F32> refractionNormal( _orientation * WORLD_Y_NEG_AXIS );
    refractionNormal.normalize();
    _reflectionPlane.set(reflectionNormal, -_waterLevel);
    _reflectionPlane.active(false);
    _refractionPlane.set(refractionNormal, -_waterLevel);
    _refractionPlane.active(false);
    _reflectionPlaneID = CLIP_PLANE_0;
    _refractionPlaneID = CLIP_PLANE_1;

    GFX_DEVICE.setClipPlane(_reflectionPlaneID, _reflectionPlane);
    GFX_DEVICE.setClipPlane(_refractionPlaneID, _refractionPlane);
    
    _dirty = true;
}

void WaterPlane::previewReflection(){
#   ifdef _DEBUG
        if (_previewReflection) {
            F32 height = _resolution.y * 0.333f;
            _refractionTexture->Bind();
            vec4<I32> viewport(_resolution.x  * 0.333f, 
                               Application::getInstance().getResolution().y - height, 
                               _resolution.x  * 0.666f, 
                               height);
			GFX_DEVICE.renderInViewport(viewport,
                                        DELEGATE_BIND((void(GFXDevice::*)(U32, size_t, ShaderProgram* const))
                                                      &GFXDevice::drawPoints,
                                                      &GFX_DEVICE,
                                                      1,
                                                      GFX_DEVICE.getDefaultStateBlock(true),
                                                      _previewReflectionShader));
        }
#   endif
    Reflector::previewReflection();
}

};

Commits for Divide-Framework/trunk/Source Code/Environment/Water/Water.cpp

Diff revisions: vs.
Revision Author Commited Message
331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

328 Diff Diff IonutCava picture IonutCava Sat 04 Oct, 2014 18:03:36 +0000

[Ionut]
- Added per SceneGraphNode material instances. (Cloned from the SceneNode’s material)
- Improved shader loading for materials

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

323 Diff Diff IonutCava picture IonutCava Thu 11 Sep, 2014 20:58:50 +0000

[Ionut]
- Reworked GFXDevice API initialization system
- Added a separate map for boolean values in ParamHandler
- More cleanups in GFXDevice class
- Fixed an infinite loop issue in Task class

322 Diff Diff IonutCava picture IonutCava Sat 06 Sep, 2014 20:33:47 +0000

[Ionut]
- Refactored most of Boost related code to be C++11 based
— Boost is only used for ThreadPool, Mutex handling, XML parsing and networking (ASIO)
— Function binds, threads, regex, NonCopyable base, Hashing algorithms and more are now using C++11 libraries
- Replaced all FOR_EACH calls with standard, range, “for” calls

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

308 Diff Diff IonutCava picture IonutCava Sat 16 Aug, 2014 19:49:31 +0000

[Ionut]
- Moved Transform from SceneGraphNode to PhysicsComponent and modified BoundingBox update system based on transform update
- Added PhysicsAsset to PhysicsComponent for interfacing with PshysicsAPIs (e.g. PhysXActor inherits PhysicsAsset)
- PhysicsAsset keeps track of the parent PhysicsComponent to have direct access to a node’s transform
- Rewrote GUIConsole to only print items when visible, and use a single circular buffer for entries.
- Updated GUI input system to match the interface that both Kernel and SceneManager share (should probably abstract all the input methods in a class and have Kernel, SceneManager, Scene and GUI inherit from that. In next commit.)
- Updated CEGUI to probably match the OIS states(mouse, keyboard, etc) and properly inject time inputs.

307 Diff Diff IonutCava picture IonutCava Tue 12 Aug, 2014 19:45:12 +0000

[Ionut]
- More editor work:
— Per object skeleton rendering, bounding box rendering and shadow map toggling
— Full screen depth preview toggle
— Text entry based transforms

299 IonutCava picture IonutCava Mon 04 Aug, 2014 22:09:22 +0000

[Ionut]
- Disabled shader binary dump/load on non-nVidia GPUs (caused issues)
- More code cleanup/comments/formating (GFXDevice.cpp)