简体   繁体   English

如何使用Java在openGL中清除屏幕

[英]How can I clear the screen in openGL using Java

I don't understand how I can simply clear the screen in Java while using OpenGL. 我不明白如何在使用OpenGL时简单地用Java清除屏幕。 I have searched all over the internet, there is like no real good resource for OpenGL information. 我在互联网上进行了搜索,好像没有真正好的OpenGL信息资源。 Basically I just want to clear the screen and re-draw a circle. 基本上,我只想清除屏幕并重新绘制一个圆圈。 Instead my code decides that it isn't going to clear the screen ever, and it most definitely isn't going to draw anything else.. I want it to clear the screen when I press "e", and then draw a new circle. 相反,我的代码确定它永远不会清除屏幕,并且绝对不会绘制任何其他内容。.我希望它在按“ e”键时清除屏幕,然后绘制一个新的圆。 I have two java files.. I will only post relevant code for the sake of any user's who can help me - but will post more code if needed. 我有两个java文件。.我只会为任何可以帮助我的用户发布相关代码-但会在需要时发布更多代码。

In the beginning of my JOGLEventListener.java file I'm also declaring a global var 在我的JOGLEventListener.java文件的开头,我还声明了一个全局变量

// Test GLAutoDrawable test = null; // Test GLAutoDrawable test = null;

JOGLEventListener.java JOGLEventListener.java

    @Override
    public void display(GLAutoDrawable gLDrawable) 
    {
        // Set a global variable to hold the gLDrawable
        // May not need this?
        test = gLDrawable;

         GL2 gl = gLDrawable.getGL().getGL2();

        gl.glClearColor(backrgb[0], 0, 1, 1);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        backrgb[0]+=0.0005;
        if (backrgb[0]> 1) backrgb[0] = 0; 


        // =============================================
        // Draw my circle here
        //
        // =============================================
        // =============================================
        System.out.println("Drawing Circle..");
        drawCircle(5.0f, 5.0f, 10.0f);

    }

    // Draw Circle
    void drawCircle(float x, float y, float radius)
    {
        System.out.println("IN DRAWCIRCLE");
        int i;
        GL2 gl = test.getGL().getGL2();
        int lineAmount = 100; //# of triangles used to draw circle
        final 
        //GLfloat radius = 0.8f; //radius
        float twicePi = (float) (2.0f * Math.PI);

        gl.glBegin(gl.GL_LINE_LOOP);
            for(i = 0; i <= lineAmount;i++) { 
                gl.glVertex2f(
                    x + (radius * (float)Math.cos(i *  twicePi / lineAmount)), 
                    y + (radius* (float)Math.sin(i * twicePi / lineAmount))
                );
            }
        gl.glEnd();
    }

@Override
public void keyTyped(KeyEvent e) 
{
    char key= e.getKeyChar();
    System.out.printf("Key typed: %c\n", key); 
    GL2 gl = test.getGL().getGL2();
    if(key == 'e')
    {
        // WHY ISNT THIS WORKING
        // CLEAR THE SCREEN AND DRAW ME A NEW CIRCLE
        gl.glClear( gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT );     
        gl.glLoadIdentity();                    
        //test
        float x = 100.0f;
        float y = 100.0f;
        float twicePi = (float) (2.0f * Math.PI);
        float radius = 100f;

        System.out.println("Draw Another Circle...");
        gl.glBegin(gl.GL_LINE_LOOP);
        for(int i = 0; i <= 360;i++) 
        { 
            gl.glVertex2f(
                x + (radius * (float)Math.cos(i *  twicePi / 360)), 
                y + (radius* (float)Math.sin(i * twicePi / 360))
            );
        }
        gl.glEnd();
    }

1) That's deprecated OpenGL , don't use it 1)那是不推荐使用的OpenGL ,请不要使用它

2) Don't save the gl object to one global value, always get it from the drawable or the GLContext 2)不要将gl对象保存为一个全局值,请始终从drawableGLContext获取它

3) Use a shader program to render and a vertex buffer to hold the vertices position. 3)使用着色器程序进行渲染,并使用顶点缓冲区保持顶点位置。 But first, I'd suggest you to start a tutorial to learn the basic of OpenGL. 但是首先,我建议您开始学习OpenGL基础的教程。 Or if you want to get something working asap, clone this hello triangle of mine and start experiment on that 或者,如果您想尽快获得某些工作,请克隆我的这个问候三角形 ,然后在该三角形上开始实验

The problem is apparently that you don't swap the front and back buffers. 问题显然是您没有交换前缓冲区和后缓冲区。

I'm not familiar with the OpenGL bindings for Java, but I guess that the library already does that for you after it calls the display() function. 我对Java的OpenGL绑定不熟悉,但是我猜该库在调用display()函数后已经为您完成了此工作。 It doesn't do that after keyTyped() . keyTyped()之后不执行此keyTyped()

The way you are supposed to do this is to always draw the scene from scratch inside the display() function based on some internal state. 您应该这样做的方法是始终根据某些内部状态从头display()函数内绘制场景。 Then in keyTyped() you shall modify that internal state and invalidate the window, which will cause the display() to be called again and redraw the scene properly. 然后在keyTyped()您将修改该内部状态并使窗口无效,这将导致display()再次被调用并正确重绘场景。

EDIT: Calling display() yourself won't be enough. 编辑:仅仅调用display()是不够的。 I can't find how to invalidate the window in Java (in C this would be so much easier). 我找不到如何在Java中使窗口无效的方法(在C语言中,这样做会容易得多)。 As a dirty hack you can try calling temp.swapBuffers() manually in display , setting setAutoSwapBufferMode(false) and calling display from keyTyped() . 作为肮脏的黑客,您可以尝试在display手动调用temp.swapBuffers() ,设置setAutoSwapBufferMode(false)并从keyTyped()调用display

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM