当前位置: 首页 > 知识库问答 >
问题:

LWJGL -游戏在glEnableVertexAttribArray(1)后崩溃

狄富
2023-03-14

我正在尝试在LWJGL中创建一个Pong游戏。我已经设法绘制了“球拍”和球,我还可以使用W和S键使球拍上下移动。

现在我正在尝试使用着色器为蝙蝠着色,但是当我尝试 - glEnableVertexAttribArray(1) - 启用包含颜色的第二个顶点数组(在循环中)时,我遇到了错误。第二个顶点数组被启用,但是当游戏尝试绘制我的蝙蝠 - glDrawElements(GL11.GL_TRIANGLES,bat.getIndicesCount(),GL11.GL_UNSIGNED_BYTE,0)时崩溃。

如果我删除glEnableVertexAttribArray(1),它会绘制蝙蝠并以某种方式将我的蝙蝠涂成红色。我试图修改Batt类中的颜色信息,但什么也没发生,所以我不确定它从哪里获得颜色。

附言:我正在运行一个Arch Linux x64系统。

致命错误是这个:

A fatal error has been detected by the Java Runtime Environment:
SIGSEGV (0xb) at pc=0x000000004027ef4b, pid=7664, tid=139636374128384
#
# JRE version: OpenJDK Runtime Environment (7.0_51-b31) (build 1.7.0_51-b31)
# Java VM: OpenJDK 64-Bit Server VM (24.51-b03 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  0x000000004027ef4b
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/borg/workspace/LWJGL-Test/hs_err_pid7664.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   http://icedtea.classpath.org/bugzilla

包含其余错误的文件是这个(我把它放在一个pastebin链接中,以便更容易阅读)http://pastebin.com/hSnJuAar

现在谈谈程序本身。

主要类别:

package myPackage;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import myPackage.Bat;
import myPackage.Ball;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;

public class Test {

    // Setup variables
    private int WIDTH = 800;
    private int HEIGHT = 600;
    private String title = "PONG GAME";

    private  Bat bat;
    private Ball ball;

    //Shader variables
    private int vsId = 0;
    private int fsId = 0;
    private int pId = 0;


    public Test() {

        // Initialize
        System.out.println("-->Initializing OpenGL<--");
        setupOpenGL();
        System.out.println("OpenGL initialized");

        System.out.println("-->Preparing to draw ...<--");
        setupDraw();
        setupShaders();
        System.out.println("...Everything is initialized...");
        System.out.println("Entering loop ...");
        while (!Display.isCloseRequested()) {

            loop();

            Display.update();
            Display.sync(60);

        }
        System.out.println("--> EXITING LOOP ");
        System.out.println("-->Cleaning memory<--");
        destroyOpenGL();

    }

    public void setupOpenGL() {

        try {

            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); // Create
            // the
            // display
            Display.setTitle(title);
            Display.create();
            System.out.println("Display created");

        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(-1); // If error , exit program
        }

        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Clears the screen with
        // black color

    }

    public void setupDraw() {

        bat = new Bat(-1f,0.3f,0,1,0.07f,0.6f);
        ball = new Ball(0.03f,0,0);

    }

    private void setupShaders() {

        // Load the vertex shader
        vsId = loadShader("/home/borg/workspace/LWJGL-Test/src/myPackage/vertex.glsl", GL20.GL_VERTEX_SHADER);
        // Load the fragment shader
        fsId = loadShader("/home/borg/workspace/LWJGL-Test/src/myPackage/fragment.glsl", GL20.GL_FRAGMENT_SHADER);

        // Create a new shader program that links both shaders
        pId = GL20.glCreateProgram();
        GL20.glAttachShader(pId, vsId);
        GL20.glAttachShader(pId, fsId);

        // Position information will be attribute 0
        GL20.glBindAttribLocation(pId, 0, "in_Position");
        // Color information will be attribute 1
        GL20.glBindAttribLocation(pId, 1, "in_Color");

        GL20.glLinkProgram(pId);
        GL20.glValidateProgram(pId);

        System.out.println("Shaders initialized");
    }

    public void loop() {

        //BAT
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); // Clears the screen color using
        // glClearColor in setupOpenGL()


        GL20.glUseProgram(pId);

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

        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, bat.getVboi());
        System.out.println("Indices buffer bound");

        // Draw the vertices
        GL11.glDrawElements(GL11.GL_TRIANGLES, bat.getIndicesCount(), GL11.GL_UNSIGNED_BYTE, 0);
        System.out.println("Drawing bat with indices");

        // Put everything back to default (deselect)
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL30.glBindVertexArray(0);
        GL20.glUseProgram(0);

        bat.readInput();

        //CIRCLE
        GL30.glBindVertexArray(ball.getVao());
        GL20.glEnableVertexAttribArray(0);

        // Draw the vertices

        GL11.glDrawArrays(GL11.GL_TRIANGLE_FAN, 0, ball.getCircleVertexSize() / 2);

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

    }

    public int loadShader(String filename, int type) {
        StringBuilder shaderSource = new StringBuilder();
        int shaderID = 0;

        try {
            BufferedReader reader = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = reader.readLine()) != null) {
                shaderSource.append(line).append("\n");
            }
            reader.close();
        } catch (IOException e) {
            System.err.println("Could not read file.");
            e.printStackTrace();
            System.exit(-1);
        }

        shaderID = GL20.glCreateShader(type);
        GL20.glShaderSource(shaderID, shaderSource);
        GL20.glCompileShader(shaderID);

        return shaderID;
    }


    public void destroyOpenGL() {
        // Delete the shaders
                GL20.glUseProgram(0);
                GL20.glDetachShader(pId, vsId);
                GL20.glDetachShader(pId, fsId);

                GL20.glDeleteShader(vsId);
                GL20.glDeleteShader(fsId);
                GL20.glDeleteProgram(pId);

        GL30.glBindVertexArray(bat.getVao());
        GL30.glBindVertexArray(ball.getVao());

        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(bat.getVbo());
        GL15.glDeleteBuffers(ball.getVbo());

        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(bat.getVboi());

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(bat.getVboc());

        GL30.glBindVertexArray(0);
        GL30.glDeleteVertexArrays(bat.getVao());
        //GL30.glDeleteVertexArrays(ball.getVao());
        System.out.println("Everything deleted");
        Display.destroy();
        System.out.println("Display destroyed");
    }


    public static void main(String[] args) {

        new Test();

    }

}

