Project

General

Profile

« Previous | Next » 

Revision c90b9e01

Added by Leszek Koltunski almost 8 years ago

Progress with getting Mesh'es bounding rectangle. Still doesn't quite work yet (visible : 'Cubes' app with a 0111 Mesh)

View differences:

src/main/java/org/distorted/library/Distorted.java
113 113
    {
114 114
    final Resources resources = context.getResources();
115 115

  
116
    final InputStream mainVertexStream   = resources.openRawResource(R.raw.main_vertex_shader);
117
    final InputStream mainFragmentStream = resources.openRawResource(R.raw.main_fragment_shader);
118

  
119
    String mainVertexHeader= ("#version 100\n#define NUM_VERTEX "  + DistortedEffects.getMaxVertex()+"\n");
120

  
121
    for(EffectNames name: EffectNames.values() )
122
      {
123
      if( name.getType()== EffectTypes.VERTEX)
124
        mainVertexHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
125
      }
126

  
127
    String mainFragmentHeader= ("#version 100\n#define NUM_FRAGMENT "  + DistortedEffects.getMaxFragment()+"\n");
128

  
129
    for(EffectNames name: EffectNames.values() )
130
      {
131
      if( name.getType()== EffectTypes.FRAGMENT)
132
        mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
133
      }
134

  
135
    DistortedProgram mainProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
136
    int mainProgramH = mainProgram.getProgramHandle();
137

  
138 116
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
139 117
    GLES20.glEnable(GLES20.GL_BLEND);
140 118
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
......
142 120
    GLES20.glCullFace(GLES20.GL_BACK);
143 121
    GLES20.glFrontFace(GLES20.GL_CW);
144 122

  
145
    EffectQueueFragment.getUniforms(mainProgramH);
146
    EffectQueueVertex.getUniforms(mainProgramH);
147
    EffectQueueMatrix.getUniforms(mainProgramH);
148
    DistortedTexture.getUniforms(mainProgramH);
123
    DistortedEffects.createProgram(resources);
124
    EffectQueuePostprocess.createProgram(resources);
149 125

  
150
    final InputStream postVertexStream   = resources.openRawResource(R.raw.post_vertex_shader);
151
    final InputStream postFragmentStream = resources.openRawResource(R.raw.post_fragment_shader);
152

  
153
    String postFragmentHeader= ("#version 100\n#define NUM_POSTPROCESS "  + DistortedEffects.getMaxPostprocess()+"\n");
154

  
155
    for(EffectNames name: EffectNames.values() )
156
      {
157
      if( name.getType()== EffectTypes.POSTPROCESS)
158
        postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
159
      }
160

  
161
    DistortedProgram postProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
162
    int postProgramH = postProgram.getProgramHandle();
163

  
164
    EffectQueuePostprocess.getUniforms(postProgramH);
165

  
166
    DistortedEffects.setProgram(mainProgram);
167
    EffectQueuePostprocess.setProgram(postProgram);
168 126
    DistortedTree.reset();
169 127
    EffectMessageSender.startSending();
128

  
129
    mInitialized = true;
170 130
    }
171 131

  
172 132
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedEffects.java
19 19

  
20 20
package org.distorted.library;
21 21

  
22
import android.content.res.Resources;
22 23
import android.opengl.GLES20;
23 24
import android.opengl.Matrix;
24 25

  
25 26
import org.distorted.library.message.EffectListener;
26 27
import org.distorted.library.program.DistortedProgram;
28
import org.distorted.library.program.FragmentCompilationException;
29
import org.distorted.library.program.FragmentUniformsException;
30
import org.distorted.library.program.LinkingException;
31
import org.distorted.library.program.VertexCompilationException;
32
import org.distorted.library.program.VertexUniformsException;
27 33
import org.distorted.library.type.Data1D;
28 34
import org.distorted.library.type.Data2D;
29 35
import org.distorted.library.type.Data3D;
......
31 37
import org.distorted.library.type.Data5D;
32 38
import org.distorted.library.type.Static3D;
33 39

  
40
import java.io.InputStream;
34 41
import java.nio.ByteBuffer;
35 42
import java.nio.ByteOrder;
36 43
import java.nio.FloatBuffer;
......
49 56

  
50 57
  private static DistortedProgram mProgram;
51 58

  
59
  /// DEBUG ONLY /////
60
  private static DistortedProgram mDebugProgram;
61
  private static final FloatBuffer mQuadPositions;
62

  
63
  static
64
    {
65
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
66
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
67
    mQuadPositions.put(positionData).position(0);
68
    }
69

  
70
  private static int mObjDH;
71
  private static int mMVPMatrixH;
72
  /// END DEBUG //////
73

  
52 74
  private static float[] mMVPMatrix = new float[16];
53 75
  private static float[] mTmpMatrix = new float[16];
54 76

  
......
66 88

  
67 89
///////////////////////////////////////////////////////////////////////////////////////////////////
68 90

  
69
  static void setProgram(DistortedProgram p)
91
  static void createProgram(Resources resources)
