-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
146 lines (126 loc) · 4.23 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "geometry.h"
#include "log.h"
#include <SDL2/SDL_keycode.h>
#include <stdint.h>
#define PERSPECTIVE_PROJ_MATRIX
#include "camera.h"
#include "cg.h"
#include "cow.h"
#include "geometry.h"
#include "light.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_video.h>
#include <assert.h>
#define WINDOW_HEIGHT 900
#define WINDOW_WIDTH 900
const uint32_t numTris = 3156;
int main(void) {
Light LIGHT = {(Vec3f){0, 1, 0}, (Vec3f){1, 1, 1}, 1};
Camera c;
Vec3f camera_start = {0, 20, 80.400412};
Vec3f lookat_point = {0, 0, 0};
init_camera(&c, camera_start, lookat_point);
float aperture_width = 25, aperture_height = 25, focal_len = 100;
uint32_t image_width = WINDOW_WIDTH, image_height = WINDOW_HEIGHT;
Matrix44f pers_proj = {0};
setPerspectiveProj(&pers_proj, 25, 0.1, 100);
uint32_t image_buffer[image_width * image_height];
float z_buffer[image_width * image_height];
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;
SDL_Window *window =
SDL_CreateWindow("cg", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WINDOW_HEIGHT, WINDOW_WIDTH, 0);
assert(window);
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
assert(renderer);
SDL_Texture *buf =
SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR32,
SDL_TEXTUREACCESS_STREAMING, image_width, image_height);
bool quit = false;
float alpha = 0.f, beta = 0.f, gamma = 0.f;
float vel = 0.5;
Uint64 start = SDL_GetTicks64();
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
Uint64 delta = SDL_GetTicks64() - start;
switch (event.type) {
case SDL_MOUSEBUTTONUP: {
c.moveType = NONE;
} break;
case SDL_MOUSEBUTTONDOWN: {
if (event.key.keysym.mod & KMOD_CTRL) {
c.moveType = event.button.button == SDL_BUTTON_LEFT ? TUMBLE
: event.button.button == SDL_BUTTON_MIDDLE ? TRACK
: event.button.button == SDL_BUTTON_RIGHT ? DOLLY
: NONE;
}
} break;
case SDL_MOUSEMOTION: {
camera_move(&c, delta, event.motion.xrel, event.motion.yrel);
} break;
case SDL_MOUSEWHEEL: {
camera_move(&c, delta, event.wheel.x, event.wheel.preciseY);
} break;
case SDL_KEYDOWN: {
switch (event.key.keysym.sym) {
default:
break;
}
break;
}
case SDL_QUIT:
quit = true;
break;
default:
break;
}
}
memset(image_buffer, 0, sizeof(uint32_t) * image_width * image_height);
for (int i = 0; i < image_height; ++i) {
for (int j = 0; j < image_width; ++j) {
z_buffer[i * image_width + j] = FLT_MAX;
}
}
// Matrix44f rot = rotationMatrix(alpha, beta, gamma);
Matrix44f worldToCamera = inverse(c.cam_to_world);
Vec3f viewDir = normalize(vec_minus(c.pos, lookat_point));
for (uint32_t i = 0; i < numTris; ++i) {
const Vec3f v0World = vertices[nvertices[i * 3]];
const Vec3f v1World = vertices[nvertices[i * 3 + 1]];
const Vec3f v2World = vertices[nvertices[i * 3 + 2]];
ccanva_render(image_buffer, z_buffer, viewDir, v0World, v1World, v2World,
worldToCamera, pers_proj, aperture_width, aperture_height,
focal_len, image_width, image_height, &LIGHT);
}
void *pixels;
int pitch;
int ret = SDL_LockTexture(buf, NULL, &pixels, &pitch);
assert(ret == 0);
for (int i = 0; i < image_height; ++i) {
memcpy(pixels + i * pitch, image_buffer + i * image_width, pitch);
}
SDL_UnlockTexture(buf);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, buf, NULL,
&(SDL_Rect){0, 0, image_width, image_height});
SDL_RenderPresent(renderer);
Uint64 end = SDL_GetTicks64();
if (end - start < 33) {
SDL_Delay(33 - (end - start));
}
start = end;
}
// SDL_FreeSurface(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}