What went wrong?
It’s a typo error. An extra ‘F’ has come here
[LEFT]glBufferSubData(GL_ELEMENT_ARRAY_BUFFFER, indexOffset, sizeof(indices), indices);[/LEFT]
and similarly extra ‘s’ has come in another sentence.
Yes, at least it’s supposed to be.
That’s what I also feel. Overall logic seems to be correct
If a buffer object is bound to GL_ELEMENT_ARRAY_BUFFER, the 4th argument to glDrawElements() is merely an offset into the buffer’s datastore.
Well, actually there is no binding being done to any data structure. Can this be a reason?
Do you get any GL errors? Is everything setup correctly so you can issue GL commands, for instance if you call glClearColor(1.f, 0.f, 0.f, 1.f) and glClear(GL_COLOR_BUFFER_BIT), do you see a red window background? Are the model-view-matrix, the projection and the viewport set correctly?
I get the previously rendered image once I rub the application. Changing background color using glClearColor() has no impact and again the previous successfully rendered image is displayed. Well, here’s the entire code. I have created a separate files.
Below code is ctest.h
#ifndef __ctest_h_
#define __ctest_h_
struct ctest
{
int numVertices;
void Begin();
void End();
void Render();
void InitBuffers();
void UpdateBuffers(float *coord);
};
#endif
The corresponding cpp file goes like this
#include "ctest.h"
// the size computation assumes 12 vertices with 4 components each
GLsizeptr vertexBufferSize = 12 * 4 * sizeof(GLfloat);
// we'll need two indices per line segment, so the total index buffer size is 2 * maxVertices = 24.
// indices aren't surpassing 256 elements in this case so choosing indices of type GL_UNSIGNED_BYTE is sufficient.
GLsizeptr indexBufferSize = 24 * sizeof(GLubyte);
// store buffer handles somewhere - we'll use buffers[0] as VBO handle, and buffers[1] as index buffer handle.
GLuint buffers[2];
void ctest::InitBuffers()
{
glGenBuffers(2, &buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, vertexBufferSize, NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBufferSize, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
}
void ctest::Begin()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 640.0f / 480.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -4.0f);
numVertices = 0;
InitBuffers();
}
void ctest::End()
{
glDeleteBuffers(2, buffers);
}
void ctest::Render()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); glDrawElements(GL_LINES, numVertices * 2, GL_UNSIGNED_BYTES, NULL);
}
void ctest::UpdateBuffers(float* coords)
{
GLintptr vertexOffset = 4 * (numVertices - 1) * sizeof(GLfloat);
GLintptr indexOffset = 2 * (numVertices - 1) * sizeof(GLubyte);
GLubyte indices[] = {numVertices - 1, numVertices};
// append new vertex
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferSubData(GL_ARRAY_BUFFER, vertexOffset, 4 * sizeof(GLfloat), coords);
// append new indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFFER, indexOffset, sizeof(indices), indices);
}
In main function, this is how it is
ctest *test = new ctest;
test->Begin();
bool Exit = false;
while(!Exit)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
{
float coords[4];
coords[0] = static_cast<float>(button.x);
coords[1] = static_cast<float>(480 - button.y);
coords[2] = coords[3] = 0;
test->UpdateBuffers(coords);
}
break;
case SDL_QUIT:
case SDL_KEYUP:
Exit = true;
break;
}
}
test->Render();
SDL_GL_SwapBuffers();
int Error;
if((Error = glGerError()) != GL_NO_ERROR)
{
const char *message =
(const char *)gluErrorString(Error);
fprintf(stderr, "OpenGL error: %s
", message);
}
}
test->End();
delete test;
SDL_QUIT();
Thanks