This repository was archived by the owner on Feb 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCube.java
135 lines (117 loc) · 4.39 KB
/
Cube.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
import gmaths.*;
import java.nio.*;
import com.jogamp.common.nio.*;
import com.jogamp.opengl.*;
/**
* Cube.java
* Cube class with functions for setting up shader data and rendering cube meshes
*
* @author Will Garside // worgarside@gmail.com
* @version 1.0 2017-12-06
*/
public class Cube extends Mesh {
private int[] textureMain;
private int[] textureSpec;
private static final Vec3 SCENE_AMBIENT = new Vec3(0.1f, 0.1f, 0.1f);
/**
* Constructor for Cube object
*
* @param gl - graphics library
* @param textureMain - the ID of the main textureMain
* @param textureSpec - the ID of the specular textureMain
*/
public Cube(GL3 gl, int[] textureMain, int[] textureSpec) {
super(gl);
super.vertices = this.vertices;
super.indices = this.indices;
this.textureMain = textureMain;
this.textureSpec = textureSpec;
material.setAmbient(SCENE_AMBIENT);
material.setShininess(32.0f);
shader = new Shader(gl, "shaders/vs_object.glsl", "shaders/fs_object.glsl");
fillBuffers(gl);
}
/**
* Initialise matrices and vectors and sends them to the shaders
*
* @param gl - grpahics library
* @param model - the model matrix used for the Cube
*/
public void render(GL3 gl, Mat4 model) {
Mat4 mvpMatrix = Mat4.multiply(perspective, Mat4.multiply(camera.getViewMatrix(), model));
shader.use(gl);
shader.setFloatArray(gl, "model", model.toFloatArrayForGLSL());
shader.setFloatArray(gl, "mvpMatrix", mvpMatrix.toFloatArrayForGLSL());
shader.setVec3(gl, "viewPos", camera.getPosition());
// Uses Mesh class to create shader values
for (int i = 0; i < Arty.lightCount; i++) {
super.setShaderValues(gl, shader, i, SCENE_AMBIENT);
}
shader.setInt(gl, "material.diffuse", 0);
shader.setInt(gl, "material.specular", 1);
shader.setFloat(gl, "material.shininess", material.getShininess());
gl.glActiveTexture(GL.GL_TEXTURE0);
gl.glBindTexture(GL.GL_TEXTURE_2D, textureMain[0]);
gl.glActiveTexture(GL.GL_TEXTURE1);
gl.glBindTexture(GL.GL_TEXTURE_2D, textureSpec[0]);
gl.glBindVertexArray(vertexArrayId[0]);
gl.glDrawElements(GL.GL_TRIANGLES, indices.length, GL.GL_UNSIGNED_INT, 0);
gl.glBindVertexArray(0);
}
/**
* Removes meshes from memory on system exit
*
* @param gl
*/
public void dispose(GL3 gl) {
super.dispose(gl);
gl.glDeleteBuffers(1, textureMain, 0);
gl.glDeleteBuffers(1, textureSpec, 0);
}
/**
* Vertices used in a Cube mesh
*/
private float[] vertices = new float[] {
-0.5f, -0.5f, -0.5f, -1, 0, 0, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -1, 0, 0, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1, 0, 0, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, -1, 0, 0, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1, 0, 0, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1, 0, 0, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1, 0, 0, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1, 0, 0, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0, 0, -1, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0, 0, 1, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0, 0, -1, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0, 0, 1, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0, 0, -1, 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