2011-07-20 60 views
1

对于我的生活,我无法使用OpenGL ES和Java获取简单的精灵画图。图形编程绝对是我的薄弱环节,而我对OpenGL的生锈并没有什么帮助,甚至如此。纹理和四方图绘制错误,Android OpenGL ES

我试图做

使用四类来渲染一个纹理四屏幕

我已经试过

我用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(); 
} 

回答

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向下)。

+0

你应该接受这个答案。 –

+0

哈哈,我打算:)我只需要再等8个小时,显然,在我可以接受我自己的答案之前。不过谢谢你的帮助!非常感谢! –