Commit ea9a7174 authored by Adam S's avatar Adam S
Browse files

upload

parent cb86d657
#pragma once
#include "../utils/Shader.h"
#include "../utils/std_image.h"
#include "../utils/MatrixGenerator.h"
#include "../utils/odeSolver.h"
#include "GameObject.h"
#include "Paddle.h"
class Ball : GameObject {
public:
Ball(float r, int stack_count, int sector_count, const vec3& position);
bool update(float time, float delta_time, const std::vector<Paddle>& paddles, float paddle_radius, float paddle_width, std::vector<std::vector<Paddle>>& bricks, float brick_radius, float brick_width, int brick_in_row_count, float outer_edge_radius, bool ball_launched);
void render(const mat4& view_m, const float camera_zoom, float scr_w, float scr_h) const;
void cleanUp() const;
void sendLightingData(const vec3& light_color, const vec3& light_position, const vec3& view_position);
void setPosition(const vec3& position) { this->position = position; }
void setDirection(const vec3& direction) { this->direction = direction; }
private:
float speed = 2.5f;
vec3 direction = vec3(-1.0f, 0.0f, 0.0f);
float radius;
unsigned int VBO, VAO, EBO, texture;
Shader shader;
mat4 model = mat4();
std::vector<float> verticies;
std::vector<float> normals;
std::vector<float> texCoords;
std::vector<unsigned int> indicies;
std::vector<float> sphere_buffer;
void sphere_data(float r, int stack_count, int sector_count);
void sphere_indicies(int stack_count, int sector_count);
void sphere_triangulate();
bool isBetweenAngles(float angle1, float angle2) const;
vec3 closestPointOnLineSegment(const vec3& p, const vec3& a, const vec3& b) const;
void ballPaddleCollisions(const std::vector<Paddle>& paddles, float paddle_radius, float paddle_width);
void ballBrickCollisions(std::vector<std::vector<Paddle>>& bricks, float brick_radius, float brick_width, int brick_in_row_count);
bool ballOuterEdgeCollisions(float outer_edge_radius) const;
};
\ No newline at end of file
#pragma once
#include "../math/vec3.h"
#include "../utils/CoordsConverter.h"
using namespace MyMath;
class GameObject
{
public:
vec3 position;
GameObject(const vec3& position) {
this->position = position;
}
};
\ No newline at end of file
#include "Ground.h"
const std::string path = "../../../../GameEngine";
Ground::Ground(float r, int sectors) : shader("./shaders/ground.vert", "./shaders/ground.frag") {
verticies = circle_verticies(r, 0.0, 0.0, sectors);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verticies.size() * sizeof(float), &verticies[0], GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// tex coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
unsigned char* data = stbi_load("./textures/s.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
shader.use();
shader.setInt("ourTexture", 0);
}
void Ground::render(const mat4& view_m, const float camera_zoom, float scr_w, float scr_h) const {
mat4 model = mat4();
mat4 view = mat4();
mat4 projection = mat4();
view = view_m;
projection = MatrixGenerator::perspective(camera_zoom, scr_w / scr_h, 0.1f, 100.0f) * projection;
shader.use();
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, verticies.size());
}
void Ground::cleanUp() const {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}
void Ground::sendLightingData(const vec3& light_color, const vec3& light_position, const vec3& view_position) {
shader.use();
shader.setVec3("light_color", light_color);
shader.setVec3("light_position", light_position);
shader.setVec3("view_position", view_position);
}
std::vector<float> Ground::circle_verticies(float r, float mid_x, float mid_y, int number_of_points) {
int a = 360 / number_of_points;
std::vector<float> verticies;
for (int i = 0; i < 360; i += a) {
float x0 = r * cosf(MatrixGenerator::radians(i)) + mid_x;
float y0 = r * sinf(MatrixGenerator::radians(i)) + mid_y;
float x1 = r * cosf(MatrixGenerator::radians(i + a)) + mid_x;
float y1 = r * sinf(MatrixGenerator::radians(i + a)) + mid_y;
//mid
verticies.push_back(0.0f);
verticies.push_back(0.0f);
verticies.push_back(0.0f);
//mid normal
verticies.push_back(0.0f);
verticies.push_back(0.0f);
verticies.push_back(1.0f);
//mid tex coords
verticies.push_back(0.5f);
verticies.push_back(0.5f);
//point
verticies.push_back(x0);
verticies.push_back(y0);
verticies.push_back(0.0f);
//point normal
verticies.push_back(0.0f);
verticies.push_back(0.0f);
verticies.push_back(1.0f);
//point tex coords
verticies.push_back(0.5f + (0.5f * cosf(MatrixGenerator::radians(i))));
verticies.push_back(0.5f + (0.5f * sinf(MatrixGenerator::radians(i))));
//point+1
verticies.push_back(x1);
verticies.push_back(y1);
verticies.push_back(0.0f);
//point+1 normal
verticies.push_back(0.0f);
verticies.push_back(0.0f);
verticies.push_back(1.0f);
//point+1 tex coords
verticies.push_back(0.5f + (0.5f * cosf(MatrixGenerator::radians(i + a))));
verticies.push_back(0.5f + (0.5f * sinf(MatrixGenerator::radians(i + a))));
}
return verticies;
}
\ No newline at end of file
#pragma once
#include "../utils/Shader.h"
#include "../utils/std_image.h"
#include "../utils/MatrixGenerator.h"
class Ground
{
public:
Ground(float r, int sectors);
void render(const mat4& view_m, const float camera_zoom, float scr_w, float scr_h) const;
void cleanUp() const;
void sendLightingData(const vec3& light_color, const vec3& light_position, const vec3& view_position);
private:
unsigned int VBO, VAO, texture;
Shader shader;
std::vector<float> verticies;
std::vector<float> circle_verticies(float r, float mid_x, float mid_y, int number_of_points);
};
\ No newline at end of file
#include "Paddle.h"
const std::string path = "../../../../GameEngine";
Paddle::Paddle(float init_angle, float end_angle, int sector_count, float width, float height, float distance_from_mid, const vec3& color, bool is_brick, float z) : GameObject(CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid, init_angle + (end_angle - init_angle) / 2.0f, z })), shader("./shaders/brick_paddle.vert", "./shaders/brick_paddle.frag") {
this->angle_width = end_angle - init_angle;
this->paddle_width = width;
this->paddle_height = height;
this->is_brick = is_brick;
paddle_data(init_angle, end_angle, sector_count, width, height, distance_from_mid);
paddle_indicies(sector_count);
paddle_triangulate();
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, paddle_buffer.size() * sizeof(float), &paddle_buffer[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicies.size() * sizeof(int), &indicies[0], GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
shader.use();
shader.setVec3("color", color);
if (is_brick) {
model = MatrixGenerator::translate(vec3(0.0f, 0.0f, position.z)) * model; // getting visuals to desired position! once
}
}
void Paddle::update(Movement m, float delta_time) {
if (is_brick) {
return;
}
if (m == Movement::right) {
position = (MatrixGenerator::rotateAround(MatrixGenerator::radians(angle_speed) * delta_time, vec3(0.0f, 0.0f, 1.0f)) * vec4(position, 1.0f)).xyz;
model = MatrixGenerator::rotateAround(MatrixGenerator::radians(angle_speed) * delta_time, vec3(0.0f, 0.0f, 1.0f)) * model;
}
else {
position = (MatrixGenerator::rotateAround(MatrixGenerator::radians(-angle_speed) * delta_time, vec3(0.0f, 0.0f, 1.0f)) * vec4(position, 1.0f)).xyz;
model = MatrixGenerator::rotateAround(MatrixGenerator::radians(-angle_speed) * delta_time, vec3(0.0f, 0.0f, 1.0f)) * model;
}
//position.print();
}
void Paddle::sendLightingData(const vec3& light_color, const vec3& light_position, const vec3& view_position) const {
shader.use();
shader.setVec3("light_color", light_color);
shader.setVec3("light_position", light_position);
shader.setVec3("view_position", view_position);
}
void Paddle::brickFall() {
position = (MatrixGenerator::translate(vec3(0.0f, 0.0f, -this->getPaddleHeight())) * vec4(position, 1.0f)).xyz;
model = MatrixGenerator::translate(vec3(0.0f, 0.0f, -this->getPaddleHeight())) * model;
}
void Paddle::render(const mat4& view_m, const float camera_zoom, float scr_w, float scr_h) const {
mat4 view = mat4();
mat4 projection = mat4();
//model = MatrixGenerator::rotateAround(MatrixGenerator::radians(45), vec3(0, 0, 1)) * model;
view = view_m;
projection = MatrixGenerator::perspective(camera_zoom, scr_w / scr_h, 0.1f, 100.0f) * projection;
shader.use();
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
}
void Paddle::cleanUp() const {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
}
void Paddle::paddle_data(float init_angle, float end_angle, int sector_count, float width, float height, float distance_from_mid) {
float step = (end_angle - init_angle) / sector_count;
float curr_angle = init_angle;
vec3 worldUp = vec3(0.0f, 0.0f, 1.0f);
//closing quad 1
vec3 p = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid - width / 2.0f, curr_angle, 0.0f });
vec3 n = p.cross(worldUp).normalized();
verticies.push_back(p.x);
verticies.push_back(p.y);
verticies.push_back(p.z);
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
p = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid + width / 2.0f, curr_angle, 0.0f });
verticies.push_back(p.x);
verticies.push_back(p.y);
verticies.push_back(p.z);
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
p = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid - width / 2.0f, curr_angle, height });
verticies.push_back(p.x);
verticies.push_back(p.y);
verticies.push_back(p.z);
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
p = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid + width / 2.0f, curr_angle, height });
verticies.push_back(p.x);
verticies.push_back(p.y);
verticies.push_back(p.z);
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
for (int i = 0; i < sector_count + 1; i++) {
//bottom
vec3 point = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid - width / 2.0f, curr_angle, 0.0f });
vec3 normal = vec3(0.0f, 0.0f, -1.0f);
verticies.push_back(point.x);
verticies.push_back(point.y);
verticies.push_back(point.z);
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
point = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid + width / 2.0f, curr_angle, 0.0f });
normal = vec3(0.0f, 0.0f, -1.0f);
verticies.push_back(point.x);
verticies.push_back(point.y);
verticies.push_back(point.z);
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
//back
point = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid + width / 2.0f, curr_angle, 0.0f });
normal = point.normalized();
verticies.push_back(point.x);
verticies.push_back(point.y);
verticies.push_back(point.z);
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
point = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid + width / 2.0f, curr_angle, height });
normal = point.normalized();
verticies.push_back(point.x);
verticies.push_back(point.y);
verticies.push_back(point.z);
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
//top
point = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid + width / 2.0f, curr_angle, height });
normal = vec3(0.0f, 0.0f, 1.0f);
verticies.push_back(point.x);
verticies.push_back(point.y);
verticies.push_back(point.z);
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
point = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid - width / 2.0f, curr_angle, height });
normal = vec3(0.0f, 0.0f, 1.0f);
verticies.push_back(point.x);
verticies.push_back(point.y);
verticies.push_back(point.z);
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
//front
point = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid - width / 2.0f, curr_angle, height });
normal = -point.normalized();
verticies.push_back(point.x);
verticies.push_back(point.y);
verticies.push_back(point.z);
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
point = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid - width / 2.0f, curr_angle, 0.0f });
normal = -point.normalized();
verticies.push_back(point.x);
verticies.push_back(point.y);
verticies.push_back(point.z);
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
curr_angle += step;
}
curr_angle -= step; //closing the hole :D
//closing quad 2
p = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid - width / 2.0f, curr_angle, 0.0f });
n = worldUp.cross(p).normalized();
verticies.push_back(p.x);
verticies.push_back(p.y);
verticies.push_back(p.z);
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
p = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid + width / 2.0f, curr_angle, 0.0f });
verticies.push_back(p.x);
verticies.push_back(p.y);
verticies.push_back(p.z);
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
p = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid - width / 2.0f, curr_angle, height });
verticies.push_back(p.x);
verticies.push_back(p.y);
verticies.push_back(p.z);
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
p = CoordsConverter::CylindricalToCartesian(positionCylindrical{ distance_from_mid + width / 2.0f, curr_angle, height });
verticies.push_back(p.x);
verticies.push_back(p.y);
verticies.push_back(p.z);
normals.push_back(n.x);
normals.push_back(n.y);
normals.push_back(n.z);
}
void Paddle::paddle_indicies(int sector_count) {
//closing quad 1
indicies.push_back(0);
indicies.push_back(3);
indicies.push_back(2);
indicies.push_back(0);
indicies.push_back(1);
indicies.push_back(3);
int curr_idx = 4;
for (int i = 0; i < sector_count; i++) {
//bottom
indicies.push_back(curr_idx + 9);
indicies.push_back(curr_idx);
indicies.push_back(curr_idx + 8);
indicies.push_back(curr_idx + 9);
indicies.push_back(curr_idx + 1);
indicies.push_back(curr_idx);
//back
indicies.push_back(curr_idx + 11);
indicies.push_back(curr_idx + 2);
indicies.push_back(curr_idx + 10);
indicies.push_back(curr_idx + 11);
indicies.push_back(curr_idx + 3);
indicies.push_back(curr_idx + 2);
//top
indicies.push_back(curr_idx + 13);
indicies.push_back(curr_idx + 4);
indicies.push_back(curr_idx + 12);
indicies.push_back(curr_idx + 13);
indicies.push_back(curr_idx + 5);
indicies.push_back(curr_idx + 4);
//front
indicies.push_back(curr_idx + 15);
indicies.push_back(curr_idx + 6);
indicies.push_back(curr_idx + 14);
indicies.push_back(curr_idx + 15);
indicies.push_back(curr_idx + 7);
indicies.push_back(curr_idx + 6);
curr_idx += 8;
}
//closing quad 2