gpt4 book ai didi

java - Opengl es 与 obj 加载器失真

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:05 24 4
gpt4 key购买 nike

我今天刚刚开始学习 OpenGL ES(我已经了解 WebGL 和 OpenGL),以便我可以继续我在 reddit 上发布的游戏项目 (reddit.com/r/gameideas/comments/3dsy8m/revolt/),并尝试移植我的 OBJLoader,但遇到了一个我无法解决的问题,想知道是否有人知道出了什么问题。我猜测它通常在 OpenGL 中默认启用,但在 OpenGL ES 中则不然。

这是扭曲的图像:

enter image description here

这是重要的代码: http://pastebin.com/1CgsJv21

public class GLMesh{
public static Activity activity;
private FloatBuffer verticesBuffer;
private FloatBuffer texcoordsBuffer;
private FloatBuffer normalsBuffer;
private float[] vertices;
private float[] texcoords;
private float[] normals;
private int numOfVertices;

public GLMesh(){
numOfVertices = 0;
}

public void setVertices(float[] vertices) {
this.vertices = vertices;
}

public void setTexcoords(float[] texcoords) {
this.texcoords = texcoords;
}

public void setNormals(float[] normals) {
this.normals = normals;
}

private static FloatBuffer asBuffer(float[] data){
ByteBuffer vbb = ByteBuffer.allocateDirect(data.length * 4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer outBuffer = vbb.asFloatBuffer();
outBuffer.put(data);
outBuffer.position(0);
return outBuffer;
}

public void load(String path){
// Define List of Vertices
ArrayList<GLVec3f> rVerts = new ArrayList<>();
ArrayList<GLVec2f> rTexs = new ArrayList<>();
ArrayList<GLVec3f> rNorms = new ArrayList<>();
ArrayList<GLVec3f> rTris = new ArrayList<>();
// Add Null Vertex
rVerts.add(null);
rTexs.add(null);
rNorms.add(null);
try{
InputStream in = activity.getAssets().open(path);
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null){
String[] splitLine = line.split(" ");
switch(splitLine[0]){
case "v":
float vX = Float.parseFloat(splitLine[1]);
float vY = Float.parseFloat(splitLine[2]);
float vZ = Float.parseFloat(splitLine[3]);
GLVec3f vertex = new GLVec3f(vX, vY, vZ);
rVerts.add(vertex);
break;
case "vt":
float tS = Float.parseFloat(splitLine[1]);
float tT = Float.parseFloat(splitLine[2]);
GLVec2f texcoord = new GLVec2f(tS, tT);
rTexs.add(texcoord);
break;
case "vn":
float nX = Float.parseFloat(splitLine[1]);
float nY = Float.parseFloat(splitLine[2]);
float nZ = Float.parseFloat(splitLine[3]);
GLVec3f normal = new GLVec3f(nX, nY, nZ);
rNorms.add(normal);
break;
case "f":
String[] vtn1 = splitLine[1].split("/");
String[] vtn2 = splitLine[2].split("/");
String[] vtn3 = splitLine[3].split("/");

float vIA = Float.parseFloat(vtn1[0]);
float vIB = Float.parseFloat(vtn2[0]);
float vIC = Float.parseFloat(vtn3[0]);

float tIA = Float.parseFloat(vtn1[1]);
float tIB = Float.parseFloat(vtn2[1]);
float tIC = Float.parseFloat(vtn3[1]);

float nIA = Float.parseFloat(vtn1[2]);
float nIB = Float.parseFloat(vtn2[2]);
float nIC = Float.parseFloat(vtn3[2]);

GLVec3f indexA = new GLVec3f(vIA, tIA, nIA);
GLVec3f indexB = new GLVec3f(vIB, tIB, nIB);
GLVec3f indexC = new GLVec3f(vIC, tIC, nIC);

rTris.add(indexA);
rTris.add(indexB);
rTris.add(indexC);
break;
}
}
}
catch(IOException e){
System.err.printf("Failed to fetch mesh @ %s", path);
return;
}
// Put Data into useful format
float[] verts = new float[rTris.size() * 3];
float[] texs = new float[rTris.size() * 2];
float[] norms = new float[rTris.size() * 3];
int i = 0;
// Get rid of need for indices
for(GLVec3f index: rTris){
// Collect Vertex Indices
int vI = (int) index.x;
int tI = (int) index.y;
int nI = (int) index.z;
// Collect Vertex Info
GLVec3f v = rVerts.get(vI);
GLVec2f t = rTexs.get(tI);
GLVec3f n = rNorms.get(nI);
// Populate arrays
verts[i * 3 + 0] = v.x;
verts[i * 3 + 1] = v.y;
verts[i * 3 + 2] = v.z;
texs[i * 2 + 0] = t.x;
texs[i * 2 + 1] = t.y;
norms[i * 3 + 0] = n.x;
norms[i * 3 + 1] = n.y;
norms[i * 3 + 2] = n.z;
// Increment Counter
i++;
}
numOfVertices = verts.length;
// Populate Mesh
setVertices(verts);
setTexcoords(texs);
setNormals(norms);
compile();
}

public void compile(){
verticesBuffer = asBuffer(vertices);
texcoordsBuffer = asBuffer(texcoords);
normalsBuffer = asBuffer(normals);
}

public void draw(GL10 gl){
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texcoordsBuffer);
gl.glNormalPointer(GL10.GL_FLOAT, 0, normalsBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, numOfVertices);

gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}

最佳答案

第三个参数传递给glDrawArrays是要渲染的索引(例如顶点)的数量。因此 numOfVertices 应该设置为已写入 verts 的顶点数,而不是 verts 中的 float :

numOfVertices = i;

关于java - Opengl es 与 obj 加载器失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31508331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com