Adding a buggy FPS Camera with position and rotation

This commit is contained in:
2019-12-09 05:46:30 +02:00
parent ad56096eb3
commit b51c63ea0c
9 changed files with 255 additions and 106 deletions
+35 -74
View File
@@ -1,5 +1,4 @@
#define _USE_MATH_DEFINES
#include <vector>
#include <vector>
#include <algorithm>
#include <limits>
#include "tgaimage.h"
@@ -8,44 +7,41 @@
#include "renderer.h"
#include "util_window.h"
#include <ctime>
#include "camera.h"
#define DEG2RAD M_PI/180
#define HORIZONTAL_CAMERA_SPEED 1
#define VERTICAL_CAMERA_SPEED 1
#define NEAR_CLIP_PLANE 0
#define FAR_CLIP_PLANE 15
#define FOV 15
#define HORIZONTAL_CAMERA_SPEED 1
#define VERTICAL_CAMERA_SPEED 1
#define VERTICAL_CAMERA_CLAMP_UP 90
#define VERTICAL_CAMERA_CLAMP_DOWN -90
#define NEAR_CLIP_PLANE 0
#define FAR_CLIP_PLANE 15
#define FOV 30
#define DEFAULT_CAMERA_POS Vec3f(0, 0, 5)
#define DEFAULT_CAMERA_ROT Vec3f(0, 0, 0)
const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor green = TGAColor(0, 255, 0, 255);
const TGAColor blue = TGAColor(0, 0, 255, 255);
const int depth = 255;
int angle_hor = 0;
int angle_ver = 0;
bool wireframe = false;
Model* model = new Model("african_head.obj");
Camera camera;
float* z_buffer = new float[screen_width * screen_height];
Vec3f light_dir = Vec3f(0, 0, 1).normalize();
Vec3f eye(0, 0, 5);
Vec3f center(0, 0, 0);
Matrix viewport(int x, int y, int w, int h) {
Matrix m = Matrix::identity();
m[0][3] = x + w / 2.f;
m[1][3] = y + h / 2.f;
m[2][3] = depth / 2.f;
m[2][3] = (FAR_CLIP_PLANE-NEAR_CLIP_PLANE) / 2.f;
m[0][0] = w / 2.f;
m[1][1] = h / 2.f;
m[2][2] = depth / 2.f;
m[2][2] = (FAR_CLIP_PLANE+NEAR_CLIP_PLANE) / 2.f;
return m;
}
@@ -143,6 +139,7 @@ void triangle(
Vec2i clamp(screen_width - 1, screen_height - 1);
TGAColor color = white;
#pragma omp parallel for
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
bounding_box_min[j] = std::fmax(0, std::fmin(bounding_box_min[j], (int)pts[i][j]));
@@ -151,6 +148,7 @@ void triangle(
}
Vec3f P;
#pragma omp parallel for
for (P.x = bounding_box_min.x; P.x <= bounding_box_max.x; P.x++) {
for (P.y = bounding_box_min.y; P.y <= bounding_box_max.y; P.y++) {
Vec3f bc_coord = barycentric(pts, P);
@@ -168,7 +166,7 @@ void triangle(
for (int i = 0; i < 3; i++) P.z += pts[i][2] * bc_coord[i];
// Coloring according to the Z-Buffer
if (P.z > z_buffer[(int)(P.x + P.y * screen_width)] && P.z > 20 && P.z < 50)
if (P.z > z_buffer[(int)(P.x + P.y * screen_width)] && P.z > 0)
{
z_buffer[(int)(P.x + P.y * screen_width)] = P.z;
@@ -196,77 +194,40 @@ int color_to_int(TGAColor col) {
return (col[2] << 16) | (col[1] << 8) | col[0];
}
void init_camera() {
camera.SetPosition(DEFAULT_CAMERA_POS);
camera.SetRotation(DEFAULT_CAMERA_ROT);
camera.SetFOV(FOV);
camera.SetNearPlane(NEAR_CLIP_PLANE);
camera.SetFarPlane(FAR_CLIP_PLANE);
camera.SetClampRotDown(VERTICAL_CAMERA_CLAMP_DOWN);
camera.SetClampRotUp(VERTICAL_CAMERA_CLAMP_UP);
camera.SetHorizontalRotSpeed(HORIZONTAL_CAMERA_SPEED);
camera.SetVerticalRotSpeed(VERTICAL_CAMERA_SPEED);
camera.ApplyChanges();
}
void clear_zbuffer()
{
for (int i = 0; i < screen_width * screen_height; i++)
z_buffer[i] = INT_MIN;
}
Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) {
Vec3f z = (eye-center).normalize();
Vec3f x = cross(up,z).normalize();
Vec3f y = cross(z,x).normalize();
Matrix Minv = Matrix::identity();
Matrix Tr = Matrix::identity();
for (int i=0; i<3; i++) {
Minv[0][i] = x[i];
Minv[1][i] = y[i];
Minv[2][i] = z[i];
Tr[i][3] = -center[i];
}
return Minv*Tr;
}
void move_camera_right() {
angle_hor += HORIZONTAL_CAMERA_SPEED;
}
void move_camera_left() {
angle_hor -= HORIZONTAL_CAMERA_SPEED;
}
void move_camera_up() {
angle_ver += VERTICAL_CAMERA_SPEED;
if (angle_ver > 90) angle_ver = 90;
}
void move_camera_down() {
angle_ver -= VERTICAL_CAMERA_SPEED;
if (angle_ver < -90) angle_ver = -90;
}
Matrix ViewPort = Matrix::identity();
Matrix Projection = Matrix::identity();
Matrix ModelView = Matrix::identity();
void render()
{
Vec3f forward = Vec3f(
sinf(angle_hor * DEG2RAD),
-sinf(angle_ver * DEG2RAD),
-cosf(angle_hor*DEG2RAD) * cosf(angle_ver*DEG2RAD));
Vec3f right = Vec3f(sinf(angle_hor*DEG2RAD + M_PI_2), 0, 0);
center = eye + forward;
//ViewPort = viewport(0, 0, screen_width, screen_height);
ViewPort = viewport(screen_width / 8, screen_height / 8, screen_width * 3 / 4, screen_height * 3 / 4);
ModelView = lookat(eye, center, cross(right, forward));
Projection[0][0] = 1 / tanf(FOV * DEG2RAD);
Projection[1][1] = 1 / tanf(FOV * DEG2RAD);
Projection[2][2] = (FAR_CLIP_PLANE + NEAR_CLIP_PLANE) / (FAR_CLIP_PLANE - NEAR_CLIP_PLANE);
Projection[2][3] = (-2 * FAR_CLIP_PLANE * NEAR_CLIP_PLANE) / (FAR_CLIP_PLANE - NEAR_CLIP_PLANE);
Projection[3][2] = -1.f / (forward).norm();
//Projection[3][2] = 1;
//Projection[3][3] = 0;
// model->rotate(Vec3f(0, 180, 0));
// model->ApplyTransform();
light_dir = camera.GetForward() * -1;
ViewPort = viewport(0, 0, screen_width, screen_height);
Projection = camera.GetProjectionMatrix();
ModelView = camera.GetModelViewMatrix();
Matrix z = ViewPort * Projection * ModelView * model->Transform;
clear_zbuffer();
#pragma omp parallel for
for (int i = 0; i < model->nfaces(); i++)
{
std::vector<int> face = model->face(i);