4 Commits

Author SHA1 Message Date
mo7sen b936db0ce9 Fixing Camera bugs 2019-12-09 12:24:34 +02:00
mo7sen b51c63ea0c Adding a buggy FPS Camera with position and rotation 2019-12-09 05:46:30 +02:00
mo7sen ad56096eb3 Editing the Projection Matrix 2019-12-08 20:57:18 +02:00
mo7sen 2f5437396c Added messed up low performance camera rotation 2019-12-08 18:47:16 +02:00
13 changed files with 372 additions and 122 deletions
-3
View File
@@ -13,9 +13,6 @@
# User-specific files (MonoDevelop/Xamarin Studio) # User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs *.userprefs
# Mono auto generated files
mono_crash.*
# Build results # Build results
[Dd]ebug/ [Dd]ebug/
[Dd]ebugPublic/ [Dd]ebugPublic/
+2
View File
@@ -115,6 +115,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="camera.cpp" />
<ClCompile Include="geometry.cpp" /> <ClCompile Include="geometry.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="model.cpp" /> <ClCompile Include="model.cpp" />
@@ -124,6 +125,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="geometry.h" /> <ClInclude Include="geometry.h" />
<ClInclude Include="camera.h" />
<ClInclude Include="model.h" /> <ClInclude Include="model.h" />
<ClInclude Include="renderer.h" /> <ClInclude Include="renderer.h" />
<ClInclude Include="tgaimage.h" /> <ClInclude Include="tgaimage.h" />
+6
View File
@@ -33,6 +33,9 @@
<ClCompile Include="renderer.cpp"> <ClCompile Include="renderer.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="util_window.h"> <ClInclude Include="util_window.h">
@@ -50,5 +53,8 @@
<ClInclude Include="renderer.h"> <ClInclude Include="renderer.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="camera.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
+117
View File
@@ -0,0 +1,117 @@
#include "camera.h"
Camera::Camera()
{
fov = 30;
position = Vec3f(0, 0, 0);
rotation = Vec3f(0, 0, 0);
up = Vec3f(0, 0, 0);
right = Vec3f(0, 0, 0);
forward = Vec3f(0, 0, 0);
near_plane = 0;
far_plane = 15;
horizontal_camera_speed = 0.5;
vertical_camera_speed = 0.5;
vertical_camera_clamp_up = 90;
vertical_camera_clamp_down = -90;
movement_speed = 1.f;
}
Vec3f Camera::GetForward() {
return forward;
}
void Camera::Move(Vec3f move_vec) {
position = position + move_vec;
}
void Camera::SetPosition(Vec3f pos) {
position = pos;
}
void Camera::Rotate(Vec3f rot_vec) {
rotation = rotation + rot_vec;
}
void Camera::SetRotation(Vec3f rot) {
rotation = rot;
}
void Camera::SetFOV(int angle) {
fov = angle;
}
void Camera::SetVerticalRotSpeed(float speed) {
vertical_camera_speed = speed;
}
void Camera::SetHorizontalRotSpeed(float speed) {
horizontal_camera_speed = speed;
}
void Camera::SetClampRotUp(float angle) {
vertical_camera_clamp_up = angle;
}
void Camera::SetClampRotDown(float angle) {
vertical_camera_clamp_down = angle;
}
void Camera::SetNearPlane(float near_val) {
near_plane = near_val;
}
void Camera::SetFarPlane(float far_val) {
far_plane = far_val;
}
void Camera::rotate_hor(float d_angle) {
rotation.y += d_angle * horizontal_camera_speed;
}
void Camera::rotate_ver(float d_angle) {
rotation.x += d_angle * vertical_camera_speed;
rotation.x = std::fmin(rotation.x, vertical_camera_clamp_up);
rotation.x = std::fmax(rotation.x, vertical_camera_clamp_down);
}
void Camera::move_camera_left() {
position = position - right * movement_speed;
}
void Camera::move_camera_right() {
position = position + right * movement_speed;
}
void Camera::move_camera_forward() {
position = position + forward * movement_speed;
}
void Camera::move_camera_backward() {
position = position - forward * movement_speed;
}
void Camera::SetMovementSpeed(float speed) {
movement_speed = speed;
}
void Camera::ApplyChanges() {
forward = Vec3f(sin(rotation.y * DEG2RAD), -sin(rotation.x * DEG2RAD), -cosf(rotation.y*DEG2RAD) * cosf(rotation.x*DEG2RAD));
right = Vec3f(cos(rotation.y*DEG2RAD), 0, sin(rotation.y * DEG2RAD));
up = cross(right, forward);
}
Matrix Camera::GetModelViewMatrix() {
Vec3f center = position + forward;
Vec3f z = (position - 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;
}
Matrix Camera::GetProjectionMatrix() {
Matrix Projection = Matrix::identity();
Projection[0][0] = 1 / tan(fov * DEG2RAD);
Projection[1][1] = 1 / tan(fov * DEG2RAD);
Projection[2][2] = (far_plane + near_plane) / (far_plane - near_plane);
Projection[2][3] = (-2 * far_plane * near_plane) / (far_plane - near_plane);
Projection[3][2] = -1;
return Projection;
}
+48
View File
@@ -0,0 +1,48 @@
#ifndef __CAMERA_HEADER__
#define __CAMERA_HEADER__
#include "geometry.h"
class Camera {
private:
Vec3f position;
Vec3f rotation;
Vec3f up;
Vec3f right;
Vec3f forward;
float fov;
float near_plane;
float far_plane;
float horizontal_camera_speed;
float vertical_camera_speed;
float vertical_camera_clamp_up;
float vertical_camera_clamp_down;
float movement_speed;
public:
Camera();
void SetPosition(Vec3f pos);
void SetRotation(Vec3f rot);
void Move(Vec3f move_vec);
void Rotate(Vec3f rot_vec);
void SetFOV(int angle);
void SetNearPlane(float near_val);
void SetFarPlane(float far_val);
void SetVerticalRotSpeed(float speed);
void SetHorizontalRotSpeed(float speed);
void SetClampRotUp(float angle);
void SetClampRotDown(float angle);
void SetMovementSpeed(float speed);
Vec3f GetForward();
void rotate_hor(float d_angle);
void rotate_ver(float d_angle);
void move_camera_left();
void move_camera_right();
void move_camera_forward();
void move_camera_backward();
void ApplyChanges();
Matrix GetModelViewMatrix();
Matrix GetProjectionMatrix();
};
#endif
+3
View File
@@ -1,10 +1,13 @@
#ifndef __GEOMETRY_H__ #ifndef __GEOMETRY_H__
#define __GEOMETRY_H__ #define __GEOMETRY_H__
#define _USE_MATH_DEFINES
#include <cmath> #include <cmath>
#include <vector> #include <vector>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#define DEG2RAD M_PI/180
template<size_t DimCols,size_t DimRows,typename T> class mat; template<size_t DimCols,size_t DimRows,typename T> class mat;
template <size_t DIM, typename T> struct vec { template <size_t DIM, typename T> struct vec {
+44 -11
View File
@@ -3,26 +3,27 @@
#include "renderer.h" #include "renderer.h"
#include "ctime" #include "ctime"
const int width = 800; const int screen_width = 1000;
const int height = 800; const int screen_height = 1000;
#define TARGET_FRAMERATE 30
int prev_mouse_x = screen_width/2;
int prev_mouse_y = screen_height/2;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{ {
HWND hwnd; HWND hwnd;
MSG Msg; MSG Msg;
hwnd = create_window(width, height, hInstance); hwnd = create_window(hInstance);
ShowCursor(false);
ShowWindow(hwnd, nCmdShow); ShowWindow(hwnd, nCmdShow);
char debug_str[100]; init_camera();
clock_t first_time = clock();
render();
clock_t end_time = clock();
Update();
sprintf_s(debug_str, "%f\n", (double)(end_time - first_time) / CLOCKS_PER_SEC); SetTimer(hwnd, NULL, 1000 / TARGET_FRAMERATE, (TIMERPROC)FixedUpdate);
OutputDebugString(debug_str);
while (GetMessage(&Msg, NULL, 0, 0)) while (GetMessage(&Msg, NULL, 0, 0))
{ {
@@ -32,14 +33,46 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
return Msg.wParam; return Msg.wParam;
} }
void HandleMouseMovement() {
POINT point;
GetCursorPos(&point);
camera.rotate_hor(point.x - prev_mouse_x);
camera.rotate_ver(point.y - prev_mouse_y);
SetCursorPos(prev_mouse_x, prev_mouse_y);
}
bool HandleButtonPressed() {
if (GetAsyncKeyState(VK_UP) & 0x8000)
camera.move_camera_forward();
if (GetAsyncKeyState(VK_DOWN) & 0x8000)
camera.move_camera_backward();
if (GetAsyncKeyState(VK_RIGHT) & 0x8000)
camera.move_camera_right();
if (GetAsyncKeyState(VK_LEFT) & 0x8000)
camera.move_camera_left();
return true;
}
void CALLBACK FixedUpdate(HWND hwnd, UINT message, UINT uInt, DWORD dWord)
{
HandleButtonPressed();
camera.ApplyChanges();
render();
Update();
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {
switch (message) switch (message)
{ {
case WM_MOUSEMOVE:
HandleMouseMovement();
break;
case WM_RBUTTONDOWN: case WM_RBUTTONDOWN:
break; break;
case WM_LBUTTONDOWN: case WM_LBUTTONDOWN:
Update();
break; break;
case WM_CLOSE: case WM_CLOSE:
DestroyWindow(hwnd); DestroyWindow(hwnd);
+3 -3
View File
@@ -58,9 +58,9 @@ void Model::ApplyTransform() {
} }
void Model::translate(Vec3f tr) { void Model::translate(Vec3f tr) {
Translation[0][3] += tr.x; Translation[0][3] = tr.x;
Translation[1][3] += tr.y; Translation[1][3] = tr.y;
Translation[2][3] += tr.z; Translation[2][3] = tr.z;
} }
void Model::rotate(Vec3f rot) { void Model::rotate(Vec3f rot) {
rot = rot * DEG2RAD; rot = rot * DEG2RAD;
Binary file not shown.
+124 -93
View File
@@ -1,5 +1,4 @@
#define _USE_MATH_DEFINES #include <vector>
#include <vector>
#include <algorithm> #include <algorithm>
#include <limits> #include <limits>
#include "tgaimage.h" #include "tgaimage.h"
@@ -8,32 +7,46 @@
#include "renderer.h" #include "renderer.h"
#include "util_window.h" #include "util_window.h"
#include <ctime> #include <ctime>
#include "camera.h"
#define HORIZONTAL_CAMERA_SPEED 0.1
#define VERTICAL_CAMERA_SPEED 0.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)
#define CAMERA_MOVEMENT_SPEED 1.f
const TGAColor white = TGAColor(255, 255, 255, 255); const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255); const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor green = TGAColor(0, 255, 0, 255); const TGAColor green = TGAColor(0, 255, 0, 255);
const TGAColor blue = TGAColor(0, 0, 255, 255); const TGAColor blue = TGAColor(0, 0, 255, 255);
const int depth = 255; bool wireframe = false;
float* z_buffer;
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 light_dir = Vec3f(0, 0, 1).normalize();
Vec3f eye(0, 0, 3);
Vec3f center(0, 0, 0);
Matrix viewport(int x, int y, int w, int h) { Matrix viewport(int x, int y, int w, int h) {
Matrix m = Matrix::identity(); Matrix m = Matrix::identity();
m[0][3] = x + w / 2.f; m[0][3] = x + w / 2.f;
m[1][3] = y + h / 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[0][0] = w / 2.f;
m[1][1] = h / 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; return m;
} }
void line(Vec2i p0, Vec2i p1, TGAImage &image, TGAColor color) void line(Vec3f p0, Vec3f p1, TGAColor color)
{ {
bool steep = false; bool steep = false;
@@ -59,10 +72,10 @@ void line(Vec2i p0, Vec2i p1, TGAImage &image, TGAColor color)
for (int x = p0[0]; x <= p1[0]; x++) { for (int x = p0[0]; x <= p1[0]; x++) {
if (steep) { if (steep) {
image.set(y, x, color); set_pixel(y, x, color_to_int(color));
} }
else { else {
image.set(x, y, color); set_pixel(x, y, color_to_int(color));
} }
error2 += derror2; error2 += derror2;
if (error2 > dx) { if (error2 > dx) {
@@ -86,124 +99,137 @@ Vec3f barycentric(Vec3f* pts, Vec3f P)
void triangle( void triangle(
Vec3f* pts, // Needed Vec3f* pts, // Needed
Model* model, // Should be removed
Vec2f* diff_pts, // Should be removed Vec2f* diff_pts, // Should be removed
float* intensities, Model* model,
Vec3f camera_pos) // Not really sure yet 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 && pts[0].y == pts[2].y) return; // i dont care about degenerate triangles
if (pts[0].y > pts[1].y) { if (pts[0].y > pts[1].y) {
std::swap(pts[0], pts[1]); std::swap(pts[0], pts[1]);
std::swap(diff_pts[0], diff_pts[1]); if(diff_pts)
std::swap(intensities[0], intensities[1]); std::swap(diff_pts[0], diff_pts[1]);
if(intensities)
std::swap(intensities[0], intensities[1]);
} }
if (pts[0].y > pts[2].y) { if (pts[0].y > pts[2].y) {
std::swap(pts[0], pts[2]); std::swap(pts[0], pts[2]);
std::swap(diff_pts[0], diff_pts[2]); if(diff_pts)
std::swap(intensities[0], intensities[2]); std::swap(diff_pts[0], diff_pts[2]);
if(intensities)
std::swap(intensities[0], intensities[2]);
} }
if (pts[1].y > pts[2].y) { if (pts[1].y > pts[2].y) {
std::swap(pts[1], pts[2]); std::swap(pts[1], pts[2]);
std::swap(diff_pts[1], diff_pts[2]); if(diff_pts)
std::swap(intensities[1], intensities[2]); std::swap(diff_pts[1], diff_pts[2]);
if(intensities)
std::swap(intensities[1], intensities[2]);
} }
Vec2i bounding_box_min(screen_width - 1, screen_height - 1);
Vec2i bounding_box_max(0, 0);
Vec2i clamp(screen_width - 1, screen_height - 1);
TGAColor color = white;
for(int i = 0; i < 3; i++) { if (wireframe)
for(int j =0; j < 2; j++) { {
bounding_box_min[j] = std::fmax(0, std::fmin(bounding_box_min[j], (int)pts[i][j])); line(pts[0], pts[1], white);
bounding_box_max[j] = std::fmin(clamp[j], std::fmax(bounding_box_max[j], (int)pts[i][j])); line(pts[1], pts[2], white);
} line(pts[2], pts[0], white);
} return;
}
Vec3f P; Vec2i bounding_box_min(screen_width - 1, screen_height - 1);
for(P.x = bounding_box_min.x; P.x <= bounding_box_max.x; P.x++) { Vec2i bounding_box_max(0, 0);
for(P.y = bounding_box_min.y; P.y <= bounding_box_max.y; P.y++) { Vec2i clamp(screen_width - 1, screen_height - 1);
Vec3f bc_coord = barycentric(pts, P); TGAColor color = white;
if(bc_coord.x < 0 || bc_coord.y < 0 || bc_coord.z < 0) continue;
#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]));
bounding_box_max[j] = std::fmin(clamp[j], std::fmax(bounding_box_max[j], (int)pts[i][j]));
}
}
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);
if (bc_coord.x < 0 || bc_coord.y < 0 || bc_coord.z < 0) continue;
float intensity = float intensity =
intensities[0] intensities[0]
+ (intensities[1] - intensities[0]) * bc_coord[1] + (intensities[1] - intensities[0]) * bc_coord[1]
+ (intensities[2] - intensities[0]) * bc_coord[2]; + (intensities[2] - intensities[0]) * bc_coord[2];
// Interpolating Z using the barycentric coordinates // Interpolating Z using the barycentric coordinates
P.z = 0; P.z = 0;
for(int i = 0; i < 3; i++) P.z += pts[i][2] * bc_coord[i]; for (int i = 0; i < 3; i++) P.z += pts[i][2] * bc_coord[i];
// Coloring according to the Z-Buffer // 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 > 0)
{ {
z_buffer[(int)(P.x + P.y * screen_width)] = P.z; z_buffer[(int)(P.x + P.y * screen_width)] = P.z;
// If diff_pts (Diffusemap Points) were passed, then find the // If diff_pts (Diffusemap Points) were passed, then find the
// color of the current pixel // color of the current pixel
if(diff_pts) { if (diff_pts) {
Vec2f diff_pt = Vec2f diff_pt =
diff_pts[0] diff_pts[0]
+ (diff_pts[1] - diff_pts[0]) * bc_coord[1] + (diff_pts[1] - diff_pts[0]) * bc_coord[1]
+ (diff_pts[2] - diff_pts[0]) * bc_coord[2]; + (diff_pts[2] - diff_pts[0]) * bc_coord[2];
color = model->diffuse(diff_pt); color = model->diffuse(diff_pt);
} }
color = color * intensity; color = color * intensity;
set_pixel(P.x, P.y, color_to_int(color)); set_pixel(P.x, P.y, color_to_int(color));
} //char debugStr[200];
} //sprintf_s(debugStr, "%f\n", P.z);
} //OutputDebugString(debugStr);
}
}
}
} }
int color_to_int(TGAColor col) { int color_to_int(TGAColor col) {
return (col[2] << 16) | (col[1] << 8) | col[0]; return (col[2] << 16) | (col[1] << 8) | col[0];
} }
void init_zbuffer() 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.SetMovementSpeed(CAMERA_MOVEMENT_SPEED);
camera.ApplyChanges();
}
void clear_zbuffer()
{ {
z_buffer = new float[screen_width*screen_height];
for (int i = 0; i < screen_width * screen_height; i++) for (int i = 0; i < screen_width * screen_height; i++)
z_buffer[i] = INT_MIN; z_buffer[i] = INT_MIN;
} }
Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) { Matrix ViewPort = Matrix::identity();
Vec3f z = (eye - center).normalize(); Matrix Projection = Matrix::identity();
Vec3f x = cross(up, z).normalize(); Matrix ModelView = Matrix::identity();
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 render() void render()
{ {
Model* model = new Model("african_head.obj"); light_dir = camera.GetForward() * -1;
ViewPort = viewport(0, 0, screen_width, screen_height);
Matrix ViewPort = viewport(screen_width / 8, screen_height / 8, screen_width * 3 / 4, screen_height * 3 / 4); Projection = camera.GetProjectionMatrix();
Matrix Projection = Matrix::identity(); ModelView = camera.GetModelViewMatrix();
Matrix ModelView = lookat(eye, center, Vec3f(0, 1, 0));
Projection[3][2] = -1.f / (eye - center).norm();
model->rotate(Vec3f(0, 0, 90));
model->scale(Vec3f(0.5, 0.5, 0.5));
model->translate(Vec3f(0.5, 0.5, -1));
model->ApplyTransform();
Matrix z = ViewPort * Projection * ModelView * model->Transform; Matrix z = ViewPort * Projection * ModelView * model->Transform;
init_zbuffer(); clear_zbuffer();
#pragma omp parallel for
for (int i = 0; i < model->nfaces(); i++) for (int i = 0; i < model->nfaces(); i++)
{ {
std::vector<int> face = model->face(i); std::vector<int> face = model->face(i);
@@ -211,6 +237,7 @@ void render()
Vec3f world_coords[3]; Vec3f world_coords[3];
Vec2f diffuse_coords[3]; Vec2f diffuse_coords[3];
float intensities[3]; float intensities[3];
bool out = true;
for (int j = 0; j < 3; j++) for (int j = 0; j < 3; j++)
{ {
@@ -218,15 +245,19 @@ void render()
Vec4f v4(v); Vec4f v4(v);
Vec3f coord(z * v4); Vec3f coord(z * v4);
if (coord.x > 0 && coord.x < screen_width
&& coord.y > 0 && coord.y < screen_height)
out = false;
screen_coords[j] = coord; screen_coords[j] = coord;
world_coords[j] = v; world_coords[j] = v;
diffuse_coords[j] = model->uv(i, j); diffuse_coords[j] = model->uv(i, j);
intensities[j] = model->normal(i, j) * light_dir; intensities[j] = model->normal(i, j) * light_dir;
} }
triangle(screen_coords, model, diffuse_coords, intensities, Vec3f(0, 0, 5)); if (out) continue;
}
delete model; triangle(screen_coords, diffuse_coords, model, intensities);
}
} }
+6
View File
@@ -1,7 +1,13 @@
#ifndef RENDERER_HEADER #ifndef RENDERER_HEADER
#define RENDERER_HEADER #define RENDERER_HEADER
#include "tgaimage.h" #include "tgaimage.h"
#include "camera.h"
extern float* z_buffer;
extern Camera camera;
void init_camera();
void render(); void render();
int color_to_int(TGAColor col); int color_to_int(TGAColor col);
#endif #endif
+13 -7
View File
@@ -8,8 +8,6 @@ BITMAPINFO info;
HBITMAP hbm; HBITMAP hbm;
const int BITCOUNT_PER_PIXEL = 24; const int BITCOUNT_PER_PIXEL = 24;
const int title_height = 39; const int title_height = 39;
int screen_width = 0;
int screen_height = 0;
long long bytes_per_row; long long bytes_per_row;
bool screen_changed = false; bool screen_changed = false;
HDC hdc; HDC hdc;
@@ -17,15 +15,13 @@ HDRAWDIB hdd;
HDC bitmap_dc; HDC bitmap_dc;
HGDIOBJ old_obj; HGDIOBJ old_obj;
HWND create_window(int width, int height, HINSTANCE &hInstance) { HWND create_window(HINSTANCE &hInstance) {
HWND hWnd; HWND hWnd;
WNDCLASSEX wnd_class = { 0 }; WNDCLASSEX wnd_class = { 0 };
init_wnd_class(wnd_class, hInstance); init_wnd_class(wnd_class, hInstance);
RegisterClassEx(&wnd_class); RegisterClassEx(&wnd_class);
screen_width = width;
screen_height = height;
create_hwnd(hWnd, hInstance); create_hwnd(hWnd, hInstance);
init(hWnd); init(hWnd);
@@ -107,6 +103,16 @@ void set_pixel(unsigned int x, unsigned int y, unsigned int color) {
screen_changed = true; screen_changed = true;
} }
void Update() { void clear_screen() {
DrawDibDraw(hdd, hdc, 0, 0, screen_width, screen_height, &info.bmiHeader, pixels, 0, 0, screen_width, screen_height, 0); 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;
}
} }
+4 -3
View File
@@ -18,18 +18,19 @@ extern HDC hdc;
extern HDRAWDIB hdd; extern HDRAWDIB hdd;
extern HDC bitmap_dc; extern HDC bitmap_dc;
extern HGDIOBJ old_obj; extern HGDIOBJ old_obj;
extern int screen_width; extern const int screen_width;
extern int screen_height; extern const int screen_height;
void init(HWND &hWnd); void init(HWND &hWnd);
void destroy_window(); 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 create_hwnd(HWND &hwnd, HINSTANCE &hInstance);
void init_wnd_class(WNDCLASSEX &wndClass, HINSTANCE &hInstance); void init_wnd_class(WNDCLASSEX &wndClass, HINSTANCE &hInstance);
void set_pixel(unsigned int x, unsigned int y, unsigned int color); void set_pixel(unsigned int x, unsigned int y, unsigned int color);
void Update(); void Update();
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void CALLBACK FixedUpdate(HWND hwnd, UINT message, UINT uInt, DWORD dWord);
#endif #endif