-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomGLRenderer.cs
372 lines (292 loc) · 12.5 KB
/
CustomGLRenderer.cs
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Opengl;
using static Android.Opengl.GLSurfaceView;
using Javax.Microedition.Khronos.Egl;
using Javax.Microedition.Khronos.Opengles;
using Java.Nio;
using Android.Graphics;
namespace OpenGLEs2Tutorial
{
public class CustomGLRenderer : Java.Lang.Object, IRenderer
{
#region Members
private float[] matrixProjection = new float[16];
private float[] matrixView = new float[16];
private float[] matrixProjectionAndView = new float[16];
public static float[] vertices;
public static short[] indices;
public static float[] uvs;
public FloatBuffer vertexBuffer;
public ShortBuffer drawListBuffer;
public FloatBuffer uvBuffer;
private float screenWidth = 1280;
private float screenHeight = 768;
private Context context;
private long lastTime;
private RectF rect;
//private int program;
#endregion
#region Constructors
public CustomGLRenderer(Context context)
{
this.context = context;
lastTime = Java.Lang.JavaSystem.CurrentTimeMillis() + 100;
}
#endregion
public void OnDrawFrame(IGL10 unused)
{
// Get the current time
long now = Java.Lang.JavaSystem.CurrentTimeMillis();
// We should make sure we are valid and sane
if (lastTime > now)
{
return;
}
// Get the amount of time the last frame took.
long timeDelta = now - lastTime;
// Update our example
// Render our example
Render(matrixProjectionAndView);
// Save the current time to see how long it took
lastTime = now;
}
internal void ProcessTouchEvent(MotionEvent motionEvent)
{
// Get the half of screen value
int screenHalfX = (int)(screenWidth / 2);
int screenHalfY = (int)(screenHeight / 2);
if (motionEvent.GetX() < screenHalfX)
{
rect.Left -= 10;
rect.Right -= 10;
}
else
{
rect.Left += 10;
rect.Right += 10;
}
if (motionEvent.GetY() < screenHalfY)
{
rect.Bottom += 10;
rect.Top += 10;
}
else
{
rect.Bottom -= 10;
rect.Top -= 10;
}
// Update the new data.
TranslateSprite();
}
private void Render(float[] matrix)
{
// clear Screen and Depth Buffer, we have set the clear color as black.
GLES20.GlClear(GLES20.GlColorBufferBit | GLES20.GlDepthBufferBit);
// get handle to vertex shader's vPosition member
int positionHandle = GLES20.GlGetAttribLocation(ShaderHelper.SpImage, "vPosition");
// Enable generic vertex attribute array
GLES20.GlEnableVertexAttribArray(positionHandle);
// Prepare the triangle coordinate data
GLES20.GlVertexAttribPointer(positionHandle, 3,
GLES20.GlFloat, false,
0, vertexBuffer);
// Get handle to texture coordinates location
int textureCoordinatesLocation = GLES20.GlGetAttribLocation(ShaderHelper.SpImage, "a_texCoord");
// Enable generic vertex attribute array
GLES20.GlEnableVertexAttribArray(textureCoordinatesLocation);
// Prepare the texturecoordinates
GLES20.GlVertexAttribPointer(textureCoordinatesLocation, 2, GLES20.GlFloat, false, 0, uvBuffer);
// Get handle to shape's transformation matrix
int matrixhandle = GLES20.GlGetUniformLocation(ShaderHelper.SpImage, "uMVPMatrix");
// Apply the projection and view transformation
GLES20.GlUniformMatrix4fv(matrixhandle, 1, false, matrix, 0);
// Get handle to textures locations
int samplerLocation = GLES20.GlGetUniformLocation(ShaderHelper.SpImage, "s_texture");
// Set the sampler texture unit to 0, where we have saved the texture.
GLES20.GlUniform1i(samplerLocation, 0);
// Draw the triangle
GLES20.GlDrawElements(GLES20.GlTriangles, indices.Length,
GLES20.GlUnsignedShort, drawListBuffer);
// Disable vertex array
GLES20.GlDisableVertexAttribArray(positionHandle);
GLES20.GlDisableVertexAttribArray(textureCoordinatesLocation);
}
public void OnSurfaceChanged(IGL10 unused, int width, int height)
{
// We need to know the current width and height.
screenWidth = width;
screenHeight = height;
// Redo the Viewport, making it fullscreen.
GLES20.GlViewport(0, 0, (int)screenWidth, (int)screenHeight);
// Clear our matrices
for (int i = 0; i < 16; i++)
{
matrixProjection[i] = 0.0f;
matrixView[i] = 0.0f;
matrixProjectionAndView[i] = 0.0f;
}
// Setup our screen width and height for normal sprite translation.
Android.Opengl.Matrix.OrthoM(matrixProjection, 0, 0f, screenWidth, 0.0f, screenHeight, 0, 50);
// Set the camera position (View matrix)
Android.Opengl.Matrix.SetLookAtM(matrixView, 0, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Android.Opengl.Matrix.MultiplyMM(matrixProjectionAndView, 0, matrixProjection, 0, matrixView, 0);
}
public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
{
// Create the triangle
SetupTriangle();
// Create the image information
SetupImage();
// Set the clear color to yellow
GLES20.GlClearColor(1.0f, 1.0f, 0f, 1);
GLES20.GlEnable(GLES20.GlBlend);
GLES20.GlBlendFunc(GLES20.GlOne, GLES20.GlOneMinusSrcAlpha);
#region Solid colors mode
/*
// Create the shaders, solid color
int vertexShader = ShaderHelper.LoadShader(GLES20.GlVertexShader, ShaderHelper.VsSolidColor);
int fragmentShader = ShaderHelper.LoadShader(GLES20.GlFragmentShader, ShaderHelper.FsSolidColor);
ShaderHelper.SpSolidColor = GLES20.GlCreateProgram(); // create empty OpenGL ES Program
GLES20.GlAttachShader(ShaderHelper.SpSolidColor, vertexShader); // add the vertex shader to program
GLES20.GlAttachShader(ShaderHelper.SpSolidColor, fragmentShader); // add the fragment shader to program
GLES20.GlLinkProgram(ShaderHelper.SpSolidColor); // creates OpenGL ES program executables
// Set our shader programm
GLES20.GlUseProgram(ShaderHelper.SpSolidColor);
*/
#endregion
#region Texture mode
// Create the shaders, images
int vertexShader = ShaderHelper.LoadShader(GLES20.GlVertexShader, ShaderHelper.VsImage);
int fragmentShader = ShaderHelper.LoadShader(GLES20.GlFragmentShader, ShaderHelper.FsImage);
ShaderHelper.SpImage = GLES20.GlCreateProgram();
GLES20.GlAttachShader(ShaderHelper.SpImage, vertexShader);
GLES20.GlAttachShader(ShaderHelper.SpImage, fragmentShader);
GLES20.GlLinkProgram(ShaderHelper.SpImage);
// Set our shader programm
GLES20.GlUseProgram(ShaderHelper.SpImage);
#endregion
}
private void SetupTriangle()
{
rect = new RectF(10, 200, 100, 10);
/*rect.Left = 10;
rect.Right = 100;
rect.Bottom = 100;
rect.Top = 200;*/
// We have to create the vertices.
/*vertices = new float[]
{
10.0f, 200f, 0.0f,
10.0f, 100f, 0.0f,
100f, 100f, 0.0f,
100f, 200f, 0.0f,
};*/
vertices = new float[]
{
rect.Left, rect.Top, 0.0f,
rect.Left, rect.Bottom, 0.0f,
rect.Right, rect.Bottom, 0.0f,
rect.Right, rect.Top, 0.0f,
};
indices = new short[] { 0, 1, 2, 0, 2, 3 }; // The order of vertexrendering.
// The vertex buffer.
ByteBuffer byteBuffer = ByteBuffer.AllocateDirect(vertices.Length * 4);
byteBuffer.Order(ByteOrder.NativeOrder());
vertexBuffer = byteBuffer.AsFloatBuffer();
vertexBuffer.Put(vertices);
vertexBuffer.Position(0);
// initialize byte buffer for the draw list
ByteBuffer byteBuffer2 = ByteBuffer.AllocateDirect(indices.Length * 2);
byteBuffer2.Order(ByteOrder.NativeOrder());
drawListBuffer = byteBuffer2.AsShortBuffer();
drawListBuffer.Put(indices);
drawListBuffer.Position(0);
}
public void TranslateSprite()
{
/*vertices = new float[]
{
rect.Left, rect.Top, 0.0f,
rect.Left, rect.Bottom, 0.0f,
rect.Right, rect.Bottom, 0.0f,
rect.Right, rect.Top, 0.0f,
};*/
vertices[0] = rect.Left;
vertices[1] = rect.Top;
vertices[2] = 0.0f;
vertices[3] = rect.Left;
vertices[4] = rect.Bottom;
vertices[5] = 0.0f;
vertices[6] = rect.Right;
vertices[7] = rect.Bottom;
vertices[8] = 0.0f;
vertices[9] = rect.Right;
vertices[10] = rect.Top;
vertices[11] = 0.0f;
vertexBuffer.Clear();
vertexBuffer.Put(vertices);
vertexBuffer.Position(0);
/*
// The vertex buffer.
ByteBuffer byteBuffer = ByteBuffer.AllocateDirect(vertices.Length * 4);
byteBuffer.Order(ByteOrder.NativeOrder());
vertexBuffer = byteBuffer.AsFloatBuffer();
vertexBuffer.Put(vertices);
vertexBuffer.Position(0);*/
}
private void SetupImage()
{
// Create our UV coordinates.
uvs = new float[] {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
// The texture buffer
ByteBuffer byteBuffer = ByteBuffer.AllocateDirect(uvs.Length * 4);
byteBuffer.Order(ByteOrder.NativeOrder());
uvBuffer = byteBuffer.AsFloatBuffer();
uvBuffer.Put(uvs);
uvBuffer.Position(0);
// Generate Textures, if more needed, alter these numbers.
int[] textureIds = new int[1];
GLES20.GlGenTextures(1, textureIds, 0);
// Retrieve our image from resources.
Bitmap bitmap = BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.plant07);
// Bind texture to texturename
GLES20.GlActiveTexture(GLES20.GlTexture0);
GLES20.GlBindTexture(GLES20.GlTexture2d, textureIds[0]);
// Set filtering
GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear);
GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear);
// Set wrapping mode
GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);
// Load the bitmap into the bound texture.
GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0);
// We are done using the bitmap so we should recycle it.
bitmap.Recycle();
bitmap.Dispose();
}
internal void OnPause()
{
/* Do stuff to pause the renderer */
}
internal void OnResume()
{
/* Do stuff to resume the renderer */
lastTime = Java.Lang.JavaSystem.CurrentTimeMillis();
}
}
}