/[zanavi_public1]/navit/navit/android/src/com/zoffcc/applications/zanavi/ZANaviGlLine.java
ZANavi

Contents of /navit/navit/android/src/com/zoffcc/applications/zanavi/ZANaviGlLine.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show annotations) (download)
Tue Aug 11 18:50:37 2015 UTC (8 years, 8 months ago) by zoff99
File size: 4420 byte(s)
many fixes, and new features
1 package com.zoffcc.applications.zanavi;
2
3 import java.nio.ByteBuffer;
4 import java.nio.ByteOrder;
5 import java.nio.FloatBuffer;
6
7 import android.opengl.GLES20;
8
9 public class ZANaviGlLine
10 {
11 // ---------------------------------------------------------------
12 // thanks to: http://stackoverflow.com/questions/16027455/what-is-the-easiest-way-to-draw-line-using-opengl-es-android
13 // ---------------------------------------------------------------
14
15 private FloatBuffer VertexBuffer;
16
17 private final String VertexShaderCode =
18 // This matrix member variable provides a hook to manipulate
19 // the coordinates of the objects that use this vertex shader
20 "uniform mat4 uMVPMatrix;" +
21
22 "attribute vec4 vPosition;" + "void main() {" +
23 // the matrix must be included as a modifier of gl_Position
24 " gl_Position = uMVPMatrix * vPosition;" + "}";
25
26 private final String FragmentShaderCode = "precision mediump float;" + "uniform vec4 vColor;" + "void main() {" + " gl_FragColor = vColor;" + "}";
27
28 protected int GlProgram;
29 protected int PositionHandle;
30 protected int ColorHandle;
31 protected int MVPMatrixHandle;
32
33 // number of coordinates per vertex in this array
34 static final int COORDS_PER_VERTEX = 3;
35 static float LineCoords[] = { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f };
36
37 private final int VertexCount = LineCoords.length / COORDS_PER_VERTEX;
38 private final int VertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
39
40 // Set color with red, green, blue and alpha (opacity) values
41 float color[] = { 0.0f, 0.0f, 0.0f, 1.0f };
42
43 public ZANaviGlLine()
44 {
45 // initialize vertex byte buffer for shape coordinates
46 ByteBuffer bb = ByteBuffer.allocateDirect(
47 // (number of coordinate values * 4 bytes per float)
48 LineCoords.length * 4);
49 // use the device hardware's native byte order
50 bb.order(ByteOrder.nativeOrder());
51
52 // create a floating point buffer from the ByteBuffer
53 VertexBuffer = bb.asFloatBuffer();
54 // add the coordinates to the FloatBuffer
55 VertexBuffer.put(LineCoords);
56 // set the buffer to read the first coordinate
57 VertexBuffer.position(0);
58
59 int vertexShader = GlRenderer.loadShader(GLES20.GL_VERTEX_SHADER, VertexShaderCode);
60 int fragmentShader = GlRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, FragmentShaderCode);
61
62 GlProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
63 GLES20.glAttachShader(GlProgram, vertexShader); // add the vertex shader to program
64 GLES20.glAttachShader(GlProgram, fragmentShader); // add the fragment shader to program
65 GLES20.glLinkProgram(GlProgram); // creates OpenGL ES program executables
66
67 }
68
69 public void SetVerts(float v0, float v1, float v2, float v3, float v4, float v5)
70 {
71 LineCoords[0] = v0;
72 LineCoords[1] = v1;
73 LineCoords[2] = v2;
74 LineCoords[3] = v3;
75 LineCoords[4] = v4;
76 LineCoords[5] = v5;
77
78 VertexBuffer.put(LineCoords);
79 // set the buffer to read the first coordinate
80 VertexBuffer.position(0);
81
82 }
83
84 public void SetColor(float red, float green, float blue, float alpha)
85 {
86 color[0] = red;
87 color[1] = green;
88 color[2] = blue;
89 color[3] = alpha;
90 }
91
92 public void draw(float[] mvpMatrix)
93 {
94 // Add program to OpenGL ES environment
95 GLES20.glUseProgram(GlProgram);
96
97 // get handle to vertex shader's vPosition member
98 PositionHandle = GLES20.glGetAttribLocation(GlProgram, "vPosition");
99
100 // Enable a handle to the triangle vertices
101 GLES20.glEnableVertexAttribArray(PositionHandle);
102
103 // Prepare the triangle coordinate data
104 GLES20.glVertexAttribPointer(PositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, VertexStride, VertexBuffer);
105
106 // get handle to fragment shader's vColor member
107 ColorHandle = GLES20.glGetUniformLocation(GlProgram, "vColor");
108
109 // Set color for drawing the triangle
110 GLES20.glUniform4fv(ColorHandle, 1, color, 0);
111
112 // get handle to shape's transformation matrix
113 MVPMatrixHandle = GLES20.glGetUniformLocation(GlProgram, "uMVPMatrix");
114 GlRenderer.checkGlError("glGetUniformLocation");
115
116 // Apply the projection and view transformation
117 GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, mvpMatrix, 0);
118 GlRenderer.checkGlError("glUniformMatrix4fv");
119
120 // Draw the triangle
121 GLES20.glDrawArrays(GLES20.GL_LINES, 0, VertexCount);
122
123 // Disable vertex array
124 GLES20.glDisableVertexAttribArray(PositionHandle);
125 }
126
127 }

   
Visit the ZANavi Wiki