gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:03:19 28 4
gpt4 key购买 nike

如果我按原样运行这段代码,我会看到一条从屏幕左下角到屏幕右上角的线。然而,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);
}

}

最佳答案

gl.glPointSize(150);

我怀疑 150 大于 GL_ALIASED_POINT_SIZE_RANGE and GL_SMOOTH_POINT_SIZE_RANGE 的上限.尝试更小的值,例如 1 或 2。

关于java - 在 glOrtho View 中绘制一条线,为什么它使用默认投影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9367774/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com