Java OpenGL无法正确绘制垂直线

时间:2020-10-27 19:39:49

标签: java opengl

经过多次测试,在画布上画一条垂直线时总是得到相同的错误结果。好像它们被绘制了两次并彼此点缀。

This is the error while drawing a vertical line.

绘制水平线时没有错误。他们看起来像预期的那样。

package FirstTest;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

public class OpenGLTest {

public static void main(String[] args) {  
  
    final GLProfile gp = GLProfile.get(GLProfile.GL2);
    GLCapabilities cap = new GLCapabilities(gp);

    final GLCanvas gc = new GLCanvas(cap);
    gc.addGLEventListener(new GLEventHandler());
    gc.setSize(400, 400);

    final JFrame frame = new JFrame("JOGL Line");
    frame.add(gc);
    frame.setSize(500, 400);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);   
}

    private static class GLEventHandler implements GLEventListener {

        @Override
        public void init(GLAutoDrawable arg0) {}

        @Override
        public void display(GLAutoDrawable drawable) {
            final GL2 gl = drawable.getGL().getGL2();
        
            gl.glBegin(GL2.GL_LINES);
            gl.glVertex3f(-0.4f, -0.4f, 0);
            gl.glVertex3f(-0.4f, 0.4f, 0);
            gl.glEnd();
        }

        @Override
        public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {}

        @Override
        public void dispose(GLAutoDrawable arg0) {}
    }
}

这是我使用的代码。我在网站上发现了类似的示例,但是它们都没有按照我想要的或像我以前那样的方式工作。

我使用Maven加强了OpenGL资源。 这是pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>3DMaven</groupId>
<artifactId>3DMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.jogamp.jogl</groupId>
        <artifactId>jogl-all-main</artifactId>
        <version>2.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.jogamp.gluegen</groupId>
        <artifactId>gluegen-rt-main</artifactId>
        <version>2.3.2</version>
    </dependency>

</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>12</release>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

0 个答案:

没有答案