所以我在Android中使用Open GL ES 2.0存在这个问题,在更改旋转矩阵时,它似乎拉伸或展平我的模型。我已经多次搜索过bing并且还没有找到这个问题的答案。我还注意到我的一些面孔没有被绘制或被绘制得错误,即使我仔细检查以确保它们都是正确的顺序,我发现它们是。
这是我的.OBJ模型加载代码
ArrayList<float[]> vertexData = new ArrayList<>();
ArrayList<Integer> indexData = new ArrayList<>();
ArrayList<float[]> normalsData = new ArrayList<>();
ArrayList<Integer> normalsIndexData = new ArrayList<>();
InputStream inputStream = manager.open(modelAddres);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
String args[];
while ((line = reader.readLine()) != null)
{
args = line.split(" ");
if(args[0].equals("v"))
{
//add new vertex
float vertex[] = new float[COORDS_PER_VERTEX];
vertex[0] = Float.parseFloat(args[1]);
vertex[1] = Float.parseFloat(args[2]);
vertex[2] = Float.parseFloat(args[3]);
vertexData.add(vertex);
}
else if(args[0].equals("vn"))
{
//read new normal
float normal[] = new float[3];
normal[0] = Float.parseFloat(args[1]);
normal[1] = Float.parseFloat(args[2]);
normal[2] = Float.parseFloat(args[3]);
normalsData.add(normal);
}
else if(args[0].equals("f"))
{
//get first face point
String[] face = args[1].split("/");
//read vertex
int index = Integer.parseInt(face[0])-1;
indexData.add(index);
//read normal index data
index = Integer.parseInt(face[2])-1;
normalsIndexData.add(index);
//get second face point
face = args[2].split("/");
//read vertex
index = Integer.parseInt(face[0])-1;
indexData.add(index);
//read normal index data
index = Integer.parseInt(face[2])-1;
normalsIndexData.add(index);
//get third face point
face = args[3].split("/");
//read vertex
index = Integer.parseInt(face[0])-1;
indexData.add(index);
//read normal index data
index = Integer.parseInt(face[2])-1;
normalsIndexData.add(index);
}
else if(args[0].equals("texture"))
{
//store the ships texture name
bitmapName = args[1];
}
}
ArrayList<Float> verticies = new ArrayList<>();
ArrayList<Float> normals = new ArrayList<>();
ArrayList<Float> textures = new ArrayList<>();
//finished parsing file so read in vetex data
for(int i = 0;i < indexData.size();i++ )
{
int index = indexData.get(i);
float[] vertex = vertexData.get(index);
verticies.add(vertex[0]);
verticies.add(vertex[1]);
verticies.add(vertex[2]);
//read normal
index = normalsIndexData.get(i);
float[] normal = normalsData.get(index);
normals.add(normal[0]);
normals.add(normal[1]);
normals.add(normal[2]);
}
m_vertices = new float[verticies.size()];
for(int i = 0;i < m_vertices.length;i++)
{
m_vertices[i] = verticies.get(i);
}
m_normals = new float[normals.size()];
for(int i = 0;i < m_normals.length;i++)
{
m_normals[i] = normals.get(i);
}
这是我的绘图代码:
//Add program to openGL ES enviroment
GLES20.glUseProgram(m_program);
//multiply wvp;
float[] MVP = new float[16];
Matrix.multiplyMM(MVP,0,projection,0,view,0);
float[] rotateMatrix = new float[16];
Matrix.multiplyMM(MVP,0,MVP,0,modelMatrix,0);
//get handle to vertex shaders vPosition member
int positionHandle = GLES20.glGetAttribLocation(m_program,"vPosition");
//enable a handle to the models vertices
GLES20.glEnableVertexAttribArray(positionHandle);
//Prepare the triangle
GLES20.glVertexAttribPointer(positionHandle, model.COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false, model.COORDS_PER_VERTEX * 4, model.getVertexBuffer()
);
int normalHandle = GLES20.glGetAttribLocation(m_program,"aNormal");
GLES20.glEnableVertexAttribArray(normalHandle);
GLES20.glVertexAttribPointer(normalHandle, model.COORDS_PER_VERTEX, GLES20.GL_FLOAT,
false, model.COORDS_PER_VERTEX * 4, model.getNormalsBuffer());
//get the handle to the framet shaders color member
int colorHandle = GLES20.glGetUniformLocation(m_program,"vColor");
//set color for drawing the triangle
GLES20.glUniform4fv(colorHandle, 1, color, 0);
//get handle to shapes transfomr matrix
int MVPHandle = GLES20.glGetUniformLocation(m_program,"uMVPMatrix");
//pass the mvp matrix
GLES20.glUniformMatrix4fv(MVPHandle,1,false,MVP,0);
int MHandle = GLES20.glGetUniformLocation(m_program,"uMMatrix");
//pass the model matrix;
GLES20.glUniformMatrix4fv(MHandle,1,false,modelMatrix,0);
int VHandle = GLES20.glGetUniformLocation(m_program,"uVMatrix");
//pass the view matrix
GLES20.glUniformMatrix4fv(VHandle,1,false,view,0);
int PHandle = GLES20.glGetUniformLocation(m_program,"uPMatrix");
GLES20.glUniformMatrix4fv(PHandle,1,false,projection,0);
//GLES20.glDisable(GLES20.GL_CULL_FACE);
//Draw the model
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, model.getVertexCount());
//Disable vertex array
GLES20.glDisableVertexAttribArray(positionHandle);
GLES20.glDisableVertexAttribArray(normalHandle);
这是我的代码转换我的矩阵
float[] translation = new float[16];
float[] rotation = new float[16];
float[] transformation = new float[16];
//identify translation
Matrix.setIdentityM(translation, 0);
//translate translation
Matrix.translateM(translation, 0, translation, 0, m_x, m_y, m_z);
Matrix.setIdentityM(rotation, 0);
//rotate rotation matrix
Matrix.rotateM(rotation, 0, rotation,0, m_rotX, 1, 0, 0);
Matrix.rotateM(rotation,0,rotation,0,m_rotY,0,1,0);
Matrix.rotateM(rotation,0,rotation,0,m_rotZ,0,0,1);
//set transform matrix
Matrix.setIdentityM(transformation,0);
Matrix.scaleM(transformation, 0, transformation, 0, m_scaleX, m_scaleY, m_scaleZ);
//update matrix
Matrix.multiplyMM(m_matrix,0,translation,0,rotation,0);
Matrix.multiplyMM(m_matrix,0,m_matrix,0,transformation,0);
m_needsGenerated = false;
着色器
precision mediump float;
uniform vec4 vColor;
uniform sampler2D uTexture;
varying vec4 vNormal;
varying vec2 vTexCoord;
void main()
{
gl_FragColor = vColor*vNormal;
}
uniform mat4 uMVPMatrix;
uniform mat4 uMMatrix;
uniform mat4 uVMatrix;
uniform mat4 uPMatrix;
attribute vec4 vPosition;
attribute vec4 aNormal;
attribute vec2 aTexCoord;
varying vec4 vNormal;
varying vec2 vTexCoord;
void main() {
gl_Position = uPMatrix*uVMatrix*uMMatrix*vPosition;
vNormal = aNormal;
vTexCoord = aTexCoord;
}
编辑:我不是最好的总结,所以如果你需要我澄清我的问题或更具体我可以做到。
答案 0 :(得分:0)
我最终能够解决这个问题,因为我一直在旋转相同的矩阵,所以它旋转了3个不同的旋转基质,然后将它们相乘,它工作得很好。我通过简单地启用深度缓冲测试来修复错误,而不是渲染。