Skip to content

Commit

Permalink
Merge pull request #13 from Lia316/feat/lightSource
Browse files Browse the repository at this point in the history
Feat/light source
  • Loading branch information
Lia316 authored May 19, 2023
2 parents b5f8c5e + 2c7e5d9 commit 99acac3
Show file tree
Hide file tree
Showing 56 changed files with 1,522 additions and 100 deletions.
28 changes: 21 additions & 7 deletions OpenGL/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ Camera::Camera() {
eye = vec3(0, 0, 400);
position = vec3(0.0f, 0.0f, 0.0f);
up = vec3(0.0f, 1.0f, 0.0f);
cameraPos = vec3(0.0f);

currentMode = SIDE;
projectionMatrix = mat4(1.0f);
viewMatrix = mat4(1.0f);
projectionViewMatrix = mat4(1.0f);
changeView(SIDE, 1);
}
Expand All @@ -14,6 +17,10 @@ mat4 Camera::getProjectionViewMatrix() {
return projectionViewMatrix;
}

vec3 Camera::getPosition() {
return eye;
}

void Camera::setCameraMode(CameraMode mode) {
currentMode = mode;
}
Expand All @@ -27,12 +34,16 @@ mat4 Camera::getCamera(CameraMode mode) {

switch (mode) {
case FRONT:
eye = frontEye;
return lookAt(frontEye, frontPos, up);
case SIDE:
eye = sideEye;
return lookAt(sideEye, position, up);
case ORTHO:
return lookAt(orthoEye, position, up); ;
eye = orthoEye;
return lookAt(orthoEye, position, up);
default:
eye = sideEye;
return lookAt(sideEye, position, up);
}
}
Expand All @@ -50,16 +61,16 @@ mat4 Camera::getProjection(CameraMode mode) {
}
}

mat4 Camera::getTransform(CameraMode mode) {
vec3 Camera::getTransform(CameraMode mode) {
switch (mode) {
case FRONT:
return translate(mat4(1.0f), vec3(0, -100, 0));
return vec3(0, -100, 0);
case SIDE:
return translate(mat4(1.0f), vec3(0, -200, 0));
return vec3(-250, -200, 0);
case ORTHO:
return translate(mat4(1.0f), vec3(0, -250, 0));
return vec3(-250, -250, 0);
default:
return translate(mat4(1.0f), vec3(0, 150, 0));
return vec3(-250, -200, 0);
}
}

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

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

viewMatrix = transform * view;
projectionMatrix = projection;
cameraPos = eye - getTransform(mode);
projectionViewMatrix = projection * transform * view;
}
8 changes: 7 additions & 1 deletion OpenGL/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ class Camera {
vec3 eye;
vec3 position;
vec3 up;
vec3 cameraPos;
mat4 viewMatrix;
mat4 projectionMatrix;
mat4 projectionViewMatrix;
CameraMode currentMode;

mat4 getCamera(CameraMode mode);
mat4 getProjection(CameraMode mode);
mat4 getTransform(CameraMode mode);
vec3 getTransform(CameraMode mode);

public:
Camera();
mat4 getViewMatrix() { return viewMatrix; }
mat4 getProjectionMatrix() { return projectionMatrix; }
mat4 getProjectionViewMatrix();
vec3 getPosition();
void changeView(CameraMode mode, float time);
void setCameraMode(CameraMode mode);
};
8 changes: 7 additions & 1 deletion OpenGL/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define KEY_FRAME_NUM 4

Character::Character(Model* models[KEY_FRAME_NUM - 1], GLuint shaderProgram)
Character::Character(Model* models[KEY_FRAME_NUM - 1], GLuint* shaderProgram)
: Entity(glutGet(GLUT_WINDOW_WIDTH) / 10, glutGet(GLUT_WINDOW_HEIGHT) / 4, 0, 0, NULL, shaderProgram) {
jumpSpeed = 14;
lowjumpSpeed = 10;
Expand All @@ -18,6 +18,8 @@ Character::Character(Model* models[KEY_FRAME_NUM - 1], GLuint shaderProgram)
}
time = 0;
currentKeyFrame = 0;

