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
@ -17,11 +18,11 @@
#include <cmath> #include <cmath>
#define glError() { \ #define glError() { \
GLenum err = glGetError(); \ GLenum err = glGetError(); \
while (err != GL_NO_ERROR) { \ while (err != GL_NO_ERROR) { \
std::cerr << "glError: " << (char *)gluErrorString(err) << " at " << __LINE__; \ std::cerr << "glError: " << (char *)gluErrorString(err) << " at " << __LINE__; \
err = glGetError(); \ err = glGetError(); \
} \ } \
} }
// picopng // picopng
@ -77,11 +78,11 @@ void render () {
glRotatef (x+=1.0, 0,1,0); glRotatef (x+=1.0, 0,1,0);
glRotatef (30.0, 1.0,0,0); glRotatef (30.0, 1.0,0,0);
GLint shader_tex_loc = glGetUniformLocation(shaders[0].prog_id, "texture"); GLint shader_tex_loc = glGetUniformLocation(shaders[0].prog_id, "texture");
glUniform1i(shader_tex_loc, 0); glUniform1i(shader_tex_loc, 0);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, popcan_tex_id); glBindTexture(GL_TEXTURE_2D, popcan_tex_id);
glError(); glError();
glBindVertexArray (popcan.vao_id); glBindVertexArray (popcan.vao_id);
glDrawElements(GL_TRIANGLES, popcan.num_poly, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, popcan.num_poly, GL_UNSIGNED_INT, 0);
@ -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);
@ -150,35 +155,35 @@ Mesh load_mesh (std::string filename) {
} }
uint32_t load_texture (std::string filename) { uint32_t load_texture (std::string filename) {
std::vector<unsigned char> image_data; std::vector<unsigned char> image_data;
long unsigned int image_w, image_h; long unsigned int image_w, image_h;
size_t png_size; size_t png_size;
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); }
fin.seekg(0, std::ios::end); fin.seekg(0, std::ios::end);
png_size = fin.tellg(); png_size = fin.tellg();
fin.seekg(0, std::ios::beg); fin.seekg(0, std::ios::beg);
unsigned char png_data[png_size]; unsigned char png_data[png_size];
fin.read((char*)png_data, png_size); fin.read((char*)png_data, png_size);
{ int r = decodePNG(image_data, image_w, image_h, png_data, png_size); { int r = decodePNG(image_data, image_w, image_h, png_data, png_size);
if(r) { std::cerr << "Couldn't read PNG data from " << filename << "\n"; exit(1); }} if(r) { std::cerr << "Couldn't read PNG data from " << filename << "\n"; exit(1); }}
uint32_t tex_id; uint32_t tex_id;
glGenTextures(1, &tex_id); glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id); glBindTexture(GL_TEXTURE_2D, tex_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
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";
return tex_id; return tex_id;
} }
uint32_t compile_individual_shader (const char *src, uint32_t len, uint32_t type) { uint32_t compile_individual_shader (const char *src, uint32_t len, uint32_t type) {
@ -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;
@ -261,7 +266,7 @@ int main () {
float t0 = glfwGetTime (); float t0 = glfwGetTime ();
uint32_t frames = 0; uint32_t frames = 0;
// glfwSwapInterval (0); // Uncomment this to test FPS without vsync //glfwSwapInterval (0); // Uncomment this to test FPS without vsync
while (running) { while (running) {
float t = glfwGetTime (); float t = glfwGetTime ();
if (t-t0 >= 5.0) { if (t-t0 >= 5.0) {
@ -277,4 +282,3 @@ int main () {
glfwTerminate (); glfwTerminate ();
} }