Git Product home page Git Product logo

kingdom's People

Contributors

ethanm-0371 avatar paufusco avatar xgauss05 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

starvinxarvin

kingdom's Issues

Meshes not drawing correctly

While we were changing our project, to be used with smart pointers, we found out that the meshes stopped drawing.

Current changes are located in branch components-structure-rework.

Some of the notable changes we have done so far:

  • We've added in the copy constructor of the Mesh some safety checks in order to prevent OpenGL errors. Also removed const in the function arguments.

    Kingdom/GameEngine/Mesh.cpp

    Lines 109 to 128 in 6d5c424

    Mesh::Mesh(Mesh& cpy) :
    Component(cpy.owner),
    meshName(cpy.meshName),
    _format(cpy._format),
    _vertex_buffer_id(cpy._vertex_buffer_id),
    _numVerts(cpy._numVerts),
    _indexs_buffer_id(cpy._indexs_buffer_id),
    _numIndexs(cpy._numIndexs),
    _numTexCoords(cpy._numTexCoords),
    _numNormals(cpy._numNormals),
    _numFaces(cpy._numFaces),
    texture(cpy.texture), // Copies the shared_ptr, so it now points to the same object
    mVertices(cpy.mVertices),
    mNormals(cpy.mNormals),
    mFaceCenters(cpy.mFaceCenters),
    mFaceNormals(cpy.mFaceNormals)
    {
    cpy._vertex_buffer_id = 0;
    cpy._indexs_buffer_id = 0;
    }

  • We've created MeshInfo for the custom file format purposes.

    class MeshInfo
    {
    public:
    inline MeshInfo(const void* vertex_data, unsigned int numVerts,
    const unsigned int* index_data = nullptr, unsigned int numIndexs = 0,
    const unsigned int numTexCoords = 0, unsigned int numNormals = 0, unsigned int numFaces = 0) :
    _vertex_data(vertex_data),
    _numVerts(numVerts),
    _index_data(index_data),
    _numIndexs(numIndexs),
    _numTexCoords(numTexCoords),
    _numNormals(numNormals),
    _numFaces(numFaces)
    {
    }
    const void* _vertex_data;
    const unsigned int* _index_data;
    const unsigned int _numVerts, _numIndexs, _numTexCoords,
    _numNormals, _numFaces;
    std::vector<vec3f> mVertices;
    std::vector<vec3f> mNormals;
    std::vector<vec3f> mFaceCenters;
    std::vector<vec3f> mFaceNormals;
    };

  • With the MeshInfo class created, we modified the loadMeshFromFile(...) function so that it returns a MeshInfo vector.

    static std::vector<MeshInfo> loadMeshFromFile(const std::string& path)
    {
    std::vector<MeshInfo> meshInfoVec;
    auto scene = aiImportFile(path.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs);
    for (size_t m = 0; m < scene->mNumMeshes; ++m) {
    auto mesh = scene->mMeshes[m];
    auto faces = mesh->mFaces;
    int numTexCoords = mesh->mNumVertices; // Number of texture coordinates is same as the number of vertices
    int numNormals = mesh->mNumVertices; // Number of normals is same as the number of vertices
    int numFaces = mesh->mNumFaces;
    vec3f* verts = (vec3f*)mesh->mVertices;
    vec3f* texCoords = (vec3f*)mesh->mTextureCoords[0];
    std::vector<Mesh::V3T2> vertex_data;
    std::vector<unsigned int> index_data;
    for (size_t i = 0; i < mesh->mNumVertices; ++i) {
    Mesh::V3T2 v = { verts[i], vec2f(texCoords[i].x, texCoords[i].y) };
    vertex_data.push_back(v);
    }
    for (size_t f = 0; f < mesh->mNumFaces; ++f) {
    index_data.push_back(faces[f].mIndices[0]);
    index_data.push_back(faces[f].mIndices[1]);
    index_data.push_back(faces[f].mIndices[2]);
    }
    auto meshInfo_ptr = MeshInfo(vertex_data.data(), vertex_data.size(), index_data.data(), index_data.size(), numTexCoords, numNormals, numFaces);
    for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
    aiVector3D normal = mesh->mNormals[i];
    vec3f glmNormal(normal.x, normal.y, normal.z);
    meshInfo_ptr.mNormals.push_back(glmNormal);
    }
    for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
    aiVector3D vert = mesh->mVertices[i];
    vec3f glmNormal(vert.x, vert.y, vert.z);
    meshInfo_ptr.mVertices.push_back(glmNormal);
    }
    for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
    aiFace face = mesh->mFaces[i];
    vec3f v0(mesh->mVertices[face.mIndices[0]].x, mesh->mVertices[face.mIndices[0]].y, mesh->mVertices[face.mIndices[0]].z);
    vec3f v1(mesh->mVertices[face.mIndices[1]].x, mesh->mVertices[face.mIndices[1]].y, mesh->mVertices[face.mIndices[1]].z);
    vec3f v2(mesh->mVertices[face.mIndices[2]].x, mesh->mVertices[face.mIndices[2]].y, mesh->mVertices[face.mIndices[2]].z);
    vec3f faceNormal = glm::cross(v1 - v0, v2 - v0);
    faceNormal = glm::normalize(faceNormal);
    meshInfo_ptr.mFaceNormals.push_back(faceNormal);
    vec3f faceCenter = (v0 + v1 + v2) / 3.0f;
    meshInfo_ptr.mFaceCenters.push_back(faceCenter);
    }
    meshInfoVec.push_back(meshInfo_ptr);
    }
    aiReleaseImport(scene);
    return meshInfoVec;
    }

  • And with this MeshInfo vector, where we add GameObjects we use the MeshInfo vector in order to create the Mesh components and add them to the GameObject list in Engine_ModuleRenderer3D

    void Engine_ModuleRenderer3D::addGameObject(const std::string & filePath)
    {
    logHistory.push_back("[Engine] Add GameObject with path " + filePath);
    auto mesh_vector = MeshLoader::loadMeshFromFile(filePath);
    auto texture_paths_vector = MeshLoader::loadTextureFromFile(filePath);
    int i = 0;
    for (auto& mesh : mesh_vector)
    {
    std::unique_ptr<GameObject> gameObjectToAdd = std::make_unique<GameObject>();
    std::string fileName = filePath;
    eraseBeforeDelimiter(fileName);
    std::string meshName = fileName;
    deleteSubstring(meshName, ".fbx");
    int currentCopies = checkNameAvailability(meshName);
    if (currentCopies > 0) {
    meshName.append("(");
    std::string copiesToString = std::to_string(currentCopies);
    meshName.append(copiesToString);
    meshName.append(")");
    }
    gameObjectToAdd->name = meshName;
    gameObjectList.push_back(std::move(gameObjectToAdd));
    Texture2D textureToPush(gameObjectList.back().get(), texture_paths_vector.at(i));
    gameObjectList.back().get()->AddComponent<Texture2D>(textureToPush);
    Mesh meshToPush(gameObjectList.back().get(), mesh);
    gameObjectList.back().get()->AddComponent<Mesh>(meshToPush);
    gameObjectList.back().get()->GetComponent<Mesh>()->setName(fileName);
    gameObjectList.back().get()->GetComponent<Mesh>()->texture = gameObjectList.back().get()->GetComponent<Texture2D>();
    i++;
    logHistory.push_back("[Engine] Mesh loaded with " + std::to_string(mesh._numFaces) + " faces, "
    + std::to_string(mesh._numIndexs) + " indexs, "
    + std::to_string(mesh._numNormals) + " normals, "
    + std::to_string(mesh._numTexCoords) + " tex coords, and "
    + std::to_string(mesh._numVerts) + " vertexs.");
    }
    }

  • We also have a Template function to add the Component to the list of components in GameObject.

    template<typename T>
    inline void GameObject::AddComponent(T& component)
    {
    components.emplace_back(std::make_unique<T>(component));
    }

