main.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. --------------------------------------------------
  3. James William Fletcher (james@voxdsp.com)
  4. December 2021
  5. --------------------------------------------------
  6. C & SDL / OpenGL ES2 / GLSL ES
  7. Colour Converter: https://www.easyrgb.com
  8. */
  9. #include <time.h>
  10. #include <SDL2/SDL.h>
  11. #include <SDL2/SDL_opengles2.h>
  12. #include "esAux2.h"
  13. #include "assets/tunnel.h"
  14. #include "assets/dead.h"
  15. #include "assets/three.h"
  16. #include "assets/two.h"
  17. #include "assets/one.h"
  18. #include "assets/lslick.h"
  19. #include "assets/mslick.h"
  20. #include "assets/rslick.h"
  21. #include "assets/lgem.h"
  22. #include "assets/mgem.h"
  23. #include "assets/rgem.h"
  24. #include "assets/lboost.h"
  25. #include "assets/mboost.h"
  26. #include "assets/rboost.h"
  27. #include "assets/lboarder.h"
  28. #include "assets/mboarder.h"
  29. #include "assets/rboarder.h"
  30. #include "assets/res.h"
  31. #define uint GLushort
  32. #define sint GLshort
  33. #define f32 GLfloat
  34. //*************************************
  35. // globals
  36. //*************************************
  37. char appTitle[] = "Snowboarder";
  38. SDL_Window* wnd;
  39. SDL_GLContext glc;
  40. SDL_Surface* s_icon = NULL;
  41. Uint32 winw = 1024, winh = 768;
  42. f32 aspect;
  43. f32 t = 0.f;
  44. // render state id's
  45. GLint projection_id;
  46. GLint modelview_id;
  47. GLint position_id;
  48. GLint lightpos_id;
  49. GLint color_id;
  50. GLint opacity_id;
  51. GLint normal_id;
  52. // render state matrices
  53. mat projection;
  54. mat view;
  55. mat model;
  56. mat modelview;
  57. // render state inputs
  58. vec lightpos = {0.f, 0.f, 0.f};
  59. // models
  60. uint bindstate = 0;
  61. ESModel mdlTunnel;
  62. ESModel mdlDead;
  63. ESModel mdlThree;
  64. ESModel mdlTwo;
  65. ESModel mdlOne;
  66. ESModel mdlLslick;
  67. ESModel mdlMslick;
  68. ESModel mdlRslick;
  69. ESModel mdlLgem;
  70. ESModel mdlMgem;
  71. ESModel mdlRgem;
  72. ESModel mdlLboost;
  73. ESModel mdlMboost;
  74. ESModel mdlRboost;
  75. ESModel mdlLboarder;
  76. ESModel mdlMboarder;
  77. ESModel mdlRboarder;
  78. // simulation / game vars
  79. f32 dt = 0;
  80. double rt = 0;
  81. uint state = 3;
  82. f32 camdist = -5.0f;
  83. uint keystate[2] = {0};
  84. uint player_x = 1;
  85. uint score = 0;
  86. #define ARRAY_MAX 6
  87. typedef struct
  88. {
  89. f32 z;
  90. uint x;
  91. uint s;
  92. } gi;
  93. uint max_slicks = 0;
  94. uint max_gems = 0;
  95. uint max_boosts = 0;
  96. gi array_slick[ARRAY_MAX];
  97. gi array_gem[ARRAY_MAX];
  98. gi array_boost[ARRAY_MAX];
  99. #define NEWGAME_SEED 1337
  100. #define MINSPEED -9.f
  101. #define MAXSPEED -32.f
  102. #define DRAGRATE 3.3f
  103. f32 pz = 0.f;
  104. f32 ps = MINSPEED;
  105. f32 pr = 0.f;
  106. f32 po = 1.0f;
  107. vec c1 = {1.000,0.325,0.251};
  108. vec c2 = {0.404,1.000,0.663};
  109. vec c3 = {0.827,0.400,1.000};
  110. //*************************************
  111. // utility functions
  112. //*************************************
  113. void timestamp(char* ts)
  114. {
  115. const time_t tt = time(0);
  116. strftime(ts, 16, "%H:%M:%S", localtime(&tt));
  117. }
  118. SDL_Surface* surfaceFromData(const Uint32* data, Uint32 w, Uint32 h)
  119. {
  120. SDL_Surface* s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SDL_PIXELFORMAT_RGBA32);
  121. memcpy(s->pixels, data, s->pitch*h);
  122. return s;
  123. }
  124. //*************************************
  125. // render functions
  126. //*************************************
  127. void rTunnel()
  128. {
  129. bindstate = 0;
  130. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &view.m[0][0]);
  131. glUniform1f(opacity_id, 1.0f);
  132. glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.vid);
  133. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  134. glEnableVertexAttribArray(position_id);
  135. glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.nid);
  136. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  137. glEnableVertexAttribArray(normal_id);
  138. glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.cid);
  139. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  140. glEnableVertexAttribArray(color_id);
  141. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlTunnel.iid);
  142. glDrawElements(GL_TRIANGLES, tunnel_numind, GL_UNSIGNED_SHORT, 0);
  143. }
  144. void rDead(f32 z)
  145. {
  146. bindstate = 0;
  147. mIdent(&model);
  148. mTranslate(&model, 0.f, 0.f, z);
  149. mMul(&modelview, &model, &view);
  150. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
  151. glUniform1f(opacity_id, 1.0f);
  152. glBindBuffer(GL_ARRAY_BUFFER, mdlDead.vid);
  153. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  154. glEnableVertexAttribArray(position_id);
  155. glBindBuffer(GL_ARRAY_BUFFER, mdlDead.nid);
  156. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  157. glEnableVertexAttribArray(normal_id);
  158. glBindBuffer(GL_ARRAY_BUFFER, mdlDead.cid);
  159. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  160. glEnableVertexAttribArray(color_id);
  161. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlDead.iid);
  162. glDrawElements(GL_TRIANGLES, dead_numind, GL_UNSIGNED_SHORT, 0);
  163. }
  164. void rCountDown(f32 z, uint c)
  165. {
  166. bindstate = 0;
  167. mIdent(&model);
  168. mTranslate(&model, 0.f, 0.f, z);
  169. mMul(&modelview, &model, &view);
  170. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
  171. glUniform1f(opacity_id, 1.0f);
  172. if(c == 3)
  173. {
  174. glBindBuffer(GL_ARRAY_BUFFER, mdlThree.vid);
  175. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  176. glEnableVertexAttribArray(position_id);
  177. glBindBuffer(GL_ARRAY_BUFFER, mdlThree.nid);
  178. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  179. glEnableVertexAttribArray(normal_id);
  180. glBindBuffer(GL_ARRAY_BUFFER, mdlThree.cid);
  181. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  182. glEnableVertexAttribArray(color_id);
  183. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlThree.iid);
  184. glDrawElements(GL_TRIANGLES, three_numind, GL_UNSIGNED_SHORT, 0);
  185. }
  186. else if(c == 2)
  187. {
  188. glBindBuffer(GL_ARRAY_BUFFER, mdlTwo.vid);
  189. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  190. glEnableVertexAttribArray(position_id);
  191. glBindBuffer(GL_ARRAY_BUFFER, mdlTwo.nid);
  192. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  193. glEnableVertexAttribArray(normal_id);
  194. glBindBuffer(GL_ARRAY_BUFFER, mdlTwo.cid);
  195. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  196. glEnableVertexAttribArray(color_id);
  197. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlTwo.iid);
  198. glDrawElements(GL_TRIANGLES, two_numind, GL_UNSIGNED_SHORT, 0);
  199. }
  200. else if(c == 1)
  201. {
  202. glBindBuffer(GL_ARRAY_BUFFER, mdlOne.vid);
  203. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  204. glEnableVertexAttribArray(position_id);
  205. glBindBuffer(GL_ARRAY_BUFFER, mdlOne.nid);
  206. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  207. glEnableVertexAttribArray(normal_id);
  208. glBindBuffer(GL_ARRAY_BUFFER, mdlOne.cid);
  209. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  210. glEnableVertexAttribArray(color_id);
  211. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlOne.iid);
  212. glDrawElements(GL_TRIANGLES, one_numind, GL_UNSIGNED_SHORT, 0);
  213. }
  214. }
  215. void rSlick(f32 z, uint x)
  216. {
  217. mIdent(&model);
  218. mTranslate(&model, 0.f, 0.f, z);
  219. mMul(&modelview, &model, &view);
  220. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
  221. glUniform1f(opacity_id, 1.0f);
  222. if(x == 0)
  223. {
  224. if(bindstate != 1)
  225. {
  226. glBindBuffer(GL_ARRAY_BUFFER, mdlLslick.vid);
  227. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  228. glEnableVertexAttribArray(position_id);
  229. glBindBuffer(GL_ARRAY_BUFFER, mdlLslick.nid);
  230. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  231. glEnableVertexAttribArray(normal_id);
  232. glBindBuffer(GL_ARRAY_BUFFER, mdlLslick.cid);
  233. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  234. glEnableVertexAttribArray(color_id);
  235. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLslick.iid);
  236. bindstate = 1;
  237. }
  238. glDrawElements(GL_TRIANGLES, lslick_numind, GL_UNSIGNED_SHORT, 0);
  239. }
  240. else if(x == 1)
  241. {
  242. if(bindstate != 2)
  243. {
  244. glBindBuffer(GL_ARRAY_BUFFER, mdlMslick.vid);
  245. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  246. glEnableVertexAttribArray(position_id);
  247. glBindBuffer(GL_ARRAY_BUFFER, mdlMslick.nid);
  248. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  249. glEnableVertexAttribArray(normal_id);
  250. glBindBuffer(GL_ARRAY_BUFFER, mdlMslick.cid);
  251. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  252. glEnableVertexAttribArray(color_id);
  253. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMslick.iid);
  254. bindstate = 2;
  255. }
  256. glDrawElements(GL_TRIANGLES, mslick_numind, GL_UNSIGNED_SHORT, 0);
  257. }
  258. else if(x == 2)
  259. {
  260. if(bindstate != 3)
  261. {
  262. glBindBuffer(GL_ARRAY_BUFFER, mdlRslick.vid);
  263. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  264. glEnableVertexAttribArray(position_id);
  265. glBindBuffer(GL_ARRAY_BUFFER, mdlRslick.nid);
  266. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  267. glEnableVertexAttribArray(normal_id);
  268. glBindBuffer(GL_ARRAY_BUFFER, mdlRslick.cid);
  269. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  270. glEnableVertexAttribArray(color_id);
  271. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRslick.iid);
  272. bindstate = 3;
  273. }
  274. glDrawElements(GL_TRIANGLES, rslick_numind, GL_UNSIGNED_SHORT, 0);
  275. }
  276. }
  277. void rGem(f32 z, uint x)
  278. {
  279. mIdent(&model);
  280. mTranslate(&model, 0.f, 0.f, z);
  281. mMul(&modelview, &model, &view);
  282. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
  283. glUniform1f(opacity_id, 1.0f);
  284. if(x == 0)
  285. {
  286. if(bindstate != 4)
  287. {
  288. glBindBuffer(GL_ARRAY_BUFFER, mdlLgem.vid);
  289. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  290. glEnableVertexAttribArray(position_id);
  291. glBindBuffer(GL_ARRAY_BUFFER, mdlLgem.nid);
  292. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  293. glEnableVertexAttribArray(normal_id);
  294. glBindBuffer(GL_ARRAY_BUFFER, mdlLgem.cid);
  295. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  296. glEnableVertexAttribArray(color_id);
  297. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLgem.iid);
  298. bindstate = 4;
  299. }
  300. glDrawElements(GL_TRIANGLES, lgem_numind, GL_UNSIGNED_SHORT, 0);
  301. }
  302. else if(x == 1)
  303. {
  304. if(bindstate != 5)
  305. {
  306. glBindBuffer(GL_ARRAY_BUFFER, mdlMgem.vid);
  307. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  308. glEnableVertexAttribArray(position_id);
  309. glBindBuffer(GL_ARRAY_BUFFER, mdlMgem.nid);
  310. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  311. glEnableVertexAttribArray(normal_id);
  312. glBindBuffer(GL_ARRAY_BUFFER, mdlMgem.cid);
  313. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  314. glEnableVertexAttribArray(color_id);
  315. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMgem.iid);
  316. bindstate = 5;
  317. }
  318. glDrawElements(GL_TRIANGLES, mgem_numind, GL_UNSIGNED_SHORT, 0);
  319. }
  320. else if(x == 2)
  321. {
  322. if(bindstate != 6)
  323. {
  324. glBindBuffer(GL_ARRAY_BUFFER, mdlRgem.vid);
  325. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  326. glEnableVertexAttribArray(position_id);
  327. glBindBuffer(GL_ARRAY_BUFFER, mdlRgem.nid);
  328. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  329. glEnableVertexAttribArray(normal_id);
  330. glBindBuffer(GL_ARRAY_BUFFER, mdlRgem.cid);
  331. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  332. glEnableVertexAttribArray(color_id);
  333. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRgem.iid);
  334. bindstate = 6;
  335. }
  336. glDrawElements(GL_TRIANGLES, rgem_numind, GL_UNSIGNED_SHORT, 0);
  337. }
  338. }
  339. void rBoost(f32 z, uint x)
  340. {
  341. mIdent(&model);
  342. mTranslate(&model, 0.f, 0.f, z);
  343. mMul(&modelview, &model, &view);
  344. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
  345. glUniform1f(opacity_id, 1.0f);
  346. if(x == 0)
  347. {
  348. if(bindstate != 7)
  349. {
  350. glBindBuffer(GL_ARRAY_BUFFER, mdlLboost.vid);
  351. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  352. glEnableVertexAttribArray(position_id);
  353. glBindBuffer(GL_ARRAY_BUFFER, mdlLboost.nid);
  354. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  355. glEnableVertexAttribArray(normal_id);
  356. glBindBuffer(GL_ARRAY_BUFFER, mdlLboost.cid);
  357. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  358. glEnableVertexAttribArray(color_id);
  359. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLboost.iid);
  360. bindstate = 7;
  361. }
  362. glDrawElements(GL_TRIANGLES, lboost_numind, GL_UNSIGNED_SHORT, 0);
  363. }
  364. else if(x == 1)
  365. {
  366. if(bindstate != 8)
  367. {
  368. glBindBuffer(GL_ARRAY_BUFFER, mdlMboost.vid);
  369. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  370. glEnableVertexAttribArray(position_id);
  371. glBindBuffer(GL_ARRAY_BUFFER, mdlMboost.nid);
  372. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  373. glEnableVertexAttribArray(normal_id);
  374. glBindBuffer(GL_ARRAY_BUFFER, mdlMboost.cid);
  375. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  376. glEnableVertexAttribArray(color_id);
  377. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMboost.iid);
  378. bindstate = 8;
  379. }
  380. glDrawElements(GL_TRIANGLES, mboost_numind, GL_UNSIGNED_SHORT, 0);
  381. }
  382. else if(x == 2)
  383. {
  384. if(bindstate != 9)
  385. {
  386. glBindBuffer(GL_ARRAY_BUFFER, mdlRboost.vid);
  387. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  388. glEnableVertexAttribArray(position_id);
  389. glBindBuffer(GL_ARRAY_BUFFER, mdlRboost.nid);
  390. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  391. glEnableVertexAttribArray(normal_id);
  392. glBindBuffer(GL_ARRAY_BUFFER, mdlRboost.cid);
  393. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  394. glEnableVertexAttribArray(color_id);
  395. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRboost.iid);
  396. bindstate = 9;
  397. }
  398. glDrawElements(GL_TRIANGLES, rboost_numind, GL_UNSIGNED_SHORT, 0);
  399. }
  400. }
  401. void rBoarder(f32 z, uint x, f32 r)
  402. {
  403. mIdent(&model);
  404. mTranslate(&model, 0.f, 0.f, z);
  405. if(x == 1)
  406. mRotX(&model, r);
  407. mMul(&modelview, &model, &view);
  408. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
  409. if(po < 1.0f)
  410. po += 1.0f * dt;
  411. else
  412. po = 1.0f;
  413. glUniform1f(opacity_id, po);
  414. if(po != 1.0f)
  415. glEnable(GL_BLEND);
  416. if(x == 0)
  417. {
  418. glBindBuffer(GL_ARRAY_BUFFER, mdlLboarder.vid);
  419. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  420. glEnableVertexAttribArray(position_id);
  421. glBindBuffer(GL_ARRAY_BUFFER, mdlLboarder.nid);
  422. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  423. glEnableVertexAttribArray(normal_id);
  424. glBindBuffer(GL_ARRAY_BUFFER, mdlLboarder.cid);
  425. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  426. glEnableVertexAttribArray(color_id);
  427. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLboarder.iid);
  428. glDrawElements(GL_TRIANGLES, lboarder_numind, GL_UNSIGNED_SHORT, 0);
  429. }
  430. else if(x == 1)
  431. {
  432. glBindBuffer(GL_ARRAY_BUFFER, mdlMboarder.vid);
  433. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  434. glEnableVertexAttribArray(position_id);
  435. glBindBuffer(GL_ARRAY_BUFFER, mdlMboarder.nid);
  436. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  437. glEnableVertexAttribArray(normal_id);
  438. glBindBuffer(GL_ARRAY_BUFFER, mdlMboarder.cid);
  439. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  440. glEnableVertexAttribArray(color_id);
  441. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMboarder.iid);
  442. glDrawElements(GL_TRIANGLES, mboarder_numind, GL_UNSIGNED_SHORT, 0);
  443. }
  444. else if(x == 2)
  445. {
  446. glBindBuffer(GL_ARRAY_BUFFER, mdlRboarder.vid);
  447. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  448. glEnableVertexAttribArray(position_id);
  449. glBindBuffer(GL_ARRAY_BUFFER, mdlRboarder.nid);
  450. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  451. glEnableVertexAttribArray(normal_id);
  452. glBindBuffer(GL_ARRAY_BUFFER, mdlRboarder.cid);
  453. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  454. glEnableVertexAttribArray(color_id);
  455. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRboarder.iid);
  456. glDrawElements(GL_TRIANGLES, rboarder_numind, GL_UNSIGNED_SHORT, 0);
  457. }
  458. if(po != 1.0f)
  459. glDisable(GL_BLEND);
  460. }
  461. //*************************************
  462. // gl functions
  463. //*************************************
  464. void doPerspective()
  465. {
  466. glViewport(0, 0, winw, winh);
  467. aspect = (f32)winw / (f32)winh;
  468. mIdent(&projection);
  469. mPerspective(&projection, 60.0f, aspect, 1.0f, 160.f);
  470. glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*)&projection.m[0][0]);
  471. }
  472. //*************************************
  473. // update & render
  474. //*************************************
  475. void main_loop()
  476. {
  477. //*************************************
  478. // time delta for interpolation
  479. //*************************************
  480. static f32 lt = 0;
  481. t = ((float)SDL_GetTicks())*0.001f;
  482. dt = t-lt;
  483. lt = t;
  484. //*************************************
  485. // input handling
  486. //*************************************
  487. SDL_Event event;
  488. while(SDL_PollEvent(&event))
  489. {
  490. switch(event.type)
  491. {
  492. case SDL_KEYDOWN:
  493. {
  494. if(event.key.keysym.sym == SDLK_LEFT){ keystate[0] = 1; player_x = 0; }
  495. else if(event.key.keysym.sym == SDLK_RIGHT){ keystate[1] = 1; player_x = 2; }
  496. }
  497. break;
  498. case SDL_KEYUP:
  499. {
  500. if(event.key.keysym.sym == SDLK_LEFT){ keystate[0] = 0; }
  501. else if(event.key.keysym.sym == SDLK_RIGHT){ keystate[1] = 0; }
  502. if(keystate[0] == 1){ player_x = 0; }
  503. else if(keystate[1] == 1){ player_x = 2; }
  504. else { player_x = 1; }
  505. }
  506. break;
  507. case SDL_FINGERDOWN:
  508. {
  509. if(event.tfinger.x < 0.3f){keystate[0] = 1; player_x = 0;} // left
  510. else if(event.tfinger.x > 0.6f){keystate[1] = 1; player_x = 2;} // right
  511. else{keystate[0] = 0; keystate[1] = 0; player_x = 1;} // center
  512. }
  513. break;
  514. case SDL_FINGERMOTION:
  515. {
  516. if(event.tfinger.x < 0.3f){keystate[0] = 1; player_x = 0;} // left
  517. else if(event.tfinger.x > 0.6f){keystate[1] = 1; player_x = 2;} // right
  518. else{keystate[0] = 0; keystate[1] = 0; player_x = 1;} // center
  519. }
  520. break;
  521. case SDL_FINGERUP:
  522. {
  523. keystate[0] = 0;
  524. keystate[1] = 0;
  525. player_x = 1;
  526. }
  527. break;
  528. case SDL_MOUSEBUTTONDOWN:
  529. {
  530. if(event.button.button == SDL_BUTTON_LEFT)
  531. {
  532. keystate[0] = 1; player_x = 0;
  533. }
  534. else if(event.button.button == SDL_BUTTON_RIGHT)
  535. {
  536. keystate[1] = 1; player_x = 2;
  537. }
  538. }
  539. break;
  540. case SDL_MOUSEBUTTONUP:
  541. {
  542. if(event.button.button == SDL_BUTTON_LEFT){ keystate[0] = 0; }
  543. else if(event.button.button == SDL_BUTTON_RIGHT){ keystate[1] = 0; }
  544. if(keystate[0] == 1){ player_x = 0; }
  545. else if(keystate[1] == 1){ player_x = 2; }
  546. else { player_x = 1; }
  547. }
  548. break;
  549. case SDL_WINDOWEVENT:
  550. {
  551. if(event.window.event == SDL_WINDOWEVENT_RESIZED)
  552. {
  553. winw = event.window.data1;
  554. winh = event.window.data2;
  555. doPerspective();
  556. }
  557. }
  558. break;
  559. case SDL_QUIT:
  560. {
  561. SDL_GL_DeleteContext(glc);
  562. SDL_FreeSurface(s_icon);
  563. SDL_DestroyWindow(wnd);
  564. SDL_Quit();
  565. exit(0);
  566. }
  567. break;
  568. }
  569. }
  570. //*************************************
  571. // begin render
  572. //*************************************
  573. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  574. //*************************************
  575. // main render
  576. //*************************************
  577. // camera
  578. mIdent(&view);
  579. mTranslate(&view, 0.f, -2.f, camdist);
  580. if(state == 0) // next tunnel
  581. {
  582. // new slick array
  583. max_slicks = esRand(0, ARRAY_MAX);
  584. const uint empty_lane = esRand(0, 2);
  585. for(uint i = 0; i < max_slicks; i++)
  586. {
  587. array_slick[i].z = esRandFloat(-41.f, -70.f);
  588. array_slick[i].x = esRand(0, 3);
  589. if(array_slick[i].x == empty_lane)
  590. array_slick[i].s = 0;
  591. else
  592. array_slick[i].s = 1;
  593. }
  594. // new gem array
  595. max_gems = esRand(0, ARRAY_MAX);
  596. for(uint i = 0; i < max_gems; i++)
  597. {
  598. array_gem[i].z = esRandFloat(-11.f, -70.f);
  599. array_gem[i].x = esRand(0, 3);
  600. array_gem[i].s = 1;
  601. }
  602. // new boost array
  603. max_boosts = esRand(0, ARRAY_MAX/2);
  604. for(uint i = 0; i < max_boosts; i++)
  605. {
  606. array_boost[i].z = esRandFloat(-11.f, -70.f);
  607. array_boost[i].x = esRand(0, 3);
  608. array_boost[i].s = 1;
  609. }
  610. // reset vars
  611. pz = 0;
  612. pr = 0.f;
  613. // transition tunnel colours
  614. vec cn;
  615. cn.x = esRandFloat(0.f, 1.f);
  616. cn.y = esRandFloat(0.f, 1.f);
  617. cn.z = esRandFloat(0.f, 1.f);
  618. const uint mi = tunnel_numvert*3;
  619. for(uint i = 0; i < mi; i += 3)
  620. {
  621. if(tunnel_colors[i] == c1.x && tunnel_colors[i+1] == c1.y && tunnel_colors[i+2] == c1.z)
  622. {
  623. tunnel_colors[i] = c2.x;
  624. tunnel_colors[i+1] = c2.y;
  625. tunnel_colors[i+2] = c2.z;
  626. }
  627. else if(tunnel_colors[i] == c2.x && tunnel_colors[i+1] == c2.y && tunnel_colors[i+2] == c2.z)
  628. {
  629. tunnel_colors[i] = c3.x;
  630. tunnel_colors[i+1] = c3.y;
  631. tunnel_colors[i+2] = c3.z;
  632. }
  633. else if(tunnel_colors[i] == c3.x && tunnel_colors[i+1] == c3.y && tunnel_colors[i+2] == c3.z)
  634. {
  635. tunnel_colors[i] = cn.x;
  636. tunnel_colors[i+1] = cn.y;
  637. tunnel_colors[i+2] = cn.z;
  638. }
  639. }
  640. c1.x = c2.x;
  641. c1.y = c2.y;
  642. c1.z = c2.z;
  643. c2.x = c3.x;
  644. c2.y = c3.y;
  645. c2.z = c3.z;
  646. c3.x = cn.x;
  647. c3.y = cn.y;
  648. c3.z = cn.z;
  649. glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.cid);
  650. glBufferData(GL_ARRAY_BUFFER, sizeof(tunnel_colors), tunnel_colors, GL_STATIC_DRAW);
  651. // reposition next tunnel angle
  652. const f32 r1 = esRandFloat(-6.f, 6.f);
  653. const f32 r2 = esRandFloat(-91.f, -130.f);
  654. for(uint i = 0; i < mi; i += 3)
  655. {
  656. if(tunnel_vertices[i+2] < -91.f)
  657. {
  658. tunnel_vertices[i] += -12.f * sinf(r1);
  659. tunnel_vertices[i+1] += -12.f * cosf(r1);
  660. tunnel_vertices[i+2] = r2;
  661. }
  662. }
  663. glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.vid);
  664. glBufferData(GL_ARRAY_BUFFER, sizeof(tunnel_vertices), tunnel_vertices, GL_STATIC_DRAW);
  665. // next state
  666. state = 1;
  667. // log
  668. char strts[16];
  669. timestamp(&strts[0]);
  670. printf("[%s] New Tunnel\n", strts);
  671. }
  672. else if(state == 1) // simulate game
  673. {
  674. // render slicks
  675. for(uint i = 0; i < max_slicks; i++)
  676. {
  677. if(array_slick[i].s == 1)
  678. {
  679. rSlick(array_slick[i].z, array_slick[i].x);
  680. if(player_x == array_slick[i].x && pz < array_slick[i].z+0.75f && pz > array_slick[i].z-0.75f)
  681. {
  682. rt = t+3;
  683. state = 2;
  684. array_slick[i].s = 0;
  685. char strts[16];
  686. timestamp(&strts[0]);
  687. printf("[%s] Slick: %.2f %u %u\n", strts, array_slick[i].z, array_slick[i].x, array_slick[i].s);
  688. printf("[%s] >>> YOU DIED, SCORE %u <<<\n", strts, score);
  689. }
  690. }
  691. }
  692. // render gems
  693. for(uint i = 0; i < max_gems; i++)
  694. {
  695. if(array_gem[i].s == 1)
  696. {
  697. rGem(array_gem[i].z, array_gem[i].x);
  698. if(player_x == array_gem[i].x && pz < array_gem[i].z+0.3f && pz > array_gem[i].z-0.3f)
  699. {
  700. array_gem[i].s = 0;
  701. score++;
  702. po = 0.3f;
  703. char title[256];
  704. sprintf(title, "SCORE %u", score);
  705. SDL_SetWindowTitle(wnd, title);
  706. char strts[16];
  707. timestamp(&strts[0]);
  708. printf("[%s] Gem: %.2f %u %u\n", strts, array_gem[i].z, array_gem[i].x, array_gem[i].s);
  709. }
  710. }
  711. }
  712. // render boosts
  713. for(uint i = 0; i < max_boosts; i++)
  714. {
  715. rBoost(array_boost[i].z, array_boost[i].x);
  716. if(array_boost[i].s == 1 && player_x == array_boost[i].x && pz < array_boost[i].z+1.6f && pz > array_boost[i].z-1.6f)
  717. {
  718. ps *= 2.3f;
  719. array_boost[i].s = 0;
  720. char strts[16];
  721. timestamp(&strts[0]);
  722. printf("[%s] Boost: %.2f %u %u\n", strts, array_boost[i].z, array_boost[i].x, array_boost[i].s);
  723. }
  724. }
  725. // move player
  726. rBoarder(pz, player_x, pr);
  727. pz += ps * dt;
  728. if(ps < MINSPEED)
  729. ps += DRAGRATE * dt;
  730. else
  731. ps = MINSPEED;
  732. if(ps < MAXSPEED)
  733. ps = MAXSPEED;
  734. camdist = -pz + -5.0f;
  735. // reset
  736. if(pz < -80.f)
  737. state = 0;
  738. }
  739. else if(state == 2) // you died
  740. {
  741. pr += 1.2f * dt;
  742. rBoarder(pz, 1, pr);
  743. rDead(pz-6.0f);
  744. if(t > rt)
  745. {
  746. // reset game
  747. srand(time(0));
  748. SDL_SetWindowTitle(wnd, appTitle);
  749. ps = MINSPEED;
  750. score = 0;
  751. state = 3;
  752. rt = t + 3;
  753. pz = 0.f;
  754. camdist = -5.0f;
  755. char strts[16];
  756. timestamp(&strts[0]);
  757. printf("[%s] *** New Game\n", strts);
  758. }
  759. }
  760. else if(state == 3) // round count down
  761. {
  762. static double lt = 0;
  763. static f32 z = 0.f;
  764. const double ft = floor(rt-t);
  765. if(lt != ft)
  766. {
  767. z = 0.f;
  768. lt = ft;
  769. }
  770. z -= 6.0f * dt;
  771. rCountDown(z, rt-t+1);
  772. if(t > rt)
  773. state = 0;
  774. }
  775. // render tunnel
  776. rTunnel();
  777. //*************************************
  778. // swap buffers / display render
  779. //*************************************
  780. SDL_GL_SwapWindow(wnd);
  781. }
  782. //*************************************
  783. // Process Entry Point
  784. //*************************************
  785. int main(int argc, char** argv)
  786. {
  787. int msaa = 16;
  788. if(argc >= 2){msaa = atoi(argv[1]);}
  789. printf("Snowboarder, A simple snowboarding game.\n");
  790. printf("James William Fletcher (github.com/mrbid)\n\n");
  791. printf("Use your keyboard arrow keys to move from left to right or left and right click.\n\n");
  792. //*************************************
  793. // setup render context / window
  794. //*************************************
  795. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS) < 0)
  796. {
  797. printf("ERROR: SDL_Init(): %s\n", SDL_GetError());
  798. return 1;
  799. }
  800. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  801. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaa);
  802. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  803. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  804. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
  805. wnd = SDL_CreateWindow(appTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, winw, winh, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  806. while(wnd == NULL)
  807. {
  808. msaa--;
  809. if(msaa == 0)
  810. {
  811. printf("ERROR: SDL_CreateWindow(): %s\n", SDL_GetError());
  812. return 1;
  813. }
  814. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaa);
  815. wnd = SDL_CreateWindow(appTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, winw, winh, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  816. }
  817. SDL_GL_SetSwapInterval(1); // 0 for immediate updates, 1 for updates synchronized with the vertical retrace, -1 for adaptive vsync
  818. glc = SDL_GL_CreateContext(wnd);
  819. if(glc == NULL)
  820. {
  821. printf("ERROR: SDL_GL_CreateContext(): %s\n", SDL_GetError());
  822. return 1;
  823. }
  824. // set icon
  825. unsigned char* ipd = (unsigned char*)&icon_image2.pixel_data;
  826. srand(time(0));
  827. const uint r = esRand(0, 2);
  828. if(r == 0) {ipd = (unsigned char*)&icon_image.pixel_data;}
  829. else if(r == 1) {ipd = (unsigned char*)&icon_image1.pixel_data;}
  830. else {ipd = (unsigned char*)&icon_image2.pixel_data;}
  831. s_icon = surfaceFromData((Uint32*)ipd, 16, 16);
  832. SDL_SetWindowIcon(wnd, s_icon);
  833. // seed random
  834. srand(time(0));
  835. //*************************************
  836. // bind vertex and index buffers
  837. //*************************************
  838. // ***** BIND TUNNEL *****
  839. esBind(GL_ARRAY_BUFFER, &mdlTunnel.vid, tunnel_vertices, sizeof(tunnel_vertices), GL_STATIC_DRAW);
  840. esBind(GL_ARRAY_BUFFER, &mdlTunnel.nid, tunnel_normals, sizeof(tunnel_normals), GL_STATIC_DRAW);
  841. esBind(GL_ARRAY_BUFFER, &mdlTunnel.cid, tunnel_colors, sizeof(tunnel_colors), GL_STATIC_DRAW);
  842. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlTunnel.iid, tunnel_indices, sizeof(tunnel_indices), GL_STATIC_DRAW);
  843. // ***** BIND DEAD *****
  844. esBind(GL_ARRAY_BUFFER, &mdlDead.vid, dead_vertices, sizeof(dead_vertices), GL_STATIC_DRAW);
  845. esBind(GL_ARRAY_BUFFER, &mdlDead.nid, dead_normals, sizeof(dead_normals), GL_STATIC_DRAW);
  846. esBind(GL_ARRAY_BUFFER, &mdlDead.cid, dead_colors, sizeof(dead_colors), GL_STATIC_DRAW);
  847. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlDead.iid, dead_indices, sizeof(dead_indices), GL_STATIC_DRAW);
  848. // ***** BIND THREE *****
  849. esBind(GL_ARRAY_BUFFER, &mdlThree.vid, three_vertices, sizeof(three_vertices), GL_STATIC_DRAW);
  850. esBind(GL_ARRAY_BUFFER, &mdlThree.nid, three_normals, sizeof(three_normals), GL_STATIC_DRAW);
  851. esBind(GL_ARRAY_BUFFER, &mdlThree.cid, three_colors, sizeof(three_colors), GL_STATIC_DRAW);
  852. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlThree.iid, three_indices, sizeof(three_indices), GL_STATIC_DRAW);
  853. // ***** BIND TWO *****
  854. esBind(GL_ARRAY_BUFFER, &mdlTwo.vid, two_vertices, sizeof(two_vertices), GL_STATIC_DRAW);
  855. esBind(GL_ARRAY_BUFFER, &mdlTwo.nid, two_normals, sizeof(two_normals), GL_STATIC_DRAW);
  856. esBind(GL_ARRAY_BUFFER, &mdlTwo.cid, two_colors, sizeof(two_colors), GL_STATIC_DRAW);
  857. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlTwo.iid, two_indices, sizeof(two_indices), GL_STATIC_DRAW);
  858. // ***** BIND ONE *****
  859. esBind(GL_ARRAY_BUFFER, &mdlOne.vid, one_vertices, sizeof(one_vertices), GL_STATIC_DRAW);
  860. esBind(GL_ARRAY_BUFFER, &mdlOne.nid, one_normals, sizeof(one_normals), GL_STATIC_DRAW);
  861. esBind(GL_ARRAY_BUFFER, &mdlOne.cid, one_colors, sizeof(one_colors), GL_STATIC_DRAW);
  862. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlOne.iid, one_indices, sizeof(one_indices), GL_STATIC_DRAW);
  863. // ***** BIND LSLICK *****
  864. esBind(GL_ARRAY_BUFFER, &mdlLslick.vid, lslick_vertices, sizeof(lslick_vertices), GL_STATIC_DRAW);
  865. esBind(GL_ARRAY_BUFFER, &mdlLslick.nid, lslick_normals, sizeof(lslick_normals), GL_STATIC_DRAW);
  866. esBind(GL_ARRAY_BUFFER, &mdlLslick.cid, lslick_colors, sizeof(lslick_colors), GL_STATIC_DRAW);
  867. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlLslick.iid, lslick_indices, sizeof(lslick_indices), GL_STATIC_DRAW);
  868. // ***** BIND MSLICK *****
  869. esBind(GL_ARRAY_BUFFER, &mdlMslick.vid, mslick_vertices, sizeof(mslick_vertices), GL_STATIC_DRAW);
  870. esBind(GL_ARRAY_BUFFER, &mdlMslick.nid, mslick_normals, sizeof(mslick_normals), GL_STATIC_DRAW);
  871. esBind(GL_ARRAY_BUFFER, &mdlMslick.cid, mslick_colors, sizeof(mslick_colors), GL_STATIC_DRAW);
  872. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlMslick.iid, mslick_indices, sizeof(mslick_indices), GL_STATIC_DRAW);
  873. // ***** BIND RSLICK *****
  874. esBind(GL_ARRAY_BUFFER, &mdlRslick.vid, rslick_vertices, sizeof(rslick_vertices), GL_STATIC_DRAW);
  875. esBind(GL_ARRAY_BUFFER, &mdlRslick.nid, rslick_normals, sizeof(rslick_normals), GL_STATIC_DRAW);
  876. esBind(GL_ARRAY_BUFFER, &mdlRslick.cid, rslick_colors, sizeof(rslick_colors), GL_STATIC_DRAW);
  877. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlRslick.iid, rslick_indices, sizeof(rslick_indices), GL_STATIC_DRAW);
  878. // ***** BIND LGEM *****
  879. esBind(GL_ARRAY_BUFFER, &mdlLgem.vid, lgem_vertices, sizeof(lgem_vertices), GL_STATIC_DRAW);
  880. esBind(GL_ARRAY_BUFFER, &mdlLgem.nid, lgem_normals, sizeof(lgem_normals), GL_STATIC_DRAW);
  881. esBind(GL_ARRAY_BUFFER, &mdlLgem.cid, lgem_colors, sizeof(lgem_colors), GL_STATIC_DRAW);
  882. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlLgem.iid, lgem_indices, sizeof(lgem_indices), GL_STATIC_DRAW);
  883. // ***** BIND MGEM *****
  884. esBind(GL_ARRAY_BUFFER, &mdlMgem.vid, mgem_vertices, sizeof(mgem_vertices), GL_STATIC_DRAW);
  885. esBind(GL_ARRAY_BUFFER, &mdlMgem.nid, mgem_normals, sizeof(mgem_normals), GL_STATIC_DRAW);
  886. esBind(GL_ARRAY_BUFFER, &mdlMgem.cid, mgem_colors, sizeof(mgem_colors), GL_STATIC_DRAW);
  887. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlMgem.iid, mgem_indices, sizeof(mgem_indices), GL_STATIC_DRAW);
  888. // ***** BIND RGEM *****
  889. esBind(GL_ARRAY_BUFFER, &mdlRgem.vid, rgem_vertices, sizeof(rgem_vertices), GL_STATIC_DRAW);
  890. esBind(GL_ARRAY_BUFFER, &mdlRgem.nid, rgem_normals, sizeof(rgem_normals), GL_STATIC_DRAW);
  891. esBind(GL_ARRAY_BUFFER, &mdlRgem.cid, rgem_colors, sizeof(rgem_colors), GL_STATIC_DRAW);
  892. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlRgem.iid, rgem_indices, sizeof(rgem_indices), GL_STATIC_DRAW);
  893. // ***** BIND LBOOST *****
  894. esBind(GL_ARRAY_BUFFER, &mdlLboost.vid, lboost_vertices, sizeof(lboost_vertices), GL_STATIC_DRAW);
  895. esBind(GL_ARRAY_BUFFER, &mdlLboost.nid, lboost_normals, sizeof(lboost_normals), GL_STATIC_DRAW);
  896. esBind(GL_ARRAY_BUFFER, &mdlLboost.cid, lboost_colors, sizeof(lboost_colors), GL_STATIC_DRAW);
  897. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlLboost.iid, lboost_indices, sizeof(lboost_indices), GL_STATIC_DRAW);
  898. // ***** BIND MBOOST *****
  899. esBind(GL_ARRAY_BUFFER, &mdlMboost.vid, mboost_vertices, sizeof(mboost_vertices), GL_STATIC_DRAW);
  900. esBind(GL_ARRAY_BUFFER, &mdlMboost.nid, mboost_normals, sizeof(mboost_normals), GL_STATIC_DRAW);
  901. esBind(GL_ARRAY_BUFFER, &mdlMboost.cid, mboost_colors, sizeof(mboost_colors), GL_STATIC_DRAW);
  902. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlMboost.iid, mboost_indices, sizeof(mboost_indices), GL_STATIC_DRAW);
  903. // ***** BIND RBOOST *****
  904. esBind(GL_ARRAY_BUFFER, &mdlRboost.vid, rboost_vertices, sizeof(rboost_vertices), GL_STATIC_DRAW);
  905. esBind(GL_ARRAY_BUFFER, &mdlRboost.nid, rboost_normals, sizeof(rboost_normals), GL_STATIC_DRAW);
  906. esBind(GL_ARRAY_BUFFER, &mdlRboost.cid, rboost_colors, sizeof(rboost_colors), GL_STATIC_DRAW);
  907. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlRboost.iid, rboost_indices, sizeof(rboost_indices), GL_STATIC_DRAW);
  908. // ***** BIND LBOARDER *****
  909. esBind(GL_ARRAY_BUFFER, &mdlLboarder.vid, lboarder_vertices, sizeof(lboarder_vertices), GL_STATIC_DRAW);
  910. esBind(GL_ARRAY_BUFFER, &mdlLboarder.nid, lboarder_normals, sizeof(lboarder_normals), GL_STATIC_DRAW);
  911. esBind(GL_ARRAY_BUFFER, &mdlLboarder.cid, lboarder_colors, sizeof(lboarder_colors), GL_STATIC_DRAW);
  912. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlLboarder.iid, lboarder_indices, sizeof(lboarder_indices), GL_STATIC_DRAW);
  913. // ***** BIND MBOARDER *****
  914. esBind(GL_ARRAY_BUFFER, &mdlMboarder.vid, mboarder_vertices, sizeof(mboarder_vertices), GL_STATIC_DRAW);
  915. esBind(GL_ARRAY_BUFFER, &mdlMboarder.nid, mboarder_normals, sizeof(mboarder_normals), GL_STATIC_DRAW);
  916. esBind(GL_ARRAY_BUFFER, &mdlMboarder.cid, mboarder_colors, sizeof(mboarder_colors), GL_STATIC_DRAW);
  917. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlMboarder.iid, mboarder_indices, sizeof(mboarder_indices), GL_STATIC_DRAW);
  918. // ***** BIND RBOARDER *****
  919. esBind(GL_ARRAY_BUFFER, &mdlRboarder.vid, rboarder_vertices, sizeof(rboarder_vertices), GL_STATIC_DRAW);
  920. esBind(GL_ARRAY_BUFFER, &mdlRboarder.nid, rboarder_normals, sizeof(rboarder_normals), GL_STATIC_DRAW);
  921. esBind(GL_ARRAY_BUFFER, &mdlRboarder.cid, rboarder_colors, sizeof(rboarder_colors), GL_STATIC_DRAW);
  922. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlRboarder.iid, rboarder_indices, sizeof(rboarder_indices), GL_STATIC_DRAW);
  923. //*************************************
  924. // projection
  925. //*************************************
  926. doPerspective();
  927. //*************************************
  928. // compile & link shader program
  929. //*************************************
  930. makeLambert3();
  931. //*************************************
  932. // configure render options
  933. //*************************************
  934. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  935. glEnable(GL_CULL_FACE);
  936. glEnable(GL_DEPTH_TEST);
  937. glClearColor(0.0, 0.0, 0.0, 0.0);
  938. shadeLambert3(&position_id, &projection_id, &modelview_id, &lightpos_id, &normal_id, &color_id, &opacity_id);
  939. glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*) &projection.m[0][0]);
  940. glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
  941. //*************************************
  942. // execute update / render loop
  943. //*************************************
  944. t = ((float)SDL_GetTicks())*0.001f;
  945. rt = t+3;
  946. while(1){main_loop();}
  947. return 0;
  948. }