1
\$\begingroup\$

I'm relatively new to OpenGL and I am trying to draw a 3D model (loaded from a 3ds file using lib3ds) using OpenGL with lighting, but about half of it is drawn in black.

model

I set up the light as such:

glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);

GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

glEnable(GL_LIGHT0);
GLfloat lightColor0[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

The model is in a VBO and drawn using glDrawArrays. The normals are in a separate VBO, and the normals are calculated using lib3ds_mesh_calculate_vertex_normals:

std::vector<std::array<float, 3>> normals;
for (std::size_t i = 0; i < model->nmeshes; ++i) {
  auto& mesh = *model->meshes[i];
  std::vector<float[3]> vertex_normals(mesh.nfaces * 3);
  lib3ds_mesh_calculate_vertex_normals(&mesh, vertex_normals.data());
  for (std::size_t j = 0; j < mesh.nfaces; ++j) {
    auto& face = mesh.faces[j];
    normals.push_back(make_array(vertex_normals[j]));
  }
}
glBindBuffer(GL_ARRAY_BUFFER, normal_vbo_);
glBufferData(GL_ARRAY_BUFFER,
             normals.size() * sizeof(decltype(normals)::value_type),
             normals.data(),
             GL_STATIC_DRAW);

The problem isn't the vertices; the model is drawn correctly when drawing it as a wireframe. I also fixed the normals in Blender using controlN. What could be the problem? Should I store the normals in a different order?

\$\endgroup\$
  • \$\begingroup\$ could the vertices/indices be in counter clockwise order instead of clockwise, or vice versa? also, could you post the 3ds file? \$\endgroup\$ – user13213 May 9 '12 at 17:11
  • \$\begingroup\$ If you enable backface culling, will half of the triangles disappear? \$\endgroup\$ – msell May 10 '12 at 5:53
  • 2
    \$\begingroup\$ Quite an odd combination, seeing C++11 with the OpenGL fixed function pipeline. :P \$\endgroup\$ – knight666 Jul 10 '12 at 11:27
1
\$\begingroup\$

Since pressing Control-N removed your problem, the normals your model had prior to you pressing Control-N were not correct. So yes, your normals were messed up, but your client code is most likely fine.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.