-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
169 lines (119 loc) · 4.33 KB
/
main.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// main.cpp
// SDL_3D
//
// Created by Matt Parsons on 22/07/2018.
// Copyright © 2018 Matt Parsons. All rights reserved.
//
#include <stdio.h>
// SDL2 Headers
#include <SDL2/SDL.h>
// SDL_3D Header
#include "SDL_3D.hpp"
int main(int argc, const char * argv[]) {
// Initialize SDL's Video subsystem
if (SDL_Init(SDL_INIT_EVERYTHING) < 0){
printf("Failed to init SDL\n");
return -1;
}
// Create our window
SDL_Window* window = SDL_CreateWindow("SDL 3D", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
320, 200, SDL_WINDOW_RESIZABLE);
Rasterizer raster;
int width=0;
int height=0;
SDL_GetWindowSize(window, &width, &height);
raster.setDisplay(SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC),width, height);
raster.focalLength = 0.2;
raster.backfaceCull= 0;
raster.camera.locate(0, 0, 1);
//Object test
TDMesh vulcanMesh;
vulcanMesh.loadPly("/Users/matt/Library/Mobile\ Documents/com\~apple\~CloudDocs/vulc1.ply");
TDObject craft(&vulcanMesh);
craft.vz=-0.1;
craft.locate(-1, 0, -4);
TDMesh deloreanMesh;
deloreanMesh.loadPly("/Users/matt/Library/Mobile\ Documents/com\~apple\~CloudDocs/delor1.ply");
TDObject car(&deloreanMesh);
car.vz=-0.1;
car.locate(1, 0, -1);
float x=-1;
float y=-0.5;
float z=0;
TDQuad plane;
//Quads test
for(int i=0;i<100*100;++i){
// plane[i].a.x=
}
int loop=true;
while (loop){
SDL_Event event;
while (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT){loop = false;}
if (event.type == SDL_WINDOWEVENT) {
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
SDL_GetWindowSize(window, &width, &height);
raster.adjustDisplay(width, height);
break;
default:
break;
}
}
if (event.type == SDL_KEYDOWN){
switch (event.key.keysym.sym){
case SDLK_ESCAPE:
loop = false;
break;
case SDLK_r:
// Cover with red
raster.clearCol.r=1;
raster.clearCol.g=0;
raster.clearCol.b=0;
break;
case SDLK_g:
// Cover with green
raster.clearCol.r=0;
raster.clearCol.g=1;
raster.clearCol.b=0;
break;
case SDLK_b:
// Cover with blue
raster.clearCol.r=0;
raster.clearCol.g=0;
raster.clearCol.b=1;
break;
case SDLK_SPACE:
// Cover with black
raster.clearCol.r=0;
raster.clearCol.g=0;
raster.clearCol.b=0;
break;
default:
raster.clearCol.r=(float)rand()/RAND_MAX;
raster.clearCol.g=(float)rand()/RAND_MAX;
raster.clearCol.b=(float)rand()/RAND_MAX;
break;
}
}
}
raster.clearDisplay();
//Draw gfx
if(craft.z()<-64 || craft.z()>0){
craft.vz=-craft.vz;
}
craft.move(0, 0, craft.vz);
craft.rotate(0.01,0.01,0.01);
raster.drawObject(&craft);
if(car.z()<-32 || car.z()>0){
car.vz=-car.vz;
}
car.move(0, 0, car.vz);
car.rotate(0.01,0.01,0.01);
raster.drawObject(&car);
// Swap our back buffer to the front
raster.updateDisplay();
}
return 0;
}