-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.java
165 lines (145 loc) · 5.75 KB
/
Model.java
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
/* This code is from exercise sheet written by Dr. Steve Maddock */
/*
slightly modified by Junxiang Chen
*/
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL3;
import gmaths.Mat4;
public class Model {
private Camera camera;
private Light light1, light2;
private MovingLight movingLight;
private Shader shader;
private Material material;
private Mat4 modelMatrix;
private Mesh mesh;
private double programStart = 0;
private float offsetX;
private int[] textureId1;
private int[] textureId2;
public Model(
GL3 gl3, Camera camera, Light light1, Light light2, MovingLight movingLight,
Shader shader, Material material, Mat4 modelMatrix, Mesh mesh,
int[] textureId1, int[] textureId2
) {
this.camera = camera;
this.light1 = light1;
this.light2 = light2;
this.movingLight = movingLight;
this.shader = shader;
this.material = material;
this.modelMatrix = modelMatrix;
this.mesh = mesh;
this.textureId1 = textureId1;
this.textureId2 = textureId2;
this.programStart = System.currentTimeMillis() / 1000.0;
}
public Model(
GL3 gl3, Camera camera, Light light1, Light light2, MovingLight movingLight,
Shader shader, Material material, Mat4 modelMatrix, Mesh mesh,
int[] textureId1
) {
this(gl3, camera, light1, light2, movingLight, shader, material, modelMatrix, mesh, textureId1, null);
}
public Model(
GL3 gl3, Camera camera, Light light1, Light light2, MovingLight movingLight,
Shader shader, Material material, Mat4 modelMatrix, Mesh mesh
) {
this(
gl3, camera, light1, light2, movingLight,
shader, material, modelMatrix, mesh, null, null
);
}
public void setCamera(Camera camera) {
this.camera = camera;
}
public void setLight1(Light light1) {
this.light1 = light1;
}
public void setLight2(Light light2) {
this.light2 = light2;
}
public void setModelMatrix(Mat4 modelMatrix) {
this.modelMatrix = modelMatrix;
}
public void dispose(GL3 gl3) {
mesh.dispose(gl3);
if (textureId1 != null) {
gl3.glDeleteBuffers(1, textureId1, 0);
}
if (textureId2 != null) {
gl3.glDeleteBuffers(1, textureId2, 0);
}
}
/**
* set moving texture
*/
private void setOffsetTexture() {
double elapsedTime = (System.currentTimeMillis() - programStart) * 0.00008;
offsetX = (float) (elapsedTime - Math.floor(elapsedTime));
// System.out.println("offsetX" + offsetX);
}
public void render(GL3 gl3, Mat4 modelMatrix) {
Mat4 mvpMatrix = Mat4.multiply(
camera.getPerspectiveMatrix(),
Mat4.multiply(
camera.getViewMatrix(),
modelMatrix
)
);
setOffsetTexture();
shader.use(gl3);
shader.setFloatArray(gl3, "model", modelMatrix.toFloatArrayForGLSL());
shader.setFloatArray(gl3, "mvpMatrix", mvpMatrix.toFloatArrayForGLSL());
shader.setVec3(gl3, "viewPos", camera.getPosition());
/*
set offset texture coordinates
*/
shader.setFloat(gl3, "offset", offsetX, 0);
/*
set static lights attributes
*/
shader.setVec3(gl3, "light1.position", light1.getPosition());
shader.setVec3(gl3, "light1.ambient", light1.getMaterial().getAmbient());
shader.setVec3(gl3, "light1.diffuse", light1.getMaterial().getDiffuse());
shader.setVec3(gl3, "light1.specular", light1.getMaterial().getSpecular());
shader.setVec3(gl3, "light2.position", light2.getPosition());
shader.setVec3(gl3, "light2.ambient", light2.getMaterial().getAmbient());
shader.setVec3(gl3, "light2.diffuse", light2.getMaterial().getDiffuse());
shader.setVec3(gl3, "light2.specular", light2.getMaterial().getSpecular());
/*
set spotlight effect
*/
shader.setVec3(gl3, "lightBulb.position", movingLight.getWorldPosition());
shader.setVec3(gl3, "lightBulb.direction", movingLight.getWorldDirection());
shader.setVec3(gl3, "lightBulb.ambient", movingLight.getMaterial().getAmbient());
shader.setVec3(gl3, "lightBulb.diffuse", movingLight.getMaterial().getDiffuse());
shader.setVec3(gl3, "lightBulb.specular", movingLight.getMaterial().getSpecular());
shader.setFloat(gl3, "lightBulb.constant", movingLight.getConstant());
shader.setFloat(gl3, "lightBulb.linear", movingLight.getLinear());
shader.setFloat(gl3, "lightBulb.quadratic", movingLight.getQuadratic());
shader.setFloat(gl3, "lightBulb.cutOff", movingLight.getCutOff());
shader.setFloat(gl3, "lightBulb.outerCutOff", movingLight.getOuterCutOff());
/*
set material
*/
shader.setVec3(gl3, "material.ambient", material.getAmbient());
shader.setVec3(gl3, "material.diffuse", material.getDiffuse());
shader.setVec3(gl3, "material.specular", material.getSpecular());
shader.setFloat(gl3, "material.shininess", material.getShininess());
if (textureId1 != null) {
shader.setInt(gl3, "first_texture", 0);
gl3.glActiveTexture(GL.GL_TEXTURE0);
gl3.glBindTexture(GL.GL_TEXTURE_2D, textureId1[0]);
}
if (textureId2 != null) {
shader.setInt(gl3, "second_texture", 1);
gl3.glActiveTexture(GL.GL_TEXTURE1);
gl3.glBindTexture(GL.GL_TEXTURE_2D, textureId2[0]);
}
mesh.render(gl3);
}
public void render(GL3 gl3) {
render(gl3, modelMatrix);
}
}