背景中的点不显示

发布于 2024-12-11 07:30:11 字数 1657 浏览 0 评论 0原文

我正在创建一个 Android 2d 太空射击游戏作为学习 OpenGL 的一种方式,并且希望在游戏中拥有星空背景。我的想法是一开始就在背景上散布白点,但这些点不会出现。首先是渲染器代码:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

}

public void onSurfaceChanged(GL10 gl, int w, int h) {
    gl.glViewport(0, 0, w, h);
    width = w;
    height = h;
    gl.glMatrixMode(GL10.GL_PROJECTION);
}

public void onDrawFrame(GL10 gl) {
    // black background
    gl.glClearColor(0f, 0f, 0f, 1.0f);
    // clear the color buffer to show the ClearColor we called above...
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();

    stars.draw(gl);
}

然后是星星的绘制:

private final FloatBuffer vertexBuffer;
private int count;

public Stars() {
    Random r = new Random();
    count = (int) ((r.nextFloat() * 200.0f) + 200.0f);
    count *= 3;

    ByteBuffer buffer = ByteBuffer.allocateDirect(count * Float.SIZE);
    buffer.order(ByteOrder.nativeOrder());

    // allocate _count number of floats
    vertexBuffer = buffer.asFloatBuffer();

    float rVtx = (r.nextFloat() * 2.0f) - 1.0f;
    for (int i = 0; i < count; i += 3) {
        vertexBuffer.put(rVtx);
        rVtx = (r.nextFloat() * 2.0f) - 1.0f;

        vertexBuffer.put(rVtx);
        rVtx = (r.nextFloat() * 2.0f) - 1.0f;

        vertexBuffer.put(0.0f);
    }
}

public void draw(GL10 gl) {
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glPointSize(3);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glDrawArrays(GL10.GL_POINTS, 0, count / 3);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

似乎有什么错误?

问候

I am creating a Android 2d space shooter as a way to learn OpenGL and want to have a starry background in the game. My idea is to just speckle the background with white points for a start but the dots dont show up. First is the renderer code:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

}

public void onSurfaceChanged(GL10 gl, int w, int h) {
    gl.glViewport(0, 0, w, h);
    width = w;
    height = h;
    gl.glMatrixMode(GL10.GL_PROJECTION);
}

public void onDrawFrame(GL10 gl) {
    // black background
    gl.glClearColor(0f, 0f, 0f, 1.0f);
    // clear the color buffer to show the ClearColor we called above...
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();

    stars.draw(gl);
}

Then the drawing of the stars:

private final FloatBuffer vertexBuffer;
private int count;

public Stars() {
    Random r = new Random();
    count = (int) ((r.nextFloat() * 200.0f) + 200.0f);
    count *= 3;

    ByteBuffer buffer = ByteBuffer.allocateDirect(count * Float.SIZE);
    buffer.order(ByteOrder.nativeOrder());

    // allocate _count number of floats
    vertexBuffer = buffer.asFloatBuffer();

    float rVtx = (r.nextFloat() * 2.0f) - 1.0f;
    for (int i = 0; i < count; i += 3) {
        vertexBuffer.put(rVtx);
        rVtx = (r.nextFloat() * 2.0f) - 1.0f;

        vertexBuffer.put(rVtx);
        rVtx = (r.nextFloat() * 2.0f) - 1.0f;

        vertexBuffer.put(0.0f);
    }
}

public void draw(GL10 gl) {
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glPointSize(3);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glDrawArrays(GL10.GL_POINTS, 0, count / 3);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

What seems to be the error?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2024-12-18 07:30:11

您需要在onSurfaceChanged中设置默认的投影矩阵。类似于:

gl.glMatrixMode( GL10.GL_PROJECTION );                
gl.glLoadIdentity();        
gl.glOrthof( -1, 1, -1, 1, -1, 1 );

gl.glMatrixMode( GL10.GL_MODELVIEW );                 
gl.glLoadIdentity();

在 Stars() 的末尾,将其作为最后一行:

vertexBuffer.position(0);

You need to set the default projection matrix in onSurfaceChanged. something like:

gl.glMatrixMode( GL10.GL_PROJECTION );                
gl.glLoadIdentity();        
gl.glOrthof( -1, 1, -1, 1, -1, 1 );

gl.glMatrixMode( GL10.GL_MODELVIEW );                 
gl.glLoadIdentity();

And at the end of the Stars(), put this as the last line:

vertexBuffer.position(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文