Fixed backface culling/blender mesh.
This commit is contained in:
parent
df1b0216bc
commit
a82f817071
92
main.cpp
92
main.cpp
|
@ -1,3 +1,4 @@
|
|||
|
||||
#ifdef __APPLE__
|
||||
#define glGenVertexArrays glGenVertexArraysAPPLE
|
||||
#define glBindVertexArray glBindVertexArrayAPPLE
|
||||
|
@ -17,11 +18,11 @@
|
|||
#include <cmath>
|
||||
|
||||
#define glError() { \
|
||||
GLenum err = glGetError(); \
|
||||
while (err != GL_NO_ERROR) { \
|
||||
std::cerr << "glError: " << (char *)gluErrorString(err) << " at " << __LINE__; \
|
||||
err = glGetError(); \
|
||||
} \
|
||||
GLenum err = glGetError(); \
|
||||
while (err != GL_NO_ERROR) { \
|
||||
std::cerr << "glError: " << (char *)gluErrorString(err) << " at " << __LINE__; \
|
||||
err = glGetError(); \
|
||||
} \
|
||||
}
|
||||
|
||||
// picopng
|
||||
|
@ -77,11 +78,11 @@ void render () {
|
|||
glRotatef (x+=1.0, 0,1,0);
|
||||
glRotatef (30.0, 1.0,0,0);
|
||||
|
||||
GLint shader_tex_loc = glGetUniformLocation(shaders[0].prog_id, "texture");
|
||||
glUniform1i(shader_tex_loc, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, popcan_tex_id);
|
||||
glError();
|
||||
GLint shader_tex_loc = glGetUniformLocation(shaders[0].prog_id, "texture");
|
||||
glUniform1i(shader_tex_loc, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, popcan_tex_id);
|
||||
glError();
|
||||
|
||||
glBindVertexArray (popcan.vao_id);
|
||||
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 m;
|
||||
uint32_t object_count = 0;
|
||||
|
||||
std::ifstream fin (filename.c_str ());
|
||||
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 ()) {
|
||||
char 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;
|
||||
char *fstr = strtok(&(line[1]), " ");
|
||||
v.pos[0] = atof(fstr);
|
||||
|
@ -150,35 +155,35 @@ Mesh load_mesh (std::string filename) {
|
|||
}
|
||||
|
||||
uint32_t load_texture (std::string filename) {
|
||||
std::vector<unsigned char> image_data;
|
||||
long unsigned int image_w, image_h;
|
||||
std::vector<unsigned char> image_data;
|
||||
long unsigned int image_w, image_h;
|
||||
|
||||
size_t png_size;
|
||||
std::ifstream fin(filename.c_str());
|
||||
if (!fin.good ()) { std::cerr << "Couldn't open " << filename << "\n"; exit(1); }
|
||||
fin.seekg(0, std::ios::end);
|
||||
png_size = fin.tellg();
|
||||
fin.seekg(0, std::ios::beg);
|
||||
size_t png_size;
|
||||
std::ifstream fin(filename.c_str());
|
||||
if (!fin.good ()) { std::cerr << "Couldn't open " << filename << "\n"; exit(1); }
|
||||
fin.seekg(0, std::ios::end);
|
||||
png_size = fin.tellg();
|
||||
fin.seekg(0, std::ios::beg);
|
||||
|
||||
unsigned char png_data[png_size];
|
||||
fin.read((char*)png_data, png_size);
|
||||
unsigned 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);
|
||||
if(r) { std::cerr << "Couldn't read PNG data from " << filename << "\n"; exit(1); }}
|
||||
{ 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); }}
|
||||
|
||||
uint32_t tex_id;
|
||||
uint32_t tex_id;
|
||||
|
||||
glGenTextures(1, &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_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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)));
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glError();
|
||||
std::cerr << "Loaded " << filename << " (" << image_w << "x" << image_h << ")\n";
|
||||
return tex_id;
|
||||
glGenTextures(1, &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_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
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);
|
||||
glError();
|
||||
std::cerr << "Loaded " << filename << " (" << image_w << "x" << image_h << ")\n";
|
||||
return tex_id;
|
||||
}
|
||||
|
||||
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);
|
||||
glEnable (GL_DEPTH_TEST);
|
||||
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);
|
||||
// glCullFace (GL_BACK);
|
||||
// glEnable (GL_CULL_FACE);
|
||||
|
||||
glFrontFace (GL_CCW);
|
||||
glCullFace (GL_BACK);
|
||||
glEnable (GL_CULL_FACE);
|
||||
|
||||
glClearColor (0,0,0,0);
|
||||
glEnable (GL_TEXTURE_2D);
|
||||
|
@ -249,8 +253,9 @@ void GL_init (float w, float h) {
|
|||
}
|
||||
|
||||
int main () {
|
||||
float w = 480;
|
||||
float h = 272;
|
||||
float scale = 3;
|
||||
float w = 480*scale;
|
||||
float h = 272*scale;
|
||||
if (!glfwInit () || !glfwOpenWindow (w, h, 8,8,8,0,32,0,GLFW_WINDOW)) {
|
||||
std::cerr << "Something GLFW failed.\n";
|
||||
return 1;
|
||||
|
@ -261,7 +266,7 @@ int main () {
|
|||
|
||||
float t0 = glfwGetTime ();
|
||||
uint32_t frames = 0;
|
||||
// glfwSwapInterval (0); // Uncomment this to test FPS without vsync
|
||||
//glfwSwapInterval (0); // Uncomment this to test FPS without vsync
|
||||
while (running) {
|
||||
float t = glfwGetTime ();
|
||||
if (t-t0 >= 5.0) {
|
||||
|
@ -277,4 +282,3 @@ int main () {
|
|||
|
||||
glfwTerminate ();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue