简体   繁体   English

Android OpenGL ES 2.0:球体纹理贴图

[英]Android OpenGL ES 2.0: Sphere Texture Mapping

I have created 2 sphere's, and I am currently trying to texture them. 我创建了2个球体,目前正在尝试对其进行纹理处理。 However the texture coordinates seem to be a bit off. 但是,纹理坐标似乎有点偏离。

The Texture (Just supposed to be a window or some kind of break): 纹理(应该是一个窗口或某种中断):
质地

The result of the texture being applied to both spheres (You can ignore the black lines not around the square, that's just the camera - yes it's an AR app): 将纹理应用于两个球体的结果(您可以忽略方形周围的黑线,这只是相机-是的,它是一个AR应用程序):

飞船

Now I exported the Sphere using a perl script I found that converts obj files to a list of vertices and texture coords OBJ2OPENGL . 现在,我使用发现的perl脚本导出了Sphere,该脚本将obj文件转换为顶点和纹理坐标OBJ2OPENGL的列表。 And the obj file has been converted with 2160 Vertices. 并且obj文件已使用2160顶点进行了转换。

When I render the sphere: 当我渲染球体时:

    GLES20.glVertexAttribPointer(vertexHandle, 3, GLES20.GL_FLOAT,
            false, 0, ufo_sphere.getVertices());
    GLES20.glVertexAttribPointer(normalHandle, 3, GLES20.GL_FLOAT,
            false, 0, ufo_sphere.getNormals());
    GLES20.glVertexAttribPointer(textureCoordHandle, 3,
            GLES20.GL_FLOAT, false, 0, ufo_sphere.getTexCoords());

    GLES20.glEnableVertexAttribArray(vertexHandle);
    GLES20.glEnableVertexAttribArray(normalHandle);
    GLES20.glEnableVertexAttribArray(textureCoordHandle);

    // activate texture 0, bind it, and pass to shader
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,
            mTextures.get(textureIndex).mTextureID[0]);
    GLES20.glUniform1i(texSampler2DHandle, 0);

    // pass the model view matrix to the shader
    GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false,
            modelViewProjection, 0);


    //GLES20.glDrawElements(GLES20.GL_TRIANGLES, sphere.getNumObjectVertex(), GLES20.GL_UNSIGNED_SHORT, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, sphere.getNumObjectVertex()); // I have tried TRIANGLES and TRIANGLE_FAN as well

    // disable the enabled arrays
    GLES20.glDisableVertexAttribArray(vertexHandle);
    GLES20.glDisableVertexAttribArray(normalHandle);
    GLES20.glDisableVertexAttribArray(textureCoordHandle);

I can see at least one thing which seems to be wrong... 我可以看到至少一件事似乎是错误的...

GLES20.glVertexAttribPointer(textureCoordHandle, 3,
        GLES20.GL_FLOAT, false, 0, ufo_sphere.getTexCoords());

Texture coordinates typically have a size of 2 (u, v). 纹理坐标的大小通常为2(u,v)。 So you could try this instead: 因此,您可以尝试以下方法:

GLES20.glVertexAttribPointer(textureCoordHandle, 2,
        GLES20.GL_FLOAT, false, 0, ufo_sphere.getTexCoords());

Also, in the link you provided they use the GL_TRIANGLES format instead of GL_TRIANGLE_STRIP so you should switch back to that. 另外,在您提供的链接中,他们使用GL_TRIANGLES格式而不是GL_TRIANGLE_STRIP格式,因此您应切换回该格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM