-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathmain.cpp
More file actions
185 lines (142 loc) · 5.38 KB
/
main.cpp
File metadata and controls
185 lines (142 loc) · 5.38 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <TinyEngine/TinyEngine>
#include <TinyEngine/image>
#include <TinyEngine/color>
#include <TinyEngine/math>
#include <TinyEngine/camera>
#define PI 3.14159265f
#include "model.h"
int main( int argc, char* args[] ) {
Tiny::view.lineWidth = 1.0f;
Tiny::window("Procedural Tree", 1200, 800);
cam::look = glm::vec3(0, 100, 0);
cam::far = 2000.0f;
cam::roty = 25.0f;
cam::zoomrate = 10.0f;
cam::init(600, cam::PROJ);
bool paused = false;
bool autorotate = true;
Tiny::event.handler = [&](){
(cam::handler)();
if(!Tiny::event.press.empty()){
if(Tiny::event.press.back() == SDLK_p)
paused = !paused;
else if(Tiny::event.press.back() == SDLK_a)
autorotate = !autorotate;
else if(Tiny::event.press.back() == SDLK_r){
Branch* newroot = new Branch(root, true);
delete(root);
root = newroot;
}
}
};
Tiny::view.interface = interfaceFunc;
root = new Branch({0.6, 0.45, 2.5}); //Create Root
Buffer positions, normals, colors;
Buffer indices;
construct(positions, normals, colors, indices);
Model treemesh({"in_Position", "in_Normal", "in_Color"});
treemesh.bind<glm::vec3>("in_Position", &positions);
treemesh.bind<glm::vec3>("in_Normal", &normals);
treemesh.bind<glm::vec4>("in_Color", &colors);
treemesh.index(&indices);
Square3D flat; //Geometry for Particle System
std::vector<glm::mat4> leaves;
addLeaves(leaves, true); //Generate the model matrices
Buffer models(leaves);
Instance particle(&flat); //Make Particle System
particle.bind<glm::mat4>("in_Model", &models); //Add Matrices
Texture tex(image::load("leaf.png"));
Shader particleShader({"shader/particle.vs", "shader/particle.fs"}, {"in_Quad", "in_Tex", "in_Model"});
Shader defaultShader({"shader/default.vs", "shader/default.fs"}, {"in_Position", "in_Normal"});
Shader depth({"shader/depth.vs", "shader/depth.fs"}, {"in_Position"});
Shader particledepth({"shader/particledepth.vs", "shader/particledepth.fs"}, {"in_Quad", "in_Tex", "in_Model"});
Target shadow(1600, 1600);
Texture depthtex(1600, 1600, {GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE});
shadow.bind(depthtex, GL_DEPTH_ATTACHMENT);
Square3D floor;
floor.model = glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1,0,0));
floor.model = glm::scale(floor.model, glm::vec3(1000));
Tiny::view.pipeline = [&](){ //Setup Drawing Pipeline
shadow.target();
if(drawshadow){
depth.use();
depth.uniform("dvp", lproj*lview);
defaultShader.uniform("model", treemesh.model);
treemesh.render(GL_TRIANGLES);
}
if(leafshadow){
particledepth.use();
particledepth.uniform("dvp", lproj*lview);
particledepth.texture("spriteTexture", tex);
addLeaves(leaves, false); //Generate the model matrices
models.fill<glm::mat4>(leaves);
particle.render(GL_TRIANGLE_STRIP); //Render Particle System
}
//Prepare Render Target
Tiny::view.target(glm::vec3(backcolor[0], backcolor[1], backcolor[2]));
if(drawwire || drawtree){
defaultShader.use();
defaultShader.uniform("model", treemesh.model);
defaultShader.uniform("projectionCamera", cam::vp);
defaultShader.uniform("lightcolor", lightcolor);
defaultShader.uniform("lookDir", cam::look - cam::pos);
defaultShader.uniform("lightDir", lightpos);
defaultShader.uniform("drawshadow", drawshadow);
if(drawshadow){
defaultShader.uniform("dbvp", bias*lproj*lview);
defaultShader.texture("shadowMap", depthtex);
defaultShader.uniform("light", lightpos);
}
defaultShader.uniform("drawfloor", true);
defaultShader.uniform("drawcolor", glm::vec4(backcolor[0],backcolor[1],backcolor[2],1));
defaultShader.uniform("model", floor.model);
floor.render();
defaultShader.uniform("drawfloor", false);
defaultShader.uniform("model", treemesh.model);
if(drawtree){
defaultShader.uniform("drawcolor", glm::vec4(treecolor[0], treecolor[1], treecolor[2], treeopacity));
defaultShader.uniform("wireframe", false);
treemesh.render(GL_TRIANGLES);
}
if(drawwire){
defaultShader.uniform("drawcolor", glm::vec4(wirecolor[0], wirecolor[1], wirecolor[2], 1.0));
defaultShader.uniform("wireframe", true);
treemesh.render(GL_LINES);
}
}
if(drawleaf){
particleShader.use();
particleShader.texture("spriteTexture", tex);
particleShader.uniform("projectionCamera", cam::vp);
particleShader.uniform("ff", glm::rotate(glm::mat4(1.0f), glm::radians(180-cam::rot), glm::vec3(0,1,0)));
particleShader.uniform("leafcolor", glm::vec4(leafcolor[0], leafcolor[1], leafcolor[2], leafopacity));
particleShader.uniform("lightcolor", lightcolor);
particleShader.uniform("selfshadow", selfshadow);
if(selfshadow){
particleShader.uniform("dbvp", bias*lproj*lview);
particleShader.texture("shadowMap", depthtex);
particleShader.uniform("light", lightpos);
}
particleShader.uniform("lookDir", cam::look - cam::pos);
addLeaves(leaves, true);
models.fill<glm::mat4>(leaves);
particle.SIZE = leaves.size();
particle.render(GL_TRIANGLE_STRIP); //Render Particle System
}
};
//Loop over Stuff
Tiny::loop([&](){ /* ... */
if(autorotate)
cam::pan(0.5f);
if(!paused)
root->grow(growthrate);
//Update Rendering Structures
construct(positions, normals, colors, indices);
treemesh.index(&indices);
models.fill<glm::mat4>(leaves);
});
//Get rid of this thing!
delete root;
Tiny::quit();
return 0;
}