Why glDrawArrays doesn't draw with OpenGL on mac - c++

I'm new to OpenGL and I can't figure out why nothing is displayed in the window. I'm using Mavericks (OS X 10.9.5) and there should be something missing I guess
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include "FirstTriangleExample.h"
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
GLuint vertexbuffer;
GLuint VertexArrayID;
void FirstTriangleExample::init() {
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glGenVertexArraysAPPLE(1, &VertexArrayID);
glBindVertexArrayAPPLE(VertexArrayID);
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
}
// called in loop
void FirstTriangleExample::update() {
glClear( GL_COLOR_BUFFER_BIT );
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glFlush();
}
void FirstTriangleExample::dispose() {
glDeleteBuffers(1, &vertexbuffer);
glDeleteVertexArraysAPPLE(1, &VertexArrayID);
glDeleteProgram(programID);
}
with the example below everything works fine(I see the red square)
#include "RedSquareExample.h"
#include <SFML/OpenGL.hpp>
void RedSquareExample::init() {}
void RedSquareExample::update() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush();
}
void RedSquareExample::dispose() {}
Maybe the problem is in using APPLE extension (glGenVertexArraysAPPLE() and glBindVertexArrayAPPLE())?

As far as I see from your code, you are using the fixed function pipeline. But this code:
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
is only supposed to work when using shaders. In case of fixed function, you might want to use something like
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, (void*)0);
In addition, you are not using the VAO correctly. The purpose of a VAO is to store the bindings of buffers to attributes, but you reset the binding in your update method anyhow.

Related

VAO not rendering colors OpenGL?

I was hoping to get some help on an OpenGL question concerning the creation and rendering of VAOs. The goal here is simply to take this:
glBegin(GL_TRIANGLES);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glEnd();
which renders a red square in the middle of the window, and turn it into a VAO with vertices, colors, and indices. Now, this is what I have so far as far as creating the VAO (sorry if this is a bit long for the code):
//initialize all data
verts_amt = 6;
Vertex* verts = (Vertex*)malloc(sizeof(Vertex) * verts_amt);
int* indices = (int*)malloc(sizeof(int) * verts_amt);
verts[0] = createVertex(-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
verts[1] = createVertex(-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
verts[2] = createVertex(0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
verts[3] = createVertex(-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
verts[4] = createVertex(0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
verts[5] = createVertex(0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
int i;
for(i = 0; i < 6; i ++)
indices[i] = i;
unsigned int vbo, ibo;
//create, bind, set data, and then unbind for the vbo and ibo
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verts_amt * sizeof(Vertex), verts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, verts_amt * sizeof(int), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//create vao, bind vao, bind and set data for vbo, bind ibo, then unbind vao
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, r));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBindVertexArray(0);
in case you are wondering, Vertex is just a struct made up of seven floats in the order x, y, z, r, g, b, a. After looking through similar questions on this topic, I'm still not seeing what I'm missing and/or not doing right, because when I render it with these simple lines:
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, verts_amt, GL_UNSIGNED_INT, NULL);
it renders just a competey white square. Keep in mind I'm not changing anything else about my rendering loop except of course getting rid of the Vertex3f, glBegin/glEnd, and Color4f calls. I should also mention that I'm not using a shader program, I'm not entirely sure if that changes anything drastically here. Any help on this would be much appreciated!
I should also mention that I'm not using a shader program.
You cannot use glVertexAttribPointer without a shader. These functions cannot interface with the OpenGL fixed function pipeline (FFP).
If you want to use the FFP and still use buffer objects, then you should use the appropriate functions. glVertexPointer (no "Attrib") and glColorPointer are the array equivalents to glVertex and glColor. These work with VAOs just fine.
So the only thing you need to change is your two glVertexAttribPointer calls to be glVertexPointer and glColorPointer (and of course adjusting the parameters accordingly).

OpenGL - Properly separating VAO definition from VBO and other buffers creation

Running a very simple draw a triangle example, I'm trying to find out which lines refer specifically to the VAO definition and which others can run apart.
Currently I'm using this code:
Buffer creation & filling:
TriangleVertices = new float[9]
{
0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
TriangleColors = new float[12]
{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
};
glGenBuffers(1, &VertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), TriangleVertices, GL_STATIC_DRAW);
glGenBuffers(1, &ColorsBuffer);
glBindBuffer(GL_ARRAY_BUFFER, ColorsBuffer);
glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), TriangleColors, GL_STATIC_DRAW);
glGenVertexArrays(1, &VAOID);
VAO definition:
glBindVertexArray(VAOID);
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, ColorsBuffer);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
Drawing Loop
glBindVertexArray(VAOID);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
The example runs fine if in the drawing loop I include the VAO definition each frame, but it doesn't work if I define the VAO only once before starting to draw so I guess the VAO definition lacks some code.
What am I missing?
EDIT: The code in the question was correct. The problem was coming from collision with other OpenGL calls I thought I wasn't running.
The code described below is just situational. As #derhass commented, there is no need to enable nor disable VertexAttribArray when making the draw call.
...
Old answer:
Unbindinding and disabling each element when not needed anymore fixed the problem, but I don't know if this solution can be widespread or it is specific to my environment only.
Buffer creation & filling:
TriangleVertices = new float[9]
{
0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
TriangleColors = new float[12]
{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
};
glGenBuffers(1, &VertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), TriangleVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &ColorsBuffer);
glBindBuffer(GL_ARRAY_BUFFER, ColorsBuffer);
glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), TriangleColors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &VAOID);
VAO definition:
glBindVertexArray(VAOID);
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, ColorsBuffer);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
Drawing Loop
glBindVertexArray(VAOID);
glEnableVertexAttribArray(0);//not needed
glEnableVertexAttribArray(1);//not needed
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);//not needed
glDisableVertexAttribArray(1);//not needed
glBindVertexArray(0);

