c++ - glBufferData and glBufferSubData Offset -
i'm attempting render suzanne (from blender) in opengl 3.3, buffer data doesn't seem correct. this:
https://gyazo.com/ab82f9acb6854a49fccc527ed96cc4e8
i tried render sphere simple texture:
https://gyazo.com/85c1e87fcc4eab128ca37b1a0cb1deaa
my importer inserts vertex data std::vector single floats:
if(line.substr(0,2) == "v ") { /** vertex position */ std::istringstream s(line.substr(2)); float v[3]; s >> v[0]; s >> v[1]; s >> v[2]; this->vertices.push_back(v[0]); this->vertices.push_back(v[1]); this->vertices.push_back(v[2]); }
i setup array buffer follows:
glgenbuffers(1, &this->vbo); glbindbuffer(gl_array_buffer, this->vbo); glbufferdata(gl_array_buffer, sizeof(float)*(this->vertices.size()+this->textures.size()+this->normals.size()), null, gl_static_draw);
and insert actual data using glbuffersubdata
glbuffersubdata(gl_array_buffer, 0, sizeof(float)*this->vertices.size(), this->vertices.data()); glbuffersubdata(gl_array_buffer, sizeof(float)*this->vertices.size(), sizeof(float)*this->textures.size(), this->textures.data()); glbuffersubdata(gl_array_buffer, sizeof(float)*(this->vertices.size()+this->textures.size()), sizeof(float)*this->normals.size(), this->normals.data());
i insert indices in same way (gl_element_array_buffer of course).
i point information:
glvertexattribpointer(0, 3, gl_float, gl_false, 3*sizeof(float), (glvoid*)0); glvertexattribpointer(0, 3, gl_float, gl_false, 3*sizeof(float), (glvoid*)(sizeof(float)*this->v.size())); glvertexattribpointer(0, 3, gl_float, gl_false, 3*sizeof(float), (glvoid*)(sizeof(float)*this->v.size()+this->vt.size()));
my vertex shader takes in data this:
layout(location = 0) in vec3 position; layout(location = 1) in vec2 texcoord; layout(location = 2) in vec3 normals;
am screwing offsets?
edit: figured out biggest issue! wrote external lua program convert obj files format easier import, ended messing data , duplicating indices on "f #/#/# #/#/# #/#/#" file looked (x->y->x) instead of (x->y->z)
also fixed few other errors responses below!
without seeing shaders can't 100% sure if calls glvertexattribpointer
legit. can't tell if want interleaved vertex data in single vbo or not. have packs of vertex positions in first, of texture coordinates, , of normals.
to interleave data first want put of single array (or vector) each vertex repeats pppttnnn
pattern. ppp
3 position floats, tt
2 texcoord floats, , nnn
3 normal floats.
it (using bogus types, values, , spacing illustrate pattern):
float[] vertices = { /* px, py, pz, tx, ty, nx, ny, nz */ 1, 1, 1, 0, 0, 1, 1, 1, // vertex 1 0, 0, 0, 1, 1, 0, 0, 0, // vertex 2 1, 1, 1, 0, 0, 1, 1, 1, // vertex 3 ... };
let's put single vector called vertices
, can upload single command:
glbufferdata(gl_array_buffer, sizeof(float) * this->vertices.size(), this->vertices.data(), gl_static_draw);
you put each attribute own vbo. how decide store data on gpu you. if purposely storing data way have it, let me know in comments , i'll update answer.
ok, shader bits.
let's you've got vertex shader looks this:
in vec3 position; in vec2 texcoord; in vec3 normal; out vec2 uv; void main() { gl_position = vec4(position, 1); uv = texcoord; }
and fragment shader looks this:
in vec2 uv; uniform sampler2d image; out vec4 color; void main() { color = texture(image, uv); }
then want following glvertexattribpointer
calls:
int stride = (3 + 2 + 3) * sizeof(float); glvertexattribpointer(0, 3, gl_float, gl_false, stride, (glvoid*)0); glvertexattribpointer(1, 2, gl_float, gl_false, stride, (glvoid*)3); glvertexattribpointer(2, 3, gl_float, gl_false, stride, (glvoid*)5);
notice how first parameter each call different number. corresponds position
, texcoord
, normal
respectively in vertex shader.
also texture coordinates typically pair of floats (e.g. vec2 texcoord
) changed second parameter 2
texcoord call.
finally last parameter, when using interleaved array, needs specify offset per vertex. 0
, 3
, , 5
position offset, texcoord offset, , normal offset respectively.
hopefully gets want be.
check out docs.gl page on glvertexattribpointer more info.
Comments
Post a Comment