bool initialize(){ // Initialize basic geometry and shaders for this example Vertex* geometry = loadOBJ(fname); loadTexture(mtl,textObj); // Create a Vertex Buffer object to store this vertex info on the GPU glGenBuffers(1, &vbo_geometry); glBindBuffer(GL_ARRAY_BUFFER, vbo_geometry); glBufferData(GL_ARRAY_BUFFER, numOfVert * sizeof(Vertex), geometry, GL_STATIC_DRAW); //--Geometry done GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); //Shader Sources // Put these into files and write a loader in the future // Note the added uniform! const char *vs; vs = shaderLoader("vertexShader"); const char *fs; fs = shaderLoader("fragmentShader"); //compile the shaders GLint shader_status; // Vertex shader first glShaderSource(vertex_shader, 1, &vs, NULL); glCompileShader(vertex_shader); //check the compile status glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &shader_status); if(!shader_status){ std::cerr << "[F] FAILED TO COMPILE VERTEX SHADER!" << std::endl; return false; } // Now the Fragment shader glShaderSource(fragment_shader, 1, &fs, NULL); glCompileShader(fragment_shader); //check the compile status glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_status); if(!shader_status){ std::cerr << "[F] FAILED TO COMPILE FRAGMENT SHADER!" << std::endl; return false; } //Now we link the 2 shader objects into a program //This program is what is run on the GPU program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); //check if everything linked ok glGetProgramiv(program, GL_LINK_STATUS, &shader_status); if(!shader_status){ std::cerr << "[F] THE SHADER PROGRAM FAILED TO LINK" << std::endl; return false; } //Now we set the locations of the attributes and uniforms //this allows us to access them easily while rendering loc_position = glGetAttribLocation(program, const_cast<const char*>("v_position")); if(loc_position == -1){ std::cerr << "[F] POSITION NOT FOUND" << std::endl; return false; } loc_color = glGetAttribLocation(program, const_cast<const char*>("v_color")); if(loc_color == -1){ std::cerr << "[F] V_COLOR NOT FOUND" << std::endl; return false; } loc_mvpmat = glGetUniformLocation(program, const_cast<const char*>("mvpMatrix")); if(loc_mvpmat == -1){ std::cerr << "[F] MVPMATRIX NOT FOUND" << std::endl; return false; } //--Init the view and projection matrices // if you will be having a moving camera the view matrix will need to more dynamic // ...Like you should update it before you render more dynamic // for this project having them static will be fine view = glm::lookAt( glm::vec3(0.0, 8.0, -16.0), //Eye Position glm::vec3(0.0, 0.0, 0.0), //Focus point glm::vec3(0.0, 1.0, 0.0)); //Positive Y is up projection = glm::perspective( 45.0f, //the FoV typically 90 degrees is good which is what this is set to float(w)/float(h), //Aspect Ratio, so Circles stay Circular 0.01f, //Distance to the near plane, normally a small value like this 100.0f); //Distance to the far plane, //enable depth testing glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); //and its done return true; }
bool Model::init(const char* fileName, const aiScene* scene){ //save our number of vertices so we know how many to draw later numVertices = vertices.size(); //set up where geometry lives for GL to draw glGenBuffers(1, &vbo_geometry); glBindBuffer(GL_ARRAY_BUFFER, vbo_geometry); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW); // --Geometry Done //setup textures initMaterials(fileName, scene); //mesh if our file name, scene is our aiScene //set up our shaders GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); //Shader Sources // --Shader loader completed, returns string with the shader file // Note the added uniform! std::string tempString1 = shaderLoader("assets/shaders/vertex-shader"); const char *vs = tempString1.c_str(); std::string tempString2 = shaderLoader("assets/shaders/fragment-shader"); const char *fs = tempString2.c_str(); //compile the shaders GLint shader_status; // Vertex shader first glShaderSource(vertex_shader, 1, &vs, NULL); glCompileShader(vertex_shader); //check the compile status glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &shader_status); if(!shader_status) { std::cerr << "[F] FAILED TO COMPILE VERTEX SHADER!" << std::endl; return false; } // Now the Fragment shader glShaderSource(fragment_shader, 1, &fs, NULL); glCompileShader(fragment_shader); //check the compile status glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_status); if(!shader_status) { std::cerr << "[F] FAILED TO COMPILE FRAGMENT SHADER!" << std::endl; return false; } //Now we link the 2 shader objects into a program //This program is what is run on the GPU program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); //check if everything linked ok glGetProgramiv(program, GL_LINK_STATUS, &shader_status); if(!shader_status){ std::cerr << "[F] THE SHADER PROGRAM FAILED TO LINK" << std::endl; return false; } //Now we set the locations of the attributes and uniforms //this allows us to access them easily while rendering loc_position = glGetAttribLocation(program, const_cast<const char*>("v_position")); if(loc_position == -1){ std::cerr << "[F] POSITION NOT FOUND" << std::endl; return false; } loc_color = glGetAttribLocation(program, const_cast<const char*>("v_color")); if(loc_color == -1){ std::cerr << "[F] V_COLOR NOT FOUND" << std::endl; return false; } loc_normal = glGetAttribLocation(program, const_cast<const char*>("v_normal")); if (loc_normal == -1){ std::cerr << "[F] v_normal not found \n"; } //add textures loc_texture = glGetAttribLocation(program, const_cast<const char*>("texcoord")); if(loc_texture == -1){ std::cerr << "[F] texcoord NOT FOUND" << std::endl; return false; } loc_mvpmat = glGetUniformLocation(program, const_cast<const char*>("mvpMatrix")); if(loc_mvpmat == -1){ std::cerr << "[F] MVPMATRIX NOT FOUND" << std::endl; return false; } return true; }