gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:55:32 26 4
gpt4 key购买 nike

我正在尝试在 LWJGL 中创建 Pong 游戏。我已经成功地画出了“球棒”和球,我还可以使用 W 和 S 键让球棒上下移动。

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

如果我删除 glEnableVertexAttribArray(1) 它会绘制 bat 并以某种方式用颜色 RED 为我的 bat 着色。我试图修改 Batt 类中的颜色信息,但没有任何 react ,所以我不确定它是从哪里获取颜色的。

P.S:我正在运行 Arch Linux x64 系统。

fatal error 是这个:

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

现在为程序本身。

主要类(class):
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();

}

}

bat 类:
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;

}

最佳答案

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

因此,颜色缓冲区永远不会出现在正确的数组中,而是什么都没有。

关于opengl - LWJGL - glEnableVertexAttribArray(1) 后游戏崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22892658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com