Fixed backface culling/blender mesh.

This commit is contained in:
Jacob Parker 2011-08-12 09:16:03 -04:00
parent df1b0216bc
commit a82f817071
2 changed files with 993 additions and 974 deletions

1875
can.obj

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,4 @@
#ifdef __APPLE__ #ifdef __APPLE__
#define glGenVertexArrays glGenVertexArraysAPPLE #define glGenVertexArrays glGenVertexArraysAPPLE
#define glBindVertexArray glBindVertexArrayAPPLE #define glBindVertexArray glBindVertexArrayAPPLE
@ -105,6 +106,7 @@ void print_shader_error (uint32_t id, uint32_t type) {
Mesh load_mesh (std::string filename) { Mesh load_mesh (std::string filename) {
Mesh m; Mesh m;
uint32_t object_count = 0;
std::ifstream fin (filename.c_str ()); std::ifstream fin (filename.c_str ());
if (!fin.good ()) { std::cerr << "Couldn't open " << filename << "\n"; exit(1); } if (!fin.good ()) { std::cerr << "Couldn't open " << filename << "\n"; exit(1); }
@ -113,7 +115,10 @@ Mesh load_mesh (std::string filename) {
while (!fin.eof ()) { while (!fin.eof ()) {
char line[128]; char line[128];
fin.getline(line, 128); fin.getline(line, 128);
if (line[0] == 'v') { if (line[0] == 'o') {
object_count++;
if (object_count != 1) { std::cerr << "More than one object in mesh.\n"; exit(1); }
} else if (line[0] == 'v') {
Vertex v; Vertex v;
char *fstr = strtok(&(line[1]), " "); char *fstr = strtok(&(line[1]), " ");
v.pos[0] = atof(fstr); v.pos[0] = atof(fstr);
@ -174,7 +179,7 @@ uint32_t load_texture (std::string filename) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_w, image_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)&(image_data.at(0))); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLint)image_w, (GLint)image_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)&(image_data.at(0)));
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
glError(); glError();
std::cerr << "Loaded " << filename << " (" << image_w << "x" << image_h << ")\n"; std::cerr << "Loaded " << filename << " (" << image_w << "x" << image_h << ")\n";
@ -220,11 +225,10 @@ void GL_init (float w, float h) {
glShadeModel (GL_SMOOTH); glShadeModel (GL_SMOOTH);
glEnable (GL_DEPTH_TEST); glEnable (GL_DEPTH_TEST);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// TODO: blender is apparently a dick and doesn't export to .obj with a consistent poly orientation.
// Fix it manually during a load or something to get this culling. glFrontFace (GL_CCW);
// glFrontFace (GL_CCW); glCullFace (GL_BACK);
// glCullFace (GL_BACK); glEnable (GL_CULL_FACE);
// glEnable (GL_CULL_FACE);
glClearColor (0,0,0,0); glClearColor (0,0,0,0);
glEnable (GL_TEXTURE_2D); glEnable (GL_TEXTURE_2D);
@ -249,8 +253,9 @@ void GL_init (float w, float h) {
} }
int main () { int main () {
float w = 480; float scale = 3;
float h = 272; float w = 480*scale;
float h = 272*scale;
if (!glfwInit () || !glfwOpenWindow (w, h, 8,8,8,0,32,0,GLFW_WINDOW)) { if (!glfwInit () || !glfwOpenWindow (w, h, 8,8,8,0,32,0,GLFW_WINDOW)) {
std::cerr << "Something GLFW failed.\n"; std::cerr << "Something GLFW failed.\n";
return 1; return 1;
@ -277,4 +282,3 @@ int main () {
glfwTerminate (); glfwTerminate ();
} }