我在 MCP(Minecraft Coder Pack)上使用 java。
shader来自shadertoy,这里是渲染方法:
public class GLSLSandboxShader {
private final int programId;
private final int timeUniform;
private final int mouseUniform;
private final int resolutionUniform;
private final int texIn;
public GLSLSandboxShader(String fragmentShaderLocation) throws IOException {
int program = glCreateProgram();
glAttachShader(program, createShader(GLSLSandboxShader.class.getClassLoader().getResourceAsStream("assets/minecraft/passthrough.vsh"), GL_VERTEX_SHADER));
glAttachShader(program, createShader(GLSLSandboxShader.class.getClassLoader().getResourceAsStream(fragmentShaderLocation), GL_FRAGMENT_SHADER));
glLinkProgram(program);
int linked = glGetProgrami(program, GL_LINK_STATUS);
if (linked == 0) {
System.err.println(glGetProgramInfoLog(program, glGetProgrami(program, GL_INFO_LOG_LENGTH)));
throw new IllegalStateException("Shader failed to link");
}
this.programId = program;
// Setup uniforms
glUseProgram(program);
this.timeUniform = glGetUniformLocation(program, "time");
this.mouseUniform = glGetUniformLocation(program, "mouse");
this.resolutionUniform = glGetUniformLocation(program, "resolution");
this.texIn = glGetUniformLocation(program, "texIn");
glUseProgram(0);
}
public void useShader(int width, int height, float mouseX, float mouseY, float time) {
int textureId = Minecraft.getMinecraft().getFramebuffer().framebufferTexture;
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
glUseProgram(this.programId);
//GL11.glViewport(0, 0, width*2, height*2);
glUniform2f(this.resolutionUniform, width*2, height*2);
glUniform2f(this.mouseUniform, mouseX / width, 1.0f - mouseY / height);
glUniform1f(this.timeUniform, time);
glUniform1i(texIn, 0);
}
private int createShader(InputStream inputStream, int shaderType) throws IOException {
int shader = glCreateShader(shaderType);
glShaderSource(shader, readStreamToString(inputStream));
glCompileShader(shader);
int compiled = glGetShaderi(shader, GL_COMPILE_STATUS);
// If compilation failed
if (compiled == 0) {
System.out.println("cacaca");
System.err.println(glGetShaderInfoLog(shader, glGetShaderi(shader, GL_INFO_LOG_LENGTH)));
throw new IllegalStateException("Failed to compile shader");
}
return shader;
}
private String readStreamToString(InputStream inputStream) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
int read;
while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, read);
}
return new String(out.toByteArray(), StandardCharsets.UTF_8);
}
}
我希望我展示了所有需要的信息,如果它是一个着色器问题告诉我,我会发布代码(我尽我所能,抱歉)。 🙏
在 MCP 上渲染模糊着色器但出现条纹