With these changes, we built the project and it does not draw correctly the meshes. The thing is, that while we were debugging, here are the values that the index_buffer_id and the vertex_buffer id have. (We are loading BakerHouse.fbx, which has 2 meshes)
This is for the Chimney.
image
And this is for the House.
image

At this point we are desperate to know what is going on. We haven't done anything regarding the 2nd assignment that its due to this 13th of December. I know that you said that these days you were going to be busy, but I hope you can read this ASAP.

Loading the Mesh custom file format

I have a question about deserializing the custom format file from a .bin type of file.

I've modified and adapted the serialize function we have done in class to our MeshInfo.

ostream& serialize(ostream& os) const {
os.write((char*)&_numVerts, sizeof(_numVerts));
os.write((char*)_vertex_data, _numVerts * sizeof(V3T2)); // we need to control this somehow
os.write((char*)&_numIndexs, sizeof(_numIndexs));
os.write((char*)_index_data, _numIndexs * sizeof(uint));
os.write((char*)&_numTexCoords, sizeof(_numTexCoords));
os.write((char*)mVertices.data(), _numVerts * sizeof(vec3f));
os.write((char*)&_numNormals, sizeof(_numNormals));
os.write((char*)mNormals.data(), _numNormals * sizeof(vec3f));
os.write((char*)&_numFaces, sizeof(_numFaces));
os.write((char*)mFaceNormals.data(), _numFaces * sizeof(vec3f));
os.write((char*)mFaceCenters.data(), _numFaces * sizeof(vec3f));
return os;
}

