vbo.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. #include "Crown.h"
  2. #include <time.h>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <ft2build.h>
  6. #include FT_FREETYPE_H
  7. #include FT_GLYPH_H
  8. #include "Types.h"
  9. #define HMSIZE 256
  10. #define CubeSize 2/3.0f
  11. void DrawAxis();
  12. void DrawGrid(float xStart, float zStart, float xStep, float zStep);
  13. void DrawWiredCube(float x, float y, float z);
  14. void DrawCube(float x, float y, float z);
  15. void DrawVoxelGrid(float xStart, float yStart, float zStart, float xStep, float yStep, float zStep);
  16. using namespace Crown;
  17. enum Visibility
  18. {
  19. V_Visible,
  20. V_Hidden_Opt,
  21. V_Hidden,
  22. };
  23. enum FaceOrientation
  24. {
  25. FO_TOP,
  26. FO_BOTTOM,
  27. FO_LEFT,
  28. FO_RIGHT,
  29. FO_FRONT,
  30. FO_BACK
  31. };
  32. struct Block {
  33. Visibility visible;
  34. };
  35. class Chunk {
  36. public:
  37. Chunk() {
  38. Clear();
  39. vb = NULL;
  40. ib = NULL;
  41. }
  42. void Clear() {
  43. mVertexCount = 0;
  44. mVertexList.Clear();
  45. for(int x = 0; x < HMSIZE; x++) {
  46. for(int z = 0; z < HMSIZE; z++) {
  47. for(int h = 0; h < HMSIZE; h++) {
  48. mBlocks[x][z][h].visible = V_Hidden;
  49. }
  50. }
  51. }
  52. }
  53. void Render() {
  54. vb->MakeCurrent();
  55. ib->MakeCurrent();
  56. ib->Render(Vec3(0, 0, 0), Vec3(0, 0, 0), 0.0f);
  57. }
  58. void RandomMap() {
  59. srand(time(NULL));
  60. Clear();
  61. for(int n = 0; n < 100; n++) {
  62. int xmin, xmax, zmin, zmax;
  63. int a, b;
  64. a = rand()%HMSIZE;
  65. b = rand()%HMSIZE;
  66. xmin = Math::Min(a, b);
  67. xmax = Math::Max(a, b);
  68. a = rand()%HMSIZE;
  69. b = rand()%HMSIZE;
  70. zmin = Math::Min(a, b);
  71. zmax = Math::Max(a, b);
  72. for(int x = xmin; x < xmax; x++) {
  73. for(int z = zmin; z < zmax; z++) {
  74. int h = 0;
  75. while (mBlocks[x][z][h].visible == V_Visible)
  76. h++;
  77. mBlocks[x][z][h].visible = V_Visible;
  78. }
  79. }
  80. }
  81. RegenerateVertexList();
  82. }
  83. void RegenerateVertexList() {
  84. mVertexCount = 0;
  85. mVertexList.Clear();
  86. for(int x = 0; x < HMSIZE; x++) {
  87. for(int z = 0; z < HMSIZE; z++) {
  88. for(int h = 0; h < HMSIZE; h++) {
  89. if (mBlocks[x][z][h].visible == V_Visible)
  90. GenerateFaces(x, z, h);
  91. }
  92. }
  93. }
  94. if (!vb)
  95. vb = GetDevice()->GetRenderer()->CreateVertexBuffer();
  96. if (!ib)
  97. ib = GetDevice()->GetRenderer()->CreateIndexBuffer();
  98. vb->SetVertexData(VBM_COLOR_COORDS, mVertexList.GetData(), mVertexCount);
  99. List<unsigned int> indexes;
  100. for(int i=0; i<mVertexCount; i++)
  101. indexes.Append(i);
  102. ib->SetIndexData(indexes.GetData(), mVertexCount);
  103. mVertexList.Clear();
  104. }
  105. void GenerateFaces(int x, int z, int h) {
  106. AddFace(x, z, h, FO_TOP , x , z , h+1);
  107. AddFace(x, z, h, FO_BOTTOM, x , z , h-1);
  108. AddFace(x, z, h, FO_LEFT , x-1, z , h);
  109. AddFace(x, z, h, FO_RIGHT , x+1, z , h);
  110. AddFace(x, z, h, FO_FRONT , x , z+1, h);
  111. AddFace(x, z, h, FO_BACK , x , z-1, h);
  112. }
  113. void AddFace(int x, int z, int h, FaceOrientation orientation, int x1, int z1, int h1) {
  114. if (!(x1 < 0 || x1 > HMSIZE-1 || z1 < 0 || z1 > HMSIZE-1 || h1 < 0 || h1 > HMSIZE-1))
  115. if (mBlocks[x1][z1][h1].visible == V_Visible)
  116. return;
  117. float xx = x * CubeSize;
  118. float zz = z * CubeSize;
  119. float hh = h * CubeSize;
  120. switch (orientation) {
  121. case FO_TOP:
  122. AppendVertex(xx , hh + CubeSize, zz);
  123. AppendColor(.3f, .0f, .0f);
  124. AppendVertex(xx + CubeSize, hh + CubeSize, zz);
  125. AppendColor(.25f, .0f, .0f);
  126. AppendVertex(xx + CubeSize, hh + CubeSize, zz - CubeSize);
  127. AppendColor(.3f, .0f, .0f);
  128. AppendVertex(xx , hh + CubeSize, zz - CubeSize);
  129. AppendColor(.25f, .0f, .0f);
  130. break;
  131. case FO_BOTTOM:
  132. AppendVertex(xx , hh, zz);
  133. AppendColor(.0f, .0f, .3f);
  134. AppendVertex(xx , hh, zz - CubeSize);
  135. AppendColor(.0f, .0f, .25f);
  136. AppendVertex(xx + CubeSize, hh, zz - CubeSize);
  137. AppendColor(.0f, .0f, .3f);
  138. AppendVertex(xx + CubeSize, hh, zz);
  139. AppendColor(.0f, .0f, .25f);
  140. break;
  141. case FO_LEFT:
  142. AppendVertex(xx, hh , zz);
  143. AppendColor(.0f, .0f, .3f);
  144. AppendVertex(xx, hh + CubeSize, zz);
  145. AppendColor(.0f, .0f, .25f);
  146. AppendVertex(xx, hh + CubeSize, zz - CubeSize);
  147. AppendColor(.0f, .0f, .3f);
  148. AppendVertex(xx, hh , zz - CubeSize);
  149. AppendColor(.0f, .0f, .25f);
  150. break;
  151. case FO_RIGHT:
  152. AppendVertex(xx + CubeSize, hh , zz);
  153. AppendColor(.0f, .0f, .3f);
  154. AppendVertex(xx + CubeSize, hh , zz - CubeSize);
  155. AppendColor(.0f, .0f, .25f);
  156. AppendVertex(xx + CubeSize, hh + CubeSize, zz - CubeSize);
  157. AppendColor(.0f, .0f, .3f);
  158. AppendVertex(xx + CubeSize, hh + CubeSize, zz);
  159. AppendColor(.0f, .0f, .25f);
  160. break;
  161. case FO_FRONT:
  162. AppendVertex(xx , hh , zz);
  163. AppendColor(.0f, .0f, .3f);
  164. AppendVertex(xx + CubeSize, hh , zz);
  165. AppendColor(.0f, .0f, .25f);
  166. AppendVertex(xx + CubeSize, hh + CubeSize, zz);
  167. AppendColor(.0f, .0f, .3f);
  168. AppendVertex(xx , hh + CubeSize, zz);
  169. AppendColor(.0f, .0f, .25f);
  170. break;
  171. case FO_BACK:
  172. AppendVertex(xx , hh , zz - CubeSize);
  173. AppendColor(.0f, .0f, .3f);
  174. AppendVertex(xx , hh + CubeSize, zz - CubeSize);
  175. AppendColor(.0f, .0f, .25f);
  176. AppendVertex(xx + CubeSize, hh + CubeSize, zz - CubeSize);
  177. AppendColor(.0f, .0f, .3f);
  178. AppendVertex(xx + CubeSize, hh , zz - CubeSize);
  179. AppendColor(.0f, .0f, .25f);
  180. break;
  181. }
  182. mVertexCount += 4;
  183. }
  184. void AppendVertex(float x, float y, float z) {
  185. mVertexList.Append(x);
  186. mVertexList.Append(y);
  187. mVertexList.Append(z);
  188. }
  189. void AppendColor(float r, float g, float b) {
  190. mVertexList.Append(r);
  191. mVertexList.Append(g);
  192. mVertexList.Append(b);
  193. }
  194. void Optimize() {
  195. for(int x = 1; x < HMSIZE-1; x++) {
  196. for(int y = 1; y < HMSIZE-1; y++) {
  197. for(int z = 1; z < HMSIZE-1; z++) {
  198. if (mBlocks[x][y][z].visible == V_Visible) {
  199. if (mBlocks[x-1][y][z].visible != V_Visible && mBlocks[x-1][y][z].visible != V_Hidden_Opt)
  200. continue;
  201. if (mBlocks[x+1][y][z].visible != V_Visible && mBlocks[x+1][y][z].visible != V_Hidden_Opt)
  202. continue;
  203. if (mBlocks[x][y-1][z].visible != V_Visible && mBlocks[x][y-1][z].visible != V_Hidden_Opt)
  204. continue;
  205. if (mBlocks[x][y+1][z].visible != V_Visible && mBlocks[x][y+1][z].visible != V_Hidden_Opt)
  206. continue;
  207. if (mBlocks[x][y][z-1].visible != V_Visible && mBlocks[x][y][z-1].visible != V_Hidden_Opt)
  208. continue;
  209. if (mBlocks[x][y][z+1].visible != V_Visible && mBlocks[x][y][z+1].visible != V_Hidden_Opt)
  210. continue;
  211. mBlocks[x][y][z].visible = V_Hidden_Opt;
  212. }
  213. }
  214. }
  215. }
  216. for(int x = 1; x < HMSIZE-1; x++) {
  217. for(int y = 1; y < HMSIZE-1; y++) {
  218. for(int z = 1; z < HMSIZE-1; z++) {
  219. if (mBlocks[x][y][z].visible == V_Hidden_Opt) {
  220. mBlocks[x][y][z].visible = V_Hidden;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. private:
  227. Block mBlocks[HMSIZE][HMSIZE][HMSIZE];
  228. List<float> mVertexList;
  229. int mVertexCount;
  230. VertexBuffer* vb;
  231. IndexBuffer* ib;
  232. };
  233. Chunk chunk;
  234. class InputReceiver : public EventReceiver {
  235. public:
  236. InputReceiver() {
  237. mDevice = GetDevice();
  238. mDevice->GetEventDispatcher()->RegisterEventReceiver(this);
  239. }
  240. void HandleEvent(const Event& event) {
  241. if (event.event_type == ET_KEYBOARD) {
  242. if (event.keyboard.type == KET_RELEASED) {
  243. if (event.keyboard.key == KK_ESCAPE) {
  244. if (mDevice) {
  245. mDevice->StopRunning();
  246. }
  247. }
  248. if (event.keyboard.key == KK_SPACE) {
  249. chunk.RandomMap();
  250. }
  251. }
  252. }
  253. }
  254. private:
  255. Device* mDevice;
  256. };
  257. int main(int argc, char** argv) {
  258. int wndW, wndH;
  259. wndW = 1280;
  260. wndH = 800;
  261. bool full = false;
  262. if (argc == 3) {
  263. wndW = atoi(argv[1]);
  264. wndH = atoi(argv[2]);
  265. }
  266. Device* device = GetDevice();
  267. if (!device->Init(wndW, wndH, 32, false)) {
  268. return 0;
  269. }
  270. InputReceiver ir;
  271. Renderer* renderer = device->GetRenderer();
  272. SceneManager* smgr = device->GetSceneManager();
  273. ResourceManager* resman = device->GetResourceManager();
  274. device->GetMainWindow()->SetTitle("Crown Engine v0.1");
  275. device->GetMainWindow()->GetCursorControl()->SetVisible(true);
  276. device->GetMainWindow()->SetFullscreen(full);
  277. Scene* scene = new Scene();
  278. MovableCamera* cam;
  279. cam = scene->AddMovableCamera(
  280. 0,
  281. Vec3(0.0f, 3.0f, HMSIZE*CubeSize),
  282. Angles(0, 0, 0),
  283. Vec3(1, 1, 1),
  284. true,
  285. 90.0f,
  286. 1.59f,
  287. true,
  288. 0.5,
  289. 1);
  290. cam->SetActive(true);
  291. cam->SetSpeed(0.1);
  292. cam->SetFarClipDistance(150.0f);
  293. std::cout << "Entity count: " << scene->GetEntityCount() << std::endl;
  294. std::cout << "Light count: " << scene->GetLightCount() << std::endl;
  295. std::cout << "Sizeof RenderWindow: " << sizeof(RenderWindow) << std::endl;
  296. /*
  297. Material material;
  298. material.mAmbient = Color(0.1, 0.1, 0.1, 1);
  299. material.mDiffuse = Color(1, 1, 1, 1);
  300. material.mSpecular = Color(0, 0, 0, 1);
  301. material.mShininess = 128;
  302. material.mSeparateSpecularColor = true;
  303. material.mTexturing = false;
  304. material.mLighting = true;
  305. renderer->SetMaterial(material);
  306. */
  307. Mat4 identity;
  308. identity.LoadIdentity();
  309. renderer->SetClearColor(Color(1.0f, 1.0f, 1.0f, 1.0f));
  310. Mat4 ortho;
  311. ortho.BuildProjectionOrthoRH(wndW, wndH, 1, -1);
  312. Mat4 perspective;
  313. perspective.BuildProjectionPerspectiveRH(90.0f, 1.59f, 0.1f, 100.0f);
  314. Mat4 text;
  315. text.LoadIdentity();
  316. text.SetTranslation(Vec3(400, 350, 0));
  317. //Crown::Font font;
  318. //Image* testImg = font.LoadFont("tests/font/arialbd.ttf");
  319. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  320. chunk.RandomMap();
  321. ////struct MyVertex
  322. //// {
  323. //// float x, y, z; //Vertex
  324. //// float r, g, b;
  325. //// //float nx, ny, nz; //Normal
  326. //// //float s0, t0; //Texcoord0
  327. //// };
  328. ////
  329. //// MyVertex pvertex[4];
  330. //// //VERTEX 0
  331. //// pvertex[0].x = 0.0;
  332. //// pvertex[0].y = 0.0;
  333. //// pvertex[0].z = 0.0;
  334. //// pvertex[0].r = 1.0f;
  335. //// pvertex[0].g = 0.0f;
  336. //// pvertex[0].b = 0.0f;
  337. ////
  338. //// //VERTEX 1
  339. //// pvertex[1].x = 1.0;
  340. //// pvertex[1].y = 0.0;
  341. //// pvertex[1].z = 0.0;
  342. //// pvertex[1].r = 1.0f;
  343. //// pvertex[1].g = 1.0f;
  344. //// pvertex[1].b = 0.0f;
  345. ////
  346. //// //VERTEX 2
  347. //// pvertex[2].x = 1.0;
  348. //// pvertex[2].y = 1.0;
  349. //// pvertex[2].z = 0.0;
  350. //// pvertex[2].r = 0.0f;
  351. //// pvertex[2].g = 1.0f;
  352. //// pvertex[2].b = 0.0f;
  353. ////
  354. //// pvertex[3].x = 0.0;
  355. //// pvertex[3].y = 1.0;
  356. //// pvertex[3].z = 0.0;
  357. //// pvertex[3].r = 0.0f;
  358. //// pvertex[3].g = 1.0f;
  359. //// pvertex[3].b = 0.0f;
  360. ////
  361. ////
  362. //// uint VertexVBOID;
  363. //// uint IndexVBOID;
  364. ////
  365. //// glGenBuffers(1, &VertexVBOID);
  366. //// glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
  367. //// glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertex)*4, pvertex, GL_DYNAMIC_DRAW);
  368. ////
  369. //// ushort pindices[4];
  370. //// pindices[0] = 0;
  371. //// pindices[1] = 1;
  372. //// pindices[2] = 2;
  373. //// pindices[3] = 3;
  374. ////
  375. //// glGenBuffers(1, &IndexVBOID);
  376. //// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
  377. //// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ushort)*4, pindices, GL_DYNAMIC_DRAW);
  378. ////
  379. //// //Define this somewhere in your header file
  380. //// #define BUFFER_OFFSET(i) ((char *)NULL + (i))
  381. while (device->IsRunning()) {
  382. WindowEventHandler::GetInstance()->ManageEvents();
  383. GetDevice()->GetMainWindow()->GetRenderContext()->MakeCurrent();
  384. // ----------- Begin Scene -----------
  385. renderer->BeginScene();
  386. renderer->SetMatrix(MT_PROJECTION, ortho);
  387. //renderer->SetMatrix(MT_MODEL, text);
  388. glDisable(GL_LIGHTING);
  389. glColor3f(1, 1, 1);
  390. //glDrawPixels(testImg->GetWidth(), testImg->GetHeight(), GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, testImg->GetBuffer());
  391. cam->Render();
  392. //renderer->SetMatrix(MT_PROJECTION, perspective);
  393. renderer->SetMatrix(MT_MODEL, identity);
  394. DrawAxis();
  395. //DrawGrid(-0.0f, 0.0f, 2/3.0f, 2/3.0f);
  396. //DrawVoxelGrid(0.0f, 0.0f, 0.0f, 2/3.0f, 2/3.0f, 2/3.0f);
  397. chunk.Render();
  398. ////glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
  399. ////glEnableClientState(GL_VERTEX_ARRAY);
  400. ////glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), BUFFER_OFFSET(0)); //The starting point of the VBO, for the vertices
  401. ////
  402. ////glEnableClientState(GL_COLOR_ARRAY);
  403. ////glColorPointer(3, GL_FLOAT, sizeof(MyVertex), BUFFER_OFFSET(12));
  404. ////glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
  405. //////To render, we can either use glDrawElements or glDrawRangeElements
  406. //////The is the number of indices. 3 indices needed to make a single triangle
  407. ////glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0)); //The starting point of the IBO
  408. renderer->EndScene();
  409. // ----------- End Scene -------------
  410. GetDevice()->GetMainWindow()->Update();
  411. GetDevice()->GetMainWindow()->SetTitle(Str(renderer->GetFPS()).c_str());
  412. }
  413. //glDeleteBuffers(1, &VertexVBOID);
  414. //glDeleteBuffers(1, &IndexVBOID);
  415. //delete testImg;
  416. device->Shutdown();
  417. //delete testImg;
  418. return 0;
  419. }
  420. void DrawAxis() {
  421. glBegin(GL_LINES);
  422. glColor3f(1, 0, 0);
  423. glVertex3f(0, 0, 0);
  424. glVertex3f(1, 0, 0);
  425. glColor3f(0, 1, 0);
  426. glVertex3f(0, 0, 0);
  427. glVertex3f(0, 1, 0);
  428. glColor3f(0, 0, 1);
  429. glVertex3f(0, 0, 0);
  430. glVertex3f(0, 0, 1);
  431. glEnd();
  432. }
  433. void DrawGrid(float xStart, float zStart, float xStep, float zStep) {
  434. glColor3f(.5f, .5f, .5f);
  435. const unsigned int steps = 128;
  436. for (unsigned int i = 0; i <= steps; i++) {
  437. glBegin(GL_LINES);
  438. glVertex3f(xStart + xStep * (float)i, -1.0f, zStart);
  439. glVertex3f(xStart + xStep * (float)i, -1.0f, zStart-steps*zStep);
  440. glEnd();
  441. glBegin(GL_LINES);
  442. glVertex3f(xStart, -1.0f, zStart - zStep * (float)i);
  443. glVertex3f(xStart+steps*xStep, -1.0f, zStart - zStep * (float)i);
  444. glEnd();
  445. }
  446. }
  447. void DrawWiredCube(float x, float y, float z) {
  448. glColor3f(.5f, .5f, .5f);
  449. glBegin(GL_LINES);
  450. glVertex3f(x, y, z);
  451. glVertex3f(x, y, z - CubeSize);
  452. glVertex3f(x + CubeSize, y, z);
  453. glVertex3f(x + CubeSize, y, z - CubeSize);
  454. glVertex3f(x, y + CubeSize, z);
  455. glVertex3f(x, y + CubeSize, z - CubeSize);
  456. glVertex3f(x + CubeSize, y + CubeSize, z);
  457. glVertex3f(x + CubeSize, y + CubeSize, z - CubeSize);
  458. glVertex3f(x , y, z);
  459. glVertex3f(x + CubeSize, y, z);
  460. glVertex3f(x , y, z - CubeSize);
  461. glVertex3f(x + CubeSize, y, z - CubeSize);
  462. glVertex3f(x , y + CubeSize , z);
  463. glVertex3f(x + CubeSize, y + CubeSize , z);
  464. glVertex3f(x , y + CubeSize , z - CubeSize);
  465. glVertex3f(x + CubeSize, y + CubeSize , z - CubeSize);
  466. glVertex3f(x, y , z);
  467. glVertex3f(x, y + CubeSize, z);
  468. glVertex3f(x + CubeSize, y , z);
  469. glVertex3f(x + CubeSize, y + CubeSize, z);
  470. glVertex3f(x, y , z - CubeSize);
  471. glVertex3f(x, y + CubeSize, z - CubeSize);
  472. glVertex3f(x + CubeSize, y , z - CubeSize);
  473. glVertex3f(x + CubeSize, y + CubeSize, z - CubeSize);
  474. glEnd();
  475. }
  476. void DrawCube(float x, float y, float z) {
  477. glBegin(GL_QUADS);
  478. //top
  479. glColor3f(.3f, .0f, .0f);
  480. glVertex3f(x , y + CubeSize, z);
  481. glColor3f(.25f, .0f, .0f);
  482. glVertex3f(x + CubeSize, y + CubeSize, z);
  483. glColor3f(.3f, .0f, .0f);
  484. glVertex3f(x + CubeSize, y + CubeSize, z - CubeSize);
  485. glColor3f(.25f, .0f, .0f);
  486. glVertex3f(x , y + CubeSize, z - CubeSize);
  487. //bottom
  488. glColor3f(.0f, .0f, .3f);
  489. glVertex3f(x , y, z);
  490. glColor3f(.0f, .0f, .25f);
  491. glVertex3f(x , y, z - CubeSize);
  492. glColor3f(.0f, .0f, .3f);
  493. glVertex3f(x + CubeSize, y, z - CubeSize);
  494. glColor3f(.0f, .0f, .25f);
  495. glVertex3f(x + CubeSize, y, z);
  496. //left
  497. glColor3f(.0f, .0f, .3f);
  498. glVertex3f(x, y , z);
  499. glColor3f(.0f, .0f, .25f);
  500. glVertex3f(x, y + CubeSize, z);
  501. glColor3f(.0f, .0f, .3f);
  502. glVertex3f(x, y + CubeSize, z - CubeSize);
  503. glColor3f(.0f, .0f, .25f);
  504. glVertex3f(x, y , z - CubeSize);
  505. //right
  506. glColor3f(.0f, .0f, .3f);
  507. glVertex3f(x + CubeSize, y , z);
  508. glColor3f(.0f, .0f, .25f);
  509. glVertex3f(x + CubeSize, y , z - CubeSize);
  510. glColor3f(.0f, .0f, .3f);
  511. glVertex3f(x + CubeSize, y + CubeSize, z - CubeSize);
  512. glColor3f(.0f, .0f, .25f);
  513. glVertex3f(x + CubeSize, y + CubeSize, z);
  514. //front
  515. glColor3f(.0f, .0f, .3f);
  516. glVertex3f(x , y , z);
  517. glColor3f(.0f, .0f, .25f);
  518. glVertex3f(x + CubeSize, y , z);
  519. glColor3f(.0f, .0f, .3f);
  520. glVertex3f(x + CubeSize, y + CubeSize, z);
  521. glColor3f(.0f, .0f, .25f);
  522. glVertex3f(x , y + CubeSize, z);
  523. //back
  524. glColor3f(.0f, .0f, .3f);
  525. glVertex3f(x , y , z - CubeSize);
  526. glColor3f(.0f, .0f, .25f);
  527. glVertex3f(x , y + CubeSize, z - CubeSize);
  528. glColor3f(.0f, .0f, .3f);
  529. glVertex3f(x + CubeSize, y + CubeSize, z - CubeSize);
  530. glColor3f(.0f, .0f, .25f);
  531. glVertex3f(x + CubeSize, y , z - CubeSize);
  532. glEnd();
  533. }
  534. void DrawVoxelGrid(float xStart, float yStart, float zStart, float xStep, float yStep, float zStep) {
  535. glColor3f(.5f, .5f, .5f);
  536. const unsigned int steps = 16;
  537. glBegin(GL_LINES);
  538. for (unsigned int i = 0; i <= steps; i++) {
  539. for(unsigned int j = 0; j <= steps; j++) {
  540. glVertex3f(xStart + xStep * (float)i, j * yStep, zStart);
  541. glVertex3f(xStart + xStep * (float)i, j * yStep, zStart-steps*zStep);
  542. glVertex3f(xStart + xStep * (float)j, yStart, zStart - zStep * (float)i);
  543. glVertex3f(xStart + xStep * (float)j, yStart+steps*yStep, zStart - zStep * (float)i);
  544. glVertex3f(xStart, j * yStep, zStart - zStep * (float)i);
  545. glVertex3f(xStart+steps*xStep, j * yStep, zStart - zStep * (float)i);
  546. }
  547. }
  548. glEnd();
  549. }