2015-08-22 2 views
-1

Я представляю простой квад и экспериментирую с шейдерами. Он работал нормально, но я следовал текстурированию, чтобы добавить текстуры, и я должен что-то изменить. Поскольку теперь, несмотря на то, что весь код текстурирования закомментирован, квадрат не отображается, когда я включаю шейдеры. Я подозреваю, что это связано с привязкой данных, поскольку я все еще пытаюсь ее изучить. Я не могу для жизни меня найти, что не так!

шейдеры (вершинные и фрагмент):LWJGL 3 шейдеры не отображаются

//vertex 
#version 400 core 

in vec3 position; 
out vec3 colour; 

void main (void) { 

colour = vec3(position.x + 0.5, 1.0, position.y + 0.5); 

} 

//fragment 
#version 400 core 

in vec3 colour; 
out vec4 out_Colour; 

void main (void) { 

out_Colour = vec4(colour, 1.0); 

} 

Shader программа:

public abstract class ShaderProgram { 

private int programId; 
private int vertexId; 
private int fragmentId; 

public ShaderProgram (String vertexFile, String fragmentFile) { 

    vertexId = loadShader(vertexFile, GL20.GL_VERTEX_SHADER); 
    fragmentId = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER); 
    programId = GL20.glCreateProgram(); 
    GL20.glAttachShader(programId, vertexId); 
    GL20.glAttachShader(programId, fragmentId); 
    bindAttributes(); 
    GL20.glLinkProgram(programId); 
    GL20.glValidateProgram(programId); 


} 

public void start() { 
    GL20.glUseProgram(programId); 
} 

public void stop() { 
    GL20.glUseProgram(0); 
} 

public void cleanUp() { 
    stop(); 
    GL20.glDetachShader(programId, vertexId); 
    GL20.glDetachShader(programId, fragmentId); 
    GL20.glDeleteShader(vertexId); 
    GL20.glDeleteShader(fragmentId); 
    GL20.glDeleteProgram(programId); 
} 

protected abstract void bindAttributes(); 

protected void bindAttribute (int attribute, String variableName) { 
    GL20.glBindAttribLocation(programId, attribute, variableName); 
} 

private static int loadShader (String file, int type) { 

    StringBuilder shaderSource = new StringBuilder(); 
    try { 
     BufferedReader reader = new BufferedReader(new FileReader(file)); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      shaderSource.append(line).append("\n"); 
     } 
     reader.close(); 
    } catch (IOException e) { 
     System.err.println("Could not read shader file!"); 
     e.printStackTrace(); 
     System.exit(-1); 
    } 
    int shaderId = GL20.glCreateShader(type); 
    GL20.glShaderSource(shaderId, shaderSource); 
    GL20.glCompileShader(shaderId); 
    if (GL20.glGetShaderi(shaderId, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) { 
     System.err.println(GL20.glGetShaderInfoLog(shaderId)); 
     System.err.println("Could not compile shader!"); 
     System.exit(-1); 
    } 
    return shaderId; 
} 

} 

Главная шейдер (расширяет программу Shader):

public class MainShader extends ShaderProgram { 

private static final String VERTEX_FILE = "src/shaders/vertexShader.txt"; 
private static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt"; 

public MainShader() { 
    super(VERTEX_FILE, FRAGMENT_FILE); 
} 

@Override 
protected void bindAttributes() { 
    super.bindAttribute(ModelLoader.VERTEX_INDICE, "position"); 
    //super.bindAttribute(1, "uv"); 
} 

} 

Модель погрузчика:

