Project

General

Profile

« Previous | Next » 

Revision a4d59c0b

Added by Leszek Koltunski over 5 years ago

Massive: make the coordinate system agree with that of OpenGL (i.e. invert the Y axis).

View differences:

src/main/java/org/distorted/examples/stencil/StencilRenderer.java
36 36
import org.distorted.library.main.DistortedScreen;
37 37
import org.distorted.library.main.DistortedTexture;
38 38
import org.distorted.library.mesh.MeshCubes;
39
import org.distorted.library.mesh.MeshFlat;
40 39
import org.distorted.library.mesh.MeshBase;
40
import org.distorted.library.mesh.MeshQuad;
41 41
import org.distorted.library.type.Dynamic1D;
42 42
import org.distorted.library.type.Static1D;
43 43
import org.distorted.library.type.Static3D;
......
56 56
    private DistortedScreen mScreen;
57 57
    private DistortedTexture mCubeTex, mFloorTex, mFBOTex;
58 58
    private DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode;
59
    private MeshBase mCubeMesh, mQuad;
60
    private Static3D mCubeMove, mCubeScale, mFloorScale, mFloorMove, mFBOScale;
59
    private Static3D mMove, mScale, mRotCenter, mFBOScale;
61 60

  
62 61
///////////////////////////////////////////////////////////////////////////////////////////////////
63 62

  
......
87 86
      {
88 87
      mView = v;
89 88

  
90
      mCubeMesh = new MeshCubes(1,1,1);
91
      mQuad     = new MeshFlat(1,1);
89
      MeshBase cube = new MeshCubes(1,1,1);
90
      MeshBase quad = new MeshQuad();
92 91

  
93
      mCubeMove   = new Static3D(0,0,0);
94
      mCubeScale  = new Static3D(1,1,1);
95
      mFloorScale = new Static3D(1,1,1);
96
      mFloorMove  = new Static3D(0,0,0);
92
      mMove       = new Static3D(0,0,0);
93
      mScale      = new Static3D(1,1,1);
94
      mRotCenter  = new Static3D(0,0,0);
97 95
      mFBOScale   = new Static3D(1,1,1);
98 96

  
99 97
      mCubeTex   = new DistortedTexture(1,1);
100
      mFloorTex  = new DistortedTexture(1,1);
98
      mFloorTex  = new DistortedTexture(2,2);
101 99
      mFBOTex    = new DistortedTexture(1,1);
102 100

  
103 101
      DistortedEffects cube1Effects = new DistortedEffects();
......
105 103
      DistortedEffects floorEffects = new DistortedEffects();
106 104
      DistortedEffects FBOEffects   = new DistortedEffects();
107 105

  
108
      cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
109

  
110
      mCube1Node = new DistortedNode(mCubeTex ,cube1Effects,mCubeMesh );
111
      mCube2Node = new DistortedNode(mCubeTex ,cube2Effects,mCubeMesh );
112
      mFloorNode = new DistortedNode(mFloorTex,floorEffects,mQuad     );
113
      mFBONode   = new DistortedNode(mFBOTex  ,FBOEffects  ,mQuad     );
106
      mCube1Node = new DistortedNode(mCubeTex , cube1Effects, cube );
107
      mCube2Node = new DistortedNode(mCubeTex , cube2Effects, cube );
108
      mFloorNode = new DistortedNode(mFloorTex, floorEffects, quad );
109
      mFBONode   = new DistortedNode(mFBOTex  , FBOEffects  , quad );
114 110

  
115 111
      ///////////////// The Meat of this App - shamelessly ripped off https://open.gl/depthstencils ///////////////////////
116 112
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
......
142 138

  
143 139
      float cw = mCubeTex.getWidth();
144 140
      float ch = mCubeTex.getHeight();
145
      float cd = mCubeTex.getDepth(mCubeMesh);
146 141

  
147 142
      float fw = mFloorTex.getWidth();
148 143
      float fh = mFloorTex.getHeight();
149
      float fd = mFloorTex.getDepth(mQuad);
150

  
151
      Static3D cubeCenter = new Static3D(cw/2,ch  ,cd/2);
152
      Static3D floorCenter= new Static3D(fw/2,fh/2,fd/2);
153 144

  
154 145
      Static3D axisX = new Static3D(1,0,0);
155
      Static3D axisY = new Static3D(0,1,0);
156 146
      Static3D axisZ = new Static3D(0,0,1);
157 147

  
158 148
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
......
160 150
      rotDyn.add(new Static1D(  0));
161 151
      rotDyn.add(new Static1D(360));
162 152

  
163
      float angle = 30.0f;
164

  
165
      Static1D rotSt1 = new Static1D(angle);
166
      Static1D rotSt2 = new Static1D(angle-90.0f);
167

  
168
      MatrixEffectRotate rot1 = new MatrixEffectRotate(rotSt1, axisX, cubeCenter);
169
      MatrixEffectRotate rot2 = new MatrixEffectRotate(rotDyn, axisY, cubeCenter);
170
      MatrixEffectScale  scal = new MatrixEffectScale(mCubeScale);
171
      MatrixEffectMove   move = new MatrixEffectMove(mCubeMove);
153
      MatrixEffectScale  scale = new MatrixEffectScale(mScale);
154
      MatrixEffectMove   move1 = new MatrixEffectMove( new Static3D( (fw-cw)/2, (fh-ch)/2, 0) );
155
      MatrixEffectMove   move2 = new MatrixEffectMove( mMove );
156
      MatrixEffectRotate rotaX = new MatrixEffectRotate(new Static1D(-60.0f), axisX, mRotCenter);
157
      MatrixEffectRotate rotaZ = new MatrixEffectRotate(rotDyn, axisZ, mRotCenter);
172 158

  
173 159
      /////////////////////////////////////////////////////////////////////////////////////////////////////
174
      // Those Matrix effects, i.e. correct positioning of Objects on the Screen, is admittedly quite hard.
175
      // Figured out of experimentation.
160
      // First move the cube and its reflection to the middle of the floor. Then scale the floor, the cube
161
      // and its reflection. Then keep rotating all along the Z axis, tilt all, and finally move all to the
162
      // middle of the screen.
176 163
      /////////////////////////////////////////////////////////////////////////////////////////////////////
177
      // Upper cube
178
      cube1Effects.apply( move );
179
      cube1Effects.apply( scal );
180
      cube1Effects.apply( rot1 );
181
      cube1Effects.apply( rot2 );
182

  
164
      // The cube
165
      cube1Effects.apply( move2 );
166
      cube1Effects.apply( rotaX );
167
      cube1Effects.apply( rotaZ );
168
      cube1Effects.apply( scale );
169
      cube1Effects.apply( move1 );
183 170
      /////////////////////////////////////////////////////////////////////////////////////////////////////
184 171
      // Floor
185
      floorEffects.apply( new MatrixEffectMove(mFloorMove));
186
      floorEffects.apply( new MatrixEffectScale(mFloorScale) );
187
      floorEffects.apply( new MatrixEffectRotate(rotSt2, axisX, floorCenter) );
188
      floorEffects.apply( new MatrixEffectRotate(rotDyn, axisZ, floorCenter) );
189

  
172
      floorEffects.apply( move2 );
173
      floorEffects.apply( rotaX );
174
      floorEffects.apply( rotaZ );
175
      floorEffects.apply( scale );
190 176
      /////////////////////////////////////////////////////////////////////////////////////////////////////
191
      // Lower cube
192
      cube2Effects.apply( move );
193
      cube2Effects.apply( scal );
194
      cube2Effects.apply( rot1 );
195
      cube2Effects.apply( rot2 );
196
      cube2Effects.apply( new MatrixEffectScale(new Static3D(1,-1,1)) );
197
      cube2Effects.apply( new MatrixEffectMove(new Static3D( 0, -2*ch , 0)) );
177
      // Reflection
178
      cube2Effects.apply( move2 );
179
      cube2Effects.apply( rotaX );
180
      cube2Effects.apply( rotaZ );
181
      cube2Effects.apply( scale );
182
      cube2Effects.apply( move1 );
183
      cube2Effects.apply( new MatrixEffectScale(new Static3D(1,1,-1)) );
184
      cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
198 185

  
199 186
      /////////////////////////////////////////////////////////////////////////////////////////////////////
200 187
      // Framebuffer
......
214 201
      {
215 202
      float cw = mCubeTex.getWidth();
216 203
      float ch = mCubeTex.getHeight();
217
      float cd = mCubeTex.getDepth(mCubeMesh);
218 204

  
219 205
      float fw = mFloorTex.getWidth();
220 206
      float fh = mFloorTex.getHeight();
221
      float fd = mFloorTex.getDepth(mQuad);
222 207

  
223 208
      float bw = mFBOTex.getWidth();
224 209
      float bh = mFBOTex.getHeight();
225 210

  
226
      float cubeScale = 0.4f*(width>height ? height/ch:width/cw);
227
      float floorScale= 0.8f*(width>height ? height/fh:width/fw);
211
      float scale = 0.4f*(width>height ? height/ch:width/cw);
228 212

  
229
      mCubeMove.set((width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2 );
230
      mCubeScale.set(cubeScale,cubeScale,cubeScale);
231
      mFloorMove.set((width-floorScale*fw)/2 ,height/2-floorScale*fh/2, floorScale*fd/2 );
232
      mFloorScale.set(floorScale,floorScale,floorScale);
213
      mRotCenter.set(scale*fw/2,scale*fh/2,0);
214
      mScale.set(scale,scale,scale);
215
      mMove.set((width-scale*fw)/2 , (height-scale*fh)/2, 0 );
233 216
      mFBOScale.set((float)width/bw, (float)height/bh, 1.0f );
234 217

  
235
      mFBONode.resize( (int)((float)width/bw), (int)((float)height/bh) );
218
      mFBONode.resize(width,height);
236 219
      mScreen.resize( width,height);
237 220
      }
238 221

  

Also available in: Unified diff