Created
November 26, 2018 05:39
-
-
Save BeMg/b97ff9d29edcdfadfac210f905f1f838 to your computer and use it in GitHub Desktop.
GPA AS2
This file contains hidden or 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
| void loadScene(string path, string img_path, vector<Shape> &vS, vector<Material> &vM) { | |
| Assimp::Importer importer; | |
| const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs); | |
| for (unsigned int i = 0; i < scene->mNumMaterials; ++i) | |
| { | |
| aiMaterial *material = scene->mMaterials[i]; | |
| Material Material; | |
| aiString texturePath; | |
| if (material->GetTexture(aiTextureType_DIFFUSE, 0, &texturePath) == | |
| aiReturn_SUCCESS) | |
| { | |
| string ttPath(texturePath.C_Str()); | |
| ttPath = img_path + ttPath.substr(0,ttPath.size()-3) + "png"; | |
| cout << ttPath << endl; | |
| // load width, height and data from texturePath.C_Str(); | |
| TextureData img = loadPNG(ttPath.c_str()); | |
| glGenTextures(1, &Material.diffuse_tex); | |
| glBindTexture(GL_TEXTURE_2D, Material.diffuse_tex); | |
| glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img.width, img.height, 0, | |
| GL_RGBA, GL_UNSIGNED_BYTE, img.data); | |
| glGenerateMipmap(GL_TEXTURE_2D); | |
| } | |
| else | |
| { | |
| // load some default image as default_diffuse_tex | |
| // Hard coding here. | |
| Material.diffuse_tex = 3; | |
| } | |
| // save material�K | |
| vM.push_back(Material); | |
| } | |
| cout << "Load Material Done" << endl; | |
| cout << "Start load scene model" << endl; | |
| for (unsigned int i = 0; i < scene->mNumMeshes; ++i) | |
| { | |
| aiMesh *mesh = scene->mMeshes[i]; | |
| Shape shape; | |
| glGenVertexArrays(1, &shape.vao); | |
| glBindVertexArray(shape.vao); | |
| // create 3 vbos to hold data | |
| glGenBuffers(1, &shape.vbo_position); | |
| glGenBuffers(1, &shape.vbo_normal); | |
| glGenBuffers(1, &shape.vbo_texcoord); | |
| vector<float> position; | |
| vector<float> normal; | |
| vector<float> texcoord; | |
| for (unsigned int v = 0; v < mesh->mNumVertices; ++v) | |
| { | |
| position.push_back(mesh->mVertices[v][0]); | |
| position.push_back(mesh->mVertices[v][1]); | |
| position.push_back(mesh->mVertices[v][2]); | |
| normal.push_back(mesh->mNormals[v][0]); | |
| normal.push_back(mesh->mNormals[v][1]); | |
| normal.push_back(mesh->mNormals[v][2]); | |
| texcoord.push_back(mesh->mTextureCoords[0][v][0]); | |
| texcoord.push_back(mesh->mTextureCoords[0][v][1]); | |
| // mesh->mVertices[v][0~2] => position | |
| // mesh->mNormals[v][0~2] => normal | |
| // mesh->mTextureCoords[0][v][0~1] => texcoord | |
| } | |
| glBindBuffer(GL_ARRAY_BUFFER, shape.vbo_position); | |
| glBufferData(GL_ARRAY_BUFFER, position.size() * sizeof(float), | |
| &position[0], GL_STATIC_DRAW); | |
| glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
| glBindBuffer(GL_ARRAY_BUFFER, shape.vbo_texcoord); | |
| glBufferData(GL_ARRAY_BUFFER, texcoord.size() * sizeof(float), | |
| &texcoord[0], GL_STATIC_DRAW); | |
| glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
| glBindBuffer(GL_ARRAY_BUFFER, shape.vbo_normal); | |
| glBufferData(GL_ARRAY_BUFFER, normal.size() * sizeof(float), | |
| &normal[0], GL_STATIC_DRAW); | |
| glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
| // create 1 ibo to hold data | |
| glGenBuffers(1, &shape.ibo); | |
| vector<int> index; | |
| for (unsigned int f = 0; f < mesh->mNumFaces; ++f) | |
| { | |
| index.push_back(mesh->mFaces[f].mIndices[0]); | |
| index.push_back(mesh->mFaces[f].mIndices[1]); | |
| index.push_back(mesh->mFaces[f].mIndices[2]); | |
| // mesh->mFaces[f].mIndices[0~2] => index | |
| } | |
| // glVertexAttribPointer / glEnableVertexArray calls�K | |
| glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shape.ibo); | |
| glBufferData(GL_ELEMENT_ARRAY_BUFFER, index.size() * sizeof(unsigned int), | |
| &index[0], GL_STATIC_DRAW); | |
| glEnableVertexAttribArray(0); | |
| glEnableVertexAttribArray(1); | |
| glEnableVertexAttribArray(2); | |
| shape.materialID = mesh->mMaterialIndex; | |
| shape.drawCount = mesh->mNumFaces * 3; | |
| // save shape�K | |
| vS.push_back(shape); | |
| } | |
| } |
This file contains hidden or 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
| void My_Display() | |
| { | |
| glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| glUseProgram(program); | |
| vec3 direction( | |
| cos(verticalAngle) * sin(horizontalAngle), | |
| sin(verticalAngle), | |
| cos(verticalAngle) * cos(horizontalAngle) | |
| ); | |
| vec3 right = vec3( | |
| sin(horizontalAngle - 3.14f / 2.0f), | |
| 0, | |
| cos(horizontalAngle - 3.14f / 2.0f) | |
| ); | |
| vec3 up = cross(right, direction); | |
| view = lookAt(position, position + direction, up); | |
| glUniformMatrix4fv(um4mv, 1, GL_FALSE, value_ptr(view)); | |
| glUniformMatrix4fv(um4p, 1, GL_FALSE, value_ptr(projection)); | |
| glActiveTexture(GL_TEXTURE0); | |
| glUniform1i(us2dtex, 0); | |
| if (flag == 0) { | |
| for (int i = 0; i < vShape.size(); i++) { | |
| glBindVertexArray(vShape[i].vao); | |
| int materialID = vShape[i].materialID; | |
| glBindTexture(GL_TEXTURE_2D, vMaterial[materialID].diffuse_tex); | |
| glDrawElements(GL_TRIANGLES, vShape[i].drawCount, GL_UNSIGNED_INT, 0); | |
| } | |
| } | |
| else { | |
| for (int i = 0; i < vShape_2.size(); i++) { | |
| glBindVertexArray(vShape_2[i].vao); | |
| int materialID = vShape_2[i].materialID; | |
| glBindTexture(GL_TEXTURE_2D, vMaterial_2[materialID].diffuse_tex); | |
| glDrawElements(GL_TRIANGLES, vShape_2[i].drawCount, GL_UNSIGNED_INT, 0); | |
| } | |
| } | |
| glutSwapBuffers(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment