forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLRender.java
More file actions
executable file
·136 lines (125 loc) · 3.92 KB
/
Copy pathGLRender.java
File metadata and controls
executable file
·136 lines (125 loc) · 3.92 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
package javaforce.gl;
/**
* Renders a view of a GLScene
*
* @author pquiring
*/
import static javaforce.gl.GL.*;
public class GLRender {
private int iwx, iwy; //window size (int)
private float dwx, dwy; //window size (float)
private float ratio; //dwx / dwy
private GLScene scene;
public GLMatrix m_camera; //camera matrix (rotation/translation)
public GLMatrix m_model; //model matrix (translation only)
public float fovy = 60.0f;
public float zNear = 0.1f; //Do NOT use zero!!!
public float zFar = 10000.0f;
public void init(GLScene scene, int width, int height) {
this.scene = scene;
resize(width, height);
reset();
}
public void resize(int width, int height) {
iwx = width;
iwy = height;
dwx = (float)width;
dwy = (float)height;
ratio = dwx/dwy;
}
public void reset() {
m_camera = new GLMatrix();
m_model = new GLMatrix();
}
public void setFOV(float fov) {
fovy = fov;
}
public void setRenderDistance(float near, float far) {
zNear = near;
zFar = far;
}
public void render() {
scene.initTextures();
GLModel mod;
GLObject obj;
GLMatrix mat = new GLMatrix();
//setup camera view
glViewport(0, 0, iwx, iwy);
//setup model view
//setup background clr
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT);
//render models
int size_ml = scene.ml.size();
mat.setIdentity();
mat.perspective(fovy, ratio, zNear, zFar);
glUniformMatrix4fv(scene.mpu, 1, GL.GL_FALSE, mat.m); //perspective matrix
for(int a=0;a<size_ml;a++) {
mod = scene.ml.get(a);
if (!mod.visible) continue;
mat.setIdentity();
mat.mult4x4(m_camera);
glUniformMatrix4fv(scene.mvu, 1, GL.GL_FALSE, mat.m); //view matrix
int size_ol = mod.ol.size();
for(int b=0;b<size_ol;b++) {
obj = mod.ol.get(b);
if (!obj.visible) continue;
if (obj.needCopyBuffers) {
obj.copyBuffers();
}
//need to optz this
mat.setIdentity();
mat.mult4x4(m_model);
mat.mult4x4(mod.m);
mat.mult4x4(obj.m);
glUniformMatrix4fv(scene.mmu, 1, GL.GL_FALSE, mat.m); //model matrix
for(int m=0;m<obj.maps.size();m++) {
GLUVMap map = obj.maps.get(m);
map.bindBuffers(scene);
GLTexture tex = null;
if ((map.textureIndex != -1) && (map.texloaded)) {
tex = scene.tl.get(mod.getTexture(map.textureIndex));
if (tex != null && tex.loaded) {
tex.bind();
} else {
tex = null;
}
}
if (tex == null) {
System.out.println("GLRender:Warning:using blank texture, missing texture?");
scene.blankTexture.bind();
}
}
obj.bindBuffers(scene);
obj.render(scene);
}
}
glFlush();
}
public void cameraReset() {
m_camera.setIdentity();
}
public void cameraSet(float angle, float ax, float ay, float az, float tx, float ty, float tz) {
m_camera.setAATranslate(angle, ax, ay, az, tx, ty, tz);
}
public void cameraRotate(float angle, float ax, float ay, float az) {
m_camera.addRotate(angle, ax, ay, az);
}
public void cameraTranslate(float tx, float ty, float tz) {
m_camera.addTranslate(tx, ty, tz);
}
public void modelReset() {
m_model.setIdentity();
}
/*
public void modelSet(float angle, float rx, float ry, float rz, float tx, float ty, float tz) {
m_model.setAATranslate(angle, rx, ry, rz, tx, ty, tz);
}
public void modelRotate(float angle, float ax, float ay, float az) {
m_model.addRotate(angle, ax, ay, az);
}
*/
public void modelTranslate(float tx, float ty, float tz) {
m_model.addTranslate(tx, ty, tz);
}
}