I'm trying to understand (in laymans terms) how VAO's & VBO's work.
I have written a small OpenGL program which graphically creates a cool looking object.
It draws a square using two triangles and VAO/VBO's. It also enlarges the Vertex Points.
Below is my code(I've included all except the win32 stuff for clarity):
My question is, do I need mulitple VAO's and VBO's to draw more objects/shapes?
Basically I adapted this program from a tutorial of a triangle. I'm wondering whether drawing the second triangle as I did is correct or whether I should have used the VAO & VBO's differently.
For example, If I wanted to draw a second square, do I need more VAOs? Sorry if this question seems silly or pointless, I'm a beginner with OpenGL3.2.
Any advice or explanations welcome.
#include <windows.h>
#include <iostream>
using namespace std;
#include <math.h>
#include "console.h"
#include "shaders/Shader.h"
#include "gl/glew.h"
#include "gl/wglew.h"
#pragma comment(lib, "glew32.lib")
unsigned int m_vaoID; // vertex array object
unsigned int m_vboID[2]; // two VBOs - used for colours and vertex data
const int numOfVerts = 6;
const int numOfTris = 2;
float verts[18];
float cols[18];
Shader myShader;
ConsoleWindow console;
int screenWidth=600, screenHeight=600; //changed from 480x480px
void display(); //called in winmain to draw everything to the screen
void init(); //called in winmain when the program starts.
void createGeometry(); //used to create Vertex Array Objects
void reshape(int width, int height);
/************* START OF OPENGL FUNCTIONS ****************/
void reshape(int width, int height) // Resize the OpenGL window
{
int b = 50; //border of 50px
screenWidth = width;
screenHeight = height;
glViewport(0+b,0+b,width-(b*2),height-(b*2)); // set Viewport dimensions
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(myShader.handle()); // use the shader
//draw objects
glBindVertexArray(m_vaoID); // select VAO
glDrawArrays(GL_TRIANGLES, 0, numOfVerts); //draw some geometry
//Enlarge ALL the Points
glEnable(GL_POINT_SMOOTH);
glPointSize(100);
glDrawArrays(GL_POINTS,0, numOfVerts);
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind the buffer
glBindVertexArray(0); //unbind the vertex array object
glUseProgram(0); //turn off the current shader
}
void createGeometry()
{
// First simple object
float dim = 0.5;
//Triangle 1
//points X Y Z
verts[0] = -dim; verts[ 1] = -dim; verts[ 2] = -dim; //Point 1
verts[3] = -dim; verts[ 4] = dim; verts[ 5] = -dim; //Point 2
verts[6] = dim; verts[ 7] = dim; verts[ 8] = -dim; //Point 3
//Triangle 2
//points X Y Z
verts[ 9] = dim; verts[10] = -dim; verts[11] = -dim; //Point 4
verts[12] = -dim; verts[13] = -dim; verts[14] = -dim; //Point 1
verts[15] = dim; verts[16] = dim; verts[17] = -dim; //Point 3
//colour R G B
cols[ 0] = 1.0; cols[ 1] = 0.0; cols[ 2] = 1.0; //Point1
cols[ 3] = 0.0; cols[ 4] = 1.0; cols[ 5] = 1.0; //Point2
cols[ 6] = 1.0; cols[ 7] = 1.0; cols[ 8] = 0.0; //Point3
//colour2 R G B
cols[ 9] = 0.0; cols[10] = 1.0; cols[11] = 1.0; //Point4
cols[12] = 1.0; cols[13] = 0.0; cols[14] = 1.0; //Point5
cols[15] = 1.0; cols[16] = 1.0; cols[17] = 0.0; //Point6
// VAO allocation
glGenVertexArrays(1, &m_vaoID);
// First VAO setup
glBindVertexArray(m_vaoID);
glGenBuffers(2, m_vboID);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
//initialises data storage of vertex buffer object
glBufferData(GL_ARRAY_BUFFER, numOfVerts*3*sizeof(GLfloat), verts, GL_STATIC_DRAW);
GLint vertexLocation= glGetAttribLocation(myShader.handle(), "in_Position");
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexLocation);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1]);
glBufferData(GL_ARRAY_BUFFER, numOfVerts*3*sizeof(GLfloat), cols, GL_STATIC_DRAW);
GLint colorLocation= glGetAttribLocation(myShader.handle(), "in_Color");
glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(colorLocation);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
}
void init()
{
glClearColor(0.0,0.0,0.0,0.0);
// Shaders
if(!myShader.load("BasicView", "glslfiles/basic.vert", "glslfiles/basic.frag"))
{
cout << "failed to load shader" << endl;
}
createGeometry();
glEnable(GL_DEPTH_TEST);
}