Using and binding multiple vbos in OpenGL - c++

I am trying to experiment and learn how to use multiple VBOs to draw very different objects in OpenGL. The first VBO is just a text renderer, and for now the shader to be used with the main VBO is just a basic vertex shader
attribute vec3 vPosition;
void main()
{
gl_Position = vec4(vPosition, 1.0);
}
Test Fragment Shader:
void main()
{
gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}
The code renders the text fine, but as soon as I call UseProgram and bind the VAO, I get a black screen.
Here's my initialization of said VAO:
//random polygon
points[0]= vec3(-0.5, 0.5, 0.3);
points[1]= vec3(-0.5, -0.5, 0.2);
points[2]= vec3(0.5, -0.5, 0.3);
points[3]= vec3(0.5, 0.5, 0.4);
//Returns a uInt name for a shader program
shader = InitShader("vshader.glsl", "fshader.glsl");
glGenVertexArrays(1, &vao);
glGenBuffers(1, &buffer);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
GLuint loc = glGetAttribLocation(shader, "vPosition");
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), BUFFER_OFFSET(0));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white background
Where vao and buffer are previously uninitialized GLuints, and after debugging they do not return -1.
And here is my code to draw
glUseProgram(shader);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBindVertexArray(vao);
glDrawArrays(GL_POLYGON, 0, 4);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

Thanks for all the help everyone, I figured out that I needed to call glFlush() before binding my second VBO and VAO and drawing them.

Related

OpenGL mix fix pipeline and shader program (Qt)

I'm working on a old code that used fixed function pipeline, the scene is a bit complex but works fine. For the sake of simplicity, I replaced it with one blue triangle :
void RenduOpenGL::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glViewport(0, 0, this->width(), this->height());
glBegin(GL_TRIANGLES);
glColor3d(0,0,1);
glVertex3d(0.7, 0.7, 0.0);
glVertex3d(-0.5, 0.7, 0.0);
glVertex3d(0.1, -0.7, 0.0);
glEnd();
}
Now I want to add shaders for new elements in the scene but keep the old elements of the scene like this blue triangle.
I've read here that I can mix the two to produce a scene containing the first then the second.
Therefore I want to add this code after the blue triangle :
float vertices[] = {
0.6, 0.6, 0.0,
-0.6, 0.6, 0.0,
0.0, -0.6, 0.0,
};
vbo.create(); // glGenBuffers(...);
vbo.bind(); // glBindBuffer(GL_ARRAY_BUFFER, vbo);
vbo.allocate(vertices, sizeof(vertices)); // glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), vertices, GL_STATIC_DRAW);
vbo.release(); // glBindBuffer(GL_ARRAY_BUFFER, 0);
prog.addShaderFromSourceFile(QOpenGLShader::Vertex, "shaders/base.vert");
prog.addShaderFromSourceFile(QOpenGLShader::Fragment, "shaders/base.frag");
vao.create(); // glGenVertexArrays(...)
vao.bind(); // glBindVertexArray(vao);
prog.enableAttributeArray("position"); // glEnableVertexAttribArray(VAO_position);
prog.setAttributeBuffer("position", GL_FLOAT, 0, 3); // (offset, size, stride=0); // glVertexAttribPointer(VAO_position, 4, GL_FLOAT, False, 0, reinterpret_cast<const void *>(offset)(0)); (False,
vao.release(); // glBindVertexArray(0);
// draw the triangle
prog.bind(); // glUseProgram(shader_program);
vao.bind(); // glBindVertexArray(vertex_array_object);
glDrawArrays(GL_TRIANGLES, 0, 3);
vao.release(); // glBindVertexArray(0);
prog.release(); // glUseProgram(0);
I use Qt to call the openGL functions, the corresponding opengl functions are in comments.
My shaders are very basic :
// base.vert
#version 330
// vertex shader
in vec3 position;
void main() {
gl_Position = vec4(position.xyz, 1);
}
// base.frag
#version 330
// fragment shader
out vec4 pixel;
void main() {
pixel = vec4(1, 0.5, 0, 1);
}
That is supposed to draw an orange triangle, but when I put the code after the blue triangle code, I don't see the orange triangle created from shaders.
Short (with code) answer:
The VBO and the prog.enableAttributeArray and prog.setAttributeBuffer should be in the VAO.
Something along the lines:
float vertices[] = {
0.6, 0.6, 0.0,
-0.6, 0.6, 0.0,
0.0, -0.6, 0.0,
};
prog.bind(); // glUseProgram(shader_program);
vao.create(); // glGenVertexArrays(...)
vao.bind(); // glBindVertexArray(vao);
vbo.create(); // glGenBuffers(...);
vbo.bind(); // glBindBuffer(GL_ARRAY_BUFFER, vbo);
vbo.allocate(vertices, sizeof(vertices)); // glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), vertices, GL_STATIC_DRAW);
//vbo.release(); // glBindBuffer(GL_ARRAY_BUFFER, 0);
prog.addShaderFromSourceFile(QOpenGLShader::Vertex, "shaders/base.vert");
prog.addShaderFromSourceFile(QOpenGLShader::Fragment, "shaders/base.frag");
prog.enableAttributeArray("position"); // glEnableVertexAttribArray(VAO_position);
prog.setAttributeBuffer("position", GL_FLOAT, 0, 3); // (offset, size, stride=0); // glVertexAttribPointer(VAO_position, 4, GL_FLOAT, False, 0, reinterpret_cast<const void *>(offset)(0)); (False,
vao.release(); // glBindVertexArray(0);
// draw the triangle
prog.bind(); // glUseProgram(shader_program);
vao.bind(); // glBindVertexArray(vertex_array_object);
glDrawArrays(GL_TRIANGLES, 0, 3);
vao.release(); // glBindVertexArray(0);
prog.release(); // glUseProgram(0);
Not so long but textual answer: OpenGL is a state machine, you need to link together: the VBO and how to read its data, inside the VAO. However, IMHO, Qt people have sadly chosen their abstractions poorly: enableAttributeArray and setAttributeBuffer would be clearer as members of the VAO class instead of the prog class.

