1
이 코드를 그대로 실행하면 화면의 왼쪽 하단 모서리부터 화면의 오른쪽 위 모서리까지의 선이 보입니다. 그러나 af af 나는 glOrthox와 glViewport를 설정했기 때문에 기본 투영법을 사용하는 것이 왜 나에게 수수께끼인가 ... 무엇이 잘못 되었나요? 화면의 왼쪽 하단은 0,0이어야하며, 1,1은 기본적으로 1 픽셀 이상이고 화면 전체에 있어야합니다. 전체 화면이 아닙니다.glOrth 뷰에서 선을 그리는 이유는 기본 투영을 사용하는 이유는 무엇입니까?
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);
}
}
아마도. 그러나 gl.glPointSize (1)로 변경하면 원본 시점의 점만 렌더링되므로 내 문제는 아닙니다. :(포인트 크기에 대해 2를 사용하는 편집 된 질문 –
glOrtho 호출에서 zNear == zFar를 지정하면 문제가 발생했습니다 .zNear를 -1로 변경하고 zFar를 1로 변경하면 문제가 해결되었습니다.이 예제를 변경하기 전에 GL_LINES ..를 사용하는 경우 여전히 GL_POINTS를 사용할 때,이 대답은 참으로 오류를 지적합니다 (문제는 아니지만). 그래서, 나는 이것을 바꿔서 표시 할 것입니다. –