Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "utils.h"
- #include <GL/glew.h>
- #include <GL/gl.h>
- #include <stdio.h>
- #include <cstring>
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vec2.h>
- #include <vec3.h>
- #include "Mesh.h"
- using namespace std;
- GLuint loadShadingProgram(const char* vs_path, const char* fs_path) {
- // Load the vertex shader:
- GLuint vs_id = loadShader(vs_path, GL_VERTEX_SHADER);
- // Load the fragment shader.
- GLuint fs_id = loadShader(fs_path, GL_FRAGMENT_SHADER);
- // Create and link the program:
- GLuint program_id = glCreateProgram();
- glAttachShader(program_id, vs_id);
- glAttachShader(program_id, fs_id);
- glLinkProgram(program_id);
- return program_id;
- }
- GLuint loadShader(const char* path, GLenum shader_type){
- string contents;
- ifstream file_stream(path);
- if(file_stream.is_open()){
- string line;
- while(getline(file_stream, line)){
- contents += line + "\n";
- }
- file_stream.close();
- }
- GLuint shader_id;
- shader_id = glCreateShader(shader_type);
- const char* shader_source = contents.c_str();
- cout << shader_source;
- glShaderSource(shader_id, 1, &shader_source, NULL );
- glCompileShader(shader_id);
- int success;
- char info_log[512];
- glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success);
- if(!success) {
- glGetShaderInfoLog(shader_id, 512, NULL, info_log);
- cout << "\nERROR COMPILING SHADER: \n" << info_log << endl;
- }
- return shader_id;
- }
- Mesh* loadMesh(const char* path) {
- printf("Loading OBJ file %s...\n", path);
- vector<unsigned int> vertexIndices, uvIndices, normalIndices;
- vector<vec3> temp_vertices;
- vector<vec2> temp_uvs;
- vector<vec3> temp_normals;
- FILE * file = fopen(path, "r");
- if( file == NULL ){
- printf("Impossible to open the file !\n");
- return NULL;
- }
- 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 ){
- vec3 vertex;
- fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );
- temp_vertices.push_back(vertex);
- }
- else if ( strcmp( lineHeader, "vt" ) == 0 ){
- vec2 uv;
- fscanf(file, "%f %f\n", &uv.x, &uv.y );
- uv.y = -uv.y; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
- temp_uvs.push_back(uv);
- }
- else if ( strcmp( lineHeader, "vn" ) == 0 ){
- 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 ){
- string vertex1, vertex2, vertex3;
- unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
- int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2] );
- if (matches != 9){
- printf("File can't be read by our simple parser :-( Try exporting with other options\n");
- fclose(file);
- return NULL;
- }
- vertexIndices.push_back(vertexIndex[0]);
- vertexIndices.push_back(vertexIndex[1]);
- vertexIndices.push_back(vertexIndex[2]);
- uvIndices .push_back(uvIndex[0]);
- uvIndices .push_back(uvIndex[1]);
- uvIndices .push_back(uvIndex[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);
- }
- }
- vector<float> data;
- cout << endl << "vertexIndices.size:" << vertexIndices.size();
- // 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 uvIndex = uvIndices[i];
- unsigned int normalIndex = normalIndices[i];
- // Get the attributes thanks to the index
- vec3 vertex = temp_vertices[ vertexIndex-1 ];
- vec2 uv = temp_uvs[ uvIndex-1 ];
- vec3 normal = temp_normals[ normalIndex-1 ];
- // Put the attributes in buffers
- data.push_back(vertex.x);
- data.push_back(vertex.y);
- data.push_back(vertex.z);
- data.push_back(uv.x);
- data.push_back(uv.y);
- data.push_back(normal.x);
- data.push_back(normal.y);
- data.push_back(normal.z);
- }
- fclose(file);
- Mesh* mesh = new Mesh(data);
- return mesh;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement