Added messed up low performance camera rotation

This commit is contained in:
2019-12-08 18:47:16 +02:00
parent f5d3196828
commit 2f5437396c
8 changed files with 189 additions and 103 deletions
-3
View File
@@ -13,9 +13,6 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
+30 -3
View File
@@ -3,15 +3,18 @@
#include "renderer.h"
#include "ctime"
const int width = 800;
const int height = 800;
const int screen_width = 1000;
const int screen_height = 1000;
int prev_mouse_x = 0;
int prev_mouse_y = 0;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
MSG Msg;
hwnd = create_window(width, height, hInstance);
hwnd = create_window(hInstance);
ShowWindow(hwnd, nCmdShow);
char debug_str[100];
@@ -36,6 +39,30 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_MOUSEMOVE:
int xPos, yPos;
xPos = LOWORD(lParam);
yPos = HIWORD(lParam);
if (xPos == prev_mouse_x)
break;
else if (xPos > prev_mouse_x)
move_camera_right();
else
move_camera_left();
prev_mouse_x = xPos;
if (yPos == prev_mouse_y)
break;
else if (yPos > prev_mouse_y)
move_camera_up();
else
move_camera_down();
prev_mouse_y = yPos;
render();
Update();
break;
case WM_RBUTTONDOWN:
break;
case WM_LBUTTONDOWN:
+3 -3
View File
@@ -58,9 +58,9 @@ void Model::ApplyTransform() {
}
void Model::translate(Vec3f tr) {
Translation[0][3] += tr.x;
Translation[1][3] += tr.y;
Translation[2][3] += tr.z;
Translation[0][3] = tr.x;
Translation[1][3] = tr.y;
Translation[2][3] = tr.z;
}
void Model::rotate(Vec3f rot) {
rot = rot * DEG2RAD;
Binary file not shown.
+74 -27
View File
@@ -9,6 +9,12 @@
#include "util_window.h"
#include <ctime>
#define DEG2RAD M_PI/180
#define HORIZONTAL_CAMERA_SPEED 1
#define NEAR_CLIP_PLANE 0.1
#define FAR_CLIP_PLANE 5
const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor green = TGAColor(0, 255, 0, 255);
@@ -16,9 +22,17 @@ const TGAColor blue = TGAColor(0, 0, 255, 255);
const int depth = 255;
float* z_buffer;
int angle_hor = 0;
int angle_ver = 0;
bool wireframe = false;
Model* model = new Model("african_head.obj");
float* z_buffer = new float[screen_width * screen_height];
Vec3f light_dir = Vec3f(0, 0, 1).normalize();
Vec3f eye(0, 0, 3);
Vec3f eye(0, 0, 5);
Vec3f center(0, 0, 0);
Matrix viewport(int x, int y, int w, int h) {
@@ -33,7 +47,7 @@ Matrix viewport(int x, int y, int w, int h) {
return m;
}
void line(Vec2i p0, Vec2i p1, TGAImage &image, TGAColor color)
void line(Vec3f p0, Vec3f p1, TGAColor color)
{
bool steep = false;
@@ -59,10 +73,10 @@ void line(Vec2i p0, Vec2i p1, TGAImage &image, TGAColor color)
for (int x = p0[0]; x <= p1[0]; x++) {
if (steep) {
image.set(y, x, color);
set_pixel(y, x, color_to_int(color));
}
else {
image.set(x, y, color);
set_pixel(x, y, color_to_int(color));
}
error2 += derror2;
if (error2 > dx) {
@@ -86,28 +100,42 @@ Vec3f barycentric(Vec3f* pts, Vec3f P)
void triangle(
Vec3f* pts, // Needed
Model* model, // Should be removed
Vec2f* diff_pts, // Should be removed
float* intensities,
Vec3f camera_pos) // Not really sure yet
Model* model,
float* intensities)
{
if (pts[0].y == pts[1].y && pts[0].y == pts[2].y) return; // i dont care about degenerate triangles
if (pts[0].y > pts[1].y) {
std::swap(pts[0], pts[1]);
if(diff_pts)
std::swap(diff_pts[0], diff_pts[1]);
if(intensities)
std::swap(intensities[0], intensities[1]);
}
if (pts[0].y > pts[2].y) {
std::swap(pts[0], pts[2]);
if(diff_pts)
std::swap(diff_pts[0], diff_pts[2]);
if(intensities)
std::swap(intensities[0], intensities[2]);
}
if (pts[1].y > pts[2].y) {
std::swap(pts[1], pts[2]);
if(diff_pts)
std::swap(diff_pts[1], diff_pts[2]);
if(intensities)
std::swap(intensities[1], intensities[2]);
}
if (wireframe)
{
line(pts[0], pts[1], white);
line(pts[1], pts[2], white);
line(pts[2], pts[0], white);
return;
}
Vec2i bounding_box_min(screen_width - 1, screen_height - 1);
Vec2i bounding_box_max(0, 0);
Vec2i clamp(screen_width - 1, screen_height - 1);
@@ -138,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)])
if (P.z > z_buffer[(int)(P.x + P.y * screen_width)] && P.z > NEAR_CLIP_PLANE)
{
z_buffer[(int)(P.x + P.y * screen_width)] = P.z;
@@ -163,9 +191,8 @@ int color_to_int(TGAColor col) {
return (col[2] << 16) | (col[1] << 8) | col[0];
}
void init_zbuffer()
void clear_zbuffer()
{
z_buffer = new float[screen_width*screen_height];
for (int i = 0; i < screen_width * screen_height; i++)
z_buffer[i] = INT_MIN;
}
@@ -185,25 +212,41 @@ Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) {
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 += HORIZONTAL_CAMERA_SPEED;
}
void move_camera_down() {
angle_ver -= HORIZONTAL_CAMERA_SPEED;
}
Matrix ViewPort = Matrix::identity();
Matrix Projection = Matrix::identity();
Matrix ModelView = Matrix::identity();
void render()
{
Model* model = new Model("african_head.obj");
Matrix ViewPort = viewport(screen_width / 8, screen_height / 8, screen_width * 3 / 4, screen_height * 3 / 4);
Matrix Projection = Matrix::identity();
Matrix ModelView = lookat(eye, center, Vec3f(0, 1, 0));
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);
Projection[3][2] = -1.f / (eye - center).norm();
center = eye + forward;
model->rotate(Vec3f(0, 0, 90));
model->scale(Vec3f(0.5, 0.5, 0.5));
model->translate(Vec3f(0.5, 0.5, -1));
ViewPort = viewport(0, 0, screen_width, screen_height);
ModelView = lookat(eye, center, cross(right, forward));
Projection[3][2] = -1.f / (forward).norm();
model->ApplyTransform();
Matrix z = ViewPort * Projection * ModelView * model->Transform;// *Projection * ModelView;// * model->Transform;// *Projection * ModelView * model->Transform;
Matrix z = ViewPort * Projection * ModelView * model->Transform;
init_zbuffer();
clear_zbuffer();
for (int i = 0; i < model->nfaces(); i++)
{
std::vector<int> face = model->face(i);
@@ -211,6 +254,7 @@ void render()
Vec3f world_coords[3];
Vec2f diffuse_coords[3];
float intensities[3];
bool out = true;
for (int j = 0; j < 3; j++)
{
@@ -218,15 +262,18 @@ void render()
Vec4f v4(v);
Vec3f coord(z * v4);
if (coord.x > 0 && coord.x < screen_height)
out = false;
screen_coords[j] = coord;
world_coords[j] = v;
diffuse_coords[j] = model->uv(i, j);
intensities[j] = model->normal(i, j) * light_dir;
}
triangle(screen_coords, model, diffuse_coords, intensities, Vec3f(0, 0, 5));
}
delete model;
if (out) continue;
triangle(screen_coords, diffuse_coords, model, intensities);
}
}
+9
View File
@@ -2,6 +2,15 @@
#define RENDERER_HEADER
#include "tgaimage.h"
extern int angle_hor;
extern int angle_ver;
extern float* z_buffer;
void move_camera_right();
void move_camera_left();
void move_camera_up();
void move_camera_down();
void render();
int color_to_int(TGAColor col);
#endif
+13 -7
View File
@@ -8,8 +8,6 @@ BITMAPINFO info;
HBITMAP hbm;
const int BITCOUNT_PER_PIXEL = 24;
const int title_height = 39;
int screen_width = 0;
int screen_height = 0;
long long bytes_per_row;
bool screen_changed = false;
HDC hdc;
@@ -17,15 +15,13 @@ HDRAWDIB hdd;
HDC bitmap_dc;
HGDIOBJ old_obj;
HWND create_window(int width, int height, HINSTANCE &hInstance) {
HWND create_window(HINSTANCE &hInstance) {
HWND hWnd;
WNDCLASSEX wnd_class = { 0 };
init_wnd_class(wnd_class, hInstance);
RegisterClassEx(&wnd_class);
screen_width = width;
screen_height = height;
create_hwnd(hWnd, hInstance);
init(hWnd);
@@ -107,6 +103,16 @@ void set_pixel(unsigned int x, unsigned int y, unsigned int color) {
screen_changed = true;
}
void Update() {
DrawDibDraw(hdd, hdc, 0, 0, screen_width, screen_height, &info.bmiHeader, pixels, 0, 0, screen_width, screen_height, 0);
void clear_screen() {
for (int x = 0; x < screen_width; x++)
for (int y = 0; y < screen_height; y++)
set_pixel(x, y, 0);
}
void Update() {
if (screen_changed) {
DrawDibDraw(hdd, hdc, 0, 0, screen_width, screen_height, &info.bmiHeader, pixels, 0, 0, screen_width, screen_height, 0);
clear_screen();
screen_changed = false;
}
}
+3 -3
View File
@@ -18,13 +18,13 @@ extern HDC hdc;
extern HDRAWDIB hdd;
extern HDC bitmap_dc;
extern HGDIOBJ old_obj;
extern int screen_width;
extern int screen_height;
extern const int screen_width;
extern const int screen_height;
void init(HWND &hWnd);
void destroy_window();
HWND create_window(int width, int height, HINSTANCE &hInstance);
HWND create_window(HINSTANCE &hInstance);
void create_hwnd(HWND &hwnd, HINSTANCE &hInstance);
void init_wnd_class(WNDCLASSEX &wndClass, HINSTANCE &hInstance);
void set_pixel(unsigned int x, unsigned int y, unsigned int color);