-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
232 lines (201 loc) · 6.64 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <cstdlib>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <string>
#include <iostream>
#include <vector>
#include "doodle.hh"
#include "include/Verlet.h"
#include "include/VerletEdge.h"
#include "include/Mesh.h"
#include "include/VerletBuilder.h"
// #include "LuaExpose.h"
#include "ModelImport.h"
//#include "assimplib/include/assimp/Importer.hpp"
#define BUNNY
#undef BUNNY
// TODO convert for iterators into boost foreach?
//
SDL_Window *screen;
SDL_Renderer *renderer;
Mesh *allPoints;
// LUA EXPOSURE
// lua_State *LuaState;
/*
int initLua()
{
LuaState = luaL_newstate();
luaL_openlibs(LuaState);
lua_pushcfunction(LuaState, latticeWrapper);
lua_setglobal(LuaState, "mkLattice");
if (luaL_loadfile(LuaState, "scripts/init.lua")) {
std::cerr << "Something went wrong loading the chunk (syntax error?)" << std::endl;
std::cerr << lua_tostring(LuaState, -1) << std::endl;
lua_pop(LuaState,1);
}
printf("lua initialized properly\n");
return 0;
}*/
// static int latticeWrapper(lua_State *L)
// {
// return 1;
// }
//
int init()
{
// initialize SDL video
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
screen = SDL_CreateWindow("Ooo Stretchy", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1080, 720, SDL_WINDOW_OPENGL);
if ( !screen ) {
printf("Unable to create window: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(screen, -1, 0);
if ( !renderer ) {
printf("Unable to create renderer: %s\n", SDL_GetError());
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
//printf("goody\n");
// set up test verlets
allPoints = new Mesh; //mkLattice(10,10,10,10,4);// mkLattice(10, 20, 10, 10, 4); //(x,y,z,l,e)
Mesh *temp;
#ifndef BUNNY
//temp = mkLattice(2,2,1000,10,2);
//allPoints->mergeInto(temp);
//delete temp;
//printf("%d %d\n", temp->edges.size(), temp->verlets.size());
temp = mkLattice(2,500,2,5,3);
allPoints->mergeInto(temp);
delete temp;
// temp = mkLattice(60,60,2,10,4);
// allPoints->mergeInto(temp);
// delete temp;
#else
temp = ImportObj("bunny.obj");
printf("managed to import\n");
allPoints->mergeInto(temp);
delete temp;
#endif
//allPoints->verlets.push_back(new Verlet(200, 100));
//allPoints->verlets.push_back(new Verlet(100, 200));
//verlets[0]->held = true;
//allPoints->edges.push_back(new VerletEdge(allPoints->verlets[0], allPoints->verlets[1], 100, 50));
//printf("hi\n");
return 0;
}
void runLoop()
{
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
if (event.key.keysym.sym == SDLK_SPACE)
for (std::vector<VerletEdge*>::iterator i = allPoints->edges.begin();
i != allPoints->edges.end(); ++i)
(*i)->freeze();
else if (event.key.keysym.sym == SDLK_z)
for (std::vector<VerletEdge*>::iterator i = allPoints->edges.begin();
i != allPoints->edges.end(); ++i)
(*i)->stretch(10);
else if (event.key.keysym.sym == SDLK_x)
for (std::vector<VerletEdge*>::iterator i = allPoints->edges.begin();
i != allPoints->edges.end(); ++i)
(*i)->stretch(-10);
// exit if ESCAPE is pressed
else if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
case SDL_MOUSEMOTION:
// when mouse down and moved, grabbed verlets get moved
{
for (std::vector<Verlet*>::iterator i = allPoints->verlets.begin();
i != allPoints->verlets.end(); ++i)
(*i)->drag(event.motion.x, event.motion.y);
}
break;
case SDL_MOUSEBUTTONDOWN:
{
for (std::vector<Verlet*>::iterator i = allPoints->verlets.begin();
i != allPoints->verlets.end(); ++i)
(*i)->grab(event.button.x, event.button.y);
}
break;
case SDL_MOUSEBUTTONUP:
{
for (std::vector<Verlet*>::iterator i = allPoints->verlets.begin();
i != allPoints->verlets.end(); ++i)
(*i)->release();
}
break;
default:;
} // end switch
} // end of message processing
// physics loops
allPoints->sim();
allPoints->sim();
//printf("physics fine\n");
// DRAWING STARTS HERE
// clear screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
int stress;
//drawLine(screen, 50, 50, 100, 100, rgb2Px(255, 0, 255));
for (std::vector<VerletEdge*>::iterator i = allPoints->edges.begin();
i != allPoints->edges.end(); ++i)
{
//printf("doodle");
stress = (*i)->getStress() * 255;
if (stress > 255) stress = 510 - stress;
SDL_SetRenderDrawColor(renderer, 255, 255-stress, 255-stress, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, (*i)->m_a->m_x, (*i)->m_a->m_y, (*i)->m_b->m_x, (*i)->m_b->m_y);
}
/*
for (std::vector<Verlet*>::iterator i = allPoints->verlets.begin();
i != allPoints->verlets.end(); ++i)
{
//printf("doodle");
drawLine(screen, (*i)->m_x, (*i)->m_y, (*i)->m_x+2, (*i)->m_y, rgb2Px(240, 156, 0));
}*/
SDL_RenderPresent(renderer);
} // end main loop
}
void cleanUp()
{
// free stuff
allPoints->wipe();
}
int main ( int argc, char** argv )
{
init();
//initLua();
printf("about to run\n");
runLoop();
cleanUp();
// all is well ;)
printf("Exited cleanly\n");
return 0;
}