gpt4 book ai didi

java - 顶点着色器上的 openGL GLSL "void"解析错误

转载 作者:行者123 更新时间:2023-11-30 08:29:25 25 4
gpt4 key购买 nike

所以我正在关注“theBennyBox”关于 3d 游戏引擎的 Youtube 系列,大约在第 9/10 集中,我遇到了一些问题。

我得到了错误

Vertex shader failed to compile with the following errors: ERROR: 0:5: error(#132) Syntax error: "void" parse error ERROR: error(#273) 1 compilation errors. No code generated

这是我所有的东西:

我的资源加载器(读取文件)

package com.base.engine;

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

public class ResourceLoader {
public static String loadShader(String fileName) {
StringBuilder shaderSource = new StringBuilder();
BufferedReader shaderReader = null;

try {
shaderReader = new BufferedReader(new FileReader("./res/shaders/" + fileName));
String line;
while ((line = shaderReader.readLine()) != null) {
shaderSource.append(line).append("\n");
}

shaderReader.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}

return shaderSource.toString();
}
}

我的着色器类:

package com.base.engine;

import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL32.*;

public class Shader {
private int program;

public Shader() {
program = glCreateProgram();

if (program == 0) {
System.err.println("Shader Creation failed: could not find valid memory location in constructor");
System.exit(1);
}
}

public void bind(){
glUseProgram(program);
}

public void addVertexShader(String text) {
addProgram(text, GL_VERTEX_SHADER);
}

public void addGeometryShader(String text) {
addProgram(text, GL_GEOMETRY_SHADER);
}

public void addFragmentShader(String text) {
addProgram(text, GL_FRAGMENT_SHADER);
}

public void compileShader() {
glLinkProgram(program);

if (glGetProgrami(program, GL_LINK_STATUS) == 0) {
System.err.println(glGetShaderInfoLog(program, 1024));
System.exit(1);
}

glValidateProgram(program);
if(glGetProgrami(program, GL_VALIDATE_STATUS) == 0){
System.err.println(glGetShaderInfoLog(program, 1024));
System.exit(1);
}
}

private void addProgram(String text, int type) {
int shader = glCreateShader(type);

if (shader == 0) {
System.err.println("Shader Creation failed: could not find valid memory location when adding shader");
System.exit(1);
}

glShaderSource(shader, text);
glCompileShader(shader);

if (glGetShaderi(shader, GL_COMPILE_STATUS) == 0) {
System.err.println(glGetShaderInfoLog(shader, 1024));
System.exit(1);
}

glAttachShader(program, shader);
}
}

我将着色器初始化为单个多边形的游戏类:

package com.base.engine;

import org.lwjgl.input.Keyboard;

public class Game {
private Mesh mesh;
private Shader shader;

public Game() {
mesh = new Mesh();
shader = new Shader();

Vertex[] data = new Vertex[] {new Vertex(new Vector3f(-1, -1, 0)),
new Vertex(new Vector3f(0, 1, 0)),
new Vertex(new Vector3f(1, -1, 0))};
mesh.addVertices(data);
shader.addVertexShader(ResourceLoader.loadShader("basicVertex.vs"));
shader.addFragmentShader(ResourceLoader.loadShader("basicFragment.fs"));

}

public void input() {
if (Input.getKeyDown(Keyboard.KEY_UP)) {
System.out.println("We've just pressed up");
}
if (Input.getKeyUp(Keyboard.KEY_UP)) {
System.out.println("We've just released up");
}

if (Input.getMouseDown(1)) {
System.out.println("We've just pressed right-mouse at "
+ Input.getMousePosition().toString());
}
if (Input.getMouseUp(1)) {
System.out.println("We've just released right-mouse");
}
}

public void update() {

}

public void render() {
shader.bind();
mesh.draw();
}
}

和我的着色器:(错误出现在顶点着色器上,但如果我注释掉顶点着色器,片段看起来就像什么都不做。)

顶点:(第 5 行,void main(),显然是麻烦制造者)

#version 330

layout (location = 0) in vec3 position

void main()
{
gl_Position = vec4(0.25 * position, 1.0);
}

片段:

#version 330

out vec3 fragColour;

void main()
{
fragColour = vec4(0.0, 1.0, 1.0, 1.0);
}

我很感激这一切很长,但如果你知道什么请帮助我!

最佳答案

除非是转录代码的拼写错误,否则您的顶点着色器中缺少分号。第 3 行是:

布局(location = 0)在vec3位置

应该是

布局(location = 0)在vec3位置;

关于

but if I comment out the vertex shader the fragment looks like it does nothing

每个着色器程序都至少需要一个顶点和片段/像素着色器。没有顶点着色器(当你注释掉它时)没有顶点被传递,这意味着没有生成图元,因此没有图元可以用你的片段着色器填充。

关于java - 顶点着色器上的 openGL GLSL "void"解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19452240/

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