蝙蝠等级:

package myPackage;

import java.nio.ByteBuffer;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;


public class Bat {

    float x,y,z,w,height,width; 
    float yp = 0;
    private int vao = 0;
    private int vbo = 0;
    private int vboi = 0;
    private int vboc = 0;
    private int indicesCount;
    float[] batVertex ;
    byte[] indices;
    float[] colors;

    public Bat(float x, float y, float z, float w, float width, float height) {

        this.x = x;
        this.y = y;
        this.z = z;
        this.w = w;
        this.height = height;
        this.width = width;

         batVertex = new float[]{ // Vertex coordinates

                x, y, z, w,                       // ID = 0
                x + width, y , z, w,              // ID = 1
                x + width, y - height, z, w,      // ID = 2
                x , -y , z , w                    // ID = 3

        };

        FloatBuffer batBuffer = BufferUtils.createFloatBuffer(batVertex.length);
            batBuffer.put(batVertex);
            batBuffer.flip();

        colors = new float[] {
                 1f, 0f, 0f, 1f,
                 0f, 1f, 0f, 1f,
                 0f, 0f, 1f, 1f,
                 1f, 1f, 1f, 1f,
                 };


        FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colors.length);
        colorsBuffer.put(colors);
        colorsBuffer.flip();


        //QUAD INDICES
        indices = new byte[] {
            0,3,2,
            2,1,0
        };

        indicesCount = indices.length;
        ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
        indicesBuffer.put(indices);
        indicesBuffer.flip();
        //===============================




        //BAT VBO AND ARRAY
        vao = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(vao);

        vbo = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, batBuffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, 0, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL30.glBindVertexArray(0);
        //=====================================


        //Colors vbo
        vboc = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboc);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorsBuffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0,0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        //======================================

        GL30.glBindVertexArray(0);

        //Indices vbo
        vboi = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboi);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
        //=======================================


        System.out.println("Bat initialized");
    }

    public void moveUp() {

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, getVbo());


        //System.out.println("x : " + bat.getVertices()[0] + " y : " + bat.getVertices()[1]);

        if (getVertices()[1] >= 1.0f) {

            yp = 0;
            for (int i = 1; i < getVertexSize(); i = i + 4) {
                getVertices()[i] = getVertices()[i] + yp;

            }
        }

        else {
            yp = 0.025f;
            for (int i = 1; i < getVertexSize(); i = i + 4) {
                getVertices()[i] = getVertices()[i] + yp;

            }
        }

        FloatBuffer vertexFloatBuffer = BufferUtils
                .createFloatBuffer(getVertexSize());
        vertexFloatBuffer.put(getVertices());
        vertexFloatBuffer.flip();

        GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, vertexFloatBuffer);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    }

    public void moveDown() {

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, getVbo());
        //System.out.println("x : " + bat.getVertices()[0] + " y : " + bat.getVertices()[1]);

        if (getVertices()[13] < -1.0f) {
            yp = 0;
            for (int i = 1; i < getVertexSize(); i = i + 4) {
                getVertices()[i] = getVertices()[i] - yp;

            }
        }

        else {
            yp = 0.025f;
            for (int i = 1; i < getVertexSize(); i = i + 4) {
                getVertices()[i] = getVertices()[i] - yp;

            }
        }

        FloatBuffer vertexFloatBuffer = BufferUtils
                .createFloatBuffer(getVertexSize());
        vertexFloatBuffer.put(getVertices());
        vertexFloatBuffer.flip();

        GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, vertexFloatBuffer);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    }

    public void readInput() {

        if (Keyboard.isKeyDown(Keyboard.KEY_W)) {

            moveUp();
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_S)) {

            moveDown();
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {

            System.exit(0);
            Display.destroy();
        }

    }

    public int getVao() {
        return vao;
    }


    public int getVbo() {
        return vbo;
    }

    public int getVboi() {
        return vboi;
    }

    public int getVboc() {
        return vboc;
    }

    public int getVertexSize(){

        return batVertex.length;

    }

    public int getIndicesCount(){
        return indicesCount;
    }

    public float[] getVertices() {

        return batVertex;

    }

    public byte[] getIndices() {
        return indices;
    }



}

