ошибка в glVertexAttribPointer
я хочу сделать вывод без vao, так как он сложнее для реализации, потому что данные спрайта находятся отдельно от его координат и параметров текстуры. вот как я текстуру создаю. это пока на первое время.
void Sprite::createFrame (const int count_x, const int count_y) {
int maxCount = count_x * count_y;
this->frame = new unsigned int[maxCount];
gl::GenTextures (1, &this->frame[0]);
printf ("genTextures: %d\n", gl::GetError());
gl::BindTexture (gl::TEXTURE_2D, this->frame[0]);
printf ("bindTexture: %d\n", gl::GetError());
gl::TexParameteri (gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST);
printf ("texparameteri min: %d\n", gl::GetError());
gl::TexParameteri (gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST);
printf ("texparameteri mag: %d\n", gl::GetError());
gl::TexImage2D (gl::TEXTURE_2D, 0, gl::RGBA, GameObject::width, GameObject::height, 0, gl::RGBA, gl::UNSIGNED_BYTE, this->commonData);
printf ("teximage2d: %d\n", gl::GetError());
this->vertex[0] = 0;
this->vertex[1] = 0;
this->vertex[2] = 0;
this->vertex[3] = GameObject::height;
this->vertex[4] = GameObject::width;
this->vertex[5] = 0;
this->vertex[6] = GameObject::width;
this->vertex[7] = 0;
this->vertex[8] = GameObject::width;
this->vertex[9] = GameObject::height;
this->vertex[10] = 0;
this->vertex[11] = GameObject::height;
this->texture[0] = 0.0f;
this->texture[1] = 0.0f;
this->texture[2] = 0.0f;
this->texture[3] = 1.0f;
this->texture[4] = 1.0f;
this->texture[5] = 0.0f;
this->texture[6] = 1.0f;
this->texture[7] = 0.0f;
this->texture[8] = 1.0f;
this->texture[9] = 1.0f;
this->texture[10] = 0.0f;
this->texture[11] = 1.0f;
}
потом рисую
void Sprite::draw () {
printf ("------------\n");
gl::UseProgram (shaderController->program[SHADERS::SPRITE]);
printf ("useprogram: %d\n", gl::GetError());
gl::ActiveTexture (gl::TEXTURE0);
printf ("activetexture: %d\n", gl::GetError());
#if 1
gl::BindTexture (gl::TEXTURE_2D, this->frame[0]);
printf ("bind texture: %d\n", gl::GetError());
#endif
gl::Uniform1i (this->location_tex, this->frame[0]);
printf ("uniform1i: %d\n", gl::GetError());
gl::UniformMatrix4fv (this->location_view, 1, gl::TRUE_, &GameObject::ortho[0][0]);
printf ("uniformmatrix4fv: %d\n", gl::GetError());
gl::Uniform3fv (this->location_pos, 1, &GameObject::pos[0]);
printf ("uniform3fv: %d\n", gl::GetError());
gl::EnableVertexAttribArray (0);
printf ("enable vertex 0: %d\n", gl::GetError());
gl::EnableVertexAttribArray (1);
printf ("enable vertex 1: %d\n", gl::GetError());
gl::VertexAttribPointer (0, 2, gl::FLOAT, gl::FALSE_, 0, &this->vertex[0]);
printf ("vertex pointer 0: %d\n", gl::GetError());
gl::VertexAttribPointer (1, 2, gl::FLOAT, gl::FALSE_, 0, &this->texture[0]);
printf ("vertexpointer 1: %d\n", gl::GetError());
gl::DrawArrays (gl::TRIANGLES, 0, 12);
printf ("draw: %d\n", gl::GetError());
exit (0);
}
вот вывод при рисовании.
viewport: 0
OpenGL 4.6 (Core Profile) Mesa 20.2.6, GLSL 4.60
invalid enum: 1280
invalid value: 1281
invalid operation: 1282
enable texture: 0
genTextures: 0
bindTexture: 0
texparameteri min: 0
texparameteri mag: 0
teximage2d: 0
------------
useprogram: 0
activetexture: 0
bind texture: 0
uniform1i: 0
uniformmatrix4fv: 0
uniform3fv: 0
enable vertex 0: 0
enable vertex 1: 0
vertex pointer 0: 1282
vertexpointer 1: 1282
draw: 1282
что я не правильно делаю в vertex pointer?
последние исправления. вот что добавил в createFrame.
gl::GenBuffers (1, &this->index_vertex);
printf ("gen buffer vertex: %d\n", gl::GetError());
gl::BindBuffer (gl::ARRAY_BUFFER, this->index_vertex);
printf ("bind buffer vertex: %d\n", gl::GetError());
gl::BufferData (gl::ARRAY_BUFFER, 12 * sizeof (float), 0, gl::STATIC_DRAW);
printf ("buffer data vertex: %d\n", gl::GetError());
gl::BufferSubData (gl::ARRAY_BUFFER, 0, 12 * sizeof (float), this->vertex);
printf ("buffer subdata vertex: %d\n", gl::GetError());
gl::GenBuffers (1, &this->index_texture);
printf ("gen buffer texture: %d\n", gl::GetError());
gl::BindBuffer (gl::ARRAY_BUFFER, this->index_texture);
printf ("bind buffer texture: %d\n", gl::GetError());
gl::BufferData (gl::ARRAY_BUFFER, 12 * sizeof (float), 0, gl::STATIC_DRAW);
printf ("buffer data texture: %d\n", gl::GetError());
и поменял в draw
gl::EnableVertexAttribArray (0);
printf ("enable vertex 0: %d\n", gl::GetError());
gl::EnableVertexAttribArray (1);
printf ("enable vertex 1: %d\n", gl::GetError());
gl::BindBuffer (gl::ARRAY_BUFFER, this->index_vertex);
printf ("bindbuffer vertex: %d\n", gl::GetError());
gl::VertexAttribPointer (0, 2, gl::FLOAT, gl::FALSE_, 0, reinterpret_cast< void * >(0));
printf ("vertex pointer 0: %d\n", gl::GetError());
но не работает.
последний код в draw
void Sprite::draw () {
printf ("------------\n");
gl::UseProgram (shaderController->program[SHADERS::SPRITE]);
printf ("useprogram: %d\n", gl::GetError());
gl::ActiveTexture (gl::TEXTURE0);
printf ("activetexture: %d\n", gl::GetError());
#if 1
gl::BindTexture (gl::TEXTURE_2D, this->frame[0]);
printf ("bind texture: %d\n", gl::GetError());
#endif
gl::Uniform1i (this->location_tex, this->frame[0]);
printf ("uniform1i: %d\n", gl::GetError());
gl::UniformMatrix4fv (this->location_view, 1, gl::TRUE_, &GameObject::ortho[0][0]);
printf ("uniformmatrix4fv: %d\n", gl::GetError());
gl::Uniform3fv (this->location_pos, 1, &GameObject::pos[0]);
printf ("uniform3fv: %d\n", gl::GetError());
gl::BindBuffer (gl::ARRAY_BUFFER, this->index_vertex);
printf ("bindbuffer vertex: %d\n", gl::GetError());
gl::VertexAttribPointer (0, 2, gl::FLOAT, gl::FALSE_, 0, reinterpret_cast< void * >(0));
printf ("vertex pointer 0: %d\n", gl::GetError());
gl::EnableVertexAttribArray (0);
printf ("enable vertex 0: %d\n", gl::GetError());
gl::BindBuffer (gl::ARRAY_BUFFER, this->index_texture);
printf ("bindbuffer texture: %d\n", gl::GetError());
gl::VertexAttribPointer (1, 2, gl::FLOAT, gl::FALSE_, 0, reinterpret_cast< void * >(0));
printf ("vertexpointer 1: %d\n", gl::GetError());
gl::EnableVertexAttribArray (1);
printf ("enable vertex 1: %d\n", gl::GetError());
gl::DrawArrays (gl::TRIANGLES, 0, 12);
printf ("draw: %d\n", gl::GetError());
exit (0);
}
вот последний вывод.
viewport: 0
OpenGL 4.6 (Core Profile) Mesa 20.2.6, GLSL 4.60
invalid enum: 1280
invalid value: 1281
invalid operation: 1282
enable texture: 0
genTextures: 0
bindTexture: 0
texparameteri min: 0
texparameteri mag: 0
teximage2d: 0
gen buffer vertex: 0
bind buffer vertex: 0
buffer data vertex: 0
gen buffer texture: 0
bind buffer texture: 0
buffer data texture: 0
------------
useprogram: 0
activetexture: 0
bind texture: 0
uniform1i: 0
uniformmatrix4fv: 0
uniform3fv: 0
bindbuffer vertex: 0
vertex pointer 0: 1282
enable vertex 0: 0
bindbuffer texture: 0
vertexpointer 1: 1282
enable vertex 1: 0
draw: 1282
Ответы (1 шт):
Автор решения: user7860670
→ Ссылка
glVertexAttribPointer устанавливает атрибуты для забинденного как GL_ARRAY_BUFFER в текущий момент буфера. Причем последний параметр - смещение данных от начала буфера, а не указатель на буфер, то бишь в данном случае должен быть reinterpret_cast< void * >(0).