简体   繁体   English

白色平方的纹理Opengl-es Android

[英]White Squared Textures Opengl-es Android

I'm currently getting nothing but a coloured square where my textures should be drawing. 我目前什么都没看到,只有一个彩色正方形可以在其中绘制我的纹理。

I have made sure to size the texture with a power of 2 (128 x 128) 我确保使用2(128 x 128)的幂来缩放纹理

I have placed the texture in the drawable-nodpi folder like suggested, but still I get a white coloured (or other colour accordingly) square. 我已按照建议将纹理放置在drawable-nodpi文件夹中,但仍然得到白色(或相应其他颜色)的正方形。

Here is my square object code: 这是我的方形目标代码:

public class MTestSquare {




private float vertices[] = {
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 0.0f,
        1.0f, 0.0f, 0.0f,
        1.0f, 1.0f, 0.0f
};

private short indices[] = {0, 1, 2, 0, 2, 3,};

private float texture[] = {
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f
};
private FloatBuffer vertexBuffer;

private FloatBuffer textureBuffer;

private ShortBuffer indexBuffer;

private int[] textures = new int[1];





public MTestSquare() {

    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
       vbb.order(ByteOrder.nativeOrder());
       vertexBuffer = vbb.asFloatBuffer();
       vertexBuffer.put(vertices);
       vertexBuffer.position(0);

    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*2);
       ibb.order(ByteOrder.nativeOrder());
       indexBuffer = ibb.asShortBuffer();
       indexBuffer.put(indices);
       indexBuffer.position(0);

     ByteBuffer tbb = ByteBuffer.allocateDirect(texture.length * 4);
         tbb.order(ByteOrder.nativeOrder());
         textureBuffer = tbb.asFloatBuffer();
         textureBuffer.put(texture);
         textureBuffer.position(0);


}


public void loadGLTexture(GL10 gl, Context context) {

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.testtexture);
    gl.glGenTextures(1, textures, 0);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

}

public void draw(GL10 gl) {

    gl.glFrontFace(GL10.GL_CCW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_BACK);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, 
                      GL10.GL_UNSIGNED_SHORT, indexBuffer);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
}

}

I draw the square in on draw square with 我用

square.draw(gl)

and I have put this line of code in onSurfaceCreated 我把这行代码放在onSurfaceCreated中

square.loadGLTexture(gl, this.context);

to load the texture 加载纹理

您实际上启用纹理了吗?

gl.glEnable(GL10.GL_TEXTURE_2D);

Maybe you can review this content. 也许您可以查看此内容。 OpenGL Texture Tutorial OpenGL纹理教程

int InitGL(GLvoid)                              // All Setup For OpenGL Goes Here
{
    if (!LoadGLTextures())                          // Jump To Texture Loading Routine ( NEW )
    {
        return FALSE;                           // If Texture Didn't Load Return FALSE ( NEW )
    }

    glEnable(GL_TEXTURE_2D);                        // Enable Texture Mapping ( NEW )
    glShadeModel(GL_SMOOTH);                        // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                   // Black Background
    glClearDepth(1.0f);                         // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                        // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                         // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);          // Really Nice Perspective Calculations
    return TRUE;                                // Initialization Went OK
}

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

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