gpt4 book ai didi

c++ - glDrawElements 分割错误

转载 作者:行者123 更新时间:2023-11-27 23:26:24 33 4
gpt4 key购买 nike

我目前正在尝试使用 VBO 绘制一个对象。在我想出的用于测试 VBO 功能的 Cube-Class 中,我创建了这样的 VBO:

//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);

我已经仔细检查了数据是否在缓冲区中,例如像这样(此时绑定(bind)了顶点缓冲区):

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;

所以数据在它应该在的地方,所以问题似乎出在渲染函数中。调用 glDrawElements() 或 glDrawArrays() 时会发生段错误。这是代码:

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();

我假设我使用的指针不正确或 glDrawElements(),但我不知道是什么。一些更多的信息:该代码使用 GNU 工具链和 GLEW 在 MacOSX Lion (OpenGL2.1) 上编译。

编辑:

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};

我知道我正在定义每个顶点,而不仅仅是 8 个...

编辑2:生成文件:

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)

最佳答案

我遇到了完全相同的问题。问题是 SFML 内部使用glEnableClientState(GL_COLOR_ARRAY) 和glEnableClientState(GL_TEXTURE_COORD_ARRAY);因此,如果您自己不使用它们,则需要禁用它们。

或者您可以使用 sf::Window 而不是 sf::RenderWindow,因为它不会在内部设置任何 OpenGL 状态,而我选择这样做。

Post on the SFML forum with this exact problem

关于c++ - glDrawElements 分割错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9151837/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com