Aaaaa988

Object.java

Dec 11th, 2020 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.85 KB | None | 0 0
  1. package com.example.user.newcurswork;
  2.  
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;
  6. import java.nio.FloatBuffer;
  7. import java.nio.ShortBuffer;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11.  
  12. import android.content.Context;
  13. import android.opengl.GLES20;
  14.  
  15. class Objects {
  16.     private FloatBuffer colorBuffer;
  17.  
  18.     private FloatBuffer verticesBuffer;
  19.     private FloatBuffer normalBuffer;
  20.  
  21.     private ShortBuffer facesVertexBuffer;
  22.     private ShortBuffer facesNormalBuffer;
  23.  
  24.     private List<String> facesList;
  25.  
  26.     Objects(Context c, float[] color, String ObjName) {
  27.         List<String> verticesList = new ArrayList<>();
  28.         facesList = new ArrayList<>();
  29.         List<String> normalList = new ArrayList<>();
  30.         try {
  31.             Scanner scanner = new Scanner(c.getAssets().open(ObjName));
  32.             while (scanner.hasNextLine()) {
  33.                 String line = scanner.nextLine();
  34.                 if (line.startsWith("v ")) {
  35.                     verticesList.add(line);
  36.                 } else if (line.startsWith("f ")) {
  37.                     facesList.add(line);
  38.                 } else if (line.startsWith("vn ")) {
  39.                     normalList.add(line);
  40.                 } else if (line.startsWith("vt ")) {
  41.                     continue;
  42.                 }
  43.             }
  44.             ByteBuffer buffer1 = ByteBuffer.allocateDirect(verticesList.size() * 3 * 4);
  45.             buffer1.order(ByteOrder.nativeOrder());
  46.             verticesBuffer = buffer1.asFloatBuffer();
  47.  
  48.             ByteBuffer buffer2 = ByteBuffer.allocateDirect(normalList.size() * 3 * 4);
  49.             buffer2.order(ByteOrder.nativeOrder());
  50.             normalBuffer = buffer2.asFloatBuffer();
  51.  
  52.             ByteBuffer buffer3 = ByteBuffer.allocateDirect(facesList.size() * 3 * 2);
  53.             buffer3.order(ByteOrder.nativeOrder());
  54.             facesVertexBuffer = buffer3.asShortBuffer();
  55.  
  56.             ByteBuffer buffer4 = ByteBuffer.allocateDirect(facesList.size() * 3 * 2);
  57.             buffer4.order(ByteOrder.nativeOrder());
  58.             facesNormalBuffer = buffer4.asShortBuffer();
  59.  
  60.             for (String vertex : verticesList) {
  61.                 String coords[] = vertex.split(" ");
  62.                 float x = Float.parseFloat(coords[1]);
  63.                 float y = Float.parseFloat(coords[2]);
  64.                 float z = Float.parseFloat(coords[3]);
  65.                 verticesBuffer.put(x);
  66.                 verticesBuffer.put(y);
  67.                 verticesBuffer.put(z);
  68.             }
  69.             verticesBuffer.position(0);
  70.  
  71.             for (String vertex : normalList) {
  72.                 String coords[] = vertex.split(" ");
  73.                 float x = Float.parseFloat(coords[1]);
  74.                 float y = Float.parseFloat(coords[2]);
  75.                 float z = Float.parseFloat(coords[3]);
  76.                 normalBuffer.put(x);
  77.                 normalBuffer.put(y);
  78.                 normalBuffer.put(z);
  79.             }
  80.             normalBuffer.position(0);
  81.  
  82.             for (String face : facesList) {
  83.                 String vertexIndices[] = face.split(" ");
  84.                 String coord1[] = vertexIndices[1].split("//");
  85.                 String coord2[] = vertexIndices[2].split("//");
  86.                 String coord3[] = vertexIndices[3].split("//");
  87.  
  88.                 short vertex1 = Short.parseShort(coord1[0]);
  89.                 short vertex2 = Short.parseShort(coord2[0]);
  90.                 short vertex3 = Short.parseShort(coord3[0]);
  91.                 facesVertexBuffer.put((short) (vertex1 - 1));
  92.                 facesVertexBuffer.put((short) (vertex2 - 1));
  93.                 facesVertexBuffer.put((short) (vertex3 - 1));
  94.  
  95.                 vertex1 = Short.parseShort(coord1[1]);
  96.                 vertex2 = Short.parseShort(coord2[1]);
  97.                 vertex3 = Short.parseShort(coord3[1]);
  98.                 facesNormalBuffer.put((short) (vertex1 - 1));
  99.                 facesNormalBuffer.put((short) (vertex2 - 1));
  100.                 facesNormalBuffer.put((short) (vertex3 - 1));
  101.             }
  102.             facesVertexBuffer.position(0);
  103.             facesNormalBuffer.position(0);
  104.  
  105.             verticesList.clear();
  106.             normalList.clear();
  107.  
  108.             scanner.close();
  109.         } catch (IOException e) {
  110.             e.printStackTrace();
  111.         }
  112.  
  113.         float[] colorData = new float[facesList.size() * 4];
  114.         for (int v = 0; v < facesList.size(); v++) {
  115.             colorData[4 * v] = color[0];
  116.             colorData[4 * v + 1] = color[1];
  117.             colorData[4 * v + 2] = color[2];
  118.             colorData[4 * v + 3] = color[3];
  119.         }
  120.  
  121.         ByteBuffer bColor = ByteBuffer.allocateDirect(colorData.length * 4);
  122.         bColor.order(ByteOrder.nativeOrder());
  123.         colorBuffer = bColor.asFloatBuffer();
  124.         colorBuffer.put(colorData).position(0);
  125.     }
  126.  
  127.     void render(int positionAttribute, int normalAttribute, int colorAttribute, boolean onlyPosition) {
  128.         facesVertexBuffer.position(0);
  129.         facesNormalBuffer.position(0);
  130.         verticesBuffer.position(0);
  131.         normalBuffer.position(0);
  132.         colorBuffer.position(0);
  133.  
  134.         GLES20.glVertexAttribPointer(positionAttribute, 3, GLES20.GL_FLOAT, false,
  135.                 0, verticesBuffer);
  136.         GLES20.glEnableVertexAttribArray(positionAttribute);
  137.  
  138.         if (!onlyPosition) {
  139.             GLES20.glVertexAttribPointer(normalAttribute, 3, GLES20.GL_FLOAT, false,
  140.                     0, normalBuffer);
  141.             GLES20.glEnableVertexAttribArray(normalAttribute);
  142.  
  143.             GLES20.glVertexAttribPointer(colorAttribute, 4, GLES20.GL_FLOAT, false,
  144.                     0, colorBuffer);
  145.             GLES20.glEnableVertexAttribArray(colorAttribute);
  146.         }
  147.  
  148.         GLES20.glDrawElements(GLES20.GL_TRIANGLES, facesList.size() * 3,
  149.                 GLES20.GL_UNSIGNED_SHORT, facesVertexBuffer);
  150.     }
  151. }
Add Comment
Please, Sign In to add comment