球类 :

package myPackage;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;

public class Ball {

    float r = 0.03f;
    float x;
    float y;
    float offSetX = 0;
    float offSetY = 0;
    private int vao = 0;
    private int vbo = 0;
    float[] circleVertex ;

    public Ball(float r, float offSetX, float offSetY) {

        this.r = r;

        this.offSetX = offSetX;
        this.offSetY = offSetY;

        int SUBDIVISIONS = 20;

         circleVertex = new float[(SUBDIVISIONS) * 2];

        for (int i = 2; i < circleVertex.length; i = i + 2) {

            double angle = Math.PI * 2 * i / SUBDIVISIONS;

            x = (float) Math.cos(angle) * r;
            y = (float) Math.sin(angle) * r;

            circleVertex[i] = x + offSetX;
            circleVertex[i + 1] = y + offSetY;

        }

        circleVertex[0] = 0 + offSetX;
        circleVertex[1] = 0 + offSetY;

        FloatBuffer circleBuffer = BufferUtils
                .createFloatBuffer(circleVertex.length);
        circleBuffer.put(circleVertex);
        circleBuffer.flip();

        vao = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(vao);

        vbo = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, circleBuffer,
                GL15.GL_STATIC_DRAW);

        GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        System.out.println("Ball initialized");
    }

    public int getVao() {
        return vao;
    }

    public int getVbo() {
        return vbo;
    }

    public int getCircleVertexSize() {
        return circleVertex.length;
    }


}

片段着色器:

#version 150

in vec4 pass_Color;

out vec4 out_Color;

void main(void) {
    out_Color = pass_Color;
}

顶点着色器:

#version 150

in vec4 in_Position;
in vec4 in_Color;

out vec4 pass_Color;

void main(void){
    gl_Position = in_Position;
    pass_Color = in_Color;

}

共有1个答案

池宸
2023-03-14

在Bat类的第88行左右,在颜色缓冲区定义之前,您有一个额外的GL30.glBindVertexArray(0);

因此,颜色缓冲区永远不会出现在正确的数组中,相反,它什么也没有链接。

 类似资料:
  • 经过大量的谷歌搜索,我在LWJGL渲染VBO立方体时遇到了这个问题。基本上,当我启用法线时,JVM会崩溃。这很可能与我在Plane.java中设置常态的方式有关。由于我仍在学习VBO,我似乎不知道如何解决这个问题。你能看一看,让我知道我可能在哪里出错了吗? Cube.java: 平面. java: 让我知道你们的想法。我已经在三台独立的计算机上测试了代码,每一台都有相同的结果。

  • 电话:索尼Xperia LT25i。 我安装了游戏。启动了它。看到黑屏和无响应应用程序。 和logcat继续在控制台中发送垃圾邮件: 有没有人知道发生了什么以及如何解决这个问题? 我尝试了不同的应用程序。我甚至用空场景创建了空项目——结果相同。

  • 即使这个问题被问了很多次(我读了所有这些,没有一个解决方案对我有效),我也试图用LWJGL和OpenGL建模一个矩形,但每次都会崩溃。我的电脑数据如下: AMD Ryzen 1600x | MSI Nvidia GTX 1060(6GB)| MSI x370 Carbon Pro主板 我还在英特尔安装程序上尝试了这个,使用i7处理器和Nvidia Quadro K1000M设置,但您可以在以下内容

  • 我试图用LWJGL编写一个opengl渲染器。为了打开窗户,我用的是GLFW。但是,当我调用glfwCreateWindow时,它会崩溃,出现以下错误: Java运行时环境检测到一个致命错误: 谢了!

  • 说一下自己学过哪些课程 TCP协议特点 TCP协议高级点的特点 慢启动,拥塞控制 为什么进行拥塞控制 https加密流程 https中证书是怎么拿到的,里面包含什么? 证书为什么放在第三方? Java中list由哪些子类? ArrayList数据结构是什么样的?具体是怎么实现的 有个电脑,向另一个电脑通过程序发送数据,这个数据在硬件层面怎么流动的,经过那些步骤 NAT原理 传输层的报头是什么? 基

  • 这个问题与minecraft 1.9版有关 我是新来的改装现场,我的测试木块有问题。我已经为我的测试木块扩展了BlockLog类。然而,每次我试图在游戏中放置积木,我的游戏就会崩溃。这是我的原木课: 最近,我一直在youtube上学习简单的教程,这就是我学习创建新区块的方式。我更进一步,没有扩展,而是继续扩展。 我尝试过BlockLog类,试图覆盖某些方法或添加枚举类型,但没有效果。我相信有大量代