在OpenGL ES中反转绘制的线条图

时间:2015-07-24 08:38:35

标签: android opengl-es

我试图在OpenGL ES中绘制连接多个顶点的简单线条图。但是,该线被倒置或与应该绘制的位置不同。我已经为下面的线条图添加了课程

ConnectingPath.java 
--------------------

public class ConnectingPath {

int positionBufferId;

PointF[] verticesList;
public float vertices[];
public FloatBuffer vertexBuffer;

public ConnectingPath(LinkedList<PointF> verticesList, float[] colors)
{
    List<PointF> tempCorners = verticesList;

    int i = 0;
    this.verticesList = new PointF[tempCorners.size()];
    for (PointF corner : tempCorners) {
        this.verticesList[i++] = corner;
    }

}

public float[] getTransformedVertices()
{
    float z;
    List<Float> finalVertices = new ArrayList<Float>();

    finalVertices.clear();
    for(PointF point : verticesList){
        finalVertices.add(point.x);
        finalVertices.add(point.y);
        finalVertices.add(0.0f);
    }

    int i = 0;
    float[] verticesArray = new float[finalVertices.size()];
    for (Float f : finalVertices) {
        verticesArray[i++] = (f != null ? f : Float.NaN);
    }
    return verticesArray;
}

public void initBooth(){
    vertices = this.getTransformedVertices();

    for(Float f : vertices){
        Log.d("Mapsv3--", f + "");
    }

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

    int[] buffers = new int[1];
    GLES11.glGenBuffers(1, buffers, 0);
    GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, buffers[0]);
    GLES11.glBufferData(GLES11.GL_ARRAY_BUFFER, 4 * vertices.length, vertexBuffer, GLES11.GL_STATIC_DRAW);
    positionBufferId = buffers[0];
}

public void Render(GL10 gl){
    GLES11.glPushMatrix();
    GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, positionBufferId);
    GLES11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    GLES11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
    GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, 0);
    GLES11.glFrontFace(GL10.GL_CW);
    GLES11.glLineWidth(10.0f);
    GLES11.glColor4f(0.0f,0.0f,0.0f,1.0f);
    GLES11.glDrawArrays(GL10.GL_LINE_STRIP, 0, verticesList.length);
    GLES11.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    GLES11.glPopMatrix();
  }
}

绘图代码:

Renderer.java
--------------

// Variables here


public void onSurfaceChanged(GL10 gl, int width, int height) {
    viewWidth = width;
    viewHeight = height;
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping
    gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
    gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);    //Grey Background
    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
    gl.glDepthFunc(GL10.GL_LEQUAL);             
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluOrtho2D(gl, -viewWidth/2, viewWidth/2, -viewHeight/2,viewHeight/2);
    gl.glTranslatef(center.x,center.y,0);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(0,0, 0);
    gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_FRONT);

    if(connectingPath!=null){
        connectingPath.Render(gl);
    }

    gl.glDisable(GL10.GL_CULL_FACE);
    gl.glLoadIdentity();
 }

屏幕截图:

Screenshot of the Path drawn inverted

1 个答案:

答案 0 :(得分:0)

由于OpenGL定义它的屏幕坐标的方式,OpenGL中的绘图似乎被颠倒了。与大多数2D绘图API相比,原点位于左下角,这意味着向上移动时y轴值会增加。 OpenGL common pitfalls (Number 12)中提供了一个非常好的解释:

  

给定一张纸,人们从页面顶部写到底部。写文本的起源位于页面的左上角(至少在欧洲语言中)。但是,如果你要求任何体面的数学学生在X-Y图上绘制几个点,原点肯定会在图的左下角。大多数2D渲染API模仿编写器并使用2D坐标系,其中原点位于屏幕或窗口的左上角(至少默认情况下)。另一方面,3D渲染API采用数学思想的惯例,并为其3D坐标系统假定一个较低的左手原点。