loadTexture(textures->getTextures(TEXTYPE::CHARACTER), 3);
}

Character::~Character() {
Expand Down Expand Up @@ -83,6 +85,10 @@ float Character::getPositionY() {
return y;
}

vec3 Character::getPosition() {
return vec3(x, y, z);
}

void Character::animation(void(*t)(int))
{
time += 0.1;
Expand Down
3 changes: 2 additions & 1 deletion OpenGL/Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Character : public Entity {
const float keyFrameTimes[KEY_FRAME_NUM] = { 0.0f, 1.0f, 2.0f, 3.0f };

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

Expand All @@ -32,5 +32,6 @@ class Character : public Entity {
bool isJumping();
float getPositionX();
float getPositionY();
vec3 getPosition();
void animation(void(*t)(int));
};
37 changes: 35 additions & 2 deletions OpenGL/Entity.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "Entity.h"
#include "RgbImage.h"

Entity::Entity(float x, float y, float z, float speed, Model* model, GLuint shaderProgram)
Entity::Entity(float x, float y, float z, float speed, Model* model, GLuint* shaderProgram)
: x(x), y(y), z(z), speed(speed), model(model), shaderProgram(shaderProgram) {
UuidCreate(&uuid);
if (model == nullptr) return;

filesize = 0;

this->z = model->getminZ() + z;
width = model->getWidth();
height = model->getHeight();
Expand All @@ -19,16 +22,46 @@ void Entity::move() {
x += speed;
}

void Entity::loadTexture(vector<RgbImage*> theTexMaps, unsigned int filesize) {
this->filesize = filesize;

for (unsigned int i = 0; i < filesize; i++) {
GLuint texture;

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, theTexMaps[i]->GetNumCols(), theTexMaps[i]->GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE, theTexMaps[i]->ImageData());
glGenerateMipmap(GL_TEXTURE_2D);

textureIds[i] = texture;
}
}

void Entity::draw() {
if (model == nullptr) return;

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

GLuint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLuint modelLoc = glGetUniformLocation(*shaderProgram, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, &modelMatrix[0][0]);

for (unsigned int i = 0; i < filesize; i++) {
GLuint textureLoc = glGetUniformLocation(*shaderProgram, textureType[i]);
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, textureIds[i]);
glUniform1i(textureLoc, i);
}
if (filesize)
glActiveTexture(GL_TEXTURE0);

model->draw();
}

Expand Down
12 changes: 10 additions & 2 deletions OpenGL/Entity.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include "Model.h"
#include "Textures.h"

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <Rpc.h>
Expand All @@ -8,13 +10,18 @@
class Entity {
protected:
UUID uuid;
GLuint shaderProgram;
GLuint* shaderProgram;
Model* model;
int filesize;
GLuint textureIds[5];
const char* textureType[3] = {"texture_diffuse", "texture_normal", "texture_specular"};

float x, y, z;
float width, height, depth;
float speed;

public:
Entity(float, float, float, float, Model*, GLuint);
Entity(float, float, float, float, Model*, GLuint*);
virtual ~Entity();

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

virtual void loadTexture(vector<RgbImage*> theTexMaps, unsigned int filesize);
virtual void draw();
virtual void move();
virtual float getPositionX();
Expand Down
6 changes: 4 additions & 2 deletions OpenGL/Fire.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "Fire.h"

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

void Fire::draw() {
if (model == nullptr)
Expand Down
2 changes: 1 addition & 1 deletion OpenGL/Fire.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Fire : public Entity {
protected:

public:
Fire(float x, float y, Model* model, GLuint shaderProgram);
Fire(float x, float y, Model* model, GLuint* shaderProgram);
~Fire() {};
const type_info& getType() override { return typeid(Fire); }

Expand Down
54 changes: 33 additions & 21 deletions OpenGL/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

using namespace std;

GameManager::GameManager(GLuint program) {
shaderProgram = program;
sceneGraph = new SceneGraph(program);
viewMode = 1;
GameManager::GameManager(GLuint* objectShader, GLuint* lightShader) {
objectProgram = objectShader;
sceneGraph = new SceneGraph(objectShader, lightShader);
text = new Text2D("Holstein.DDS");

viewMode = 1;
isGameEnd = false;
score = 0;
firenum = 0;
Expand All @@ -17,28 +18,24 @@ GameManager::GameManager(GLuint program) {

groundMaxX = 1000;
isHole = false;
lightAngle = 0;
}

// ###### Draw ######

void GameManager::draw() {
glUseProgram(*objectProgram);
sceneGraph->draw();

string scoreText = "score: " + to_string(score);
showText(300, glutGet(GLUT_WINDOW_HEIGHT) - 300, scoreText);
if (isGameEnd) {
showText(300, glutGet(GLUT_WINDOW_HEIGHT) - 300, "The Game End");
}
}

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

for (const char* c = str; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
}
glUseProgram(0);
}

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

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

// 6. Point Light
PointLight* light = dynamic_cast<PointLight*>(sceneGraph->findNode(typeid(PointLight))->getEntity());

lightAngle += 0.05;
if (lightAngle >= 360)
lightAngle -= 360;

vec3 characterPos = character->getPosition();
light->rotate(characterPos, lightAngle);
vec4 lightPos = vec4(light->getPosition(), 1.0f);

GLuint pointLightLoc = glGetUniformLocation(*objectProgram, "lightPosition");
glUniform4fv(pointLightLoc, 1, &lightPos[0]);

if (isGameEnd) {
character->sink();
glutPostRedisplay();
Expand Down Expand Up @@ -225,7 +237,7 @@ void GameManager::firemaker(void(*t)(int)) {
firenum--;
}
if (firenum < MAXFIRE) {
Fire* newFire = new Fire(glutGet(GLUT_WINDOW_WIDTH), pos, sceneGraph->materials->getModel(FIRE), shaderProgram);
Fire* newFire = new Fire(glutGet(GLUT_WINDOW_WIDTH), pos, sceneGraph->materials->getModel(FILENAME::FIRE), objectProgram);
SceneNode* fireNode = new SceneNode(newFire);
sceneGraph->addChild(fireGroup, fireNode);
firenum++;
Expand All @@ -248,7 +260,7 @@ void GameManager::starmaker(void(*t)(int)) {
starnum--;
}
if (starnum < MAXSTAR) {
Star* newStar = new Star(glutGet(GLUT_WINDOW_WIDTH), pos, sceneGraph->materials->getModel(STAR), shaderProgram);
Star* newStar = new Star(glutGet(GLUT_WINDOW_WIDTH), pos, sceneGraph->materials->getModel(FILENAME::STAR), objectProgram);
SceneNode* starNode = new SceneNode(newStar);
sceneGraph->addChild(starGroup, starNode);
starnum++;
Expand All @@ -273,7 +285,7 @@ void GameManager::groundmaker(void(*t)(int)) {
}
if (groundnum < MAXGROUND) {
for (int i = 0; i < height[random]; i++) {
Ground* newGround = new Ground(groundMaxX, i, sceneGraph->materials->getModel(GROUND), shaderProgram);
Ground* newGround = new Ground(groundMaxX, i, sceneGraph->materials->getModel(FILENAME::GROUND), objectProgram);
SceneNode* groundNode = new SceneNode(newGround);

sceneGraph->addChild(groundGroup, groundNode);
Expand All @@ -299,7 +311,7 @@ void GameManager::mushmaker(void(*t)(int)) {
mushnum--;
}
if (mushnum < MAXMUSH) {
Mush* newMush = new Mush(sceneGraph->materials->getModel(MUSHROOM), shaderProgram);
Mush* newMush = new Mush(sceneGraph->materials->getModel(FILENAME::MUSHROOM), objectProgram);
SceneNode* mushNode = new SceneNode(newMush);
sceneGraph->addChild(mushGroup, mushNode);

Expand Down
Loading

0 comments on commit 99acac3

Please sign in to comment.