Skip to content

Commit ce82beb

Browse files
committed
refactoring
1 parent 2391693 commit ce82beb

28 files changed

+975
-603
lines changed

SGWW/SGWW/Engine/Camera.cs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
using Android.App;
7+
using Android.Content;
8+
using Android.Opengl;
9+
using Android.OS;
10+
using Android.Runtime;
11+
using Android.Views;
12+
using Android.Widget;
13+
using Javax.Microedition.Khronos.Opengles;
14+
15+
namespace SGWW
16+
{
17+
public class Camera
18+
{
19+
//Store the model matrix. This matrix is used to move models from object space (where each model can be thought of being located at the center of the universe) to world space.
20+
public float[] mProjectionMatrix = new float[16];
21+
public float[] mViewMatrix = new float[16];
22+
23+
// Position the eye behind the origin.
24+
public float eyeX = 0.0f;
25+
public float eyeY = 0.0f;
26+
public float eyeZ = -4.5f;
27+
28+
// We are looking toward the distance
29+
public float lookX = 0.0f;
30+
public float lookY = 0.0f;
31+
public float lookZ = 10.0f;
32+
33+
// Set our up vector. This is where our head would be pointing were we holding the camera.
34+
public float upX = 0.0f;
35+
public float upY = 1.0f;
36+
public float upZ = 0.0f;
37+
38+
public Camera(int width, int height)
39+
{
40+
GLES20.GlViewport(0, 0, width, height);
41+
float ratio = (float)width / height;
42+
float left = -ratio;
43+
float right = ratio;
44+
float top = 1.0f;
45+
float bottom = -1.0f;
46+
float near = 1.0f;
47+
float far = 100.0f;
48+
Matrix.FrustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
49+
//Alternative
50+
//Matrix.PerspectiveM(mProjectionMatrix, 0, 10.0f, ratio, near, far);
51+
}
52+
53+
public void OnDrawFrame()
54+
{
55+
// Set the view matrix. This matrix can be said to represent the camera position.
56+
// NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
57+
// view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
58+
Matrix.SetLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
59+
}
60+
}
61+
}

SGWW/SGWW/Engine/CanvasView.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Android.Views;
5+
using Android.Content;
6+
using Android.Graphics;
7+
8+
namespace SGWW
9+
{
10+
/// <summary>
11+
/// view class for public section (canvas draw handler)
12+
/// </summary>
13+
public class CanvasView : View
14+
{
15+
public Renderer renderer;
16+
17+
private ConsoleDrawer consoleDrawer = new ConsoleDrawer();
18+
19+
public CanvasView(Context context) : base(context)
20+
{
21+
22+
}
23+
24+
protected override void OnDraw(Canvas canvas)
25+
{
26+
base.OnDraw(canvas);
27+
28+
//draw console
29+
consoleDrawer.OnDraw(canvas);
30+
//call all GLObjects to draw data on View.Canvas
31+
if (renderer != null)
32+
{
33+
foreach (GLObject glObject in renderer.glObjects)
34+
{
35+
glObject.OnDraw(canvas);
36+
}
37+
}
38+
}
39+
40+
41+
public void ConsoleWrite(string text, int status)
42+
{
43+
consoleDrawer.Add(text, status);
44+
}
45+
}
46+
}

SGWW/SGWW/Engine/ConsoleDrawer.cs

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
using Android.App;
7+
using Android.Content;
8+
using Android.Graphics;
9+
using Android.OS;
10+
using Android.Runtime;
11+
using Android.Views;
12+
using Android.Widget;
13+
14+
namespace SGWW
15+
{
16+
/// <summary>
17+
/// Console item to draw in canvas
18+
/// </summary>
19+
class ConsoleItem
20+
{
21+
public string text;
22+
public DateTime time;
23+
public int status;
24+
}
25+
26+
class ConsoleDrawer
27+
{
28+
/// <summary>
29+
/// app console
30+
/// </summary>
31+
private List<ConsoleItem> console = new List<ConsoleItem>();
32+
private DateTime lastTime;
33+
private int refreshPeriod = 500;
34+
private int linesCount = 20;
35+
private int consoleX = 50;
36+
private int consoleY = 50;
37+
38+
private int n = 0;
39+
private int lastCount = 0;
40+
41+
public void OnDraw(Canvas canvas)
42+
{
43+
try
44+
{
45+
TimeSpan span = DateTime.Now - lastTime;
46+
if (span.TotalMilliseconds > refreshPeriod)
47+
{
48+
49+
if (console.Count > linesCount)
50+
{
51+
console.RemoveRange(0, console.Count - linesCount);
52+
}
53+
lastTime = DateTime.Now;
54+
55+
}
56+
57+
58+
if (span.TotalMilliseconds > 300)
59+
{
60+
if (lastCount != console.Count)
61+
n++;
62+
}
63+
lastCount = console.Count;
64+
65+
66+
67+
Paint paint = new Paint();
68+
int startColor = 0;
69+
paint.TextSize = 20;
70+
int top = consoleY - n;
71+
72+
foreach (ConsoleItem consoleItem in console)
73+
{
74+
string text = consoleItem.time.ToString() + " " + consoleItem.text;
75+
Rect bounds = new Rect();
76+
paint.GetTextBounds(text, 0, text.Length, bounds);
77+
78+
79+
if (n > bounds.Height() + 4) n = 0;
80+
81+
switch (consoleItem.status)
82+
{
83+
case 3: paint.Color = Color.Argb(0xF0, startColor, 0x00, 0x00); break;
84+
case 2: paint.Color = Color.Argb(0xF0, 0x00, startColor, 0x00); break;
85+
case 1: paint.Color = Color.Argb(0xF0, 0x00, 0x00, startColor); break;
86+
default: paint.Color = Color.Argb(0xF0, 0x00, 0x00, 0x00); break;
87+
}
88+
89+
canvas.DrawText(text, consoleX, top, paint);
90+
91+
top += bounds.Height() + 4;
92+
startColor += 0xFF / console.Count;
93+
}
94+
}
95+
catch{ }
96+
97+
98+
}
99+
100+
public void Add(string text, int status)
101+
{
102+
ConsoleItem consoleItem = new ConsoleItem();
103+
consoleItem.text = text;
104+
consoleItem.time = DateTime.Now;
105+
consoleItem.status = status;
106+
console.Add(consoleItem);
107+
}
108+
}
109+
}

