//OpenGL initialization void OnInit() { //get the OBJ mesh path std::string mesh_path = mesh_filename.substr(0, mesh_filename.find_last_of("/")+1); //load the OBJ model if(!obj.Load(mesh_filename.c_str(), meshes, vertices, indices, materials)) { cout<<"Cannot load the obj mesh"<<endl; exit(EXIT_FAILURE); } GL_CHECK_ERRORS //load material textures for(size_t k=0;k<materials.size();k++) { //if the diffuse texture name is not empty if(materials[k]->map_Kd != "") { GLuint id = 0; //generate a new OpenGL texture glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); int texture_width = 0, texture_height = 0, channels=0; const string& filename = materials[k]->map_Kd; std::string full_filename = mesh_path; full_filename.append(filename); //use SOIL to load the texture GLubyte* pData = SOIL_load_image(full_filename.c_str(), &texture_width, &texture_height, &channels, SOIL_LOAD_AUTO); if(pData == NULL) { cerr<<"Cannot load image: "<<full_filename.c_str()<<endl; exit(EXIT_FAILURE); } //Flip the image on Y axis int i,j; for( j = 0; j*2 < texture_height; ++j ) { int index1 = j * texture_width * channels; int index2 = (texture_height - 1 - j) * texture_width * channels; for( i = texture_width * channels; i > 0; --i ) { GLubyte temp = pData[index1]; pData[index1] = pData[index2]; pData[index2] = temp; ++index1; ++index2; } } //get the image format GLenum format = GL_RGBA; switch(channels) { case 2: format = GL_RG32UI; break; case 3: format = GL_RGB; break; case 4: format = GL_RGBA; break; } //allocate the texture glTexImage2D(GL_TEXTURE_2D, 0, format, texture_width, texture_height, 0, format, GL_UNSIGNED_BYTE, pData); //release the SOIL image data SOIL_free_image_data(pData); //add the texture id to a vector textures.push_back(id); } } GL_CHECK_ERRORS //load the flat shader flatShader.LoadFromFile(GL_VERTEX_SHADER, "shaders/flat.vert"); flatShader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/flat.frag"); //compile and link shader flatShader.CreateAndLinkProgram(); flatShader.Use(); //add attribute and uniform flatShader.AddAttribute("vVertex"); flatShader.AddUniform("MVP"); flatShader.UnUse(); //load spherical harmonics shader sh_shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/sh_shader.vert"); sh_shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/sh_shader.frag"); //compile and link shader sh_shader.CreateAndLinkProgram(); sh_shader.Use(); //add attribute and uniform sh_shader.AddAttribute("vVertex"); sh_shader.AddAttribute("vNormal"); sh_shader.AddAttribute("vUV"); sh_shader.AddUniform("MV"); sh_shader.AddUniform("N"); sh_shader.AddUniform("P"); sh_shader.AddUniform("textureMap"); sh_shader.AddUniform("light_position"); sh_shader.AddUniform("useDefault"); sh_shader.AddUniform("diffuse_color"); //set values of constant uniforms as initialization glUniform1i(sh_shader("textureMap"), 0); sh_shader.UnUse(); GL_CHECK_ERRORS //load mesh rendering shader shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/shader.vert"); shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/sh_shader.frag"); shader.CreateAndLinkProgram(); shader.Use(); //add attribute and uniform shader.AddAttribute("vVertex"); shader.AddAttribute("vNormal"); shader.AddAttribute("vUV"); shader.AddUniform("MV"); shader.AddUniform("N"); shader.AddUniform("P"); shader.AddUniform("textureMap"); shader.AddUniform("light_position"); shader.AddUniform("useDefault"); shader.AddUniform("diffuse_color"); //set values of constant uniforms as initialization glUniform1i(shader("textureMap"), 0); shader.UnUse(); GL_CHECK_ERRORS //setup the vertex array object and vertex buffer object for the mesh //geometry handling glGenVertexArrays(1, &vaoID); glGenBuffers(1, &vboVerticesID); glGenBuffers(1, &vboIndicesID); glBindVertexArray(vaoID); glBindBuffer (GL_ARRAY_BUFFER, vboVerticesID); //pass mesh vertices glBufferData (GL_ARRAY_BUFFER, sizeof(Vertex)*vertices.size(), &(vertices[0].pos.x), GL_STATIC_DRAW); GL_CHECK_ERRORS //enable vertex attribute array for vertex position glEnableVertexAttribArray(sh_shader["vVertex"]); glVertexAttribPointer(sh_shader["vVertex"], 3, GL_FLOAT, GL_FALSE,sizeof(Vertex),0); GL_CHECK_ERRORS //enable vertex attribute array for vertex normal glEnableVertexAttribArray(sh_shader["vNormal"]); glVertexAttribPointer(sh_shader["vNormal"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)(offsetof(Vertex, normal)) ); GL_CHECK_ERRORS //enable vertex attribute array for vertex texture coordinates glEnableVertexAttribArray(sh_shader["vUV"]); glVertexAttribPointer(sh_shader["vUV"], 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)(offsetof(Vertex, uv)) ); GL_CHECK_ERRORS //if we have a single material, it means the 3ds model contains one mesh //we therefore load it into an element array buffer if(materials.size()==1) { //pass indices to the element array buffer if there is a single material glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*indices.size(), &(indices[0]), GL_STATIC_DRAW); } GL_CHECK_ERRORS //set spherical harmonics as the current shader pCurrentShader = &sh_shader; //enable depth test and culling glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); //set the clear colour to corn blue glClearColor(0.5,0.5,1,1); //get the light position lightPosOS.x = radius * cos(theta)*sin(phi); lightPosOS.y = radius * cos(phi); lightPosOS.z = radius * sin(theta)*sin(phi); //setup vao and vbo stuff for the light position crosshair glm::vec3 crossHairVertices[6]; crossHairVertices[0] = glm::vec3(-0.5f,0,0); crossHairVertices[1] = glm::vec3(0.5f,0,0); crossHairVertices[2] = glm::vec3(0, -0.5f,0); crossHairVertices[3] = glm::vec3(0, 0.5f,0); crossHairVertices[4] = glm::vec3(0,0, -0.5f); crossHairVertices[5] = glm::vec3(0,0, 0.5f); //setup light gizmo vertex array and vertex buffer object IDs glGenVertexArrays(1, &lightVAOID); glGenBuffers(1, &lightVerticesVBO); glBindVertexArray(lightVAOID); glBindBuffer (GL_ARRAY_BUFFER, lightVerticesVBO); //pass crosshair vertices to the buffer object glBufferData (GL_ARRAY_BUFFER, sizeof(crossHairVertices), &(crossHairVertices[0].x), GL_STATIC_DRAW); GL_CHECK_ERRORS //enable vertex attribute array for vertex position glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,0); GL_CHECK_ERRORS cout<<"Initialization successfull"<<endl; }
int main(int argc, char** argv) { // @todo used for alter monitor or NULL when we're create window. string isDebug = (argc > 1 ? argv[1] : ""); glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); monitor = glfwGetPrimaryMonitor(); mode = glfwGetVideoMode(monitor); window = glfwCreateWindow(mode->width, mode->height, "Project Melkor - Roch Studio", NULL, NULL); if (!window) { cout << "could not create Window" << endl; glfwTerminate(); return -1; } // Setting Window pos Center in Monitor 1920 - 1080 // glfwSetWindowPos(window, 320, 180); glfwSetErrorCallback(error_callback); glfwSetKeyCallback(window, keyCallBack); glfwSetMouseButtonCallback(window, mouseButtonCallback); glfwSetCharModsCallback(window, character_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwMakeContextCurrent(window); // @todo remove this line if (isDebug != "debug") { glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); } glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { cout << "Failed to initialize Glew" << endl; return -1; } glMatrixMode(GL_PROJECTION); glViewport(0, 0, mode->width, mode->height); glMatrixMode(GL_MODELVIEW); Shader modelViewProjectionTextured( "assets/Shaders/ModelViewProjectionTextured.vert", "assets/Shaders/ModelViewProjectionTextured.frag" ); Shader modelViewProjectionColor( "assets/Shaders/ModelViewProjection.vert", "assets/Shaders/ModelViewProjection.frag" ); ProjectionMatrix projectionMatrix; projectionMatrix.setAspectRatio((GLfloat)mode->width / mode->height); int imageW, imageH; unsigned char *dirtTexture; Entity *entity[50]; Cube *cubes[50]; for (int i = 0; i < 50; i++) { // int num = (i >= 10 ? (i / 10) : 0); // entity[i] = new Entity(0, vec3((i * 1), -2, (num * -1)), 0, 0, 0, 1); // if (i < 10) entity[i] = new Entity(0, vec3(i * 1, -2, 0), 0, 0, 0, 1); else if (i >= 10 && i < 20) entity[i] = new Entity(0, vec3((i - 10) * 1, -2, -1), 0, 0, 0, 1); else if (i >= 20 && i < 30) entity[i] = new Entity(0, vec3((i - 20) * 1, -2, -2), 0, 0, 0, 1); else if (i >= 30 && i < 40) entity[i] = new Entity(0, vec3((i - 30) * 1, -2, -3), 0, 0, 0, 1); else if (i >= 40 && i < 50) entity[i] = new Entity(0, vec3((i - 40) * 1, -2, -4), 0, 0, 0, 1); cubes[i] = new Cube(mode->width, mode->height); cubes[i]->setEntity(*entity[i]); cubes[i]->shader(&modelViewProjectionTextured); dirtTexture = SOIL_load_image((i >= 30 ? "assets/images/stone.jpg" : "assets/images/dirt.jpg"), &imageW, &imageH, 0, SOIL_LOAD_RGB); cubes[i]->textureImage(dirtTexture, imageW, imageH, GL_RGB); } // ### Font Configs Shader fontShader("assets/Shaders/FontVertexShader.glsl", "assets/Shaders/FontFragmentShader.glsl"); FontConfigs fontConfigs(18); // ### End Font Configs TextDraw playerText(&fontShader, mode->width, mode->height); playerText.characters(fontConfigs.Characters); playerText.color(glm::vec3(0.5, 0.8f, 0.2f)); Loader loader; ObjLoader stallObj = ObjLoader(); RawModel firstRawModel = stallObj.loadObj("assets/Models/stall.obj", loader); ObjModel stallModel(modelViewProjectionTextured, firstRawModel); unsigned char *stallTexture = SOIL_load_image("assets/images/stallTexture.png", &imageW, &imageH, 0, SOIL_LOAD_RGB); stallModel.textureImage(stallTexture, imageW, imageH, GL_RGB); Entity test(0, vec3(0, 0, 0), 0, 0, 0, 1); stallModel.setEntity(test); while (!glfwWindowShouldClose(window)) { // Check and call events glfwPollEvents(); // Game Logic here glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0, 0.0, 0.0, 1.0f); // Set frame time GLfloat currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; Do_Movement(); // Render Text playerText.x(1.0f); playerText.y(1.0f); playerText.scale(1.0f); playerText.text("Created By Cod3r Kane"); playerText.render(); for (int i = 0; i < 50; i++) { cubes[i]->setViewMatrix(camera.GetViewMatrix()); cubes[i]->setProjectionMatrix(projectionMatrix); cubes[i]->render(); } stallModel.setViewMatrix(camera.GetViewMatrix()); stallModel.setProjectionMatrix(projectionMatrix); stallModel.render(); glfwSwapBuffers(window); // clean inputs keyboardkey = 0; charCodePoint = 0; } glfwDestroyWindow(window); glfwTerminate(); return 0; }