Opengl triangle not drawn

I'm trying to draw a simple triangle on the screen. The window gets properly set up and everything seems okay to the point, where I'm trying to actually draw stuff. I have the following code, but it doesn't work for some reason.
default_shader_program is program linking shaders shown after the function code
void window::run(void(*update)(), void(*draw)()) {
while (glfwWindowShouldClose(wnd) == 0)
{
update();
glClear(GL_COLOR_BUFFER_BIT);
draw();
GLuint vb;
GLfloat data[12]{
1.f, 1.f, 0.f,
1.f, -1.f, 0.f,
-1.f, 0.f, 0.f
};
glGenBuffers(1, &vb);
glBindBuffer(GL_ARRAY_BUFFER, vb);
glBufferData(GL_ARRAY_BUFFER, 12, data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint va;
glGenVertexArrays(1, &va);
glBindVertexArray(va);
glUseProgram(default_shader_program);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vb);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(wnd);
glfwPollEvents();
}
glfwDestroyWindow(wnd);
}
Shaders:
vertexshader:
#version 330 core
layout(location=0) in vec3 vertexPosition;
void main() {
gl_Position = vec4(vertexPosition, 1);
}
fragmentshader:
#version 330 core
out vec3 color;
void main() {
color = vec3(1.0, 0.0, 0.0);
}
Yes, I know, that I am buffering data and doing other stuff in while loop. I know, that it shouldn't be done like that, but the actual problem occured somewhere else and I wanted to see if code will work properly directly inside the loop.
Your array contains 9 elements instead of 12, but that doesn't cause the issue.
The 2nd argument of glBufferData is the buffer size in bytes. Hence the size of the buffer is 9*sizeof(GLfloat) rather than 12:
glBufferData(GL_ARRAY_BUFFER, 12, data, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), data, GL_STATIC_DRAW);

Cannot Link GLSL Program

