I have been reading the red book on open gl and to help me understand the tutorials I have been rewriting them into Java using LWJGL.
I’ve have gotten stuck on one of the tutorials in chapter 3 where the shader will not load.
The goal of the tutorial is to render four triangles using four different buffers, sub-buffer, arrays, elements.
Here is the init code which sets up the triangles and the shaders.
private void init()
{
// A single triangle
FloatBuffer vertexPositions = BufferUtils.createFloatBuffer(16);
vertexPositions.put(new float[] { -1.0f, -1.0f, 0.0f, 1.0f });
vertexPositions.put(new float[] { 1.0f, -1.0f, 0.0f, 1.0f });
vertexPositions.put(new float[] { -1.0f, 1.0f, 0.0f, 1.0f });
vertexPositions.put(new float[] { -1.0f, -1.0f, 0.0f, 1.0f });
vertexPositions.flip();
// A colours for the triangle
FloatBuffer vertexColours = BufferUtils.createFloatBuffer(16);
vertexColours.put(new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
vertexColours.put(new float[] { 1.0f, 1.0f, 0.0f, 1.0f });
vertexColours.put(new float[] { 1.0f, 0.0f, 1.0f, 1.0f });
vertexColours.put(new float[] { 0.0f, 1.0f, 1.0f, 1.0f });
vertexColours.flip();
// indices for the triangle strips
final byte[] vertexIndices = new byte[] { 0, 1, 2 };
ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(vertexIndices.length);
indicesBuffer.put(vertexIndices);
indicesBuffer.flip();
// Set up the element array buffer
elementBufferId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, elementBufferId);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
// Set up the vertex attributes
vaoId = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vaoId);
vboId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexPositions.capacity() + vertexColours.capacity(), GL15.GL_STATIC_DRAW);
GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, vertexPositions);
GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, vertexPositions.capacity(), vertexColours);
GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0L);
GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, vertexPositions.capacity());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
// create program
Matrix4f model_matrix = new Matrix4f();
Matrix4f projection_matrix = new Matrix4f();
vertexShader = ShaderUtils.loadShader("src/dw/redbook/tutorials/ex002drawingcommands/primitive_restart.vs.glsl", GL20.GL_VERTEX_SHADER);
fragmentShader = ShaderUtils.loadShader("src/dw/redbook/tutorials/ex002drawingcommands/primitive_restart.fs.glsl", GL20.GL_FRAGMENT_SHADER);
// vertexShader = ShaderUtils.loadShader("src/dw/redbook/tutorials/ex002drawingcommands/triangles.vs", GL20.GL_VERTEX_SHADER);
// fragmentShader = ShaderUtils.loadShader("src/dw/redbook/tutorials/ex002drawingcommands/triangles.fs", GL20.GL_FRAGMENT_SHADER);
renderProgramId = GL20.glCreateProgram();
glAttachShader(renderProgramId, vertexShader);
glAttachShader(renderProgramId, fragmentShader);
glLinkProgram(renderProgramId);
glValidateProgram(renderProgramId);
GL20.glUseProgram(renderProgramId);
// Create a FloatBuffer with the proper size to store our matrices later
matrix44Buffer = BufferUtils.createFloatBuffer(16);
}
If you need to see anything else then I can paste it.
The shader it fails to load is
#version 330
uniform mat4 model_matrix;
uniform mat4 projection_matrix;
layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;
out vec4 vs_fs_color;
void main(void)
{
vs_fs_color = color;
gl_Position = projection_matrix * (model_matrix * position);
}