How to bind multiple IBOs from one VBO in OpenGL

I am trying to render two different triangles with IBOs. I stored the six vertices in one VBO and tried to access them through two separate IBOs. The problem is the first IBO renders but the second doesn't. The createVertices and createIndices are called at initialization.
void createVertices()
{
//Vertex Data
GLfloat v[] = { 0.95f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
0.0f, -0.75f, 0.0f, 1.0f, // END OF TRIANGLE 1
-0.75, 0.75f, 0.5f, 1.0f,
-0.75, -0.75f, 0.5f, 1.0f,
0.0f, -0.75f, 0.5f, 1.0f }; // END OF TRIANGLE 2
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(v), v, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//////////
void createIndices()
{
GLushort i[] = { 0,1,2};
glGenBuffers(2, IBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(i), i, GL_STATIC_DRAW);
size = (sizeof(i)/sizeof(GLushort)); // USED IN DRAWELEMENTS
GLushort w[] = { 3,4,5};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(w), w, GL_STATIC_DRAW);
size2 = (sizeof(i)/sizeof(GLushort)); // USED IN DRAWELEMENTS
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
/////////
void Render()
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader.SProgram);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0,4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)IBO[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]);
glDrawElements(GL_TRIANGLES,size2,GL_UNSIGNED_SHORT,(GLvoid*)IBO[1]);
glUseProgram(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
This doesn't make sense:
glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)IBO[0]);
^^^^^^^^^^^^^^
The parameter to glDrawElements with an IBO bound is an offset into the buffer just as it is with gl…Pointer and VBOs. You probably just want this
glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)0);
for both your IBOs and just bind the IBO itself with
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[…]);

VBO texturing does not work

The problem is that this code display nothing. I have problem with my loads of textures. If I comment the code lines about the textures treatments it works perfectly. It displays a 4 colored square. Can you help me ?
#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)
GLfloat vertices[12] =
{
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
};
GLfloat colors[12] =
{
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
};
GLfloat texture[8] =
{
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Texture Mapping",NULL);
SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);
bool continuer = true;
SDL_Event event;
GLuint buf[2];
GLuint texture1;
glClearDepth(1.0f);
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
glewInit();
texture1 = loadTexture("caisse.jpg");
glBindTexture(GL_TEXTURE_2D, texture1);
glGenBuffers(2, &buf[0]);
glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, texture1);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture), texture, GL_STATIC_DRAW);
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = false;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
glColorPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
glBindBuffer(GL_ARRAY_BUFFER, texture1);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
Firstly, you need to start putting glGetError() in your code for debugging, as you're certainly generating some errors here.
You're binding your texture texture1 with glBindBuffer, which is not correct to do so. Texture coordinates are not the same as textures. You should be generating 3 buffers instead of 2, and loading the texture[8] texcoords into the third buffer.

Texture binding with VBO does not work correctly

the purpose of my program is to display 2 quads with 2 different textures. The problem is that the oldest texture loaded is set on the two quads (only texQuadB). I tried to replace the two call of buf[3] by texQuadA and texQuadB but it doesn't work. I cannot find the right way to bind a texture with a specific VBO.
#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)
GLfloat verticesQuadA[12] =
{
-1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f,
};
GLfloat verticesQuadB[12] =
{
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f,
};
GLfloat colors[12] =
{
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
};
GLfloat texture[8] =
{
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Texture Mapping",NULL);
SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);
bool continuer = true;
SDL_Event event;
GLuint buf[4];
GLuint texQuadA, texQuadB;
glClearDepth(1.0f);
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
glewInit();
texQuadA = loadTexture("caisse.jpg");
texQuadB = loadTexture("metal.jpg");
//glBindTexture(GL_TEXTURE_2D, texQuadA);
//glBindTexture(GL_TEXTURE_2D, texQuadB);
glGenBuffers(4, buf);
glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesQuadA), verticesQuadA, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesQuadB), verticesQuadB, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, buf[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, buf[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture), texture, GL_STATIC_DRAW);
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = false;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
//Draw Quad A ---------------------------------------
glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
glBindBuffer(GL_ARRAY_BUFFER, buf[2]);
glColorPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
glBindBuffer(GL_ARRAY_BUFFER, buf[3]);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
//Draw Quad B ---------------------------------------
glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
glBindBuffer(GL_ARRAY_BUFFER, buf[2]);
glColorPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
glBindBuffer(GL_ARRAY_BUFFER, buf[3]);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
//----------------------------------------------------
glFlush();
SDL_GL_SwapBuffers();
}
glDeleteBuffers(3, buf);
glDisable(GL_TEXTURE_2D);
SDL_Quit();
return 0;
}
OpenGL is a state machine. Binding a texture is setting part of that state. When textures are enabled, OpenGL will use the current texture state (the last texture you bound) when it goes to draw the geometry. Before you draw Quad A, bind texQuadA. Before you draw Quad B, bind texQuadB:
glBindTexture(GL_TEXTURE_2D, texQuadA);
// Draw Quad A
glBindTexture(GL_TEXTURE_2D, texQuadB);
// Draw Quad B
VBOs are unrelated in this case.
VBOs have no interaction with texture objects. Binding a VBO does not associate it to a texture.
To select which texture you want to use for drawing you call glBindTexture before doing the drawing commands using it.