I can't get the following Code to work. I want to render the triangle I describe in position[].
The program gives me sometimes a shader compile error or a program linking error and sometimes even both without me changing the code in between.
Program:
Window window(TITLE, WIDTH, HEIGHT) // Context and glew gets init here
float position[] = {-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0};
// Shader program gets init
sID = glCreateProgram();
vertexShaderID = glCreateShader(GL_Vertex_SHADER);
fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexShaderID, 1, vertexShaderCodeString); // The Code strings are valid, I printed them out
glShaderSource(fragmentShaderID, 1, fragShaderCodeString);
glCompileShader(vertexShaderID);
glCompileShader(fragShaderID);
shaderDidCompileCheck(vertexShaderID); // A function I wrote that checks for errors
shaderDidCompileCheck(fragShaderID);
glAttachShader(sID, vertexShaderID);
glAttachShader(sID, fragShaderID);
glLinkProgram(sID);
programDidLinkCheck(); // A function I wrote that checks for errors
glValidateProgram(sID);
glUseProgram(sID);
glBindAttribLocation(sID, 0, "position");
glUseProgram(0);
// Defining VBOs and VAOs
int bufferID;
glGenBuffers(1, &bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBufferData(GL_ARRAY_BUFFER, 3 * 3 * sizeof(float), mDataPtr, GL_STATIC_DRAW); // I have 3 vertices with 3 coordinates each
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); // I Want to load it to the index 0 of the VAO, the vertex size is 3, the data type is GL_FLOAT
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindVertexArray(vaoID);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
// Main loop
while(!window.close()) {
glClearColor(0.3, 0.8, 0.6, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(sID);
glBindVertexArray(vaoID);
glDrawArrays(GL_TRIANGLES, 0, 1);
glBindVertexArray(0);
glUseProgram(0);
glfwSwapBuffers(mWindow);
glfwPollEvents();
}
Vertex Shader:
#version 410
in vec3 position;
out vec3 color;
void main() {
gl_Position = vec4(position, 1.0);
color = vec3(position.x + 0.5, 0.5, position.y + 0.5);
}
Fragment Shader:
#version 410
in vec3 color;
out vec4 outputColor;
void main() {
outputColor = vec4(color, 1.0);
}
I think there are a few typos in your code. You write "vertexShaderID = glCreateShader(GL_Vertex_SHADER)", but macros are indicated in capital letters -> "GL_VERTEX_SHADER". Try glShaderSource(vertexShaderID, 1, vertexShaderCodeString, NULL), instead of passing 3 parametrs.
How did you saved your sourcecode of the shaders ? Make shure to have \n
after the version declaration, otherwise you will end up having the code in the same line as the #, which is bad.

C++ openGL static window with VBOs and shaders

So I've been building a pretty simple piece of code that renders .OBJ files and lets the user look around using VBOs and the most simple GLSL shaders immaginable, but when run the window is just a static image (looks like the object seen from 0,0,0). Without the shader program it still renders normally, just without color. Any idea what I colud be doing wrong?
VBO & shader stuff:
//Create buffer for vertex data (x,y,z)
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vector3f), &vertices[0], GL_STATIC_DRAW);
//Create buffer for color data (r,g,b)
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, temp.size() * sizeof(vector3f), &temp[0], GL_STATIC_DRAW);
//Create and compile shaders
GLuint vertShader = 0, fragShader = 0, program = 0;
createShader("vertex.glsl", GL_VERTEX_SHADER, vertShader);
createShader("fragment.glsl", GL_FRAGMENT_SHADER, fragShader);
program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glBindAttribLocation(program, vertexAttribIndex, "vertex_position");
glBindAttribLocation(program, colorAttribIndex, "vertex_colour");
glLinkProgram(program);
printShaderInfoLog(vertShader);
printProgramInfoLog(program);
//Set attribute pointers for GLSL
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(vertexAttribIndex, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(vertexAttribIndex);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(colorAttribIndex, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(colorAttribIndex);
Render loop:
while (!glfwWindowShouldClose(window)) {
glClearColor(0.0F, 0.0F, 0.0F, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glUseProgram(program);
glViewport(0, 0, width, height);
gluPerspective(70.0, width / height, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertBufferSize);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glfwSwapBuffers(window);
glfwPollEvents();
}
Vertex shader:
#version 420
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_colour;
out vec3 colour;
out vec4 gl_Position;
void main () {
colour = vertex_colour;
gl_Position = vec4 (vertex_position, 1.0);
}
Fragment shader:
#version 420
in vec3 colour;
out vec4 colorOut;
void main () {
colorOut = vec4(colour, 1.0);
}
Sorry if I've missed out anything obvious, I've been fighting this for hours.

Correct Way to Texture Square Made of Two Triangles?

I using OpenGL 4.1 and GLSL 410. I am attempting to texture a square that I made using the following coordinates:
float points[] = {
-0.5, 0.5,
-0.5, -0.5,
0.5, -0.5,
-0.5, 0.5,
0.5, -0.5,
0.5, 0.5
};
I draw the square like this:
glDrawArrays (GL_TRIANGLES, 0, 6);
From all of the tutorials I have read, the author uses an element buffer to draw the square or has just four vertices. This means that all the tutorials I have read have texture coordinates that line up with each vertex. For me, I'm using 6 vertices, so I'm not sure how to line up the texture coordinates.
Would coordinates like this work for my case:
float texcoords[] = {
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0
};
I've done lots of reading, but haven't come across anyone else who is using six vertices like I am.
Would my texture coordinates work and if not, what is the best way to come up with texture coordinates.
Yes, that will work. You've divided the sqaure into 2 triangles and mapped the tex coords to the vertices of the triangles. What are your results?
See this question. The code uses one array for the vertices (although there each vertex has xyz values) and another array for the texture coords.
You need to be careful when defining the vertex attributes.
E.g. to setup the vertex data uisng 2 buffers
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
glGenBuffers(1, &texbuffer);
glBindBuffer(GL_ARRAY_BUFFER, texbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, // attribute 0
2, // 2 floats per vertex
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride -> tightly packed so make it zero
0); // array buffer offset
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, texbuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glDrawArrays(GL_TRIANGLES, 0, 6);
To load the texture (Update and setup the sampler, see comments)
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
GLuint TextureID = glGetUniformLocation(programID, "texture");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
Vertex shader
#version 410 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texcoord;
out vec2 texture_coordinates;
uniform mat4 MVP;
void main() {
gl_Position = position;
texture_coordinates = texcoord;
}
Fragment shader
in vec2 texture_coordinates;
uniform sampler2D texture;
out vec4 colour;
void main() {
colour = texture2D(basic_texture, texture_coordinates);
}