-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (107 loc) · 4.76 KB
/
main.cpp
File metadata and controls
120 lines (107 loc) · 4.76 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
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
#include <iostream>
#include <d3d12.h>
#include "SimpleMath.h"
#include "tgaimage.h"
#include "model.h"
#include "my_gl.h"
using namespace DirectX::SimpleMath;
const int width = 800;
const int height = 800;
const int depth = 1000;
const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);
Vector3 light_dir = Vector3(0.5,0,0.3);
struct Shader : public IShader {
Vector2* uv_coords; // triangle uv coordinates, written by the vertex shader, read by the fragment shader
Vector4* ss_coords; // triangle coordinates (screen space), written by VS, read by FS
float* intensity;
Shader() {
uv_coords = new Vector2[3];
ss_coords = new Vector4[3];
intensity = new float[3];
}
~Shader() {
delete uv_coords;
delete ss_coords;
delete intensity;
}
Vector4 vertex (int iface, int nthvert,Model* _model,Matrix mat) {
Vector3 vert = _model->vert(_model->face(iface).verts[nthvert]);
Vector4 hom_vert = Vector4(vert.x, vert.y, vert.z, 1.0f);
Vector4 gl_Vertex = Vector4::Transform(hom_vert,WorldMatrix*mat);
ss_coords[nthvert] = gl_Vertex;
uv_coords[nthvert] = _model->uv_coords(_model->face(iface).uv_coords[nthvert]);
intensity[nthvert] = std::max(0.0f,Vector3::Transform(_model->normal(_model->face(iface).normals[nthvert]),WorldMatrix.Transpose()).Dot(light_dir));
return gl_Vertex;
}
bool fragment(Vector3 bar, TGAColor &fragcolor, Model* _model, float transp) {
Vector3 intvec = Vector3(intensity[0],intensity[1],intensity[2]);
float diff1 = intvec.Dot(bar)/2.;
Vector3 uvXs = Vector3(uv_coords[0].x,uv_coords[1].x,uv_coords[2].x);
Vector3 uvYs = Vector3(uv_coords[0].y,uv_coords[1].y,uv_coords[2].y);
Vector2 uv = Vector2(uvXs.Dot(bar),uvYs.Dot(bar));
Vector3 norm = _model->normal1(uv);
norm.Normalize();
// reflected light
Vector3 r = (norm*(norm.Dot(light_dir)*2.f) - light_dir);
r.Normalize();
float spec = _model->DoesHaveSpecMap? pow(std::max(r.z, 0.0f), _model->specular(uv)) : 1.0f;
float diff2 = std::max(0.0f,norm.Dot(light_dir));
float diff = _model->DoesHaveNMap? diff2 : diff1;
TGAColor c = _model->diffuse_color(uv);
// std::cout << cur_intensity << std::endl;
TGAColor newcolor;
for (int i=0; i<3; i++) newcolor[i] = std::min<float>(10+ c[i]*(diff*.5 + spec*.3)*2., 255);
fragcolor = fragcolor*(1-transp) + newcolor*transp;
return false;
}
};
int main()
{
light_dir.Normalize();
Camera* cam = new Camera(Vector3(0,0.40,4),Quaternion(0.05,0 ,0,1));
// view and projection DX matrix
Matrix Projection_DX = DirectX::XMMatrixPerspectiveFovLH(DirectX::XM_PI/2.5,1,0.1f,10.f);
Matrix ViewMatrix_DX = DirectX::XMMatrixLookAtRH(cam->getPosition(),cam->getPosition()+cam->getForward(), cam->getUp());
// making view, projection and viewport matrix
lookat(cam->getPosition(),cam->getPosition()+cam->getForward(),cam->getUp());
viewport(0,0,width,height);
float coeff = -1.f/(-cam->getForward()).Length();
projection(coeff);
Matrix mat0 = ViewMatrix.Transpose()*Projection.Transpose()*ViewPort;
printmatrix(mat0);
Matrix mat1 = ViewMatrix_DX*Projection_DX*ViewPort;
std::vector<Model*> Models = {
new Model( "../obj/african_head_eye_inner.obj",Vector3(0,0,0),Quaternion(0,-3.14/9,0,1),Vector3(1,1,1)*2,1),
new Model( "../obj/african_head.obj",Vector3(0,0,0),Quaternion(0.0,-3.14/9,0,1),Vector3(1,1,1)*2,1),
//new Model( "../obj/Glasses.obj",Vector3(0,0,1),Quaternion(0,0,0,1),Vector3(1,1,1)/2,1),
//new Model( "../obj/wolf.obj",Vector3(1,-0.5,-0.5),Quaternion(0,0,0,1),Vector3(0.1,0.1,0.1)*3,1),
};
auto* z_buffer = new float[width*height];
for (int i=0; i<width*height; i++) {
z_buffer[i] = std::numeric_limits<int>::min();
}
TGAImage image2(width, height, TGAImage::RGB);
// rendering polygons
for (int m = 0; m < Models.size(); m++) {
world(Models[m]->scale_,Models[m]->rotation_,Models[m]->position_);
std::cout << "MODEL CREATED!\n";
Shader shader;
std::cout << "MATRIX: \n";
printmatrix(mat0);
for (int i=0; i<Models[m]->nfaces(); i++) {
for (int j = 0; j < 3; j++) {
shader.vertex(i, j, Models[m],mat1);
}
triangle1(shader.ss_coords, shader, image2, z_buffer, Models[m], Models[m]->transparency_);
}
std::cout << "model " << m+1 << " rendered successfully\n";
}
for (int i = 0; i < Models.size(); ++i) {
delete Models[i];
}
delete cam;
image2.flip_horizontally();
image2.write_tga_file("../output.tga");
return 0;
}