在glOrtho视图中绘制一条线,为什么它使用默认投影?

时间:2012-02-20 20:13:05

标签: java android opengl-es

如果按原样运行此代码,我会看到屏幕左下角到屏幕右上角的一条线。但是,afaik我设置了glOrthox和glViewport,那么为什么它使用默认投影对我来说是一个谜......出了什么问题?屏幕的左下方应该是0,0,1,1应该基本上是1像素,一直到屏幕上方..而不是整个屏幕。

package com.opengl.hello;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;

public class Renderer2d implements Renderer {
    Context mContext;

    public static ByteBuffer newByteBuffer(int size)
    { return ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()); }

    public static FloatBuffer newFloatBuffer(int size)
    { return newByteBuffer(size * 4).asFloatBuffer(); }

    public static ByteBuffer newByteBuffer(byte[] buf)
    { ByteBuffer out=newByteBuffer(buf.length); out.put(buf).position(0); return out; }

    public static FloatBuffer newFloatBuffer(float[] buf)
    { FloatBuffer out=newFloatBuffer(buf.length); out.put(buf).position(0); return out; }

    public Renderer2d(Context context)
    {
        mContext=context;
    }

    int mWidth;
    int mHeight;
    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glViewport(0, 0, mWidth, mHeight);
        // Reset projection
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        // Set up our ortho view
        gl.glOrthox(0, mWidth, 0, mHeight, 0, 0);

        // Reset model view
        gl.glMatrixMode(GL10.GL_MODELVIEW);

        gl.glLoadIdentity();
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        // Build buffers sufficient to draw a single
        ByteBuffer indicesBuffer=newByteBuffer(new byte[] { 0, 1 });

        // PROBLEM: I expect this to draw a small line, from the bottom left to 1,1 (a single pixel over and up into the image).. 
        // as well as sticking off-screen down and to the left.  Instead, this covers the entire screen, corner to corner!
        FloatBuffer vertexBuffer=newFloatBuffer(new float[]{ -1.f, -1.f, 1.f, 1.f });

        // Enable vertex arrays, as we're about to use them
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        // Each point in the vertex buffer has two dimensions.
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);

        // Draw one point
        gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_BYTE, indicesBuffer);

        // No longer need vertex arrays
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

    /**
     * If the surface changes, reset the view.
     */
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        mWidth=width;
        mHeight=height;
        // Moved code to onDrawFrame just to group everything together
    }

    /**
     * The Surface is created/init()
     */
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Yellow Background
        gl.glClearColor(1.0f, 1.0f, 0.0f, 0.0f);        
        // Disable dithering for better performance
        gl.glDisable(GL10.GL_DITHER);
        // enable texture mapping
        gl.glEnable(GL10.GL_TEXTURE_2D);          
        //enable transparency blending
        gl.glEnable(GL10.GL_BLEND);
        // use opaque texturing 
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_SRC_COLOR);
        // Disable the depth buffer, we're drawing in 2D.
        gl.glDisable(GL10.GL_DEPTH_TEST);
        // Smooth shading
        gl.glShadeModel(GL10.GL_SMOOTH);
        // Really Nice Perspective Calculations
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    }

}

1 个答案:

答案 0 :(得分:3)

gl.glPointSize(150);

我怀疑150 大于GL_ALIASED_POINT_SIZE_RANGE and GL_SMOOTH_POINT_SIZE_RANGE的上端。尝试小于1或2的东西。