Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#ifndef _MODELLOADERCLASS_H_ #define _MODELLOADERCLASS_H_ #include <fstream> #include <string> #include <vector> #include <algorithm> #include <map> #include <glm.hpp> using namespace std; struct FaceType { unsigned int uvIndex1, uvIndex2, uvIndex3; unsigned int normalIndex1, normalIndex2, normalIndex3; unsigned int vertexIndex1, vertexIndex2, vertexIndex3; }; struct Model { public: vector<glm::vec3> vertices; vector<glm::vec2> texCoords; vector<glm::vec3> normals; vector<unsigned int> indices; }; class ModelLoader { public: ModelLoader(); ~ModelLoader(); bool LoadObjModel(const string&); Model IndexModel(); private: vector<glm::vec3> tmp_vertices; vector<glm::vec2> tmp_texCoords; vector<glm::vec3> tmp_normals; vector<FaceType> tmp_faces; int m_faceindex; }; #endif |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
#include "../include/modelloaderclass.h" ModelLoader::ModelLoader() { m_faceindex = 0; } ModelLoader::~ModelLoader() { } bool ModelLoader::LoadObjModel(const string& file) { ifstream fin; char buffer[256]; vector<string*> text; fin.open(file); if (fin.fail()) { return false; } while (!fin.eof()) { fin.getline(buffer, 256); text.push_back(new string(buffer)); } for (int i = 0; i < text.size(); i++) { //Ignore Comments if (text[i]->c_str()[0] == '#') continue; else if (text[i]->c_str()[0] == 'v' && text[i]->c_str()[1] == ' ') { glm::vec3 tmpVertex; //Read in the vertex data scanf_s(text[i]->c_str(), "v %f %f %f", &tmpVertex.x, &tmpVertex.y, &tmpVertex.z); //Put the vertex data into the vector tmp_vertices.push_back(tmpVertex); } else if (text[i]->c_str()[0] == 'v' && text[i]->c_str()[1] == 'n') { glm::vec3 tmpNormals; scanf_s(text[i]->c_str(), "vn %f %f %f", &tmpNormals.x, &tmpNormals.y, &tmpNormals.z); tmp_normals.push_back(tmpNormals); } else if (text[i]->c_str()[0] == 'v' && text[i]->c_str()[1] == 't') { glm::vec2 tmpTexCoord; scanf_s(text[i]->c_str(), "vt %f %f", &tmpTexCoord.x, &tmpTexCoord.y); tmp_texCoords.push_back(tmpTexCoord); } else if (text[i]->c_str()[0] == 'f') { FaceType face; scanf_s(text[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &face.vertexIndex1, &face.uvIndex1, &face.normalIndex1, &face.vertexIndex2, &face.uvIndex2, &face.normalIndex2, &face.vertexIndex3, &face.uvIndex3, &face.normalIndex3); tmp_faces.push_back(face); m_faceindex++; } else { } } fin.close(); //Release all alocated memory for the strings for (int i = 0; i < text.size(); i++) { delete text[i]; } } Model ModelLoader::IndexModel() { Model result; int vIndex = 0; int tIndex = 0; int nIndex = 0; //Hier leuft irgendwas falsch for (unsigned int i = 0; i < tmp_faces.size(); i++) { vIndex = tmp_faces[i].vertexIndex1 - 1; tIndex = tmp_faces[i].uvIndex1 - 1; nIndex = tmp_faces[i].normalIndex1 - 1; result.vertices.push_back(glm::vec3(tmp_vertices[vIndex].x, tmp_vertices[vIndex].y, tmp_vertices[vIndex].z)); result.texCoords.push_back(glm::vec2(tmp_texCoords[tIndex].x, tmp_texCoords[tIndex].y)); result.normals.push_back(glm::vec3(tmp_normals[nIndex].x, tmp_normals[nIndex].y, tmp_normals[nIndex].z)); vIndex = tmp_faces[i].vertexIndex2 - 1; tIndex = tmp_faces[i].uvIndex2 - 1; nIndex = tmp_faces[i].normalIndex2 - 1; result.vertices.push_back(glm::vec3(tmp_vertices[vIndex].x, tmp_vertices[vIndex].y, tmp_vertices[vIndex].z)); result.texCoords.push_back(glm::vec2(tmp_texCoords[tIndex].x, tmp_texCoords[tIndex].y)); result.normals.push_back(glm::vec3(tmp_normals[nIndex].x, tmp_normals[nIndex].y, tmp_normals[nIndex].z)); vIndex = tmp_faces[i].vertexIndex3 - 1; tIndex = tmp_faces[i].uvIndex3 - 1; nIndex = tmp_faces[i].normalIndex3 - 1; result.vertices.push_back(glm::vec3(tmp_vertices[vIndex].x, tmp_vertices[vIndex].y, tmp_vertices[vIndex].z)); result.texCoords.push_back(glm::vec2(tmp_texCoords[tIndex].x, tmp_texCoords[tIndex].y)); result.normals.push_back(glm::vec3(tmp_normals[nIndex].x, tmp_normals[nIndex].y, tmp_normals[nIndex].z)); result.indices.push_back(i); } return result; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#ifndef _MESHCLASS_H_ #define _MESHCLASS_H_ #include "Util.h" #include "openglclass.h" #include "modelclass.h" #include <vector> enum MeshBufferPositions { POSITION_VB, TEXCOORD_VB, NORMAL_VB, INDEX_VB }; class MeshClass { public: MeshClass(); ~MeshClass(); bool Initialize(const ModelType&); void Render(); void Shutdown(); private: static const unsigned int NUM_BUFFERS = 4; GLuint m_vertexArrayObject; GLuint m_vertexArrayBuffers[NUM_BUFFERS]; unsigned int m_indexCount; }; #endif |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include "../include/meshclass.h" MeshClass::MeshClass() { } MeshClass::~MeshClass() { } bool MeshClass::Initialize(const ModelType& model) { m_indexCount = model.indices.size(); glGenVertexArrays(1, &m_vertexArrayObject); glBindVertexArray(m_vertexArrayObject); glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers); glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]); glBufferData(GL_ARRAY_BUFFER, sizeof(model.positions[0]) * model.positions.size(), &model.positions[0], GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]); glBufferData(GL_ARRAY_BUFFER, sizeof(model.texcoords[0]) * model.texcoords.size(), &model.texcoords[0], GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[NORMAL_VB]); glBufferData(GL_ARRAY_BUFFER, sizeof(model.normals[0]) * model.normals.size(), &model.normals[0], GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[INDEX_VB]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(model.indices[0]) * model.indices.size(), &model.indices[0], GL_STATIC_DRAW); glBindVertexArray(0); return true; } void MeshClass::Render() { glBindVertexArray(m_vertexArrayObject); glDrawElements(GL_TRIANGLES, m_indexCount, GL_UNSIGNED_INT, 0); glBindVertexArray(0); } void MeshClass::Shutdown() { glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers); glDeleteVertexArrays(1, &m_vertexArrayObject); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#ifndef _GRAPHICSCLASS_H_ #define _GRAPHICSCLASS_H_ #include "openglclass.h" #include "shaderclass.h" #include "meshclass.h" #include "transformclass.h" #include "cameraclass.h" #include "modelclass.h" class GraphicsClass { public: GraphicsClass(); ~GraphicsClass(); bool Initialize(HWND); void Shutdown(); bool Frame(); bool Render(); bool InitializeShaders(HWND); private: ShaderClass* m_Shader; MeshClass* m_Mesh; TransformClass* m_Transform; CameraClass m_Camera; ModelClass* m_Loader; float m_rotation; float rotation; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
#include "../include/graphicsclass.h" GraphicsClass::GraphicsClass() { } GraphicsClass::~GraphicsClass() { } bool GraphicsClass::Initialize(HWND hwnd) { bool result; ModelType data; //Create the shader object m_Shader = new ShaderClass; if (!m_Shader) { return false; } //Initialize the shader result = InitializeShaders(hwnd); if (!result) { return false; } m_Mesh = new MeshClass; if (!m_Mesh) { return false; } m_Transform = new TransformClass; if (!m_Transform) { return false; } m_Loader = new ModelClass; if (!m_Loader) { return false; } result = m_Loader->LoadSimpleModelData("../source/ktalol/model.txt", data); if (!result) { MessageBox(hwnd, L"Failed to Load Model", L"Error", MB_OK); return false; } result = m_Mesh->Initialize(data); if (!result) { return false; } m_Camera = CameraClass(glm::vec3(0, 0, -5), 70.0f, (float)800 / (float)600, 0.01f, 1000.0f); return true; } bool GraphicsClass::Frame() { bool result; result = Render(); if (!result) { return false; } return true; } bool GraphicsClass::Render() { m_Shader->BindShader(); m_Mesh->Render(); //m_rotation += 0.174532925f * 3.0f; //if (m_rotation > 360.0f) //{ // m_rotation -= 360.0f; //} /*m_Transform->SetRotation(glm::vec3(0.0f, m_rotation, 0.0f));*/ m_Shader->addUniform("gWorld", m_Camera.GetViewProjection() * m_Transform->GetModel()); return true; } bool GraphicsClass::InitializeShaders(HWND hwnd) { bool result; result = m_Shader->Initialize(); if (!result){ return false; } result = m_Shader->addVertexShader(hwnd, "../source/shader/shader.vs"); if (!result){ return false; } result = m_Shader->addFragmentShader(hwnd, "../source/shader/shader.fs"); if (!result){ return false; } result = m_Shader->LinkShader(hwnd); if (!result){ return false; } return true; } void GraphicsClass::Shutdown() { if (m_Shader) { m_Shader->Shutdown(); delete m_Shader; m_Shader = 0; } if (m_Mesh) { m_Mesh->Shutdown(); delete m_Mesh; m_Mesh = 0; } if (m_Transform) { delete m_Transform; m_Transform = 0; } } |
Werbeanzeige