SGWW/SGWW/Engine/Face.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
3+
namespace SGWW
4+
{
5+
public class Face
6+
{
7+
public int vertex = 0;
8+
public int texture = 0;
9+
public int normal = 0;
10+
}
11+
}

SGWW/SGWW/Engine/GLObject.cs

+57-18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace SGWW
1616
{
1717
public class GLObject
1818
{
19+
protected Renderer renderer;
1920
private Shader shader;
2021
private VBO vertexVBO;
2122
private VBO normalVBO;
@@ -48,30 +49,53 @@ public class GLObject
4849
/// <param name="normalFile">raw/normal file name_objnormal</param>
4950
/// <param name="textureFile">raw/UVMap file name_objtexture</param>
5051
/// <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)
5253
{
54+
this.renderer = renderer;
5355
//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);
5557
compileResult = shader.Compile();
5658
//Loading vertexes from resource file to VBO
57-
vertexVBO = new VBO(context, vertexFile);
59+
vertexVBO = new VBO(renderer.context, vertexFile);
5860
//Loading UVMap from resource file to VBO
59-
textureVBO = new VBO(context, textureFile);
61+
textureVBO = new VBO(renderer.context, textureFile);
6062
//Loading normales from resource file to VBO
61-
normalVBO = new VBO(context, normalFile);
63+
normalVBO = new VBO(renderer.context, normalFile);
6264
//Loading texture image file
63-
texture = new Texture(context, textureImage);
65+
texture = new Texture(renderer.context, textureImage);
6466
//Ask android to run RAM garbage cleaner
6567
System.GC.Collect();
6668
}
6769

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+
6891
/// <summary>
6992
/// 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
7094
/// </summary>
7195
/// <param name="gl">IGL10 access object pointer from orign OpenGL.OnDraw(IGL10 gl)</param>
7296
/// <param name="mViewMatrix">Camere View matrix</param>
7397
/// <param name="mProjectionMatrix">Camera Projection matrix</param>
74-
public void DrawFrame(IGL10 gl, float[] mViewMatrix, float[] mProjectionMatrix)
98+
public virtual void DrawFrame()
7599
{
76100

77101
float[] mModelMatrix = new float[16];
@@ -89,17 +113,28 @@ public void DrawFrame(IGL10 gl, float[] mViewMatrix, float[] mProjectionMatrix)
89113
GLES20.GlEnableVertexAttribArray(shader.mPositionHandle);
90114
GLES20.GlVertexAttribPointer(shader.mPositionHandle, mPositionDataSize, GLES20.GlFloat, false, 0, 0);
91115

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+
{
95125

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+
}
99130

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+
}
103138

104139
GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);
105140
//END OF Draw with VBO
@@ -112,13 +147,13 @@ public void DrawFrame(IGL10 gl, float[] mViewMatrix, float[] mProjectionMatrix)
112147
// Allocate storage for the final combined matrix. This will be passed into the shader program.
113148

114149
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);
116151

117152
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
118153
// (which now contains model * view * projection).
119154
// THIS IS NOT WORK AT C# Matrix class -> Matrix.MultiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
120155
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);
122157

123158
GLES20.GlUniformMatrix4fv(shader.mMVPMatrixHandle, 1, false, _mMVPMatrix, 0);
124159
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)
127162
GLES20.GlUseProgram(0);
128163
}
129164

165+
public virtual void OnDraw(Android.Graphics.Canvas canvas)
166+
{
167+
168+
}
130169
}
131170
}

0 commit comments

Comments
 (0)