92
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
70 93
    {
71
    mProgram = p;
94
    final InputStream mainVertexStream   = resources.openRawResource(R.raw.main_vertex_shader);
95
    final InputStream mainFragmentStream = resources.openRawResource(R.raw.main_fragment_shader);
96

  
97
    String mainVertexHeader= ("#version 100\n#define NUM_VERTEX "  + getMaxVertex()+"\n");
98

  
99
    for(EffectNames name: EffectNames.values() )
100
      {
101
      if( name.getType()== EffectTypes.VERTEX)
102
        mainVertexHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
103
      }
104

  
105
    String mainFragmentHeader= ("#version 100\n#define NUM_FRAGMENT "  + getMaxFragment()+"\n");
106

  
107
    for(EffectNames name: EffectNames.values() )
108
      {
109
      if( name.getType()== EffectTypes.FRAGMENT)
110
        mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
111
      }
112

  
113
    mProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
114

  
115
    int mainProgramH = mProgram.getProgramHandle();
116
    EffectQueueFragment.getUniforms(mainProgramH);
117
    EffectQueueVertex.getUniforms(mainProgramH);
118
    EffectQueueMatrix.getUniforms(mainProgramH);
119
    DistortedTexture.getUniforms(mainProgramH);
120

  
121
    // DEBUG ONLY //////////////////////////////////////
122
    final InputStream debugVertexStream   = resources.openRawResource(R.raw.test_vertex_shader);
123
    final InputStream debugFragmentStream = resources.openRawResource(R.raw.test_fragment_shader);
124

  
125
    mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, "#version 100\n", "#version 100\n");
126

  
127
    int debugProgramH = mDebugProgram.getProgramHandle();
128
    mObjDH      = GLES20.glGetUniformLocation( debugProgramH, "u_objD");
129
    mMVPMatrixH = GLES20.glGetUniformLocation( debugProgramH, "u_MVPMatrix");
130
    // END DEBUG  //////////////////////////////////////
72 131
    }
73 132

  
74 133
///////////////////////////////////////////////////////////////////////////////////////////////////
......
121 180
    }
122 181

  
123 182
///////////////////////////////////////////////////////////////////////////////////////////////////
124
   
183
// DEBUG ONLY
184

  
185
  private void displayBoundingRect(float halfX, float halfY, float halfZ, DistortedFramebuffer df, float[] mvp, float[] vertices)
186
    {
187
    int len  = vertices.length/3;
188
    int minx = Integer.MAX_VALUE;
189
    int maxx = Integer.MIN_VALUE;
190
    int miny = Integer.MAX_VALUE;
191
    int maxy = Integer.MIN_VALUE;
192
    int wx,wy;
193

  
194
    float x,y,z, X,Y,W, ndcX,ndcY;
195

  
196
    for(int i=0; i<len; i++)
197
      {
198
      x = 2*halfX*vertices[3*i  ];
199
      y = 2*halfY*vertices[3*i+1];
200
      z = 2*halfZ*vertices[3*i+2];
201

  
202
      X = mvp[ 0]*x + mvp[ 4]*y + mvp[ 8]*z + mvp[12];
203
      Y = mvp[ 1]*x + mvp[ 5]*y + mvp[ 9]*z + mvp[13];
204
      W = mvp[ 3]*x + mvp[ 7]*y + mvp[11]*z + mvp[15];
205

  
206
      ndcX = X/W;
207
      ndcY = Y/W;
208

  
209
      wx = (int)(df.mWidth *(ndcX+1)/2);
210
      wy = (int)(df.mHeight*(ndcY+1)/2);
211

  
212
      if( wx<minx ) minx = wx;
213
      if( wx>maxx ) maxx = wx;
214
      if( wy<miny ) miny = wy;
215
      if( wy>maxy ) maxy = wy;
216
      }
217

  
218
    mDebugProgram.useProgram();
219
    df.setAsOutput();
220

  
221
    Matrix.setIdentityM(mTmpMatrix, 0);
222
    Matrix.translateM(mTmpMatrix, 0, -df.mWidth/2, df.mHeight/2, -df.mDistance);
223

  
224
    Matrix.translateM(mTmpMatrix, 0, minx, -df.mHeight+maxy, 0.0f);
225
    Matrix.scaleM(mTmpMatrix, 0, (float)(maxx-minx)/(2*halfX), (float)(maxy-miny)/(2*halfY), 1.0f);
226

  
227
    Matrix.translateM(mTmpMatrix, 0, halfX,-halfY, 0);
228
    Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mTmpMatrix, 0);
229

  
230
    GLES20.glUniform2f( mObjDH , 2*halfX, 2*halfY);
231
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
232

  
233
    GLES20.glVertexAttribPointer(mDebugProgram.mAttribute[0], 2, GLES20.GL_FLOAT, false, 0, mQuadPositions);
234
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
235
    }
236

  
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

  
125 239
  void drawPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedFramebuffer df, long currTime)
