I just get a black screen when I try to use a vbo. I'm using GLFW with GLEW. It does the same with textures but I didn't use textures to see if that'd work, but for some reason it's not. I had it working but I made some changes to the code, so I think I may have done something. PS: If the code has an error in it, let me know, as I removed code that doesn't affect rendering and I may have deleted important code on accident
Here is main.cpp, with some stuff that doesn't affect OpenGL removed:
//Include all OpenGL stuff, such as gl.h, glew.h, and glfw3.h
#include "gl.h"
//Include a header which adds some functions for loading shaders easier, there is nothing wrong with this code though
#include "shader.h"
float data[3][3] = {
{0.0, 1.0, 0.0},
{-1.0, -1.0, 0.0},
{1.0, -1.0, 0.0}
};
int main()
{
if (!glfwInit())
return 1;
GLFWwindow* window;
window = glfwCreateWindow(800, 600, "VBO Test", NULL, NULL);
if (!window)
{
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
if (GLEW_OK != glewInit())
{
return 1;
glfwTerminate();
}
//There are normal error handling stuff I do to ensure everything is loaded properly, so the shaders not loading isn't a concern as it'll clearly tell me :)
GLuint vertexShader = loadShader("shader.vert", GL_VERTEX_SHADER);
GLuint fragmentShader = loadShader("shader.frag", GL_VERTEX_SHADER);
GLuint program = createProgram(vertexShader, fragmentShader);
//Also, the shader files should make everything I draw yellow, and they are not defective
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glColorPointer(3, GL_FLOAT, 0, 0);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glUsePorgram(program);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
shader.vert
void main(void)
{
gl_Position = gl_Vertex;
}
shader.frag
void main()
{
//Set fragment
gl_FragColor = vec4( 1.0, 1.0, 0.0, 1.0 );
}
The main thing that jumps to mind is not enabling the client states for vertex and colour pointers.
Since the colour and position data overlaps, the colours will look a little strange. I'd start with just the glVertexPointer and glEnableClientState(GL_VERTEX_ARRAY), using a single glColor3f call. Then add a colour array later.
Also, you're buffering the same data to the same VBO twice. The VBO only needs to be bound before one call to glBufferData and the gl*Pointer functions.
Personally I don't like the 2D array, even though it probably works when declared statically. OpenGL will take data out of memory linearly. Using an array of structs can be quite nice.
See this for an small example:
http://goanna.cs.rmit.edu.au/~pknowles/SimpleVBO.c
Related
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.
I'm learning opengl and currently I'm struggeling with VAOs.
I would like to draw a cube and a triangle using VAOs but unfortunately, only the object that I create later is drawn. This is what I do in the main loop:
void main()
{
//loading shader, generate window, etc ...
//generate a cube:
GLuint cube_vao = generateCube();
//next, generate a triangle:
GLuint triangle_vao = generateTriangle();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
do
{
//draw:
glBindVertexArray(triangle_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(cube_vao);
glDrawArrays(GL_TRIANGLES, 0, 12*3);
glfwPollEvents();
glfwSwapBuffers(window);
} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
}
Both, generateCube() and generateTriangle() do basically the same thing: create the vertices, create vbo, create vao and set the attributes. Then they return the vao id.
This is generateTriangle() for example:
generateTriangle()
{
//generate the vertex positions:
GLfloat triangle_pos[] = //not part of the snippet -> too long
//generate vbo for the positions:
GLuint pos_vbo;
glGenBuffers(1, &pos_vbo);
glBindBuffer(GL_ARRAY_BUFFER, pos_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_pos), triangle_pos, GL_STATIC_DRAW);
//next, generate the vertex colors:
GLfloat triangle_color[] = //not part of the snippet -> too long
//generate vbo for the colors:
GLuint col_vbo;
glGenBuffers(1, &col_vbo);
glBindBuffer(GL_ARRAY_BUFFER, col_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_color), triangle_color, GL_STATIC_DRAW);
//generate VAO:
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLint pos_attrib_id = glGetAttribLocation(programID, "line_pos");
glEnableVertexAttribArray(pos_attrib_id);
glBindBuffer(GL_ARRAY_BUFFER, pos_vbo);
glVertexAttribPointer(pos_attrib_id, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
GLint col_attrib_id = glGetAttribLocation(programID, "color");
glEnableVertexAttribArray(col_attrib_id);
glBindBuffer(GL_ARRAY_BUFFER, col_vbo);
glVertexAttribPointer(col_attrib_id, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
//function to set the perspective (argument is the model matrix)
setPerspective(glm::mat4(1.0f));
return vao;
}
With this code, only the cube gets drawn.
Furthermore, if I comment out the lines:
glBindVertexArray(cube_vao); and glDrawArrays(GL_TRIANGLES, 0, 12*3); in the main, the triangle gets drawn but has the color and position of the cube, this is driving me crazy.
I'm using OSX with the shader versions 120 if that helps.
VAOs were introduced as standard functionality in OpenGL 3.0. On Mac OS, the default context version is 2.1. So you will need to specifically request a 3.x context during setup.
The exact mechanics of getting a 3.x context will depend on the window system interface/toolkit you are using. For example in GLUT, you include the GLUT_3_2_CORE_PROFILE flag in the argument to glInitDisplayMode(). With Cocoa, you include NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core in the pixel format attributes.
Note that Mac OS only supports the Core Profile for 3.x and later contexts. So you will not be able to use deprecated functionality anymore.
I'm just starting out trying to learn opengl, and in the process of decoupling various parts of the code into classes I'm running into issues.
This is a pretty simple question. In the following code, it draws the image to the screen if the glBindVertexArray(0) at the bottom of the Mesh constructor is commented out, but the image is not drawn if I put it in.
There is a glBindVertexArray(vao) command RIGHT before the glDrawElements call in the Mesh.draw() function, so it seems like it should work, but it doesn't.
Main code:
int main(int argc, char*argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0)
std::cout << "SDL did not intizlize. SDL Error: " << SDL_GetError() << std::endl;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window* window = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
GLenum glewTest = glewInit();
if (glewTest != GLEW_OK)
std::cout << glewGetErrorString(glewTest) << std::endl;
std::cout << glGetError() << std::endl;
Mesh mesh;
Shaders shaders("basicShader");
Texture texture("kitten.png");
//Loop stuff
bool quit = false;
SDL_Event e;
double frameCounter = 0;
double time = SDL_GetTicks();
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_ESCAPE)
quit = true;
}
}
++frameCounter;
if (SDL_GetTicks() - time >= 500) {
std::cout << "FPS: " << frameCounter / ((SDL_GetTicks() - time) / 1000) << std::endl;
frameCounter = 0;
time = SDL_GetTicks();
}
// Clear the screen to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
mesh.draw();
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// Swap buffers
SDL_GL_SwapWindow(window);
}
mesh.~Mesh();
SDL_GL_DeleteContext(context);
SDL_Quit();
return 0;
}
Mesh class code:
Mesh::Mesh() {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
GLfloat vertices[] = {
-0.5f, 0.5f, 0.0, 0.0,
0.5f, 0.5f, 1.0, 0.0,
0.5f, -0.5f, 1.0, 1.0,
-0.5f, -0.5f, 0.0, 1.0
};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &ebo);
GLuint elements[] = {
0, 1, 2,
2, 3, 0
};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
//glBindVertexArray(0);
}
void Mesh::draw() {
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//glBindVertexArray(0);
}
Mesh::~Mesh() {
glDeleteVertexArrays(1, &vao);
}
I also have shader setup code not posted here, which calls glVertexArrayPointer() and glEnableVertexAttribArray() to set up the vertex attributes.
The problem is with the glVertexAttribPointer() and glEnableVertexAttribArray() calls that you make while setting up your shader. Both of these calls modify state of the current VAO.
If you call:
glBindVertexArray(0);
at the end of setting up your mesh, 0 is now your current VAO (which is a legal VAO in the Compatibility Profile, but not in the Core Profile). So calls to glVertexAttribPointer() and glEnableVertexAttribArray() you make after this will now modify the state in VAO 0.
When you make the call to bind your VAO at the start of your draw() method:
glBindVertexArray(vao);
you're using all the state in your mesh VAO. Which in turn means that the state you set up in VAO 0 is not used. Since you never made glVertexAttribPointer() and glEnableVertexAttribArray() calls while your mesh VAO was bound, the vertex attributes are now not set up at all.
You were really just lucky that it worked when you did not unbind the VAO at the end of the constructor. Since the mesh VAO was still bound while you set up your shader, the glVertexAttribPointer() and glEnableVertexAttribArray() calls modified your mesh VAO state, which produced the desired result. But this would most likely fail miserably if you ever use more than one mesh/shader.
You really need to set up the vertex state while you set up your mesh, while the VAO for the specific mesh is bound. The following calls all modify VAO state, and must be made while the correct VAO is bound:
glEnableVertexAttribArray(...)
glDisableVertexAttribArray(...)
glVertexAttribPointer(...)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...)
Modifying vertex setup state while setting up a shader program is also conceptually questionable. The vertex setup state is not part of the shader program state.
I'm going through the second "chapter" on http://www.open.gl and running into a drawing issue which I can't figure out.
int main()
{
//Initialize GLFW, create the window and the context
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(1024, 768, "Open.GL Drawing 1", nullptr, nullptr);
glfwMakeContextCurrent(window);
//initialize GLEW after context creation
glewExperimental = GL_TRUE;
glewInit();
//loading, compiling, and checking shaders takes place here
[...]
//create, link, and use the shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
//create and bind the vao for the position data
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//triangle vertecies
GLfloat verticies[] = {
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
};
//vbo for verticies
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW);
//link, define, and enable the position attribute (aka, the verticies)
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posAttrib);
//main loop!
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
}
//clean up after yourself
glfwTerminate();
glDeleteProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
return EXIT_SUCCESS;
}
Vertex shader:
#version 150
in vec2 position;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
}
Fragment shader:
#version 150
out vec4 outColor;
void mian()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
When compiled with
g++ -fdiagnostics-color -Wall -Wextra -std=c++11 -lGLEW -lGLU -lGL -lglfw -o drawing1 drawing1.cpp
I get no warnings, but a blank screen when the program runs. I checked the example code he links as well as the github copy of the example source. Unfortunately, he uses SFML whereas I'm using GLFW, so I attempted to compensate for that difference.
I did my best to mimic his example code (ordering of things, etc) as well as adding in the last few lines of main() and the main loop (there was no mention of deleting shaders or clearing the viewport's color in the tutorial, but those statements were present in his example code) but still wound up with the same result. Checking glGetError() gave me an invalid enum error, but this post says that might be an expected behavior with glewExperimental.
After that, I'm not sure how to further isolate the issue. I look forward to someone pointing out what will obviously be a simple mistake. =)
As Joey Dewd pointed out, I didn't proof read my shader code very well. For the record, it was me typing mian instead of main in my fragment shader that caused the issues. On his suggestion, I added code to check for errors in linking (I was already checking for compilation errors) and sure enough, when I recreated my typo, the linker complained.
Fragment info
-------------
(0) : error C3001: no program defined
Thanks Joey. =)
I am really new to Open GL and I am trying to build non deprecated code. Now what I can't grasp is VBO. This is all I got so far, can you please explain what I'm supposed to be doing. Also, I have the OpenGL programming guide, so if you can point out some pages to read that would be very helpful as well.
#include <GL/glew.h>
#include <GL/freeglut.h>
GLuint ID[2];
GLfloat PositionData[][2] ={{50,50},{100,50},{50,100}, {100,50}, {100, 100}, {50, 100}};
GLfloat Colors[][3] = {{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3}};
void init(){
glewInit();
glClearColor(1.0, 1.0, 1.0, 0.0);
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_FLAT);
glGenBuffers(2, ID);
glBindBuffer(GL_ARRAY_BUFFER, ID[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(PositionData), PositionData, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, ID[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
glEnableClientState(GL_COLOR_ARRAY);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 12);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDeleteBuffers(2, ID);
glFlush();
}
void reshape(int w, int h){
glViewport(0,0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, (GLdouble) w, 0.0f, (GLdouble) h);
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("I don't get VBOs");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
I did a quick look on your code, an one thing stood out: You're completely missing the point of VBOs. In each draw call you're creating the VBO objects, upload the data, try to draw them (it won't work, because the shaders giving the vertex attributes sense are misssing) and then delete them.
The whole point of VBO is to upload the geometry data into a GL object once and then only refer to it by it's abstract object ID handle and index arrays, which one would also place in a buffer object.
But there's so much else screwed in that code, like setting projection matrix in reshape callback. For one thing, in OpenGL-3 core you don't have those built-in matrices anymore. It's all shader uniforms. And then you set those matrices on demand in the rendering handler.
I think I should really write that definitive OpenGL-3 core tutorial, demonstrating the proper idioms.
You are setting up custom vertex attributes with indices 0 and 1, but I don't see a shader that uses them. If you don't have a shader yet, then it will not work, because Fixed-Function pipeline accepts specific attributes (set up by glVertexPointer and glColorPointer for your case).
If you decide to make a shader (your goal is to make it for GL-3 core, right?), don't forget to assign your vertex attributes with actual names you use in GLSL code before linking it:
glBindAttribLocation( program_id, attribute_id, attribute_variable_name )