纹理& Quad Drawing错误,Android OpenGL ES

时间:2011-07-20 02:46:54

标签: android opengl-es

我,对于我的生活,我无法使用OpenGL ES和Java获得简单的精灵绘图。图形编程绝对是我的弱点,而我对OpenGL的生锈并没有帮助,无论如何。

我正在尝试做什么

使用四元组将纹理四边形渲染到屏幕

我尝试了什么

我正在使用glDrawTexfOES,一切都很花花公子。但是,经过一些检查,我意识到我将无法使用我需要的翻译和轮换来构建更强大的系统;所以,我决定选择一个有纹理的四边形,因为很久以前,我在大学里就是这么做的。

问题

我的纹理最终会占据整个屏幕并且没有正确映射,这让我有些脾气暴躁。我希望能看到我的代码和一些专业意见。

结果

应该是这样的:

Correct Image

但是这是:

Incorrect Image

非常感谢你,提前!

我的四级课程:

public class Quad
{
    private float _Vertices[] = 
        {
            -1.0f, 1.0f, 0.0f,  // Top left
            -1.0f, -1.0f, 0.0f, // Bottom left
            1.0f, -1.0f, 0.0f,  // Bottom right
            1.0f, 1.0f, 0.0f,   // Top right
        };

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

    private FloatBuffer _VertexBuffer;
    private ShortBuffer _IndexBuffer;
    private FloatBuffer _TextureBuffer;

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

    public Quad()
    {
        Setup();
    }

    public void Setup()
    {
        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( _TextureCoords.length * 4 );
        tbb.order( ByteOrder.nativeOrder() );

        _TextureBuffer = tbb.asFloatBuffer();
        _TextureBuffer.put( _TextureCoords );
        _TextureBuffer.position( 0 );
    }

    public void Draw( GL10 gl, Texture texture )
    {               
        gl.glBindTexture( GL10.GL_TEXTURE_2D, texture.getID() );
        gl.glTexCoordPointer( 2, GL10.GL_FLOAT, 0, _TextureBuffer );

        gl.glVertexPointer( 3, GL10.GL_FLOAT, 0, _VertexBuffer );
        gl.glDrawElements( GL10.GL_TRIANGLES, _Indices.length,
                GL10.GL_UNSIGNED_SHORT, _IndexBuffer );
    }
}

我的渲染器:

public class FoxRenderer implements Renderer
{
private Context _Context;
private Quad _Quad;

private int _Width;
private int _Height;

public FoxRenderer( Context context, Game game )
{
    // Assignment
    _Context = context;
    _Game = game;

    _Quad = new Quad();
}

@Override
public void onDrawFrame(GL10 gl)
{
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();

    _Quad.Draw( gl, _Game.getGameData().TextureSystem.GetTexture( "buddha") );
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
    _Width = width;
    _Height = height;

    gl.glViewport(0, 0, width, height);

    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(0.0f, width, 0.0f, height, 0.0f, 1.0f);
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
    // Initialize various OpenGL elements
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

    gl.glClearColor(1f, 1f, 1f, 1);
    gl.glShadeModel(GL10.GL_FLAT);
    gl.glDisable(GL10.GL_DEPTH_TEST);
    gl.glEnable(GL10.GL_TEXTURE_2D);

    // Disabling certain elements for performance
    gl.glDisable(GL10.GL_DITHER);
    gl.glDisable(GL10.GL_LIGHTING);

    // Stuff for rendering quads
    gl.glFrontFace( GL10.GL_CCW );
    gl.glEnable( GL10.GL_CULL_FACE );
    gl.glCullFace( GL10.GL_BACK );

    gl.glShadeModel(GL10.GL_FLAT);
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

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

而且,以防万一,我的纹理加载:

public void LoadTexture( String name, int resource )
{
    Texture texture = new Texture();

    // LOADING TEXTURE CODE
    int[] temp = new int[ 1 ];

    _GL10.glGenTextures( 1, temp, 0 );

    // Get newly generated OpenGL texture ID and bind it and store in our texture class
    int id = temp[ 0 ];
    _GL10.glBindTexture( GL10.GL_TEXTURE_2D, id );
    texture.setID( id );

    _GL10.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST );
    _GL10.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST );
    _GL10.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT );
    _GL10.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT );
    _GL10.glTexEnvf( GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE );

    // Load actual resource into bitmap
    Bitmap bitmap = BitmapFactory.decodeResource( _Context.getResources(), resource );

    // Not 100% sure what this does
    GLUtils.texImage2D( GL10.GL_TEXTURE_2D, 0, bitmap, 0 );

    texture.setWidth( bitmap.getWidth() );
    texture.setHeight( bitmap.getHeight() );

    _Map.put( name, texture );

    bitmap.recycle();
}

1 个答案:

答案 0 :(得分:2)

我最终在我工作的图形工程师的帮助下解决了这个问题。只需要改变我的onSurfaceChanged:

public void onSurfaceChanged(GL10 gl, int width, int height)
{
    _Width = width;
    _Height = height;

    gl.glViewport(0, 0, width, height);

        gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
        gl.glOrthof(0.0f, width, height, 0.0f, 0.0f, 1.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
}

也改为顺时针缠绕:

gl.glFrontFace( GL10.GL_CW );

改变了我的顶点:

private static float _Vertices[] = 
{
    -0.5f, -0.5f, 0.0f, 
    0.5f, -0.5f, 0.0f,  
    0.5f, 0.5f, 0.0f,   
    -0.5f, 0.5f, 0.0f,  
};

现在一切都按照我想要的方式运行,包括让它在大多数引擎中都像屏幕空间一样(正Y向下)。