Programming > OpenGL
Basic Rendering from VBOs with Shaders
(1/1)
DylanP1234:
Hey guys,
I think I have a problem... Well possibly I am just too dumb to understand OpenGL but anyways...
As the title says, I am just trying to put some simple data (a quad) into a VBO and then render it with a vertexshader and a fragmentshader.
So this is the vertexshader code:
--- Code: ---#version 450
attribute vec3 position;
attribute vec4 color;
out vec4 pass_color;
void main()
{
pass_color = color;
gl_Position = vec4(position, 1.0);
}
--- End code ---
And that's the fragmentshader code:
--- Code: ---#version 450
in vec4 pass_color;
void main()
{
gl_FragColor = pass_color;
}
--- End code ---
So they are very basic - nothing special. Just always set the position to the position coming from the attribute and always set the color to the color coming from the attribute.
First, I am going to show you my "data" which are 2 float arrays with some position data and color data:
--- Code: ---public static float[] plane_vertices =
{
-0.5f, -0.5f, 0f,
-0.5f, 0.5f, 0f,
0.5f, 0.5f, 0f,
0.5f, 0.5f, 0f,
0.5f, -0.5f, 0f,
-0.5f, -0.5f, 0f
};
public static float[] plane_colors =
{
1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.6f, 0.0f, 0.0f, 1.0f,
0.3f, 0.0f, 0.0f, 1.0f
};
--- End code ---
Yes, I know they should be called "quad" instead of "plane" because they are representing a quad and not a 3D-plane but just bare with me there.
I am using this data to put them into FloatBuffers like this:
--- Code: ---// Just generate some Floatbuffers
FloatBuffer plane_verts = BufferUtils.createFloatBuffer(_Const.plane_vertices.length);
plane_verts.put(_Const.plane_vertices);
plane_verts.flip();
FloatBuffer plane_cols = BufferUtils.createFloatBuffer(_Const.plane_colors.length);
plane_cols.put(_Const.plane_colors);
plane_cols.flip();
--- End code ---
That's still nothing special isn't it?
Now we are getting to the interesting parts...
I am setting up the shaders and a shaderprogram. Also I am creating a VAO and two VBOs which shall represent my position and color buffers.
--- Code: ---vertexShader = new Shader(GL_VERTEX_SHADER, TextLoader.fileToString(_Const.RES_SHADERS + "minimalist_vertexShader.glsl"));
fragmentShader = new Shader(GL_FRAGMENT_SHADER, TextLoader.fileToString(_Const.RES_SHADERS + "minimalist_fragmentShader.glsl"));
testShaderProgram = new TestShaderProgram(vertexShader, fragmentShader);
testShaderProgram.compile();
vao = new VAO();
vao.bind();
vbo_position = new VBO();
vbo_color = new VBO();
--- End code ---
After that I am using the following code to "upload" the data into the VBOs:
--- Code: ---// Upload the data into the vbos
vbo_position.bind(GL_ARRAY_BUFFER);
glBufferData(GL_ARRAY_BUFFER, plane_verts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, plane_verts);
VBO.unbind(GL_ARRAY_BUFFER);
vbo_color.bind(GL_ARRAY_BUFFER);
glBufferData(GL_ARRAY_BUFFER, plane_cols, GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, plane_cols);
VBO.unbind(GL_ARRAY_BUFFER);
VAO.unbind();
--- End code ---
Please tell me if I have done something wrong here already^^. I hope that It's correct though. Just set the position-data to the first attribute and the color-data to the second. (I am connecting the variable-name in the shader and attribute-location in the testShaderProgram.compile() which we had after creating the shaderprogram.
And this is the code I use to render everything:
--- Code: --- public void render()
{
clear();
// RENDER HERE
testShaderProgram.use();
vao.bind();
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
VAO.unbind();
ShaderProgram.stopUse();
// RENDER END
glfwSwapBuffers(window);
glfwPollEvents();
}
--- End code ---
So again:
"teshShaderProgram.use();" just activates my shaderprogram which we have created above.
The vao variable is just the VAO for holding my "quad" as you can see above (plane_vertices, plane_colors).
"vao.bind();" obviously binds that vao.
Unfortunately, everything shows me a black window, which is kinda sad :(
My guess is that I am a bit confused by all the states and that I have set something wrong at the wrong moment so everything doesn't work.
I really hope some of you guys can help me out with this (I hope super-simple) problem.
P.S.: What's the difference in using glBufferData(with buffer) and then glVertexAttribPointer(without a buffer) and just using glVertexAttribPointer(with a buffer)?
Thanks in advance for your help! :)
ErChick3n:
I didn't read all ur code but certainly u have to add an output in ur fragment shader otherwise u won't be able to see anything.
in the end your fragment shader should look more like this:
--- Code: ---#version 450
in vec4 pass_color;
out vec4 gl_FragColor;
void main()
{
gl_FragColor = pass_color;
}
--- End code ---
Navigation
[0] Message Index
Go to full version