Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在使用LWJGL的Java中使用数组缓冲区进行渲染_Java_Opengl_Lwjgl_Vertex Buffer_Vertex Array Object - Fatal编程技术网

无法在使用LWJGL的Java中使用数组缓冲区进行渲染

无法在使用LWJGL的Java中使用数组缓冲区进行渲染,java,opengl,lwjgl,vertex-buffer,vertex-array-object,Java,Opengl,Lwjgl,Vertex Buffer,Vertex Array Object,我试图学习如何以现代方式在OpenGL中编程,使用顶点数组/顶点缓冲区对象。我现在正在使用LWJGL wiki上的教程,即使我复制并粘贴了教程代码,我也会得到一个背景颜色设置正确的窗口,但上面没有渲染形状。教程页面显示了一个屏幕截图,背景上呈现了一个白色矩形。这是一个常见的问题,还是有什么方法可以让我获得关于我的错误的进一步信息 编辑:使用着色器并在顶点上添加一些颜色可以解决问题。不过,我还没有将此作为答案发布,因为我假设教程代码在不使用着色器的情况下工作。这将在本教程的后面部分中介绍 这是教程

我试图学习如何以现代方式在OpenGL中编程,使用顶点数组/顶点缓冲区对象。我现在正在使用LWJGL wiki上的教程,即使我复制并粘贴了教程代码,我也会得到一个背景颜色设置正确的窗口,但上面没有渲染形状。教程页面显示了一个屏幕截图,背景上呈现了一个白色矩形。这是一个常见的问题,还是有什么方法可以让我获得关于我的错误的进一步信息

编辑:使用着色器并在顶点上添加一些颜色可以解决问题。不过,我还没有将此作为答案发布,因为我假设教程代码在不使用着色器的情况下工作。这将在本教程的后面部分中介绍

这是教程页面上的代码:

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.PixelFormat;
import org.lwjgl.util.glu.GLU;

public class TheQuadExampleDrawArrays {
    // Entry point for the application
    public static void main(String[] args) {
        new TheQuadExampleDrawArrays();
    }

    // Setup variables
    private final String WINDOW_TITLE = "The Quad: glDrawArrays";
    private final int WIDTH = 320;
    private final int HEIGHT = 240;
    // Quad variables
    private int vaoId = 0;
    private int vboId = 0;
    private int vertexCount = 0;

    public TheQuadExampleDrawArrays() {
        // Initialize OpenGL (Display)
        this.setupOpenGL();

        this.setupQuad();

        while (!Display.isCloseRequested()) {
            // Do a single loop (logic/render)
            this.loopCycle();

            // Force a maximum FPS of about 60
            Display.sync(60);
            // Let the CPU synchronize with the GPU if GPU is tagging behind
            Display.update();
        }

        // Destroy OpenGL (Display)
        this.destroyOpenGL();
    }

    public void setupOpenGL() {
        // Setup an OpenGL context with API version 3.2
        try {
            PixelFormat pixelFormat = new PixelFormat();
            ContextAttribs contextAtrributes = new ContextAttribs(3, 2)
                .withForwardCompatible(true)
                .withProfileCore(true);

            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.setTitle(WINDOW_TITLE);
            Display.create(pixelFormat, contextAtrributes);

            GL11.glViewport(0, 0, WIDTH, HEIGHT);
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        // Setup an XNA like background color
        GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);

        // Map the internal OpenGL coordinate system to the entire screen
        GL11.glViewport(0, 0, WIDTH, HEIGHT);

        this.exitOnGLError("Error in setupOpenGL");
    }

    public void setupQuad() {       
        // OpenGL expects vertices to be defined counter clockwise by default
        float[] vertices = {
                // Left bottom triangle
                -0.5f, 0.5f, 0f,
                -0.5f, -0.5f, 0f,
                0.5f, -0.5f, 0f,
                // Right top triangle
                0.5f, -0.5f, 0f,
                0.5f, 0.5f, 0f,
                -0.5f, 0.5f, 0f
        };
        // Sending data to OpenGL requires the usage of (flipped) byte buffers
        FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
        verticesBuffer.put(vertices);
        verticesBuffer.flip();

        vertexCount = 6;

        // Create a new Vertex Array Object in memory and select it (bind)
        // A VAO can have up to 16 attributes (VBO's) assigned to it by default
        vaoId = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(vaoId);

        // Create a new Vertex Buffer Object in memory and select it (bind)
        // A VBO is a collection of Vectors which in this case resemble the location of each vertex.
        vboId = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);
        // Put the VBO in the attributes list at index 0
        GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
        // Deselect (bind to 0) the VBO
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        // Deselect (bind to 0) the VAO
        GL30.glBindVertexArray(0);

        this.exitOnGLError("Error in setupQuad");
    }

    public void loopCycle() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

        // Bind to the VAO that has all the information about the quad vertices
        GL30.glBindVertexArray(vaoId);
        GL20.glEnableVertexAttribArray(0);

        // Draw the vertices
        GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertexCount);

        // Put everything back to default (deselect)
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);

        this.exitOnGLError("Error in loopCycle");
    }

    public void destroyOpenGL() {       
        // Disable the VBO index from the VAO attributes list
        GL20.glDisableVertexAttribArray(0);

        // Delete the VBO
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(vboId);

        // Delete the VAO
        GL30.glBindVertexArray(0);
        GL30.glDeleteVertexArrays(vaoId);

        Display.destroy();
    }

    public void exitOnGLError(String errorMessage) {
        int errorValue = GL11.glGetError();

        if (errorValue != GL11.GL_NO_ERROR) {
            String errorString = GLU.gluErrorString(errorValue);
            System.err.println("ERROR - " + errorMessage + ": " + errorString);

            if (Display.isCreated()) Display.destroy();
            System.exit(-1);
        }
    }
}

这是一个迟来的答案,但由于所有顶点上的Z坐标都为0,它不应该位于近剪裁平面的确切位置,因此不会渲染,因为它超出范围,因此您应该尝试沿Z轴将顶点移回,理论上应该渲染。

着色器在哪里?对于核心配置文件,您需要提供自己用GLSL编写的着色器程序。@RetoKoradi是否有其他方法可以设置它,这样我就不需要了?我试图删除。withProfileCoretrue无效。在这一点上,本教程没有提到任何关于自定义着色器的内容。我不确定LWJGL是如何处理上下文创建的。删除withForwardCompatibletrue和withProfileCoretrue似乎是有意义的。或者可能将值更改为false。不幸的是,做这两件事似乎都不能解决我的问题。过一会儿我会看另一个教程,比较不同之处。不。从给定的代码可以看出,这是直接绘制在剪辑空间,剪辑空间Z=0正好在观察截锥的中间。