-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggameobject.cpp
319 lines (290 loc) · 9.69 KB
/
ggameobject.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "ggameobject.h"
#include "glog.h"
#include "ggraphiclibapi.h"
#include "gmathutils.h"
#include "gutils.h"
using namespace GMath;
using namespace std;
GGameObject* GGameObject::activeCamera = nullptr;
vector<GGameObject> GGameObject::lights;
GGameObject::GGameObject(GGameObject::GGameObjectType t, int subType)
:mtype(t)
{
switch (mtype)
{
case GGameObjectType::kCamera:
cameraType = (GCameraType)subType;
break;
case GGameObjectType::kLight:
lightInfo.lightType = (GLightType)subType;
break;
case GGameObjectType::kModel:
break;
}
}
void GGameObject::SetT(vec3f pos)
{
this->_position = pos;
_trs_dirty = true;
}
void GGameObject::SetR(vec3f rotation)
{
this->_rotation.SetX(GMathUtils::Deg2Rad(rotation.x()));
this->_rotation.SetY(GMathUtils::Deg2Rad(rotation.y()));
this->_rotation.SetZ(GMathUtils::Deg2Rad(rotation.z()));
_trs_dirty = true;
}
void GGameObject::SetS(vec3f scale)
{
this->_scale = scale;
_trs_dirty = true;
}
void GGameObject::SetTRS(vec3f pos, vec3f rotation, vec3f scale)
{
SetT(pos);
SetR(rotation);
SetS(scale);
}
void GGameObject::TRSInvertTRS(const mat4f* &trs, const mat4f* &invertTRS)
{
if(_trs_dirty)
{
transform = GMathUtils::TRS(_position, _rotation, _scale);
invertTransform = transform.invert();
_trs_dirty = false;
}
trs = &transform;
invertTRS = &invertTransform;
}
GGameObject GGameObject::CreateProjCamera(float near, float far, float fov)
{
float aspectRatio = GUtils::screenAspectRatio();
GGameObject cameraGObj(GGameObjectType::kCamera, GCameraType::kProjection);
cameraGObj.near = near;
cameraGObj.far = far;
cameraGObj.fov = fov;
assert(far>near);
cameraGObj.aspectRatio = aspectRatio;
cameraGObj._proj_dirty = true;
return cameraGObj;
}
void GGameObject::SetViewport(int x, int y, int w, int h)
{
viewportX = x;
viewportY = y;
viewportW = w;
viewportH = h;
}
void GGameObject::SetFov(float fov)
{
this->fov = fov;
_proj_dirty = true;
}
vec2 GGameObject::NDCPosToScreenPos(vec3 ndc)
{
vec2 ret;
ret.SetX((ndc.x()+1.0)*viewportW*0.5+viewportX);
ret.SetY((ndc.y()+1.0)*viewportH*0.5+viewportY);
return ret;
}
float GGameObject::ToWBufferValue(float wValue)
{
wValue = min(max(wValue, near), far);
wValue -= near;
wValue /= (far-near);
return (wValue*2.0-1.0);
}
GMath::mat4f &GGameObject::LookAt(GMath::vec3f eyePos, GMath::vec3f lookAtPoint, GMath::vec3f up)
{
_position = eyePos;
_scale = vec3f::one;
transform.identity();
vec3f forward = (lookAtPoint - eyePos).normalize();
vec3f right = cross(up, forward).normalize();
up = cross(forward, right).normalize();
transform.set_col(0, embed<float,4>(right,0));
transform.set_col(1, embed<float,4>(up,0));
transform.set_col(2, embed<float,4>(forward,0));
_rotation = GMathUtils::RotationMatrixToEulerAngle(transform);
transform[0][3] = eyePos[0];
transform[1][3] = eyePos[1];
transform[2][3] = eyePos[2];
// TODO calc invertTransform to set _trs_dirty false
_trs_dirty = true;
return transform;
}
void GGameObject::ProjInvertProj(const mat4f*& tproj,const mat4f*& tinvertProj)
{
if(_proj_dirty)
{
// fov axis is yAxis
float zoomY = 1.0/std::tan(GMathUtils::Deg2Rad(fov/2.0));
float zoomX = zoomY / aspectRatio;
projMat.zero();
projMat[0][0] = zoomX;
projMat[1][1] = zoomY;
projMat[2][2] = (far+near)/(far-near);
projMat[2][3] = (-2*near*far)/(far-near);
projMat[3][2] = 1;
projMat[3][3] = 0;
invertProjMat.zero();
invertProjMat[0][0] = 1.0f/zoomX;
invertProjMat[1][1] = 1.0f/zoomY;
invertProjMat[2][2] = 0;
invertProjMat[2][3] = 1;
invertProjMat[3][2] = (near-far)/(2*near*far);
invertProjMat[3][3] = (near+far)/(2*near*far);
_proj_dirty = false;
}
tproj = &projMat;
tinvertProj = &invertProjMat;
}
GGameObject& GGameObject::CreateLightGObj(GLightType lightType, GColor lColor, float lIntensity)
{
GGameObject light = GGameObject(GGameObject::GGameObjectType::kLight, lightType);
light.lightInfo.lightColor = lColor;
light.lightInfo.lightIntensity = lIntensity;
lights.push_back(std::move(light));
return lights[lights.size()-1];
}
GGameObject GGameObject::CreateModelGObj(GModelType modelType, std::string modelPath, bool init_texture)
{
GGameObject gObj(GGameObjectType::kModel, modelType);
GOBJModel gObjModel(modelType, modelPath);
gObj.model = GGLModel::CreateWithObjModel(&gObjModel,init_texture);
gObj.shaderType = GShaderType::kSTDefault;
return gObj;
}
void GGameObject::InitShader(GGraphicLibAPI *GLAPI, GShaderType shaderType, BlendConfigT blendConfig)
{
this->shaderType = shaderType;
modelShader = GLAPI->CreateProgram(shaderType);
this->blendConfig = std::move(blendConfig);
}
void GGameObject::SetupDraw(GGraphicLibAPI *GLAPI)
{
if(modelShader==nullptr)
{
InitShader(GLAPI, shaderType);
}
modelShader->diffusemaps_ = &(model.diffusemap_mipmaps_);
modelShader->diff_mipmaptype = model.diff_mipmaptype;
modelShader->normalmaps_ = &(model.normalmap_mipmaps_);
modelShader->norm_mipmaptype = model.norm_mipmaptype;
modelShader->specularmaps_ = &(model.specularmap_mipmaps_);
modelShader->spec_mipmaptype = model.spec_mipmaptype;
auto tVAO = GLAPI->GenVAO();
modelVAO = tVAO;
GLAPI->BindVAO(tVAO);
auto tArrBuffer = GLAPI->GenDataBuffer(GDataBufferType::kArrayBuffer);
GLAPI->BindDataBuffer(tArrBuffer);
auto tElemArrBuffer = GLAPI->GenDataBuffer(GDataBufferType::kElementArrayBuffer);
GLAPI->BindDataBuffer(tElemArrBuffer);
int offset = 0;
int vertCount = model.nverts();
int dataSize = 0;
auto slotInfoArr = modelShader->GetSlotInfoArr();
for(auto slotInfo : slotInfoArr)
{
switch (slotInfo.slotType)
{
case GSlotType::kSlotTColor:
{
break;
}
case GSlotType::kSlotTNormal:
{
dataSize = sizeof(vec3)*vertCount;
GLAPI->FillDataBuffer(GDataBufferType::kArrayBuffer, model.norms_p(), dataSize, offset);
// keep mesh data layout == slot data layout
assert(slotInfo.slot == 2 && slotInfo.datumCount==3);
GLAPI->VertexAttriPointer(2, 3, GDatumType::kDouble, false, 0, offset);
offset += dataSize;
GLAPI->SetEnableVertexAttriSlot(slotInfo.slot, true);
break;
}
case GSlotType::kSlotTPosition:
{
dataSize = sizeof(vec3)*vertCount;
GLAPI->FillDataBuffer(GDataBufferType::kArrayBuffer, model.verts_p(), dataSize);
// keep mesh data layout == slot data layout
assert(slotInfo.slot == 0 && slotInfo.datumCount==3);
GLAPI->VertexAttriPointer(0, 3, GDatumType::kDouble, false, 0, offset);
offset += dataSize;
GLAPI->SetEnableVertexAttriSlot(slotInfo.slot, true);
break;
}
case GSlotType::kSlotTTangent:
{
break;
}
case GSlotType::kSlotTUV0:
{
dataSize = sizeof(vec2)*vertCount;
GLAPI->FillDataBuffer(GDataBufferType::kArrayBuffer, model.uv_p(), dataSize, offset);
// keep mesh data layout == slot data layout
assert(slotInfo.slot == 1 && slotInfo.datumCount==2);
GLAPI->VertexAttriPointer(1, 2, GDatumType::kDouble, false, 0, offset);
offset += dataSize;
GLAPI->SetEnableVertexAttriSlot(slotInfo.slot, true);
break;
}
case GSlotType::kSlotTUV1:
{
break;
}
}
}
dataSize = sizeof(int)*model.indexCount();
GLAPI->FillDataBuffer(GDataBufferType::kElementArrayBuffer, model.index_p(), dataSize, 0);
GLAPI->BindVAO(nullptr);
}
void GGameObject::DrawModel(GGraphicLibAPI *GLAPI)
{
GLAPI->UseProgram(modelShader);
// set draw status
for(auto blendInfo : blendConfig)
{
GLAPI->SetEnableBlend(std::get<0>(blendInfo), std::get<1>(blendInfo));
}
GLAPI->depthMask = depthMask;
// set obj uniform variable
const mat4f* tMat;
const mat4f* tInvertMat;
TRSInvertTRS(tMat, tInvertMat);
GLAPI->activeShader->world2Obj = *tInvertMat;
GLAPI->activeShader->obj2World = *tMat;
// set camera uniform variable
GGameObject::activeCamera->TRSInvertTRS(tMat, tInvertMat);
GLAPI->activeShader->world2View = *tInvertMat;
GGameObject::activeCamera->ProjInvertProj(tMat, tInvertMat);
GLAPI->activeShader->projMat = *tMat;
GLAPI->activeShader->invertProjMat = *tInvertMat;
GLAPI->activeShader->wCamPos = GGameObject::activeCamera->position();
FillLightData(GLAPI);
GLAPI->BindVAO(modelVAO);
GLAPI->DrawElements(GPrimitiveType::kTriangles, model.indexCount(), GDatumType::kInt, 0);
GLAPI->BindVAO(nullptr);
}
void GGameObject::FillLightData(GGraphicLibAPI* GLAPI)
{
GLAPI->activeShader->lights.clear();
for(size_t i=0; i<lights.size(); i++)
{
GGameObject& light = lights[i];
GLightInfo* lightInfo = &(light.lightInfo);
if(lightInfo->lightType == GLightType::kLTPoint)
{
lightInfo->lightPosOrDir = light.position();
}
else if(lightInfo->lightType == GLightType::kLTDirection)
{
const mat4f* tMat;
const mat4f* tInvertMat;
light.TRSInvertTRS(tMat, tInvertMat);
lightInfo->lightPosOrDir = (*tInvertMat).get_minor(3,3).transpose() * vec3f(0,0,1);
lightInfo->lightPosOrDir.normalize().inverse();
}
GLAPI->activeShader->lights.push_back(lightInfo);
}
}