Reputation: 556
I am currently trying to draw an object using VBOs. In the Cube-Class I came up with to test the functionality of VBOs, I create the VBOs like this:
//Vertex
glGenBuffers(1, &m_bufVertex);
glBindBuffer(GL_ARRAY_BUFFER, m_bufVertex);
glBufferData(GL_ARRAY_BUFFER, 6*4*3*sizeof(GLfloat), vertex_data, GL_STATIC_DRAW);
//TexCoord
glGenBuffers(1, &m_bufTexCoord);
glBindBuffer(GL_ARRAY_BUFFER, m_bufTexCoord);
glBufferData(GL_ARRAY_BUFFER, 6*4*2*sizeof(GLfloat), texcoord_data, GL_STATIC_DRAW);
//Index
glGenBuffers(1,&m_bufIndex);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_bufIndex);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,24*sizeof(GLuint), indices, GL_STATIC_DRAW);
I already double checked if the data is in the buffers e.g. like this (the vertex buffer is binded at that time):
GLfloat* test = (GLfloat *) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
for (unsigned int i = 0; i < 6*4*3; i++)
cout << "Value " << i << " = " << test[i] << endl;
So the data is where it is supposed to be, so the problem seems to lie within the Render-Function. The Segmentation-Error happens when glDrawElements() or glDrawArrays() is called. Here is the code:
glBindTexture(GL_TEXTURE_2D, m_texture);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// some translation, currently fixed, just testing code
glTranslatef(0.0f, 0.0f, -200.0f);
glRotatef(30.f, 1.f, 0.f, 0.f);
glRotatef(30.f, 0.f, 1.f, 0.f);
glRotatef(30.f, 0.f, 0.f, 1.f);
// enable client states
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// set pointers
glBindBuffer(GL_ARRAY_BUFFER, m_bufVertex);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_bufTexCoord);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_bufIndex);
// just error check to see if something went wrong previously
GLenum err = glGetError();
if (GL_NO_ERROR != err)
cout << "GL: " << err << " - " << gluErrorString(err) << endl;
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, 0);
// disable state and pop matrix
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glPopMatrix();
I assume I am using the pointers not right or glDrawElements(), but I don't know what. Some more informations: The code is compiled on MacOSX Lion (OpenGL2.1) using the GNU-toolchain and GLEW.
EDIT:
GLfloat vertex_data[6*4*3] = {-m_fSize, -m_fSize, -m_fSize,
-m_fSize, m_fSize, -m_fSize,
m_fSize, m_fSize, -m_fSize,
m_fSize, -m_fSize, -m_fSize,
-m_fSize, -m_fSize, m_fSize,
-m_fSize, m_fSize, m_fSize,
m_fSize, m_fSize, m_fSize,
m_fSize, -m_fSize, m_fSize,
-m_fSize, -m_fSize, -m_fSize,
-m_fSize, m_fSize, -m_fSize,
-m_fSize, m_fSize, m_fSize,
-m_fSize, -m_fSize, m_fSize,
m_fSize, -m_fSize, -m_fSize,
m_fSize, m_fSize, -m_fSize,
m_fSize, m_fSize, m_fSize,
m_fSize, -m_fSize, m_fSize,
-m_fSize, -m_fSize, m_fSize,
-m_fSize, -m_fSize, -m_fSize,
m_fSize, -m_fSize, -m_fSize,
m_fSize, -m_fSize, m_fSize,
-m_fSize, m_fSize, m_fSize,
-m_fSize, m_fSize, -m_fSize,
m_fSize, m_fSize, -m_fSize,
m_fSize, m_fSize, m_fSize};
GLfloat texcoord_data[6*4*2] = {0, 0, 0, 1, 1, 1, 1,
0, 0, 0, 0, 1, 1, 1,
1, 0, 0, 0, 0, 1, 1,
1, 1, 0, 0, 0, 0, 1,
1, 1, 1, 0, 0, 1, 0,
0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1};
GLuint indices[24] = {0,1,2,3,
4,5,6,7,
8,9,10,11,
12,13,14,15,
16,17,18,19,
20,21,22,23};
I know am defining every vertex instead of just 8...
EDIT2: The makefile:
OUT = bin/test
CC = g++
INCDIR = inc
OBJDIR = obj
SRCDIR = src
INC = -I$(INCDIR) -I/opt/local/include
LIBS = -L/opt/local/lib -lGLEW -lsfml-graphics -lsfml-window -lsfml-system -framework AGL -framework OpenGL -framework Foundation
_OBJS = Main.o \
Timer.o \
Cube.o \
chrono.o process_cpu_clocks.o thread_clock.o error_code.o
OBJS = $(patsubst %,$(OBJDIR)/%,$(_OBJS))
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CC) -c $(INC) -o $@ $< $(CFLAGS)
$(OUT): $(OBJS)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
strip $(OUT)
clean:
rm -f $(OBJDIR)/*.o $(OUT)
Upvotes: 0
Views: 3804
Reputation: 330
I had the exact same problem. The problem is that SFML internally uses glEnableClientState(GL_COLOR_ARRAY) and glEnableClientState(GL_TEXTURE_COORD_ARRAY); so you need to disable those if you don't use them yourself.
Or you could use sf::Window instead of sf::RenderWindow, since it doesn't set any OpenGL state internally, which I chose to do.
Post on the SFML forum with this exact problem
Upvotes: 3
Reputation: 1463
Is it possible you left a vertex array enabled in a previous draw? For example, if you do something like this:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(...);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(...);
glDrawElements(...); // draw first object
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(...);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(...);
glDrawElements(...); // draw second object
The second glDrawElements could crash because the normal array is still enabled but probably not the right size for the second draw.
Upvotes: 1