Created
July 18, 2012 04:10
-
-
Save iKlsR/3134110 to your computer and use it in GitHub Desktop.
parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <stdio.h> | |
#include <string> | |
#include <cstring> | |
#include <iostream> | |
#include <glm/glm.hpp> | |
#include <glm/glm.hpp> | |
#include <glm/gtc/matrix_transform.hpp> | |
#include <glm/gtc/type_ptr.hpp> | |
#include <GL/glew.h> | |
#include <GL/glfw.h> | |
bool loadOBJ( | |
const char * path, | |
std::vector<glm::vec3> & out_vertices, | |
std::vector<glm::vec3> & out_normals | |
){ | |
printf("Loading OBJ file %s...\n", path); | |
std::vector<unsigned int> vertexIndices, normalIndices; | |
std::vector<glm::vec3> temp_vertices; | |
std::vector<glm::vec3> temp_normals; | |
FILE * file = fopen(path, "r"); | |
if( file == NULL ){ | |
printf("Impossible to open the file ! Are you in the right path ? See Tutorial 1 for details\n"); | |
return false; | |
} | |
while( 1 ){ | |
char lineHeader[128]; | |
// read the first word of the line | |
int res = fscanf(file, "%s", lineHeader); | |
if (res == EOF) | |
break; // EOF = End Of File. Quit the loop. | |
// else : parse lineHeader | |
if ( strcmp( lineHeader, "v" ) == 0 ){ | |
glm::vec3 vertex; | |
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z ); | |
temp_vertices.push_back(vertex); | |
} else if ( strcmp( lineHeader, "vn" ) == 0 ){ | |
glm::vec3 normal; | |
fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z ); | |
temp_normals.push_back(normal); | |
}else if ( strcmp( lineHeader, "f" ) == 0 ){ | |
std::string vertex1, vertex2, vertex3; | |
unsigned int vertexIndex[4], normalIndex[4]; | |
int matches = fscanf(file, "%d//%d %d//%d %d//%d\n", &vertexIndex[0], &normalIndex[0], &vertexIndex[1], | |
&normalIndex[1], &vertexIndex[2], &normalIndex[2], &vertexIndex[3], &normalIndex[3]); | |
if (matches != 6){ | |
printf("File can't be read by our simple parser :-( Try exporting with other options\n"); | |
return false; | |
} | |
vertexIndices.push_back(vertexIndex[0]); | |
vertexIndices.push_back(vertexIndex[1]); | |
vertexIndices.push_back(vertexIndex[2]); | |
normalIndices.push_back(normalIndex[0]); | |
normalIndices.push_back(normalIndex[1]); | |
normalIndices.push_back(normalIndex[2]); | |
}else{ | |
// Probably a comment, eat up the rest of the line | |
char stupidBuffer[1000]; | |
fgets(stupidBuffer, 1000, file); | |
} | |
} | |
// For each vertex of each triangle | |
for( unsigned int i=0; i<vertexIndices.size(); i++ ){ | |
// Get the indices of its attributes | |
unsigned int vertexIndex = vertexIndices[i]; | |
unsigned int normalIndex = normalIndices[i]; | |
// Get the attributes thanks to the index | |
glm::vec3 vertex = temp_vertices[ vertexIndex-1 ]; | |
glm::vec3 normal = temp_normals[ normalIndex-1 ]; | |
// Put the attributes in buffers | |
out_vertices.push_back(vertex); | |
out_normals.push_back(normal); | |
} | |
printf("loaded file %s successfully.\n", path); | |
return true; | |
} | |
std::vector<glm::vec3> vertices; | |
std::vector<glm::vec3> normals; | |
using namespace std; | |
void debug() { | |
bool res = loadOBJ("cube.obj", vertices, normals); | |
puts("VERTEX______________________________________________________"); | |
for (unsigned int i = 0; i < vertices.size(); i++) { | |
cout << vertices[i].x << vertices[i].y << vertices[i].z << endl; | |
} | |
} | |
int wWidth = 800, | |
wHeight = 600, | |
wWindowHandle = 0; | |
#define ES 0 | |
#define Ef -1 | |
#define wTitle "core" | |
bool running = true; | |
static void create (void); | |
static void cleanup (void); | |
static GLFWCALL void window_size_callback (int, int); | |
static GLFWCALL void event_key_callback (int, int); | |
static GLFWCALL void event_mouse_callback (int, int); | |
GLuint vaoId, vboId, normalId; | |
//GLint posAttrib, colAttrib, texAttrib, uvAttrib; | |
int main() { | |
glfwInit(); | |
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 0); | |
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE); | |
wWindowHandle = glfwOpenWindow(wWidth, wHeight, 0, 0, 0, 0, 32, 0, GLFW_WINDOW); | |
glfwSetWindowTitle(wTitle); | |
glfwSetKeyCallback(event_key_callback); | |
glfwSetMouseButtonCallback(event_mouse_callback); | |
glfwSetWindowSizeCallback(window_size_callback); | |
GLenum glewInitResult = glewInit(); | |
glfwEnable(GLFW_STICKY_KEYS); | |
glfwEnable(GLFW_STICKY_MOUSE_BUTTONS); | |
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); | |
create(); | |
while (running == true) { | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glDrawArrays(GL_TRIANGLES, 0, vertices.size()); | |
glfwSwapBuffers(); | |
}; | |
glfwTerminate(); | |
cleanup(); | |
return ES; | |
} | |
static void create (void) { | |
glGenVertexArrays(1, &vaoId); | |
glBindVertexArray(vaoId); | |
bool obj = loadOBJ("cube.obj", vertices, normals); | |
glGenBuffers(1, &vboId); | |
glBindBuffer(GL_ARRAY_BUFFER, vboId); | |
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); | |
glEnableVertexAttribArray(0); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
} | |
static void cleanup (void) { | |
//delete resources.. | |
glDisableVertexAttribArray(0); | |
glDeleteVertexArrays(1, &vaoId); | |
glDeleteBuffers(1, &vboId); | |
} | |
static GLFWCALL void window_size_callback (int width, int height) { | |
glViewport(0, 0, width, height); | |
} | |
static GLFWCALL void event_key_callback (int key, int action) { | |
if(GLFW_PRESS && glfwGetWindowParam(GLFW_OPENED)) { | |
if(glfwGetKey(GLFW_KEY_ESC)) { | |
running = false; | |
} | |
} | |
} | |
static GLFWCALL void event_mouse_callback (int character, int action) { | |
if(glfwGetMouseButton(GLFW_MOUSE_BUTTON_MIDDLE)) { | |
running = false; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <stdio.h> | |
#include <string> | |
#include <cstring> | |
#include <iostream> | |
#include <glm/glm.hpp> | |
#include <glm/glm.hpp> | |
#include <glm/gtc/matrix_transform.hpp> | |
#include <glm/gtc/type_ptr.hpp> | |
#include <GL/glew.h> | |
#include <GL/glfw.h> | |
bool loadOBJ( | |
const char * path, | |
std::vector<glm::vec3> & out_vertices, | |
std::vector<glm::vec3> & out_normals | |
){ | |
printf("Loading OBJ file %s...\n", path); | |
std::vector<unsigned int> vertexIndices, normalIndices; | |
std::vector<glm::vec3> temp_vertices; | |
std::vector<glm::vec3> temp_normals; | |
FILE * file = fopen(path, "r"); | |
if( file == NULL ){ | |
printf("Impossible to open the file ! Are you in the right path ? See Tutorial 1 for details\n"); | |
return false; | |
} | |
while( 1 ){ | |
char lineHeader[128]; | |
// read the first word of the line | |
int res = fscanf(file, "%s", lineHeader); | |
if (res == EOF) | |
break; // EOF = End Of File. Quit the loop. | |
// else : parse lineHeader | |
if ( strcmp( lineHeader, "v" ) == 0 ){ | |
glm::vec3 vertex; | |
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z ); | |
temp_vertices.push_back(vertex); | |
} else if ( strcmp( lineHeader, "vn" ) == 0 ){ | |
glm::vec3 normal; | |
fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z ); | |
temp_normals.push_back(normal); | |
}else if ( strcmp( lineHeader, "f" ) == 0 ){ | |
std::string vertex1, vertex2, vertex3; | |
unsigned int vertexIndex[4], normalIndex[4]; | |
int matches = fscanf(file, "%d//%d %d//%d %d//%d\n", &vertexIndex[0], &normalIndex[0], &vertexIndex[1], | |
&normalIndex[1], &vertexIndex[2], &normalIndex[2], &vertexIndex[3], &normalIndex[3]); | |
if (matches != 6){ | |
printf("File can't be read by our simple parser :-( Try exporting with other options\n"); | |
return false; | |
} | |
vertexIndices.push_back(vertexIndex[0]); | |
vertexIndices.push_back(vertexIndex[1]); | |
vertexIndices.push_back(vertexIndex[2]); | |
normalIndices.push_back(normalIndex[0]); | |
normalIndices.push_back(normalIndex[1]); | |
normalIndices.push_back(normalIndex[2]); | |
}else{ | |
// Probably a comment, eat up the rest of the line | |
char stupidBuffer[1000]; | |
fgets(stupidBuffer, 1000, file); | |
} | |
} | |
// For each vertex of each triangle | |
for( unsigned int i=0; i<vertexIndices.size(); i++ ){ | |
// Get the indices of its attributes | |
unsigned int vertexIndex = vertexIndices[i]; | |
unsigned int normalIndex = normalIndices[i]; | |
// Get the attributes thanks to the index | |
glm::vec3 vertex = temp_vertices[ vertexIndex-1 ]; | |
glm::vec3 normal = temp_normals[ normalIndex-1 ]; | |
// Put the attributes in buffers | |
out_vertices.push_back(vertex); | |
out_normals.push_back(normal); | |
} | |
printf("loaded file %s successfully.\n", path); | |
return true; | |
} | |
std::vector<glm::vec3> vertices; | |
std::vector<glm::vec3> normals; | |
using namespace std; | |
void debug() { | |
bool res = loadOBJ("cube.obj", vertices, normals); | |
puts("VERTEX______________________________________________________"); | |
for (unsigned int i = 0; i < vertices.size(); i++) { | |
cout << vertices[i].x << vertices[i].y << vertices[i].z << endl; | |
} | |
} | |
int wWidth = 800, | |
wHeight = 600, | |
wWindowHandle = 0; | |
#define ES 0 | |
#define Ef -1 | |
#define wTitle "core" | |
bool running = true; | |
static void create (void); | |
static void cleanup (void); | |
static GLFWCALL void window_size_callback (int, int); | |
static GLFWCALL void event_key_callback (int, int); | |
static GLFWCALL void event_mouse_callback (int, int); | |
GLuint vaoId, vboId, normalId; | |
//GLint posAttrib, colAttrib, texAttrib, uvAttrib; | |
int main() { | |
glfwInit(); | |
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 0); | |
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE); | |
wWindowHandle = glfwOpenWindow(wWidth, wHeight, 0, 0, 0, 0, 32, 0, GLFW_WINDOW); | |
glfwSetWindowTitle(wTitle); | |
glfwSetKeyCallback(event_key_callback); | |
glfwSetMouseButtonCallback(event_mouse_callback); | |
glfwSetWindowSizeCallback(window_size_callback); | |
GLenum glewInitResult = glewInit(); | |
glfwEnable(GLFW_STICKY_KEYS); | |
glfwEnable(GLFW_STICKY_MOUSE_BUTTONS); | |
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); | |
create(); | |
while (running == true) { | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glDrawArrays(GL_TRIANGLES, 0, vertices.size()); | |
glfwSwapBuffers(); | |
}; | |
glfwTerminate(); | |
cleanup(); | |
return ES; | |
} | |
static void create (void) { | |
glGenVertexArrays(1, &vaoId); | |
glBindVertexArray(vaoId); | |
bool obj = loadOBJ("cube.obj", vertices, normals); | |
glGenBuffers(1, &vboId); | |
glBindBuffer(GL_ARRAY_BUFFER, vboId); | |
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); | |
glEnableVertexAttribArray(0); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
} | |
static void cleanup (void) { | |
//delete resources.. | |
glDisableVertexAttribArray(0); | |
glDeleteVertexArrays(1, &vaoId); | |
glDeleteBuffers(1, &vboId); | |
} | |
static GLFWCALL void window_size_callback (int width, int height) { | |
glViewport(0, 0, width, height); | |
} | |
static GLFWCALL void event_key_callback (int key, int action) { | |
if(GLFW_PRESS && glfwGetWindowParam(GLFW_OPENED)) { | |
if(glfwGetKey(GLFW_KEY_ESC)) { | |
running = false; | |
} | |
} | |
} | |
static GLFWCALL void event_mouse_callback (int character, int action) { | |
if(glfwGetMouseButton(GLFW_MOUSE_BUTTON_MIDDLE)) { | |
running = false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment