-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJOGL2_0_MV_Proj.java
More file actions
224 lines (154 loc) · 7.47 KB
/
JOGL2_0_MV_Proj.java
File metadata and controls
224 lines (154 loc) · 7.47 KB
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
/*************************************************
* Created on Oct 24, 2019, @author: Jim X. Chen
*
* This program is to demonstrate setting up MODEVIEW and PROJECTION Matrices, and send them into the vertex shader;
* This is to facilitate needed mechanism for the project.
*/
import static com.jogamp.opengl.GL.GL_ARRAY_BUFFER;
import static com.jogamp.opengl.GL.GL_FLOAT;
import static com.jogamp.opengl.GL.GL_LINES;
import static com.jogamp.opengl.GL.GL_POINTS;
import static com.jogamp.opengl.GL.GL_STATIC_DRAW;
import static com.jogamp.opengl.GL.GL_TRIANGLES;
import static com.jogamp.opengl.GL2ES3.GL_COLOR;
import java.nio.FloatBuffer;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.*;
import static com.jogamp.opengl.GL4.*;
import com.jogamp.opengl.util.awt.TextRenderer;
import java.awt.Font;
public class JOGL2_0_MV_Proj extends JOGL1_4_5_Circle {
static int depth; // number of subdivisions
static int cRadius = 2, flip = 2;
int count=0; // used to generate triangle vertex indices
double dt = 0;
// vertex data for the 4 triangles
static float cVdata[][] = { { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f },
{ -1.0f, 0.0f, 0.0f }, { 0.0f, -1.0f, 0.0f } };
float PROJECTION_Matrix[] = new float [16];
public void display(GLAutoDrawable drawable) {
// when the circle is too big or small, change
// the direction (growing or shrinking)
if (cRadius >= (HEIGHT / 2) || cRadius <= 1) {
flip = -flip;
depth++; // number of subdivisions
depth = depth % 7;
}
cRadius += flip; // circle's radius change
// use cRadius as an angle to show 3D model through rotations
//Connect JOGL variable with shader variable by name
int thetaLoc = gl.glGetUniformLocation(vfPrograms, "theta");
gl.glProgramUniform1f(vfPrograms, thetaLoc, cRadius);
// we do some transformation here
dt = dt + 0.05;
//we do the rotation along z axis as an example
float MODELVIEW_Matrix[] = {(float)Math.cos(dt), (float)-Math.sin(dt), 0.0f, 0.0f,
(float)Math.sin(dt), (float)Math.cos(dt), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
// connect the modelview matrix
int mvLoc = gl.glGetUniformLocation(vfPrograms, "mv_matrix");
gl.glProgramUniformMatrix4fv(vfPrograms, mvLoc, 1, false, MODELVIEW_Matrix, 0);
// clear the framebuffer and depthbuffer
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
drawCircle(cRadius, depth);
}
// called for handling drawing area when it is reshaped
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
float l, r, b, t, n, f;
l = -width/2; r = width/2;
b = -height/2; t = height/2;
n = -width; f = width;
PROJECTION_Matrix[0] = 2/(r-l); PROJECTION_Matrix[1] = 0; PROJECTION_Matrix[2] = 0; PROJECTION_Matrix[3] = 0;
PROJECTION_Matrix[4] = 0; PROJECTION_Matrix[5] = 2/(t-b); PROJECTION_Matrix[6] = 0; PROJECTION_Matrix[7] = 0;
PROJECTION_Matrix[8] = 0; PROJECTION_Matrix[9] = 0; PROJECTION_Matrix[10] = -2/(f-n); PROJECTION_Matrix[11] = 0;
PROJECTION_Matrix[12] = -(r+l)/(r-l); PROJECTION_Matrix[13] = -(t+b)/(t-b); PROJECTION_Matrix[14] = -(f+n)/(f-n); PROJECTION_Matrix[15] = 1;
// connect the projection matrix
int projLoc = gl.glGetUniformLocation(vfPrograms, "proj_matrix");
gl.glProgramUniformMatrix4fv(vfPrograms, projLoc, 1, false, PROJECTION_Matrix, 0);
System.out.println("b) reshape is called whenever the frame is resized.");
}
// draw a circle with center at the origin in xy plane
public void drawCircle(int cRadius, int depth) {
int numofTriangle= 4*(int)Math.pow(2,depth); // number of triangles after subdivision
float vPoints[] = new float[3*3*numofTriangle]; // 3 vertices each triangle, and 3 values each vertex
//System.out.println("vPoints[] is used to save all triangle vertex values, pervertex values to be sent to the vertex shader");
count = 0; // start filling triangle array to be sent to vertex shader
subdivideCircle(vPoints, cRadius, cVdata[0], cVdata[1], depth);
subdivideCircle(vPoints, cRadius, cVdata[1], cVdata[2], depth);
subdivideCircle(vPoints, cRadius, cVdata[2], cVdata[3], depth);
subdivideCircle(vPoints, cRadius, cVdata[3], cVdata[0], depth);
// load vbo[0] with vertex data
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // use handle 0
FloatBuffer vBuf = Buffers.newDirectFloatBuffer(vPoints);
gl.glBufferData(GL_ARRAY_BUFFER, vBuf.limit()*Float.BYTES, //# of float * size of floats in bytes
vBuf, // the vertex array
GL_STATIC_DRAW);
gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // associate vbo[0] with active VAO buffer
gl.glDrawArrays(GL_TRIANGLES, 0, vBuf.limit()/3);
}
// subdivide a triangle recursively, and draw them
private void subdivideCircle(float[] vPoints, int radius, float[] v1, float[] v2, int depth) {
float v11[] = new float[3];
float v22[] = new float[3];
float v00[] = { 0, 0, 0.5f };
float v12[] = new float[3];
if (depth == 0) { // draw triangle
for (int i = 0; i < 3; i++) {
v11[i] = v1[i] * radius;
v22[i] = v2[i] * radius;
v00[2] = radius; // I added this for changing the length;
}
//drawtriangle(v11, v22, v00);
// load vPoints with the triangle vertex values
for (int i = 0; i < 3; i++) vPoints[count++] = v11[i] ;
for (int i = 0; i < 3; i++) vPoints[count++] = v22[i] ;
for (int i = 0; i < 3; i++) vPoints[count++] = v00[i] ;
return;
}
v12[0] = v1[0] + v2[0];
v12[1] = v1[1] + v2[1];
v12[2] = v1[2] + v2[2];
normalize(v12);
// subdivide a triangle recursively, and draw them
subdivideCircle(vPoints, radius, v1, v12, depth - 1);
subdivideCircle(vPoints, radius, v12, v2, depth - 1);
}
public void normalize(float[] vector) {
// TODO Auto-generated method stub
float d = (float) Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1]
+ vector[2] * vector[2]);
if (d == 0) {
System.err.println("0 length vector: normalize().");
return;
}
vector[0] /= d;
vector[1] /= d;
vector[2] /= d;
}
public void init(GLAutoDrawable drawable) {
gl = (GL4) drawable.getGL();
String vShaderSource[], fShaderSource[] ;
vShaderSource = readShaderSource("src/JOGL2_0_V.shader"); // read vertex shader
fShaderSource = readShaderSource("src/JOGL1_4_3_F.shader"); // read fragment shader
vfPrograms = initShaders(vShaderSource, fShaderSource);
// 1. generate vertex arrays indexed by vao
gl.glGenVertexArrays(vao.length, vao, 0); // vao stores the handles, starting position 0
gl.glBindVertexArray(vao[0]); // use handle 0
// 2. generate vertex buffers indexed by vbo: here vertices and colors
gl.glGenBuffers(vbo.length, vbo, 0);
// 3. enable VAO with loaded VBO data
gl.glEnableVertexAttribArray(0); // enable the 0th vertex attribute: position
// if you don't use it, you should not enable it
//gl.glEnableVertexAttribArray(1); // enable the 1th vertex attribute: color
//4. specify drawing into only the back_buffer
gl.glDrawBuffer(GL.GL_BACK);
// 5. Enable zbuffer and clear framebuffer and zbuffer
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
}
public static void main(String[] args) {
new JOGL2_0_MV_Proj();
}
}