So for the question, I have the vertex_data and the index_data as const both.

const void* _vertex_data;
const unsigned int* _index_data;

Should I just use const cast? It feels that I am doing a bad praxis by doing the const cast, but maybe I should do it in another way.


Another question that I have, is since I have a void* in:

const void* _vertex_data;

and a unsigned int* in:
const unsigned int* _index_data;

How should I load the data loaded from the .bin file into these variables? Obviously, if I were to keep the const keyword, I will use the const cast, for both, but I still do not know how to load it into a void*.

This is the function we have done in class.

istream& deserialize(istream& is) {
size_t temp = 0;
is.read((char*)&temp, sizeof(temp));
//_vertex_data.resize(temp);
//is.read((char*)_vertex_data.data(), temp * sizeof(V3T2));
//is.read((char*)&temp, sizeof(temp));
//_index_data.resize(temp);
//is.read((char*)_index_data.data(), temp * sizeof(uint));
return is;
}

@pepcots

Compilation error on doing a pushback to the gameObjectsList.

When trying to add a GameObject to the gameObjectsList (currently in Engine_ModuleRenderer.h), a compilation error happens with code C2280 'std::unique_ptr<Component,std::default_delete<Component>>::unique_ptr(const std::unique_ptr<Component,std::default_delete<Component>> &)': attempting to reference a deleted function.
The error is believed to be emerging due to GameObject having a vector of unique_ptr, which keeps exculsive ownerships and when trying to push_back into the gameObjectsList this exclusive ownership prevents it from doing so.

The push_back is located in

gameObjectList.push_back(currentObject);

The vector of unique_ptr is located in

std::vector<std::unique_ptr<Component>> components;

WeirdMesh issues

Hi @Ethanm-0371 @xGauss05 @PauFusco
It seems the MeshLoader that creates a MeshInfo has some errors.
You are passing temporary data to the MeshInfo (vertex_data and index_data are local variables)

auto meshInfo_ptr = MeshInfo(vertex_data.data(), vertex_data.size(), index_data.data(), index_data.size(), numTexCoords, numNormals, numFaces);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D normal = mesh->mNormals[i];
vec3f glmNormal(normal.x, normal.y, normal.z);
meshInfo_ptr.mNormals.push_back(glmNormal);
}
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D vert = mesh->mVertices[i];
vec3f glmNormal(vert.x, vert.y, vert.z);
meshInfo_ptr.mVertices.push_back(glmNormal);
}
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
vec3f v0(mesh->mVertices[face.mIndices[0]].x, mesh->mVertices[face.mIndices[0]].y, mesh->mVertices[face.mIndices[0]].z);
vec3f v1(mesh->mVertices[face.mIndices[1]].x, mesh->mVertices[face.mIndices[1]].y, mesh->mVertices[face.mIndices[1]].z);
vec3f v2(mesh->mVertices[face.mIndices[2]].x, mesh->mVertices[face.mIndices[2]].y, mesh->mVertices[face.mIndices[2]].z);
vec3f faceNormal = glm::cross(v1 - v0, v2 - v0);
faceNormal = glm::normalize(faceNormal);
meshInfo_ptr.mFaceNormals.push_back(faceNormal);
vec3f faceCenter = (v0 + v1 + v2) / 3.0f;
meshInfo_ptr.mFaceCenters.push_back(faceCenter);
}
meshInfoVec.push_back(meshInfo_ptr);

Camera LookAtPos not updating correctly.

Doing the transform rework to glm, when trying to add the forward vector multiplied by the offset to the position vector, the result ends up being (0,0,0) or a number of order ^-15.
This error is located here:

lookAtPos = this->gameObject.GetComponent<Transform>()->position() + this->gameObject.GetComponent<Transform>()->forward() * camOffset;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.