2012-02-20 52 views
1

如果我按照原样运行此代码,则会看到从屏幕左下角到屏幕右上角的一行。然而,afaik我设置了glOrthox和glViewport,所以为什么它使用默认投影对我来说是个谜......有什么不对?屏幕的左下角应该是0,0,1,1应该基本上是1像素,并且不会在整个屏幕上。在glOrtho视图中绘制一条线,它为什么使用默认投影?

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); 
    } 

} 

回答

3
gl.glPointSize(150); 

我怀疑150比的GL_ALIASED_POINT_SIZE_RANGE and GL_SMOOTH_POINT_SIZE_RANGE上端更大。尝试一些更小的像1或2.

+0

也许。但是,将其改为gl.glPointSize(1),它只能在原点处呈现点...所以这不是我的问题。 :(编辑问题使用2的点大小 – 2012-02-20 20:23:58

+1

问题只是在glOrtho调用中指定zNear == zFar是无效的。将zNear更改为-1并将zFar更改为1可以解决问题。但是,在更改此示例之前要使用GL_LINES ..当它仍然使用GL_POINTS时,这个答案确实指出了一个错误(尽管不是错误)。所以,我会加注并标记这个正确的。 – 2012-02-20 22:06:41