Skip to content

Commit 99acac3

Browse files
authored
Merge pull request #13 from Lia316/feat/lightSource
Feat/light source
2 parents b5f8c5e + 2c7e5d9 commit 99acac3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1522
-100
lines changed

OpenGL/Camera.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ Camera::Camera() {
44
eye = vec3(0, 0, 400);
55
position = vec3(0.0f, 0.0f, 0.0f);
66
up = vec3(0.0f, 1.0f, 0.0f);
7+
cameraPos = vec3(0.0f);
78

89
currentMode = SIDE;
10+
projectionMatrix = mat4(1.0f);
11+
viewMatrix = mat4(1.0f);
912
projectionViewMatrix = mat4(1.0f);
1013
changeView(SIDE, 1);
1114
}
@@ -14,6 +17,10 @@ mat4 Camera::getProjectionViewMatrix() {
1417
return projectionViewMatrix;
1518
}
1619

20+
vec3 Camera::getPosition() {
21+
return eye;
22+
}
23+
1724
void Camera::setCameraMode(CameraMode mode) {
1825
currentMode = mode;
1926
}
@@ -27,12 +34,16 @@ mat4 Camera::getCamera(CameraMode mode) {
2734

2835
switch (mode) {
2936
case FRONT:
37+
eye = frontEye;
3038
return lookAt(frontEye, frontPos, up);
3139
case SIDE:
40+
eye = sideEye;
3241
return lookAt(sideEye, position, up);
3342
case ORTHO:
34-
return lookAt(orthoEye, position, up); ;
43+
eye = orthoEye;
44+
return lookAt(orthoEye, position, up);
3545
default:
46+
eye = sideEye;
3647
return lookAt(sideEye, position, up);
3748
}
3849
}
@@ -50,16 +61,16 @@ mat4 Camera::getProjection(CameraMode mode) {
5061
}
5162
}
5263

53-
mat4 Camera::getTransform(CameraMode mode) {
64+
vec3 Camera::getTransform(CameraMode mode) {
5465
switch (mode) {
5566
case FRONT:
56-
return translate(mat4(1.0f), vec3(0, -100, 0));
67+
return vec3(0, -100, 0);
5768
case SIDE:
58-
return translate(mat4(1.0f), vec3(0, -200, 0));
69+
return vec3(-250, -200, 0);
5970
case ORTHO:
60-
return translate(mat4(1.0f), vec3(0, -250, 0));
71+
return vec3(-250, -250, 0);
6172
default:
62-
return translate(mat4(1.0f), vec3(0, 150, 0));
73+
return vec3(-250, -200, 0);
6374
}
6475
}
6576

@@ -68,8 +79,11 @@ void Camera::changeView(CameraMode mode, float time) {
6879
if (time > 1 || time <= 0) return;
6980

7081
mat4 view = getCamera(currentMode) + time * (getCamera(mode) - getCamera(currentMode));
71-
mat4 transform = time * getTransform(mode);
82+
mat4 transform = translate(mat4(1.0f), (getTransform(currentMode) + time * (getTransform(mode) - getTransform(currentMode))));
7283
mat4 projection = getProjection(currentMode) + time * (getProjection(mode) - getProjection(currentMode));
7384

85+
viewMatrix = transform * view;
86+
projectionMatrix = projection;
87+
cameraPos = eye - getTransform(mode);
7488
projectionViewMatrix = projection * transform * view;
7589
}

OpenGL/Camera.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ class Camera {
1616
vec3 eye;
1717
vec3 position;
1818
vec3 up;
19+
vec3 cameraPos;
20+
mat4 viewMatrix;
21+
mat4 projectionMatrix;
1922
mat4 projectionViewMatrix;
2023
CameraMode currentMode;
2124

2225
mat4 getCamera(CameraMode mode);
2326
mat4 getProjection(CameraMode mode);
24-
mat4 getTransform(CameraMode mode);
27+
vec3 getTransform(CameraMode mode);
2528

2629
public:
2730
Camera();
31+
mat4 getViewMatrix() { return viewMatrix; }
32+
mat4 getProjectionMatrix() { return projectionMatrix; }
2833
mat4 getProjectionViewMatrix();
34+
vec3 getPosition();
2935
void changeView(CameraMode mode, float time);
3036
void setCameraMode(CameraMode mode);
3137
};

OpenGL/Character.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#define KEY_FRAME_NUM 4
66

7-
Character::Character(Model* models[KEY_FRAME_NUM - 1], GLuint shaderProgram)
7+
Character::Character(Model* models[KEY_FRAME_NUM - 1], GLuint* shaderProgram)
88
: Entity(glutGet(GLUT_WINDOW_WIDTH) / 10, glutGet(GLUT_WINDOW_HEIGHT) / 4, 0, 0, NULL, shaderProgram) {
99
jumpSpeed = 14;
1010
lowjumpSpeed = 10;
@@ -18,6 +18,8 @@ Character::Character(Model* models[KEY_FRAME_NUM - 1], GLuint shaderProgram)
1818
}
1919
time = 0;
2020
currentKeyFrame = 0;
21+
22+
loadTexture(textures->getTextures(TEXTYPE::CHARACTER), 3);
2123
}
2224

