@@ -16,6 +16,7 @@ namespace SGWW
16
16
{
17
17
public class GLObject
18
18
{
19
+ protected Renderer renderer ;
19
20
private Shader shader ;
20
21
private VBO vertexVBO ;
21
22
private VBO normalVBO ;
@@ -48,30 +49,53 @@ public class GLObject
48
49
/// <param name="normalFile">raw/normal file name_objnormal</param>
49
50
/// <param name="textureFile">raw/UVMap file name_objtexture</param>
50
51
/// <param name="textureImage">drawable/file name (use .PNG files with alpha chanel is allowed)</param>
51
- public GLObject ( Context context , string vertextShader , string fragmentShader , string vertexFile , string normalFile , string textureFile , string textureImage )
52
+ public GLObject ( Renderer renderer , string vertextShader , string fragmentShader , string vertexFile , string normalFile , string textureFile , string textureImage )
52
53
{
54
+ this . renderer = renderer ;
53
55
//Loading vertex and fragment shader source codes from resource files, than compile and link it
54
- shader = new Shader ( context , vertextShader , fragmentShader ) ;
56
+ shader = new Shader ( renderer . context , vertextShader , fragmentShader ) ;
55
57
compileResult = shader . Compile ( ) ;
56
58
//Loading vertexes from resource file to VBO
57
- vertexVBO = new VBO ( context , vertexFile ) ;
59
+ vertexVBO = new VBO ( renderer . context , vertexFile ) ;
58
60
//Loading UVMap from resource file to VBO
59
- textureVBO = new VBO ( context , textureFile ) ;
61
+ textureVBO = new VBO ( renderer . context , textureFile ) ;
60
62
//Loading normales from resource file to VBO
61
- normalVBO = new VBO ( context , normalFile ) ;
63
+ normalVBO = new VBO ( renderer . context , normalFile ) ;
62
64
//Loading texture image file
63
- texture = new Texture ( context , textureImage ) ;
65
+ texture = new Texture ( renderer . context , textureImage ) ;
64
66
//Ask android to run RAM garbage cleaner
65
67
System . GC . Collect ( ) ;
66
68
}
67
69
70
+ public GLObject ( Renderer renderer , string vertextShader , string fragmentShader , string objFile , string textureImage )
71
+ {
72
+ this . renderer = renderer ;
73
+ //Loading vertex and fragment shader source codes from resource files, than compile and link it
74
+ shader = new Shader ( renderer . context , vertextShader , fragmentShader ) ;
75
+ compileResult = shader . Compile ( ) ;
76
+
77
+ List < VBO > VBOs = new ObjParser ( ) . GetVBOs ( renderer . context , objFile ) ;
78
+ //Loading vertexes from resource file to VBO
79
+ vertexVBO = VBOs [ 0 ] ;
80
+ //Loading UVMap from resource file to VBO
81
+ textureVBO = VBOs [ 1 ] ;
82
+ //Loading normales from resource file to VBO
83
+ normalVBO = VBOs [ 2 ] ;
84
+ //Loading texture image file
85
+ texture = new Texture ( renderer . context , textureImage ) ;
86
+ //Ask android to run RAM garbage cleaner
87
+ System . GC . Collect ( ) ;
88
+
89
+ }
90
+
68
91
/// <summary>
69
92
/// This method is called from Renderer when OpenGL ready to draw scene
93
+ /// The code of this method show "standard" OpenGL object drawing routine with using transformation matrix
70
94
/// </summary>
71
95
/// <param name="gl">IGL10 access object pointer from orign OpenGL.OnDraw(IGL10 gl)</param>
72
96
/// <param name="mViewMatrix">Camere View matrix</param>
73
97
/// <param name="mProjectionMatrix">Camera Projection matrix</param>
74
- public void DrawFrame ( IGL10 gl , float [ ] mViewMatrix , float [ ] mProjectionMatrix )
98
+ public virtual void DrawFrame ( )
75
99
{
76
100
77
101
float [ ] mModelMatrix = new float [ 16 ] ;
@@ -89,17 +113,28 @@ public void DrawFrame(IGL10 gl, float[] mViewMatrix, float[] mProjectionMatrix)
89
113
GLES20 . GlEnableVertexAttribArray ( shader . mPositionHandle ) ;
90
114
GLES20 . GlVertexAttribPointer ( shader . mPositionHandle , mPositionDataSize , GLES20 . GlFloat , false , 0 , 0 ) ;
91
115
92
- GLES20 . GlBindBuffer ( GLES20 . GlArrayBuffer , textureVBO . handle ) ;
93
- GLES20 . GlEnableVertexAttribArray ( shader . mTextureCoordHandle ) ;
94
- GLES20 . GlVertexAttribPointer ( shader . mTextureCoordHandle , 2 , GLES20 . GlFloat , false , 0 , 0 ) ;
116
+ if ( textureVBO . handle != - 1 )
117
+ {
118
+ GLES20 . GlBindBuffer ( GLES20 . GlArrayBuffer , textureVBO . handle ) ;
119
+ GLES20 . GlEnableVertexAttribArray ( shader . mTextureCoordHandle ) ;
120
+ GLES20 . GlVertexAttribPointer ( shader . mTextureCoordHandle , 2 , GLES20 . GlFloat , false , 0 , 0 ) ;
121
+ }
122
+
123
+ if ( normalVBO . handle != - 1 )
124
+ {
95
125
96
- GLES20 . GlBindBuffer ( GLES20 . GlArrayBuffer , normalVBO . handle ) ;
97
- GLES20 . GlEnableVertexAttribArray ( shader . mNormalHandle ) ;
98
- GLES20 . GlVertexAttribPointer ( shader . mNormalHandle , 3 , GLES20 . GlFloat , false , 0 , 0 ) ;
126
+ GLES20 . GlBindBuffer ( GLES20 . GlArrayBuffer , normalVBO . handle ) ;
127
+ GLES20 . GlEnableVertexAttribArray ( shader . mNormalHandle ) ;
128
+ GLES20 . GlVertexAttribPointer ( shader . mNormalHandle , 3 , GLES20 . GlFloat , false , 0 , 0 ) ;
129
+ }
99
130
100
- GLES20 . GlActiveTexture ( GLES20 . GlTexture0 ) ;
101
- GLES20 . GlBindTexture ( GLES20 . GlTexture2d , texture . handle ) ;
102
- GLES20 . GlUniform1i ( shader . mTextureHandle , 0 ) ;
131
+ if ( texture . handle != - 1 )
132
+ {
133
+
134
+ GLES20 . GlActiveTexture ( GLES20 . GlTexture0 ) ;
135
+ GLES20 . GlBindTexture ( GLES20 . GlTexture2d , texture . handle ) ;
136
+ GLES20 . GlUniform1i ( shader . mTextureHandle , 0 ) ;
137
+ }
103
138
104
139
GLES20 . GlBindBuffer ( GLES20 . GlArrayBuffer , 0 ) ;
105
140
//END OF Draw with VBO
@@ -112,13 +147,13 @@ public void DrawFrame(IGL10 gl, float[] mViewMatrix, float[] mProjectionMatrix)
112
147
// Allocate storage for the final combined matrix. This will be passed into the shader program.
113
148
114
149
float [ ] mMVPMatrix = new float [ 16 ] ;
115
- Matrix . MultiplyMM ( mMVPMatrix , 0 , mViewMatrix , 0 , mModelMatrix , 0 ) ;
150
+ Matrix . MultiplyMM ( mMVPMatrix , 0 , renderer . camera . mViewMatrix , 0 , mModelMatrix , 0 ) ;
116
151
117
152
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
118
153
// (which now contains model * view * projection).
119
154
// THIS IS NOT WORK AT C# Matrix class -> Matrix.MultiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
120
155
float [ ] _mMVPMatrix = new float [ 16 ] ;
121
- Matrix . MultiplyMM ( _mMVPMatrix , 0 , mProjectionMatrix , 0 , mMVPMatrix , 0 ) ;
156
+ Matrix . MultiplyMM ( _mMVPMatrix , 0 , renderer . camera . mProjectionMatrix , 0 , mMVPMatrix , 0 ) ;
122
157
123
158
GLES20 . GlUniformMatrix4fv ( shader . mMVPMatrixHandle , 1 , false , _mMVPMatrix , 0 ) ;
124
159
GLES20 . GlDrawArrays ( GLES20 . GlTriangles , 0 , vertexVBO . objectSize ) ; //Cube has 12 triagle faces each face has 3 coord
@@ -127,5 +162,9 @@ public void DrawFrame(IGL10 gl, float[] mViewMatrix, float[] mProjectionMatrix)
127
162
GLES20 . GlUseProgram ( 0 ) ;
128
163
}
129
164
165
+ public virtual void OnDraw ( Android . Graphics . Canvas canvas )
166
+ {
167
+
168
+ }
130
169
}
131
170
}
0 commit comments