-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
87 lines (72 loc) · 2.5 KB
/
main.cpp
File metadata and controls
87 lines (72 loc) · 2.5 KB
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
#include <vector>
#include <cstdlib>
#include <limits>
#include <iostream>
#include "tgaimage.h"
#include "model.h"
#include "geometry.h"
#include "our_gl.h"
Model *model = NULL;
const int width = 800;
const int height = 800;
Vec3f eye(0,0,2);
Vec3f center(0,0,0);
Vec3f up(0,1,0);
struct ZShader : public IShader {
mat<4,3,float> varying_tri;
Vec4f vertex(int iface, int nthvert) override {
Vec4f gl_Vertex = Projection*ModelView*embed<4>(model->vert(iface, nthvert));
varying_tri.set_col(nthvert, gl_Vertex);
return gl_Vertex;
}
bool fragment(Vec3f gl_FragCoord, Vec3f bar, TGAColor &color) override {
color = TGAColor(0, 0, 0);
return false;
}
};
float max_elevation_angle(float *zbuffer, Vec2f p, Vec2f dir) {
float maxangle = 0;
for (float t=0.; t<1000.; t+=1.) {
Vec2f cur = p + dir*t;
if (cur.x>=width || cur.y>=height || cur.x<0 || cur.y<0) return maxangle;
float distance = (p-cur).norm();
if (distance < 1.f) continue;
float elevation = zbuffer[int(cur.x)+int(cur.y)*width]-zbuffer[int(p.x)+int(p.y)*width];
maxangle = std::max(maxangle, atanf(elevation/distance));
}
return maxangle;
}
int main(int argc, char** argv) {
model = new Model("../object/diablo3_pose/diablo3_pose.obj");
// model = new Model("../object/statue/b_statue.obj");
float *zbuffer = new float[width*height];
for (int i=width*height; i--; zbuffer[i] = -std::numeric_limits<float>::max());
TGAImage frame(width, height, TGAImage::RGB);
lookat(eye, center, up);
viewport(width/8, height/8, width*3/4, height*3/4);
projection(-1.f/(eye-center).norm());
ZShader zshader;
for (int i=0; i<model->nfaces(); i++) {
for (int j=0; j<3; j++) {
zshader.vertex(i, j);
}
triangle(zshader.varying_tri, zshader, frame, zbuffer);
}
for (int x=0; x<width; x++) {
for (int y=0; y<height; y++) {
if (zbuffer[x+y*width] < -1e5) continue;
float total = 0;
for (float a=0; a<M_PI*2-1e-4; a += M_PI/4) {
total += M_PI/2 - max_elevation_angle(zbuffer, Vec2f(x, y), Vec2f(cos(a), sin(a)));
}
total /= (M_PI/2)*8;
total = pow(total, 100.f);
frame.set(x, y, TGAColor(total*255, total*255, total*255));
}
}
frame.flip_vertically();
frame.write_tga_file("framebuffer.tga");
delete [] zbuffer;
delete model;
return 0;
}