【问题标题】:Interleaved VBO with coords,normals and color具有坐标、法线和颜色的交错 VBO
【发布时间】:2013-11-14 19:27:51
【问题描述】:

我有一个关于交错 vbo 的问题。我有一个看起来像这样的结构

struct VertexData{
    float x,y,z;   //vertex coordinates
    float normalx,normaly,normalz;  //vertex normal
    float cx,cy,cz;  //vertex color
};

这就是我创建 VBO、VAO、IBO 的方式:

    //creat OpenGL objects to use in drawing
    unsigned int gl_vertex_array_object, gl_vertex_buffer_object, gl_index_buffer_object;

    //vertex array object 
    glGenVertexArrays(1, &gl_vertex_array_object);
    glBindVertexArray(gl_vertex_array_object);

    //vertex buffer object -> we hold the vertices 
    glGenBuffers(1,&gl_vertex_buffer_object);
    glBindBuffer(GL_ARRAY_BUFFER, gl_vertex_buffer_object);
    glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(VertexData), &vertices[0], GL_STATIC_DRAW);


    //index buffer object -> we hold the index of vertex
    glGenBuffers(1,&gl_index_buffer_object);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_index_buffer_object);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);

    //the connection between attributes, interleaved data
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(VertexData),(void*)0);                       //send positions on pipe 0
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(VertexData),(void*)(sizeof(float)*3));       //send normals on pipe 1
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,sizeof(VertexData),(void*)(3*sizeof(float)*3));     //send colors on pipe 2

    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glNormalPointer(GL_FLOAT,sizeof(VertexData),(void*)(sizeof(float)*3));   
    glColorPointer(3,GL_FLOAT,sizeof(VertexData),(void*)(3*sizeof(float)*3));

    vao = gl_vertex_array_object;
    vbo = gl_vertex_buffer_object;
    ibo = gl_index_buffer_object;
    num_indices = indices.size();

如果我的 VBO 是 VertexData 的向量,是否意味着它是交错的?我在上面发送的数据是否正确?步幅和偏移量? 这一切都在另一个班级。这是我如何加载网格并在主类中绘制它

    //load
 teren::loadTerrain("resurse\\heightmap.bmp","resurse\\heightmap_color.bmp",mesh_vao_ground, mesh_vbo_ground, mesh_ibo_ground, mesh_num_indices_ground,20);
    //draw
    glUniformMatrix4fv(glGetUniformLocation(gl_program_shader_curent, "model_matrix"),1,false,glm::value_ptr(matrice_translatie));
    glBindVertexArray(mesh_vao_ground);
    glDrawElements(GL_TRIANGLES, mesh_num_indices_ground, GL_UNSIGNED_INT, 0);

我的地形已绘制,但颜色很奇怪。它有粉红色和蓝色等等,看起来不像我从中获得顶点颜色的彩色图像。这是我的 gourard 顶点着色器:

layout(location = 0) in vec3 in_position;       
layout(location = 1) in vec3 in_normal; 
layout(location = 2) in vec3 in_color;  

uniform mat4 model_matrix, view_matrix, projection_matrix;
uniform vec3 light_position;
uniform vec3 eye_position;
uniform int material_shininess;
uniform float material_kd;
uniform float material_ks;

out vec3 light;
void main(){


gl_Position = projection_matrix*view_matrix*model_matrix*vec4(in_position,1.0); 
float fDiffuseIntensity = material_kd * max(0.0, dot(normalize(in_normal), normalize(light_position)));
light = material_ks * in_color * fDiffuseIntensity;
}

还有我的 gourard 片段着色器:

in vec3 light;
out vec4 out_color;

void main()
{
    out_color = vec4(light,1.0);
}

我现在真的被困住了。这是我的着色器或我的 vbo/属性等的问题,或者甚至两者都有问题。我真的很感激一些帮助。(PS:如果在顶点着色器中我更改为 light=in_color ,一切都是白色的)。我还有一个在地形上方旋转的光。这是正在发生的事情的照片: http://i44.tinypic.com/69j0cg.jpg

【问题讨论】:

    标签: c++ vbo opengl-3


    【解决方案1】:

    这一行:

    glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,sizeof(VertexData),(void*)(3*sizeof(float)*3));
    

    应该是:

    glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,sizeof(VertexData),(void*)(sizeof(float)*6));
    

    你也不需要这些东西:

    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glNormalPointer(GL_FLOAT,sizeof(VertexData),(void*)(sizeof(float)*3));   
    glColorPointer(3,GL_FLOAT,sizeof(VertexData),(void*)(3*sizeof(float)*3));
    

    其余的都是正确的。


    意思是交错的吗?

    是的。


    语言律师可能会告诉您,由于允许编译器在 VertexData 结构内的任何位置添加填充,将这些数据传递给 GL 的正确方法是手动将您的 VertexData 向量转换为浮动。实际上,没有编译器填充在这样的结构内浮动。

    【讨论】:

    • 是的,我正在考虑将顶点缓冲区转换为浮点缓冲区,如果这样做更好的话对我来说没问题。我改变了那条线,现在它似乎渲染得更好(当我保存颜色时我忘了除以 255 ,我的错)但我的结果仍然不正确。我的彩色高度图主要是棕色,但渲染的网格是绿色的,所以这很奇怪。不知道是照明问题还是我的正常计算。必须弄清楚这一点。
    • 尝试了一组不同的图像。仍然奇怪的结果。这是渲染的地形i41.tinypic.com/16kzvyb.jpg。有些颜色看起来不错,但其余的都是乱码。这是用于提取颜色的图像:read.pudn.com/downloads186/sourcecode/windows/opengl/871583/res/…
    • 问题解决了,我接受了你的回答 sbabbi,谢谢。如果 some1 感兴趣,我将 r,g,b 值保存为浮点数,而像素是 char 的一维数组,所以我更改为 unsigned char 然后除以 255.0f 以转换为浮点数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 2015-09-28
    • 2018-06-04
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多