2325
Character::~Character() {
@@ -83,6 +85,10 @@ float Character::getPositionY() {
8385
return y;
8486
}
8587

88+
vec3 Character::getPosition() {
89+
return vec3(x, y, z);
90+
}
91+
8692
void Character::animation(void(*t)(int))
8793
{
8894
time += 0.1;

OpenGL/Character.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Character : public Entity {
1616
const float keyFrameTimes[KEY_FRAME_NUM] = { 0.0f, 1.0f, 2.0f, 3.0f };
1717

1818
public:
19-
Character(Model* models[KEY_FRAME_NUM - 1], GLuint shaderProgram);
19+
Character(Model* models[KEY_FRAME_NUM - 1], GLuint* shaderProgram);
2020
~Character();
2121
const type_info& getType() override { return typeid(Character); }
2222

@@ -32,5 +32,6 @@ class Character : public Entity {
3232
bool isJumping();
3333
float getPositionX();
3434
float getPositionY();
35+
vec3 getPosition();
3536
void animation(void(*t)(int));
3637
};

OpenGL/Entity.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include "Entity.h"
2+
#include "RgbImage.h"
23

3-
Entity::Entity(float x, float y, float z, float speed, Model* model, GLuint shaderProgram)
4+
Entity::Entity(float x, float y, float z, float speed, Model* model, GLuint* shaderProgram)
45
: x(x), y(y), z(z), speed(speed), model(model), shaderProgram(shaderProgram) {
56
UuidCreate(&uuid);
67
if (model == nullptr) return;
78

9+
filesize = 0;
10+
811
this->z = model->getminZ() + z;
912
width = model->getWidth();
1013
height = model->getHeight();
@@ -19,16 +22,46 @@ void Entity::move() {
1922
x += speed;
2023
}
2124

25+
void Entity::loadTexture(vector<RgbImage*> theTexMaps, unsigned int filesize) {
26+
this->filesize = filesize;
27+
28+
for (unsigned int i = 0; i < filesize; i++) {
29+
GLuint texture;
30+
31+
glGenTextures(1, &texture);
32+
glBindTexture(GL_TEXTURE_2D, texture);
33+
34+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
35+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
36+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
37+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
38+
39+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, theTexMaps[i]->GetNumCols(), theTexMaps[i]->GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE, theTexMaps[i]->ImageData());
40+
glGenerateMipmap(GL_TEXTURE_2D);
41+
42+
textureIds[i] = texture;
43+
}
44+
}
45+
2246
void Entity::draw() {
2347
if (model == nullptr) return;
2448

2549
mat4 adjust = model->adjustMatrix();
2650
mat4 move = translate(mat4(1.0f), vec3(x, y, 0));
2751
mat4 modelMatrix = move * adjust;
2852

29-
GLuint modelLoc = glGetUniformLocation(shaderProgram, "model");
53+
GLuint modelLoc = glGetUniformLocation(*shaderProgram, "model");
3054
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, &modelMatrix[0][0]);
3155

56+
for (unsigned int i = 0; i < filesize; i++) {
57+
GLuint textureLoc = glGetUniformLocation(*shaderProgram, textureType[i]);
58+
glActiveTexture(GL_TEXTURE0 + i);
59+
glBindTexture(GL_TEXTURE_2D, textureIds[i]);
60+
glUniform1i(textureLoc, i);
61+
}
62+
if (filesize)
63+
glActiveTexture(GL_TEXTURE0);
64+
3265
model->draw();
3366
}
3467

OpenGL/Entity.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22
#include "Model.h"
3+
#include "Textures.h"
4+
35
#include <glm/gtc/matrix_transform.hpp>
46
#include <glm/gtc/type_ptr.hpp>
57
#include <Rpc.h>
@@ -8,13 +10,18 @@
810
class Entity {
911
protected:
1012
UUID uuid;
11-
GLuint shaderProgram;
13+
GLuint* shaderProgram;
1214
Model* model;
15+
int filesize;
16+
GLuint textureIds[5];
17+
const char* textureType[3] = {"texture_diffuse", "texture_normal", "texture_specular"};
18+
1319
float x, y, z;
1420
float width, height, depth;
1521
float speed;
22+
1623
public:
17-
Entity(float, float, float, float, Model*, GLuint);
24+
Entity(float, float, float, float, Model*, GLuint*);
1825
virtual ~Entity();
1926

2027
virtual const type_info& getType() { return typeid(Entity); }
@@ -27,6 +34,7 @@ class Entity {
2734
return UuidCompare(&uuid1, &uuid2, &s) == 0;
2835
}
2936

37+
virtual void loadTexture(vector<RgbImage*> theTexMaps, unsigned int filesize);
3038
virtual void draw();
3139
virtual void move();
3240
virtual float getPositionX();

OpenGL/Fire.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "Fire.h"
22

3-
Fire::Fire(float x, float y, Model* model, GLuint shaderProgram)
4-
: Entity(x, y, 0, -10, model, shaderProgram) { }
3+
Fire::Fire(float x, float y, Model* model, GLuint* shaderProgram)
4+
: Entity(x, y, 0, -10, model, shaderProgram) {
5+
loadTexture(textures->getTextures(TEXTYPE::FIRE), 3);
6+
}
57

68
void Fire::draw() {
79
if (model == nullptr)

OpenGL/Fire.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Fire : public Entity {
55
protected:
66

77
public:
8-
Fire(float x, float y, Model* model, GLuint shaderProgram);
8+
Fire(float x, float y, Model* model, GLuint* shaderProgram);
99
~Fire() {};
1010
const type_info& getType() override { return typeid(Fire); }
1111

OpenGL/GameManager.cpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
using namespace std;
55

6-
GameManager::GameManager(GLuint program) {
7-
shaderProgram = program;
8-
sceneGraph = new SceneGraph(program);
9-
viewMode = 1;
6+
GameManager::GameManager(GLuint* objectShader, GLuint* lightShader) {
7+
objectProgram = objectShader;
8+
sceneGraph = new SceneGraph(objectShader, lightShader);
9+
text = new Text2D("Holstein.DDS");
1010

11+
viewMode = 1;
1112
isGameEnd = false;
1213
score = 0;
1314
firenum = 0;
@@ -17,28 +18,24 @@ GameManager::GameManager(GLuint program) {
1718

1819
groundMaxX = 1000;
1920
isHole = false;
21+
lightAngle = 0;
2022
}
2123

2224
// ###### Draw ######
2325

2426
void GameManager::draw() {
27+
glUseProgram(*objectProgram);
2528
sceneGraph->draw();
26-
27-
string scoreText = "score: " + to_string(score);
28-
showText(300, glutGet(GLUT_WINDOW_HEIGHT) - 300, scoreText);
29-
if (isGameEnd) {
30-
showText(300, glutGet(GLUT_WINDOW_HEIGHT) - 300, "The Game End");
31-
}
3229
}
3330

34-
void GameManager::showText(float x, float y, std::string string) {
35-
glColor3f(1.0, 1.0, 1.0);
36-
glRasterPos3f(x, y, 0);
37-
const char* str = string.c_str();
31+
void GameManager::drawText() {
32+
glUseProgram(text->getProgramID());
33+
string scoreText = "score: " + to_string(score);
34+
text->printText2D(scoreText.c_str(), 20, 60, 20);
35+
if (isGameEnd)
36+
text->printText2D("Game Over", 20, 20, 20);
3837

39-
for (const char* c = str; *c != '\0'; c++) {
40-
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
41-
}
38+
glUseProgram(0);
4239
}
4340

4441
// ###### Update ######
@@ -48,6 +45,7 @@ void GameManager::move(void(*t)(int)) {
4845
SceneNode* fireGroup = sceneGraph->findGroup(typeid(Fire));
4946
SceneNode* starGroup = sceneGraph->findGroup(typeid(Star));
5047
SceneNode* mushGroup = sceneGraph->findGroup(typeid(Mush));
48+
SceneNode* lightGroup = sceneGraph->findGroup(typeid(PointLight));
5149

5250
// 1. Character move
5351
Character* character = dynamic_cast<Character*>(sceneGraph->findNode(typeid(Character))->getEntity());
@@ -142,6 +140,20 @@ void GameManager::move(void(*t)(int)) {
142140
}
143141
}
144142

143+
// 6. Point Light
144+
PointLight* light = dynamic_cast<PointLight*>(sceneGraph->findNode(typeid(PointLight))->getEntity());
145+
146+
lightAngle += 0.05;
147+
if (lightAngle >= 360)
148+
lightAngle -= 360;
149+
150+
vec3 characterPos = character->getPosition();
151+
light->rotate(characterPos, lightAngle);
152+
vec4 lightPos = vec4(light->getPosition(), 1.0f);
153+
154+
GLuint pointLightLoc = glGetUniformLocation(*objectProgram, "lightPosition");
155+
glUniform4fv(pointLightLoc, 1, &lightPos[0]);
156+
145157
if (isGameEnd) {
146158
character->sink();
147159
glutPostRedisplay();
@@ -225,7 +237,7 @@ void GameManager::firemaker(void(*t)(int)) {
225237
firenum--;
226238
}
227239
if (firenum < MAXFIRE) {
228-
Fire* newFire = new Fire(glutGet(GLUT_WINDOW_WIDTH), pos, sceneGraph->materials->getModel(FIRE), shaderProgram);
240+
Fire* newFire = new Fire(glutGet(GLUT_WINDOW_WIDTH), pos, sceneGraph->materials->getModel(FILENAME::FIRE), objectProgram);
229241
SceneNode* fireNode = new SceneNode(newFire);
230242
sceneGraph->addChild(fireGroup, fireNode);
231243
firenum++;
@@ -248,7 +260,7 @@ void GameManager::starmaker(void(*t)(int)) {
248260
starnum--;
249261
}
250262
if (starnum < MAXSTAR) {
251-
Star* newStar = new Star(glutGet(GLUT_WINDOW_WIDTH), pos, sceneGraph->materials->getModel(STAR), shaderProgram);
263+
Star* newStar = new Star(glutGet(GLUT_WINDOW_WIDTH), pos, sceneGraph->materials->getModel(FILENAME::STAR), objectProgram);
252264
SceneNode* starNode = new SceneNode(newStar);
253265
sceneGraph->addChild(starGroup, starNode);
254266
starnum++;
@@ -273,7 +285,7 @@ void GameManager::groundmaker(void(*t)(int)) {
273285
}
274286
if (groundnum < MAXGROUND) {
275287
for (int i = 0; i < height[random]; i++) {
276-
Ground* newGround = new Ground(groundMaxX, i, sceneGraph->materials->getModel(GROUND), shaderProgram);
288+
Ground* newGround = new Ground(groundMaxX, i, sceneGraph->materials->getModel(FILENAME::GROUND), objectProgram);
277289
SceneNode* groundNode = new SceneNode(newGround);
278290

279291
sceneGraph->addChild(groundGroup, groundNode);
@@ -299,7 +311,7 @@ void GameManager::mushmaker(void(*t)(int)) {
299311
mushnum--;
300312
}
301313
if (mushnum < MAXMUSH) {
302-
Mush* newMush = new Mush(sceneGraph->materials->getModel(MUSHROOM), shaderProgram);
314+
Mush* newMush = new Mush(sceneGraph->materials->getModel(FILENAME::MUSHROOM), objectProgram);
303315
SceneNode* mushNode = new SceneNode(newMush);
304316
sceneGraph->addChild(mushGroup, mushNode);
305317

0 commit comments

Comments
 (0)