Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Model {
- ArrayList<Vertex> vertices = new ArrayList<Vertex>();
- ArrayList<Vector3f> pos = new ArrayList<Vector3f>();
- ArrayList<Vector2f> tex = new ArrayList<Vector2f>();
- ArrayList<Vector3f> norm = new ArrayList<Vector3f>();
- ArrayList<FaceData> faces = new ArrayList<FaceData>();
- public ModelTransformation transformation;
- int vbo;
- public int indexCount;
- public int vertexCount;
- Texture texture;
- public LightingTechnique effect;
- public Model(String name)
- {
- texture = new Texture("asd", "/res/texture.png", GL13.GL_TEXTURE0);
- parseObj(name);
- FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(vertices.size() * 8);
- for(Vertex v : vertices)
- {
- vertBuffer.put(v.getElements());
- vertexCount++;
- }
- vertBuffer.flip();
- vbo = GL15.glGenBuffers();
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
- GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
- effect = new LightingTechnique();
- effect.init();
- effect.enable();
- effect.setTextureUnit(0);
- effect.disable();
- transformation = new ModelTransformation();
- transformation.worldPos(0, 0, 0);
- }
- public void startRender()
- {
- effect.enable();
- effect.setWorld(transformation.getWorld());
- effect.setWVP(transformation.getWVP(Main.cam));
- effect.setEyeWorldPos(Main.cam.pos);
- effect.setMatSpecularIntensity(0.0f);
- effect.setMatSpecularPower(0);
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId());
- GL20.glEnableVertexAttribArray(0);
- GL20.glEnableVertexAttribArray(1);
- GL20.glEnableVertexAttribArray(2);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
- GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 32, 0);
- GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 32, 12);
- GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, 32, 20);
- }
- public void render()
- {
- GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertexCount);
- }
- public void endRender()
- {
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
- GL20.glDisableVertexAttribArray(0);
- GL20.glDisableVertexAttribArray(1);
- GL20.glDisableVertexAttribArray(2);
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
- effect.disable();
- }
- public void parseObj(String path)
- {
- try {
- BufferedReader reader = new BufferedReader(new FileReader(new File("").getAbsolutePath() + "/res/" + path));
- String line = reader.readLine();
- while(line != null)
- {
- if(line.startsWith("v "))
- {
- int start = line.indexOf(" ");
- String sub = line.substring(start + 1);
- String[] vertexStr = sub.split(" " );
- pos.add(new Vector3f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1]), Float.parseFloat(vertexStr[2])));
- }
- if(line.startsWith("vt "))
- {
- int start = line.indexOf(" ");
- String sub = line.substring(start + 1);
- String[] vertexStr = sub.split(" " );
- tex.add(new Vector2f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1])));
- }
- if(line.startsWith("vn "))
- {
- int start = line.indexOf(" ");
- String sub = line.substring(start + 1);
- String[] vertexStr = sub.split(" " );
- norm.add(new Vector3f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1]), Float.parseFloat(vertexStr[2])));
- }
- if(line.startsWith("f "))
- {
- int start = line.indexOf(" ");
- String sub = line.substring(start + 1);
- String[] vertexStr = sub.split(" ");
- for(int i = 0; i < vertexStr.length; i++)
- {
- String verStr = vertexStr[i].replaceAll("//", "/0/");
- String[] asd = verStr.split("/");
- FaceData data = new FaceData();
- data.vertexID = Integer.parseInt(asd[0]);
- data.texID = Integer.parseInt(asd[1]);
- data.normalID = Integer.parseInt(asd[2]);
- faces.add(data);
- }
- }
- line = reader.readLine();
- }
- reader.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- for(int i = 0; i < faces.size(); i++)
- {
- Vertex v = new Vertex();
- v.pos = pos.get(faces.get(i).vertexID - 1);
- if(faces.get(i).texID != 0)
- {
- Vector2f t = new Vector2f();
- t = tex.get(faces.get(i).texID - 1);
- t.y = t.y;
- v.texCoord.set(t);
- }
- else
- v.texCoord = new Vector2f(0,0);
- v.normal = norm.get(faces.get(i).normalID - 1);
- vertices.add(v);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement