Ich beschäftige mich gerade mit Shadern und versuche mit verschiedenen Tutorials, eine Textur auf mein Dreieck zu werfen, doch kann der keine textureSampler uniform finden.
Ich bin neu bei OpenGL und habe keine Ahnung, was dort falsch ist...
Texture.fs
Texture.vs
Ich bin neu bei OpenGL und habe keine Ahnung, was dort falsch ist...
Java:
public abstract class Shader {
private int programID;
private int vertexID;
private int fragmentID;
private HashMap<String, Integer> uniforms;
private FloatBuffer matrix = BufferUtils.createFloatBuffer(16);
public static String folder = "res/";
public Shader(String Vert, String Frag) {
uniforms = new HashMap<>();
vertexID = loadShader(Vert,GL20.GL_VERTEX_SHADER);
fragmentID = loadShader(Frag, GL20.GL_FRAGMENT_SHADER);
programID = GL20.glCreateProgram();
GL20.glAttachShader(programID, vertexID);
GL20.glAttachShader(programID, fragmentID);
bindAttributes();
GL20.glLinkProgram(programID);
GL20.glValidateProgram(programID);
getAllUniformLocations();
}
public void start() {
GL20.glUseProgram(programID);
}
public void stop() {
GL20.glUseProgram(0);
}
private static int loadShader(String file, int type) {
StringBuilder shaderSource = new StringBuilder();
try {
System.out.println("read:" + folder + "shaders/" + file);
BufferedReader reader = new BufferedReader(new FileReader(folder + "shaders/" + file));
String line;
while((line = reader.readLine()) != null) {
shaderSource.append(line).append("\n");
}
reader.close();
}
catch(IOException e){
System.err.println("Can't read file");
e.printStackTrace();
System.exit(-1);
}
int ID = GL20.glCreateShader(type);
GL20.glShaderSource(ID, shaderSource);
GL20.glCompileShader(ID);
if(GL20.glGetShaderi(ID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
System.out.println(GL20.glGetShaderInfoLog(ID, 512));
System.err.println("Couldn't compile the shader");
System.exit(-1);
}
System.out.println("id0:" + ID);
return ID;
}
protected int getUniformLocation(String uniformName) {
System.out.println("uniform " + uniformName);
System.out.println(programID);
return GL20.glGetUniformLocation(programID, uniformName);
}
protected void bindAttribute(int attribute, String variableName) {
GL20.glBindAttribLocation(programID, attribute, variableName);
}
protected abstract void bindAttributes();
protected abstract void getAllUniformLocations();
protected void createUniform(String uniformName) {
int location = getUniformLocation(uniformName);
if(location < 0) {
System.err.println("Could not find uniform " + uniformName);
System.exit(-1);
}
uniforms.put(uniformName, getUniformLocation(uniformName));
System.out.println("uni:" + getUniformLocation(uniformName));
}
protected void loadInt(int location, int value) {
GL20.glUniform1i(location, value);
}
public void setUniform(String uniformName, int value) {
loadInt(uniforms.get(uniformName), value);
}
protected void loadFloat(int location, float value) {
GL20.glUniform1f(location, value);
}
public void setUniform(String uniformName, float value) {
loadFloat(uniforms.get(uniformName), value);
}
protected void loadVector(int location, Vector3f vector) {
GL20.glUniform3f(location, vector.x, vector.y, vector.z);
}
public void setUniform(String uniformName, Vector3f value) {
loadVector(uniforms.get(uniformName), value);
}
protected void loadBoolean(int location, boolean value) {
float tovec = 0;
if(value) {
tovec = 1;
}
GL20.glUniform1f(location, tovec);
}
public void setUniform(String uniformName, boolean value) {
loadBoolean(uniforms.get(uniformName), value);
}
protected void loadMatrix(int location, Matrix4f value) {
System.out.println("loadm: ");
value.store(matrix);
matrix.flip();
GL20.glUniformMatrix4fv(location, false, matrix);
}
public void setUniform(String uniformName, Matrix4f value) {
loadMatrix(uniforms.get(uniformName), value);
}
}
Code:
public class ShaderTextured extends Shader{
public ShaderTextured() {
super("Textured.vs", "Textured.fs");
}
@Override
protected void bindAttributes() {
super.bindAttribute(0, "position");
super.bindAttribute(1, "textureCoords");
}
@Override
protected void getAllUniformLocations() {
super.createUniform("textureSampler");
}
}
Code:
#version 450 core
in vec2 pass_texCoords;
out vec4 out_Color;
uniform sampler2D textureSampler;
void main(){
out_Color = texture(textureSampler,pass_texCoords);
}
Code:
#version 450 core
in vec3 position;
in vec2 uvs;
out vec2 pass_uvs;
void main(void){
gl_Position = vec4(position, 1.0);
pass_uvs = uvs;
}