public class ModelLoader { 

private static List<Integer> vaos = new ArrayList<Integer>(); 
private static List<Integer> vbos = new ArrayList<Integer>(); 
private static List<Integer> textures = new ArrayList<Integer>(); 
public static final int VERTEX_INDICE = 0; 
public static final int UV_INDICE = 1; 

public static RawModel loadToVAO (float[] positions, int[] indices) { 

    int vaoId = createVAO(); 
    bindIndicesBuffer(indices); 
    storeDataInAttributeList(VERTEX_INDICE, 3, positions); 
    //storeDataInAttributeList(UV_INDICE, 2, uvCoords); 
    unbindVAO(); 
    return new RawModel(vaoId, indices.length); 

} 

/*public static int loadTexture (String file, int textureUnit) { 

    try { 
     return TextureLoader.loadTexture("rsrc/" + file + ".png", GL11.GL_TEXTURE_2D); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return 0; 
}*/ 

public static int loadTexture(String file, int textureUnit) { 
    String filename = "rsrc/" + file + ".png"; 
    ByteBuffer buf = null; 
    int tWidth = 0; 
    int tHeight = 0; 

    try { 
     // Open the PNG file as an InputStream 
     InputStream in = new FileInputStream(filename); 
     // Link the PNG decoder to this stream 
     PNGDecoder decoder = new PNGDecoder(in); 

     // Get the width and height of the texture 
     tWidth = decoder.getWidth(); 
     tHeight = decoder.getHeight(); 


     // Decode the PNG file in a ByteBuffer 
     buf = ByteBuffer.allocateDirect(
       4 * decoder.getWidth() * decoder.getHeight()); 
     decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA); 
     buf.flip(); 

     in.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
    } 

    // Create a new texture object in memory and bind it 
    int texId = GL11.glGenTextures(); 
    GL13.glActiveTexture(textureUnit); 
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId); 

    // All RGB bytes are aligned to each other and each component is 1 byte 
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); 

    // Upload the texture data and generate mip maps (for scaling) 
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, 
      GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf); 
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); 

    // Setup the ST coordinate system 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT); 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); 

    // Setup what to do when the texture has to be scaled 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, 
      GL11.GL_NEAREST); 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, 
      GL11.GL_LINEAR_MIPMAP_LINEAR); 

    return texId; 
} 

private static int createVAO() { 

    int vaoId = GL30.glGenVertexArrays(); 
    vaos.add(vaoId); 
    GL30.glBindVertexArray(vaoId); 
    return vaoId; 

} 

private static void bindIndicesBuffer (int[] indices) { 

    int vbo = GL15.glGenBuffers(); 
    vbos.add(vbo); 
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vbo); 
    IntBuffer buffer = storeDataInIntBuffer(indices); 
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); 

} 

public static void cleanUp() { 

    for (int vao : vaos) { 
     GL30.glDeleteVertexArrays(vao); 
    } 
    for (int vbo : vbos) { 
     GL15.glDeleteBuffers(vbo); 
    } 
    for (int texture : textures) { 
     GL11.glDeleteTextures(texture); 
    } 

} 

private static void storeDataInAttributeList (int attributeNumber, int size, float[] data) { 

    int vboId = GL15.glGenBuffers(); 
    vbos.add(vboId); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId); 
    FloatBuffer buffer = storeDataInFloatBuffer(data); 
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); 
    GL20.glVertexAttribPointer(attributeNumber, size, GL11.GL_FLOAT, false, 0, 0); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); 

} 

private static void unbindVAO() { 

    GL30.glBindVertexArray(0); 

} 

private static IntBuffer storeDataInIntBuffer (int[] data) { 

    IntBuffer buffer = BufferUtils.createIntBuffer(data.length); 
    buffer.put(data); 
    buffer.flip(); 
    return buffer; 

} 

private static FloatBuffer storeDataInFloatBuffer (float[] data) { 

    FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length); 
    buffer.put(data); 
    buffer.flip(); 
    return buffer; 

} 

} 
+0

Вы должны установить 'gl_Position' в вершинном шейдере. – BDL

+0

Это сработало! большое спасибо :) – dodo

ответ

0

Я забыл назначить gl_Position в вершинном шейдере.

Смежные вопросы