Added lasers firing with mouse click. Changed spaceship and laser classes and libraries

This commit is contained in:
CybArciom 2023-01-21 20:46:13 +01:00
parent fdd5cd41c9
commit 64d1cbdfbd
5 changed files with 310 additions and 47 deletions

View File

@ -5,16 +5,18 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "Render_Utils.h"
Laser::Laser() {
}
Laser::Laser(glm::vec3 dirOfLaser, glm::vec3 posOfLaser, float speedOfLaser) {
laserDir = dirOfLaser;
laserPos = posOfLaser;
laserSpeed = speedOfLaser;
Laser::Laser(glm::vec3 dirOfLaser, glm::vec3 posOfLaser, float speedOfLaser, bool lasFly) {
this->laserDir = dirOfLaser;
this->laserPos = posOfLaser;
this->laserSpeed = speedOfLaser;
this->laserFly = lasFly;
}
void Laser::setLaserDir(glm::vec3 dirOfLaser) {
@ -48,3 +50,19 @@ void Laser::setTimeSinceShootLaser(float tSinceShootLaser) {
float Laser::getTimeSinceShootLaser() {
return timeSinceShootLaser;
}
void Laser::setLaserContext(Core::RenderContext lasContext) {
this->laserContext = lasContext;
}
Core::RenderContext Laser::getLaserContext() {
return this->laserContext;
}
void Laser::setLaserFly(bool lasFly) {
this->laserFly = lasFly;
}
bool Laser::isLaserFly() {
return this->laserFly;
}

View File

@ -5,21 +5,23 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "Render_Utils.h"
class Laser
{
private:
Core::RenderContext laserContext;
glm::vec3 laserDir;
glm::vec3 laserPos;
float timeSinceShootLaser;
bool isLaserFire;
float laserSpeed;
bool laserFly;
public:
Laser();
Laser(glm::vec3 dirOfLaser, glm::vec3 posOfLaser, float speedOfLaser);
Laser(glm::vec3 dirOfLaser, glm::vec3 posOfLaser, float speedOfLaser, bool lasFly);
void setLaserDir(glm::vec3 dirOfLaser);
@ -34,5 +36,11 @@ public:
void setTimeSinceShootLaser(float tSinceShootLaser);
float getTimeSinceShootLaser();
void setLaserContext(Core::RenderContext lasContext);
Core::RenderContext getLaserContext();
void setLaserFly(bool lasFly);
bool isLaserFly();
};

View File

@ -5,14 +5,25 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <vector>
#include "Laser.h"
SpaceShip::SpaceShip() {
}
SpaceShip::SpaceShip(bool attackDec) {
attackDecision = attackDec;
SpaceShip::SpaceShip(int numberOfLasers) {
maxNumberOfLasers = numberOfLasers;
numberOfLeftLasers = maxNumberOfLasers;
gun.reserve(maxNumberOfLasers);
this->spaceShipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f);
this->spaceShipPos = glm::vec3(0.065808f, 1.250000f, -2.189549f);
for (int i = 0; i < maxNumberOfLasers; i++) {
gun.push_back(Laser(this->spaceShipDir, this->spaceShipPos, 0.01, false));
}
}
@ -41,3 +52,45 @@ void SpaceShip::setSpaceShipPos(glm::vec3 spShipPos) {
glm::vec3 SpaceShip::getSpaceShipPos() {
return spaceShipPos;
}
void SpaceShip::setMaxNumberOfLasers(int numberOfLasers) {
this->maxNumberOfLasers = numberOfLasers;
}
int SpaceShip::getMaxNumberOfLasers() {
return this->maxNumberOfLasers;
}
void SpaceShip::setNumberOfLeftLasers(int numberOfLasers) {
this->numberOfLeftLasers = numberOfLasers;
}
int SpaceShip::getNumberOfLeftLasers() {
return this->numberOfLeftLasers;
}
void SpaceShip::setGun(std::vector<Laser> g) {
this->gun = g;
}
std::vector<Laser> SpaceShip::getGun() {
return this->gun;
}
void SpaceShip::laserPreparationBeforeShoot(float time) {
if (this->numberOfLeftLasers > 0) {
this->numberOfLeftLasers -= 1;
}
else if (this->numberOfLeftLasers == 0) {
this->numberOfLeftLasers = this->maxNumberOfLasers;
this->numberOfLeftLasers -= 1;
}
//this->numberOfLeftLasers -= 1;
this->gun[this->numberOfLeftLasers].setTimeSinceShootLaser(time);
this->gun[this->numberOfLeftLasers].setLaserDir(this->spaceShipDir);
this->gun[this->numberOfLeftLasers].setLaserPos(this->spaceShipPos);
this->gun[this->numberOfLeftLasers].setLaserSpeed(0.01);
}

View File

@ -5,20 +5,28 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <iostream>
#include <vector>
#include "Laser.h"
class SpaceShip
{
private:
bool attackDecision = false;
glm::vec3 spaceShipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f);
glm::vec3 spaceShipPos = glm::vec3(0.065808f, 1.250000f, -2.189549f);
glm::vec3 spaceShipDir;
glm::vec3 spaceShipPos;
std::vector<Laser> gun;
int maxNumberOfLasers;
int numberOfLeftLasers;
public:
SpaceShip();
SpaceShip(bool attackDec);
SpaceShip(int numberOfLasers);
//getters and setters
void setAttackDecision(bool attackDec);
bool getAttackDecision();
@ -27,5 +35,18 @@ public:
void setSpaceShipPos(glm::vec3 spShipPos);
glm::vec3 getSpaceShipPos();
void setGun(std::vector<Laser> g);
std::vector<Laser> getGun();
void setMaxNumberOfLasers(int numberOfLasers);
int getMaxNumberOfLasers();
void setNumberOfLeftLasers(int numberOfLasers);
int getNumberOfLeftLasers();
//methods
void laserPreparationBeforeShoot(float time);
};

View File

@ -39,7 +39,27 @@ namespace models {
Core::RenderContext testContext;
Core::RenderContext laserContext;
Core::RenderContext laserContext1;
Core::RenderContext laserContext2;
Core::RenderContext laserContext3;
Core::RenderContext laserContext4;
Core::RenderContext laserContext5;
Core::RenderContext laserContext6;
Core::RenderContext laserContext7;
Core::RenderContext laserContext8;
Core::RenderContext laserContext9;
Core::RenderContext laserContext10;
Core::RenderContext laserContext11;
Core::RenderContext laserContext12;
Core::RenderContext laserContext13;
Core::RenderContext laserContext14;
Core::RenderContext laserContext15;
Core::RenderContext laserContext16;
Core::RenderContext laserContext17;
Core::RenderContext laserContext18;
Core::RenderContext laserContext19;
Core::RenderContext laserContext20;
}
GLuint depthMapFBO;
@ -82,11 +102,24 @@ float spotlightPhi = 3.14 / 4;
bool drawLaserShot = false;
double startCursorXPos;
double startCursorYPos;
float lastTime = -1.f;
float deltaTime = 0.f;
Laser laser = Laser();
SpaceShip spaceShip = SpaceShip();
Laser laser1 = Laser();
Laser laser2 = Laser();
SpaceShip spaceShip = SpaceShip(20);
std::vector<Core::RenderContext> lasersContexts{ models::laserContext1, models::laserContext2, models::laserContext3, models::laserContext4, models::laserContext5,
models::laserContext6, models::laserContext7, models::laserContext8, models::laserContext9, models::laserContext10,
models::laserContext11, models::laserContext12, models::laserContext13, models::laserContext14, models::laserContext15,
models::laserContext16, models::laserContext17, models::laserContext18, models::laserContext19, models::laserContext20 };
std::vector<Laser> lasers;
void updateDeltaTime(float time) {
if (lastTime < 0) {
@ -166,7 +199,7 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec
}
void drawLaser() {
void drawLaser(Laser laser, Core::RenderContext laserContext, float dyst) {
float time = glfwGetTime();
@ -185,11 +218,12 @@ void drawLaser() {
laser.setLaserPos(newLaserPos);
drawObjectPBR(models::laserContext,
glm::translate(laser.getLaserPos()) * laserCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.03f)),
drawObjectPBR(laserContext,
glm::translate(glm::vec3(laser.getLaserPos().x + dyst, laser.getLaserPos().y, laser.getLaserPos().z)) * laserCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.03f)),
glm::vec3(1.0f, 0.f, 0.f), 0.f, 0.f);
}
void renderShadowapSun() {
float time = glfwGetTime();
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
@ -266,6 +300,24 @@ void renderScene(GLFWwindow* window)
spotlightConeDir = spaceShip.getSpaceShipDir();
//draw lasers
for (int i = 0; i < lasers.size(); i++) {
drawLaser(lasers[i], lasersContexts[i], 0.1);
drawLaser(lasers[i], lasersContexts[i], -0.1);
/*if (i % 2 == 0) {
drawLaser(lasers[i], lasersContexts[i], 0.1);
drawLaser(lasers[i], lasersContexts[i], -0.1);
}*/
/*else {
drawLaser(lasers[i], lasersContexts[i], 0.1);
}*/
}
//test depth buffer
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -274,10 +326,6 @@ void renderScene(GLFWwindow* window)
//glBindTexture(GL_TEXTURE_2D, depthMap);
//Core::DrawContext(models::testContext);
if (drawLaserShot) {
drawLaser();
}
glUseProgram(0);
glfwSwapBuffers(window);
}
@ -301,10 +349,71 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
context.initFromAssimpMesh(scene->mMeshes[0]);
}
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
std::cout << "yStart: " << startCursorYPos << "\n";
float angleSpeed = 0.05f * deltaTime * 60;
if (ypos < startCursorYPos) {
startCursorYPos = ypos;
spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleX(angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0)));
//startCursorYPos = ypos;
std::cout << "yNew: " << ypos << "\n";
}
else if (ypos > startCursorYPos) {
startCursorYPos = ypos;
spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleX(-angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0)));
//startCursorYPos = ypos;
std::cout << "yNew: " << ypos << "\n";
}
//spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleZ(angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0)));
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
drawLaserShot = true;
spaceShip.setAttackDecision(true);
/*spaceShip.laserPreparationBeforeShoot(glfwGetTime());*/
for (int i = 0; i < lasers.size(); i++) {
if (!lasers[i].isLaserFly() && spaceShip.getAttackDecision()) {
lasers[i].setTimeSinceShootLaser(glfwGetTime());
lasers[i].setLaserDir(spaceShip.getSpaceShipDir());
lasers[i].setLaserPos(spaceShip.getSpaceShipPos());
lasers[i].setLaserSpeed(1);
lasers[i].setLaserFly(true);
spaceShip.setAttackDecision(false);
std::cout << "i = " << i << " " << "laser fly = " << lasers[i].isLaserFly() << "\n";
}
}
}
}
void init(GLFWwindow* window)
{
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwGetCursorPos(window, &startCursorXPos, &startCursorYPos);
glfwSetMouseButtonCallback(window, mouse_button_callback);
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, cursor_position_callback);
glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag");
programTest = shaderLoader.CreateProgram("shaders/test.vert", "shaders/test.frag");
@ -331,7 +440,23 @@ void init(GLFWwindow* window)
loadModelToContext("./models/window.obj", models::windowContext);
loadModelToContext("./models/test.obj", models::testContext);
loadModelToContext("./models/newlaser2.obj", models::laserContext);
//loadModelToContext("./models/newlaser2.obj", models::laserContext);
//lasersContexts.reserve(spaceShip.getMaxNumberOfLasers());
for (int i = 0; i < spaceShip.getMaxNumberOfLasers(); i++) {
//lasersContexts.push_back(models::laserContext);
loadModelToContext("./models/newlaser2.obj", lasersContexts[i]);
lasers.push_back(Laser(spaceShip.getSpaceShipDir(), spaceShip.getSpaceShipPos(), 1, false));
}
/*for (int i = 0; i < spaceShip.getMaxNumberOfLasers(); i++) {
loadModelToContext("./models/newlaser2.obj", spaceShip.getGun()[i].getLaserContext());
}*/
}
@ -340,9 +465,14 @@ void shutdown(GLFWwindow* window)
shaderLoader.DeleteProgram(program);
}
//obsluga wejscia
void processInput(GLFWwindow* window)
{
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceShip.getSpaceShipDir(), glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f);
float angleSpeed = 0.05f * deltaTime * 60;
@ -384,7 +514,7 @@ void processInput(GLFWwindow* window)
spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0)));
if (!spaceShip.getAttackDecision()) {
laser.setLaserDir(spaceShip.getSpaceShipDir());
//laser.setLaserDir(spaceShip.getSpaceShipDir());
}
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
@ -393,7 +523,7 @@ void processInput(GLFWwindow* window)
spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0)));
if (!spaceShip.getAttackDecision()) {
laser.setLaserDir(spaceShip.getSpaceShipDir());
//laser.setLaserDir(spaceShip.getSpaceShipDir());
}
}
@ -414,12 +544,45 @@ void processInput(GLFWwindow* window)
drawLaserShot = true;
spaceShip.setAttackDecision(true);
/*spaceShip.laserPreparationBeforeShoot(glfwGetTime());
laser.setTimeSinceShootLaser(glfwGetTime());
laser.setLaserDir(spaceShip.getSpaceShipDir());
laser.setLaserPos(spaceShip.getSpaceShipPos());
laser.setLaserSpeed(0.001);
if (spaceShip.getNumberOfLeftLasers() > 0) {
spaceShip.setNumberOfLeftLasers(spaceShip.getNumberOfLeftLasers() - 1);
}
else if (spaceShip.getNumberOfLeftLasers() == 0) {
spaceShip.setNumberOfLeftLasers(spaceShip.getMaxNumberOfLasers());
spaceShip.setNumberOfLeftLasers(spaceShip.getNumberOfLeftLasers() - 1);
}
lasers[spaceShip.getNumberOfLeftLasers()].setTimeSinceShootLaser(glfwGetTime());
lasers[spaceShip.getNumberOfLeftLasers()].setLaserDir(spaceShip.getSpaceShipDir());
lasers[spaceShip.getNumberOfLeftLasers()].setLaserPos(spaceShip.getSpaceShipPos());
lasers[spaceShip.getNumberOfLeftLasers()].setLaserSpeed(0.01);*/
for (int i = 0; i < lasers.size(); i++) {
if (!lasers[i].isLaserFly() && spaceShip.getAttackDecision()) {
lasers[i].setTimeSinceShootLaser(glfwGetTime());
lasers[i].setLaserDir(spaceShip.getSpaceShipDir());
lasers[i].setLaserPos(spaceShip.getSpaceShipPos());
lasers[i].setLaserSpeed(1);
lasers[i].setLaserFly(true);
spaceShip.setAttackDecision(false);
std::cout << "i = " << i << " " << "laser fly = " << lasers[i].isLaserFly() << "\n";
}
}
//laser.setTimeSinceShootLaser(glfwGetTime());
//laser.setLaserDir(spaceShip.getSpaceShipDir());
//laser.setLaserPos(spaceShip.getSpaceShipPos());
//laser.setLaserSpeed(0.01);
}
//if(glfwGetKey(window, GLFW_MOUSE))
//cameraDir = glm::normalize(-cameraPos);