Repository navigation
Expand file tree
/
Copy pathMyMesh.java
More file actions
188 lines (123 loc) · 5.65 KB
/
Copy pathMyMesh.java
File metadata and controls
188 lines (123 loc) · 5.65 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
public class MyMesh {
private int vbo; // Acts as a sort of pointer/handle
private int ibo;
private int size;
private void initMeshData(){
vbo = glGenBuffers();
ibo = glGenBuffers();
size = 0;
}
public MyMesh(String filename){
initMeshData();
loadMesh(filename);
}
public MyMesh(Vertex[] vertices, int[] indices){
this(vertices, indices, false);
}
public MyMesh(Vertex[] vertices, int[] indices, boolean calcNormals){
initMeshData();
addVertices(vertices, indices, calcNormals);
}
// private void addVertices(Vertex[] vertices, int[] indices){
//
// addVertices(vertices, indices, false);
//
// }
private void addVertices(Vertex[] vertices, int[] indices, boolean calcNormals){
if (calcNormals){
calcNormals(vertices, indices);
}
size = indices.length; // * Vertex.VERTEX_SIZE;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertices), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Util.createFlippedBuffer(indices), GL_STATIC_DRAW);
}
public void draw(){
glEnableVertexAttribArray(0); // Actual shape data
glEnableVertexAttribArray(1); // Texture data
glEnableVertexAttribArray(2); // Normals data
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Position Data pointer:
glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.VERTEX_SIZE * 4, 0);
// Texture position pointer:
glVertexAttribPointer(1, 2, GL_FLOAT, false, Vertex.VERTEX_SIZE * 4, 12);
// Normals pointer:
glVertexAttribPointer(2, 3, GL_FLOAT, false, Vertex.VERTEX_SIZE * 4, 20);
// glDrawArrays(GL_TRIANGLES, 0, size); // causes strange issues when loading levels
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
}
private void calcNormals(Vertex[] vertices, int[] indices){
for (int i = 0; i < indices.length; i += 3) {
int i0 = indices[i];
int i1 = indices[i + 1];
int i2 = indices[i + 2];
Vector3F v1 = vertices[i1].getPosition().sub(vertices[i0].getPosition());
Vector3F v2 = vertices[i2].getPosition().sub(vertices[i0].getPosition());
Vector3F normal = v1.crossProd(v2).normalized();
vertices[i0].setNormal(vertices[i0].getNormal().add(normal));
vertices[i1].setNormal(vertices[i1].getNormal().add(normal));
vertices[i2].setNormal(vertices[i2].getNormal().add(normal));
}
for (int i = 0; i < vertices.length; i++) {
vertices[i].setNormal(vertices[i].getNormal().normalized());
}
}
private MyMesh loadMesh(String filename) {
String[] splitArray = filename.split("\\.");
String fileExt = splitArray[splitArray.length - 1];
if (!fileExt.equals("obj")) {
System.err.println("Invalid file type, only .obj files are accepted.");
new Exception().printStackTrace();
System.exit(1);
}
ArrayList<Vertex> vertices = new ArrayList<Vertex>();
ArrayList<Integer> indices = new ArrayList<Integer>();
BufferedReader objReader;
try {
objReader = new BufferedReader(new FileReader("./res/models/" + filename));
String line;
while ((line = objReader.readLine()) != null) {
String[] tokens = line.split(" ");
tokens = Util.removeEmptyStrings(tokens);
// if (tokens.length == 0 || tokens[0].equals("#")) {
// continue;
// } else
if (tokens.length > 0){ // In case we hit a blank line
if (tokens[0].equals("v")) {
vertices.add(new Vertex(new Vector3F(Float.parseFloat(tokens[1]), Float.parseFloat(tokens[2]), Float.parseFloat(tokens[3]))));
} else if (tokens[0].equals("f")){
indices.add(Integer.parseInt(tokens[1].split("/")[0]) - 1);
indices.add(Integer.parseInt(tokens[2].split("/")[0]) - 1);
indices.add(Integer.parseInt(tokens[3].split("/")[0]) - 1);
if (tokens.length > 4){
indices.add(Integer.parseInt(tokens[1].split("/")[0]) - 1);
indices.add(Integer.parseInt(tokens[3].split("/")[0]) - 1);
indices.add(Integer.parseInt(tokens[4].split("/")[0]) - 1);
}
}
}
}
objReader.close();
Vertex[] vertices1 = new Vertex[vertices.size()];
Integer[] indices1 = new Integer[indices.size()];
vertices.toArray(vertices1);
indices.toArray(indices1);
addVertices(vertices1, Util.integerToIntArr(indices1), true);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
}