Graphics With OpenGL


C++ // Visual Studio 2019 // OpenGL

MeshLoader


This program uses OpenGL (Open Graphics Library) to generate a stream of textured cubes which move towards the screen.
The MeshLoader class is responsible for reading data from a text file and loading a cube mesh. Elsewhere, a texture as well as lighting properties are applied to the mesh. The cube is then created many times in random locations. In order to view the cubes, a camera and light source is also created.


    #include "MeshLoader.h"
    #include <iostream>
    #include <fstream>

    using namespace std;

    namespace MeshLoader
    {
        void LoadVertices(ifstream& inFile, Mesh& mesh)
        {
            // Sets mesh.VertexCount to the total number of vertices in the text file
            inFile >> mesh.VertexCount;

            if (mesh.VertexCount > 0)
            {
                mesh.Vertices = new Vertex[mesh.VertexCount];
                // Adds the vertices in the file to the cube mesh
                for (int i = 0; i < mesh.VertexCount; i++)
                {
                    inFile >> mesh.Vertices[i].x;
                    inFile >> mesh.Vertices[i].y;
                    inFile >> mesh.Vertices[i].z;
                }
            }
        }

        void LoadTexCoords(ifstream& inFile, TexturedMesh& mesh)
        {
            // Sets mesh.TexCoordCount to the total number of texture co-ordinates in the text file
            inFile >> mesh.TexCoordCount;

            if (mesh.TexCoordCount > 0)
            {
                mesh.TexCoords = new TexCoord[mesh.TexCoordCount];
                // Adds the co-ordinates in the file to the cube mesh
                for (int i = 0; i < mesh.TexCoordCount; i++)
                {
                    inFile >> mesh.TexCoords[i].u;
                    inFile >> mesh.TexCoords[i].v;
                }
            }
        }

        void LoadIndices(ifstream& inFile, Mesh& mesh)
        {
            // Sets mesh.IndexCount to the total number of indices in the text file
            inFile >> mesh.IndexCount;

            if (mesh.IndexCount > 0)
            {
                mesh.Indices = new GLushort[mesh.IndexCount];
                // Adds the indices in the file to the cube mesh
                for (int i = 0; i < mesh.IndexCount; i++)
                {
                    inFile >> mesh.Indices[i];
                }
            }
        }

        void LoadNormals(ifstream& inFile, Mesh& mesh) 
        {
            // Sets mesh.NormalCount to the total number of normals in the text file
            inFile >> mesh.NormalCount;

            if (mesh.NormalCount > 0)
            {
                mesh.Normals = new Vector3[mesh.NormalCount];
                // Adds the normals in the file to the cube mesh
                for (int i = 0; i < mesh.NormalCount; i++)
                {
                    inFile >> mesh.Normals[i].x;
                    inFile >> mesh.Normals[i].y;
                    inFile >> mesh.Normals[i].z;
                }
            }
        }

        TexturedMesh* MeshLoader::LoadTextured(char* path)
        {
            // Creates a new mesh for the cube data to be loaded to
            TexturedMesh* mesh = new TexturedMesh();
            mesh->Mesh = new Mesh();

            // Opens the text file containing the cube data
            ifstream inFile;
            inFile.open(path);

            if (!inFile.good())
            {
                cerr << "Can't open texture file " << path << endl;
                return nullptr;
            }
            // Performs all of the necessary load functions
            LoadVertices(inFile, *mesh->Mesh);
            LoadTexCoords(inFile, *mesh);
            LoadNormals(inFile, *mesh->Mesh);
            LoadIndices(inFile, *mesh->Mesh);
            // Closes the file when it is no longer needed
            inFile.close();

            return mesh;
        }
    }

                

Get In Touch