-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCScene.cpp
More file actions
98 lines (85 loc) · 2.05 KB
/
CScene.cpp
File metadata and controls
98 lines (85 loc) · 2.05 KB
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
#include "CScene.h"
#include "opengl.h"
#include "stb_image.h"
CScene::CScene() :
ActiveCam(0), SkyDome(0)
{
}
void CScene::addObjectToRoot(CObject* obj)
{
obj->setParent(&Root);
}
void CScene::render()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(ClearColor.X, ClearColor.Y, ClearColor.Z, 1);
if (ActiveCam)
{
ActiveCam->render();
if (SkyDome)
{
SkyDome->setPosition(ActiveCam->getPosition());
SkyDome->render();
}
}
float pos[4] = {500, 1000, 500, 1};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
Root.render();
}
void CScene::animate(float dt)
{
Root.animate(dt);
if (ActiveCam)
ActiveCam->animate(dt);
}
uint CScene::loadTexture(const char* fname, bool mipmaps)
{
int w, h, n;
unsigned char* data = stbi_load(fname, &w, &h, &n, 4);
if (!data)
{
printf("Cannot load image %s\n", fname);
return 0;
}
else
{
uint img;
glGenTextures(1, &img);
glBindTexture(GL_TEXTURE_2D, img);
if (mipmaps)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
if (mipmaps)
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
return img;
}
}
CObjectMesh* CScene::addObjectMesh(CMesh* mesh, bool physics)
{
CObjectMesh* object = new CObjectMesh(mesh);
addObjectToRoot(object);
// TODO physics object
return object;
}
CObjectSphere* CScene::addObjectSphere(float radius, bool physics)
{
CObjectSphere* object = new CObjectSphere(radius);
addObjectToRoot(object);
// TODO physics object
return object;
}
CObjectCube* CScene::addObjectCube(float size, bool physics)
{
CObjectCube* object = new CObjectCube(size);
addObjectToRoot(object);
// TODO physics object
return object;
}