GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — code/Chapter11/Mesh.cpp at master · gameprogcpp/code

Expand file tree

/

Mesh.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
177 lines (153 loc) · 4.18 KB
/

Mesh.cpp

Copy path

File metadata and controls

177 lines (153 loc) · 4.18 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "Mesh.h"
#include "Renderer.h"
#include "Texture.h"
#include "VertexArray.h"
#include <fstream>
#include <sstream>
#include <rapidjson/document.h>
#include <SDL/SDL_log.h>
#include "Math.h"
Mesh::Mesh()
:mBox(Vector3::Infinity, Vector3::NegInfinity)
,mVertexArray(nullptr)
,mRadius(0.0f)
,mSpecPower(100.0f)
{
}
Mesh::~Mesh()
{
}
bool Mesh::Load(const std::string& fileName, Renderer* renderer)
{
std::ifstream file(fileName);
if (!file.is_open())
{
SDL_Log("File not found: Mesh %s", fileName.c_str());
return false;
}
std::stringstream fileStream;
fileStream << file.rdbuf();
std::string contents = fileStream.str();
rapidjson::StringStream jsonStr(contents.c_str());
rapidjson::Document doc;
doc.ParseStream(jsonStr);
if (!doc.IsObject())
{
SDL_Log("Mesh %s is not valid json", fileName.c_str());
return false;
}
int ver = doc["version"].GetInt();
// Check the version
if (ver != 1)
{
SDL_Log("Mesh %s not version 1", fileName.c_str());
return false;
}
mShaderName = doc["shader"].GetString();
// Skip the vertex format/shader for now
// (This is changed in a later chapter's code)
size_t vertSize = 8;
// Load textures
const rapidjson::Value& textures = doc["textures"];
if (!textures.IsArray() || textures.Size() < 1)
{
SDL_Log("Mesh %s has no textures, there should be at least one", fileName.c_str());
return false;
}
mSpecPower = static_cast<float>(doc["specularPower"].GetDouble());
for (rapidjson::SizeType i = 0; i < textures.Size(); i++)
{
// Is this texture already loaded?
std::string texName = textures[i].GetString();
Texture* t = renderer->GetTexture(texName);
if (t == nullptr)
{
// Try loading the texture
t = renderer->GetTexture(texName);
if (t == nullptr)
{
// If it's still null, just use the default texture
t = renderer->GetTexture("Assets/Default.png");
}
}
mTextures.emplace_back(t);
}
// Load in the vertices
const rapidjson::Value& vertsJson = doc["vertices"];
if (!vertsJson.IsArray() || vertsJson.Size() < 1)
{
SDL_Log("Mesh %s has no vertices", fileName.c_str());
return false;
}
std::vector<float> vertices;
vertices.reserve(vertsJson.Size() * vertSize);
mRadius = 0.0f;
for (rapidjson::SizeType i = 0; i < vertsJson.Size(); i++)
{
// For now, just assume we have 8 elements
const rapidjson::Value& vert = vertsJson[i];
if (!vert.IsArray() || vert.Size() != 8)
{
SDL_Log("Unexpected vertex format for %s", fileName.c_str());
return false;
}
Vector3 pos(vert[0].GetDouble(), vert[1].GetDouble(), vert[2].GetDouble());
mRadius = Math::Max(mRadius, pos.LengthSq());
mBox.UpdateMinMax(pos);
// Add the floats
for (rapidjson::SizeType i = 0; i < vert.Size(); i++)
{
vertices.emplace_back(static_cast<float>(vert[i].GetDouble()));
}
}
// We were computing length squared earlier
mRadius = Math::Sqrt(mRadius);
// Load in the indices
const rapidjson::Value& indJson = doc["indices"];
if (!indJson.IsArray() || indJson.Size() < 1)
{
SDL_Log("Mesh %s has no indices", fileName.c_str());
return false;
}
std::vector<unsigned int> indices;
indices.reserve(indJson.Size() * 3);
for (rapidjson::SizeType i = 0; i < indJson.Size(); i++)
{
const rapidjson::Value& ind = indJson[i];
if (!ind.IsArray() || ind.Size() != 3)
{
SDL_Log("Invalid indices for %s", fileName.c_str());
return false;
}
indices.emplace_back(ind[0].GetUint());
indices.emplace_back(ind[1].GetUint());
indices.emplace_back(ind[2].GetUint());
}
// Now create a vertex array
mVertexArray = new VertexArray(vertices.data(), static_cast<unsigned>(vertices.size()) / vertSize,
indices.data(), static_cast<unsigned>(indices.size()));
return true;
}
void Mesh::Unload()
{
delete mVertexArray;
mVertexArray = nullptr;
}
Texture* Mesh::GetTexture(size_t index)
{
if (index < mTextures.size())
{
return mTextures[index];
}
else
{
return nullptr;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.