126 240
    {
127 241
    mM.compute(currTime);
......
174 288
        mP.render(df.mWidth, df.mHeight, mMVPMatrix, df);
175 289
        }
176 290
      }
291

  
292
    /// DEBUG ONLY //////
293
    displayBoundingRect(halfInputH, halfInputW, halfZ, df, mM.getMVP(), mesh.getBoundingVertices() );
294
    /// END DEBUG ///////
177 295
    }
178 296

  
179 297
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectQueuePostprocess.java
19 19

  
20 20
package org.distorted.library;
21 21

  
22
import android.content.res.Resources;
22 23
import android.opengl.GLES20;
23 24

  
24 25
import org.distorted.library.message.EffectMessage;
25 26
import org.distorted.library.program.DistortedProgram;
27
import org.distorted.library.program.FragmentCompilationException;
28
import org.distorted.library.program.FragmentUniformsException;
29
import org.distorted.library.program.LinkingException;
30
import org.distorted.library.program.VertexCompilationException;
31
import org.distorted.library.program.VertexUniformsException;
26 32
import org.distorted.library.type.Data1D;
27 33
import org.distorted.library.type.Data2D;
28 34
import org.distorted.library.type.Dynamic1D;
......
30 36
import org.distorted.library.type.Static1D;
31 37
import org.distorted.library.type.Static2D;
32 38

  
39
import java.io.InputStream;
33 40
import java.nio.ByteBuffer;
34 41
import java.nio.ByteOrder;
35 42
import java.nio.FloatBuffer;
......
75 82
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
76 83
    }
77 84

  
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

  
87
  static void createProgram(Resources resources)
88
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
89
    {
90
    final InputStream postVertexStream   = resources.openRawResource(R.raw.post_vertex_shader);
91
    final InputStream postFragmentStream = resources.openRawResource(R.raw.post_fragment_shader);
92

  
93
    String postFragmentHeader= ("#version 100\n#define NUM_POSTPROCESS "  + DistortedEffects.getMaxPostprocess()+"\n");
94

  
95
    for(EffectNames name: EffectNames.values() )
96
      {
97
      if( name.getType()== EffectTypes.POSTPROCESS)
98
        postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
99
      }
100

  
101
    mProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
102

  
103
    int postProgramH = mProgram.getProgramHandle();
104
    getUniforms(postProgramH);
105
    }
106

  
78 107
///////////////////////////////////////////////////////////////////////////////////////////////////
79 108

  
80 109
  static void getUniforms(int mProgramH)
......
86 115
    mMVPMatrixH = GLES20.glGetUniformLocation(mProgramH, "u_MVPMatrix");
87 116
    }
88 117

  
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

  
91
  static void setProgram(DistortedProgram p)
92
    {
93
    mProgram = p;
94
    }
95

  
96 118
///////////////////////////////////////////////////////////////////////////////////////////////////
97 119
  
98 120
  synchronized void compute(long currTime) 
src/main/res/raw/main_fragment_shader.glsl
105 105
    }
106 106
#endif
107 107

  
108
  gl_FragColor = vec4(pixel.rgb * v_Normal.z, pixel.a);
108
  gl_FragColor = vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a);
109 109
  }
src/main/res/raw/test_fragment_shader.glsl
1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of Distorted.                                                          //
5
//                                                                                          //
6
// Distorted is free software: you can redistribute it and/or modify                        //
7
// it under the terms of the GNU General Public License as published by                     //
8
// the Free Software Foundation, either version 2 of the License, or                        //
9
// (at your option) any later version.                                                      //
10
//                                                                                          //
11
// Distorted is distributed in the hope that it will be useful,                             //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
14
// GNU General Public License for more details.                                             //
15
//                                                                                          //
16
// You should have received a copy of the GNU General Public License                        //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
precision lowp float;
21

  
22
//////////////////////////////////////////////////////////////////////////////////////////////
23

  
24
void main()
25
  {
26
  gl_FragColor = vec4(1.0,0.0,0.0,0.2);
27
  }
src/main/res/raw/test_vertex_shader.glsl
1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of Distorted.                                                          //
5
//                                                                                          //
6
// Distorted is free software: you can redistribute it and/or modify                        //
7
// it under the terms of the GNU General Public License as published by                     //
8
// the Free Software Foundation, either version 2 of the License, or                        //
9
// (at your option) any later version.                                                      //
10
//                                                                                          //
11
// Distorted is distributed in the hope that it will be useful,                             //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
14
// GNU General Public License for more details.                                             //
15
//                                                                                          //
16
// You should have received a copy of the GNU General Public License                        // 
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
uniform vec2 u_objD;             // object width X object height.
21
                                 // point (0,0) is the center of the object
22
uniform mat4 u_MVPMatrix;        // the combined model/view/projection matrix.
23

  
24
attribute vec2 a_Position;       // Per-vertex position information we will pass in.
25

  
26
//////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
void main()
29
  {
30
  gl_Position = u_MVPMatrix*vec4( u_objD*a_Position, 0.0, 1.0);
31
  }

Also available in: Unified diff