Wrapper.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. #include "Wrapper.h"
  2. #include <fstream>
  3. #include <cstring>
  4. #include <iostream>
  5. using namespace std;
  6. DummyClient DummyClient::dummy;
  7. IClient* Engine::client = &DummyClient::dummy;
  8. GLFWwindow* Engine::window = nullptr;
  9. GLuint Engine::vShader = 0;
  10. GLuint Engine::fShader = 0;
  11. GLuint Engine::program = 0;
  12. GLuint Engine::postVba = 0;
  13. GLuint Engine::postVbo = 0;
  14. GLuint Engine::postVShader = 0;
  15. GLuint Engine::postFShader = 0;
  16. GLuint Engine::postProgram = 0;
  17. GLuint Engine::frameBuffer = 0;
  18. GLuint Engine::frameTexture = 0;
  19. GLuint Engine::depthTexture = 0;
  20. int Engine::scale = 1;
  21. int Engine::width = 0;
  22. int Engine::height = 0;
  23. bool Engine::lineMode = false;
  24. bool Engine::init(int width, int height, const char* name)
  25. {
  26. Engine::width = width;
  27. Engine::height = height;
  28. updateScale();
  29. if(!glfwInit())
  30. {
  31. cout << "could not initialize GLFW" << endl;
  32. return false;
  33. }
  34. glfwDefaultWindowHints();
  35. glfwWindowHint(GLFW_VISIBLE, 0);
  36. glfwWindowHint(GLFW_RESIZABLE, 1);
  37. window = glfwCreateWindow(width, height, name, nullptr, nullptr);
  38. if(!window)
  39. {
  40. cout << "could not create window" << endl;
  41. glfwTerminate();
  42. return false;
  43. }
  44. glfwMakeContextCurrent(window);
  45. glfwSwapInterval(1);
  46. glfwShowWindow(window);
  47. GLenum err = glewInit();
  48. if(GLEW_OK != err)
  49. {
  50. cout << "could not initialize GLEW: " << glewGetErrorString(err) << endl;
  51. return false;
  52. }
  53. cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
  54. vShader = 0;
  55. fShader = 0;
  56. program = createProgram("shader/vertex.vs", "shader/fragment.fs", vShader, program);
  57. if(program == 0)
  58. {
  59. if(vShader != 0)
  60. {
  61. glDeleteShader(vShader);
  62. }
  63. if(fShader != 0)
  64. {
  65. glDeleteShader(fShader);
  66. }
  67. glfwDestroyWindow(window);
  68. glfwTerminate();
  69. return false;
  70. }
  71. postVShader = 0;
  72. postFShader = 0;
  73. postProgram = createProgram("shader/postVertex.vs", "shader/postFragment.fs", postVShader, postFShader);
  74. if(postProgram == 0)
  75. {
  76. if(postVShader != 0)
  77. {
  78. glDeleteShader(postVShader);
  79. }
  80. if(postFShader != 0)
  81. {
  82. glDeleteShader(postFShader);
  83. }
  84. glfwDestroyWindow(window);
  85. glfwTerminate();
  86. return false;
  87. }
  88. glfwSetKeyCallback(window, onKeyEvent);
  89. glfwSetMouseButtonCallback(window, onMouseClick);
  90. glfwSetFramebufferSizeCallback(window, onWindowResize);
  91. //glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  92. //glfwSetCursorPosCallback(window, onMouseMove);
  93. return true;
  94. }
  95. void Engine::start(IClient* client)
  96. {
  97. if(client != nullptr)
  98. {
  99. Engine::client = client;
  100. }
  101. generateFramebuffer();
  102. generatePostData();
  103. glEnable(GL_CULL_FACE);
  104. glDepthFunc(GL_LEQUAL);
  105. uint64_t newTime = glfwGetTimerValue();
  106. uint64_t oldTime = newTime;
  107. uint64_t lag = 0;
  108. while(!glfwWindowShouldClose(window))
  109. {
  110. oldTime = newTime;
  111. newTime = glfwGetTimerValue();
  112. lag += newTime - oldTime;
  113. int ticksPerFrame = 0;
  114. while(lag >= NANOS_PER_TICK)
  115. {
  116. lag -= NANOS_PER_TICK;
  117. Engine::client->tick();
  118. ticksPerFrame++;
  119. if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
  120. {
  121. long skip = lag / NANOS_PER_TICK;
  122. lag -= skip * NANOS_PER_TICK;
  123. if(skip > 0)
  124. {
  125. cout << "skipped " << skip << " game ticks " << lag << endl;
  126. }
  127. break;
  128. }
  129. }
  130. // draw scene in framebuffer
  131. glActiveTexture(GL_TEXTURE0);
  132. glUseProgram(program);
  133. glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  134. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  135. glEnable(GL_DEPTH_TEST);
  136. if(lineMode)
  137. {
  138. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  139. Engine::client->renderTick((float) lag / NANOS_PER_TICK);
  140. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  141. }
  142. else
  143. {
  144. Engine::client->renderTick((float) lag / NANOS_PER_TICK);
  145. }
  146. // draw textured framebuffer
  147. glActiveTexture(GL_TEXTURE1);
  148. glBindTexture(GL_TEXTURE_2D, frameTexture);
  149. glActiveTexture(GL_TEXTURE2);
  150. glBindTexture(GL_TEXTURE_2D, depthTexture);
  151. glUseProgram(postProgram);
  152. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  153. glClear(GL_COLOR_BUFFER_BIT);
  154. glDisable(GL_DEPTH_TEST);
  155. glBindVertexArray(postVba);
  156. glBindBuffer(GL_ARRAY_BUFFER, postVbo);
  157. glDrawArrays(GL_TRIANGLES, 0, 6);
  158. glfwSwapBuffers(window);
  159. glfwPollEvents();
  160. }
  161. destroyPostData();
  162. glDeleteFramebuffers(1, &frameBuffer);
  163. glDeleteShader(vShader);
  164. glDeleteShader(fShader);
  165. glDeleteProgram(program);
  166. glDeleteShader(postVShader);
  167. glDeleteShader(postFShader);
  168. glDeleteProgram(postProgram);
  169. glfwDestroyWindow(window);
  170. glfwTerminate();
  171. }
  172. void Engine::stop()
  173. {
  174. glfwSetWindowShouldClose(window, 1);
  175. }
  176. GLchar* Engine::readFile(const char* name)
  177. {
  178. ifstream in;
  179. in.open(name);
  180. if(!in.fail())
  181. {
  182. int size = 128;
  183. int index = 0;
  184. GLchar* content = new GLchar[size];
  185. while(true)
  186. {
  187. GLchar c = in.get();
  188. if(in.eof())
  189. {
  190. break;
  191. }
  192. if(index >= size - 1)
  193. {
  194. GLchar* newContent = new GLchar[size * 2];
  195. memcpy(newContent, content, size);
  196. size *= 2;
  197. delete[] content;
  198. content = newContent;
  199. }
  200. content[index] = c;
  201. index++;
  202. }
  203. content[index] = '\0';
  204. index++;
  205. in.close();
  206. return content;
  207. }
  208. return nullptr;
  209. }
  210. bool Engine::checkShaderErrors(const char* name, GLuint shader)
  211. {
  212. bool returnValue = false;
  213. cout << "compiling " << name << " shader ..." << endl;
  214. GLenum error = glGetError();
  215. if(error)
  216. {
  217. cout << "error: " << glGetError() << endl;
  218. returnValue = true;
  219. }
  220. else
  221. {
  222. cout << "no error occured ..." << endl;
  223. }
  224. GLint compiled[1];
  225. glGetShaderiv(shader, GL_COMPILE_STATUS, compiled);
  226. if(compiled[0])
  227. {
  228. cout << name << " shader successfully compiled" << endl;
  229. }
  230. else
  231. {
  232. cout << name << "compiling of " << name << " failed:" << endl;
  233. GLchar buffer[512];
  234. GLsizei bufferSize = 512;
  235. GLsizei charsUsed = 0;
  236. glGetShaderInfoLog(shader, bufferSize, &charsUsed, buffer);
  237. // glGetProgramInfoLog should be null terminated ...
  238. buffer[bufferSize - 1] = '\0';
  239. cout << buffer << endl;
  240. returnValue = true;
  241. }
  242. return returnValue;
  243. }
  244. GLuint Engine::compileProgram(GLuint& vShader, GLuint& fShader, const GLchar* v, const GLchar* f)
  245. {
  246. vShader = glCreateShader(GL_VERTEX_SHADER);
  247. glShaderSource(vShader, 1, &v, nullptr);
  248. glCompileShader(vShader);
  249. if(checkShaderErrors("vertex", vShader))
  250. {
  251. return 0;
  252. }
  253. fShader = glCreateShader(GL_FRAGMENT_SHADER);
  254. glShaderSource(fShader, 1, &f, nullptr);
  255. glCompileShader(fShader);
  256. if(checkShaderErrors("fragment", fShader))
  257. {
  258. return 0;
  259. }
  260. GLuint program = glCreateProgram();
  261. glAttachShader(program, vShader);
  262. glAttachShader(program, fShader);
  263. glLinkProgram(program);
  264. cout << "linking shaders to program ..." << endl;
  265. GLenum error = glGetError();
  266. if(error)
  267. {
  268. cout << "error: " << glGetError() << endl;
  269. return 0;
  270. }
  271. else
  272. {
  273. cout << "no error occured ..." << endl;
  274. }
  275. GLint compiled[1];
  276. glGetProgramiv(program, GL_LINK_STATUS, compiled);
  277. if(compiled[0])
  278. {
  279. cout << "shaders successfully linked" << endl;
  280. }
  281. else
  282. {
  283. cout << "linking of shaders failed:" << endl;
  284. GLchar buffer[512];
  285. GLsizei bufferSize = 512;
  286. GLsizei charsUsed = 0;
  287. glGetProgramInfoLog(program, bufferSize, &charsUsed, buffer);
  288. // glGetProgramInfoLog should be null terminated ...
  289. buffer[bufferSize - 1] = '\0';
  290. cout << buffer << endl;
  291. return 0;
  292. }
  293. return program;
  294. }
  295. GLuint Engine::createProgram(const char* v, const char* f, GLuint& vShader, GLuint& fShader)
  296. {
  297. GLchar* vertex = readFile(v);
  298. if(vertex == nullptr)
  299. {
  300. cout << "cannot read " << v << endl;
  301. return 0;
  302. }
  303. GLchar* fragment = readFile(f);
  304. if(fragment == nullptr)
  305. {
  306. cout << "cannot read " << f << endl;
  307. delete[] vertex;
  308. return 0;
  309. }
  310. GLuint program = compileProgram(vShader, fShader, vertex, fragment);
  311. delete[] vertex;
  312. delete[] fragment;
  313. return program;
  314. }
  315. void Engine::onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods)
  316. {
  317. client->onKeyEvent(key, scancode, action, mods);
  318. }
  319. void Engine::onMouseClick(GLFWwindow* w, int button, int action, int mods)
  320. {
  321. client->onMouseClick(button, action, mods);
  322. }
  323. void Engine::onWindowResize(GLFWwindow* w, int width, int height)
  324. {
  325. glViewport(0, 0, width, height);
  326. Engine::width = width;
  327. Engine::height = height;
  328. updateScale();
  329. glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  330. glBindTexture(GL_TEXTURE_2D, frameTexture);
  331. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  332. glBindTexture(GL_TEXTURE_2D, 0);
  333. glBindRenderbuffer(GL_RENDERBUFFER, depthTexture);
  334. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
  335. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  336. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  337. }
  338. void Engine::updateScale()
  339. {
  340. scale = 1;
  341. while(width / (scale + 1) >= 400 && height / (scale + 1) >= 300)
  342. {
  343. scale++;
  344. }
  345. }
  346. int Engine::getScale()
  347. {
  348. return scale;
  349. }
  350. int Engine::getWidth()
  351. {
  352. return width;
  353. }
  354. int Engine::getHeight()
  355. {
  356. return height;
  357. }
  358. GLint Engine::getUniformLocation(const GLchar* name)
  359. {
  360. return glGetUniformLocation(program, name);
  361. }
  362. void Engine::setMatrix(GLint location, const GLfloat* m)
  363. {
  364. glUniformMatrix4fv(location, 1, 0, m);
  365. }
  366. void Engine::setInt(GLint location, GLint i)
  367. {
  368. glUniform1i(location, i);
  369. }
  370. void Engine::setFloat(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4)
  371. {
  372. glUniform4f(location, f1, f2, f3, f4);
  373. }
  374. void Engine::printError()
  375. {
  376. GLenum error = glGetError();
  377. switch(error)
  378. {
  379. case GL_NO_ERROR:
  380. cout << "> No error has been recorded." << endl;
  381. break;
  382. case GL_INVALID_ENUM:
  383. cout << "> An unacceptable value is specified for an enumerated argument." << endl;
  384. break;
  385. case GL_INVALID_VALUE:
  386. cout << "> A numeric argument is out of range." << endl;
  387. break;
  388. case GL_INVALID_OPERATION:
  389. cout << "> The specified operation is not allowed in the current state." << endl;
  390. break;
  391. case GL_INVALID_FRAMEBUFFER_OPERATION:
  392. cout << "> The framebuffer object is not complete." << endl;
  393. break;
  394. case GL_OUT_OF_MEMORY:
  395. cout << "> There is not enough memory left to execute the command." << endl;
  396. break;
  397. case GL_STACK_UNDERFLOW:
  398. cout << "> An attempt has been made to perform an operation that would cause an internal stack to underflow." << endl;
  399. break;
  400. case GL_STACK_OVERFLOW:
  401. cout << "> An attempt has been made to perform an operation that would cause an internal stack to overflow." << endl;
  402. break;
  403. default:
  404. cout << "> Unknown OpenGL error: " << error << endl;
  405. }
  406. }
  407. void Engine::generateFramebuffer()
  408. {
  409. glGenFramebuffers(1, &frameBuffer);
  410. glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  411. glGenTextures(1, &frameTexture);
  412. glBindTexture(GL_TEXTURE_2D, frameTexture);
  413. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  414. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  415. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  416. glBindTexture(GL_TEXTURE_2D, 0);
  417. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, frameTexture, 0);
  418. glGenTextures(1, &depthTexture);
  419. glBindTexture(GL_TEXTURE_2D, depthTexture);
  420. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
  421. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  422. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  423. glBindTexture(GL_TEXTURE_2D, 0);
  424. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
  425. if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  426. {
  427. cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << endl;
  428. }
  429. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  430. }
  431. void Engine::generatePostData()
  432. {
  433. glGenVertexArrays(1, &postVba);
  434. glBindVertexArray(postVba);
  435. glGenBuffers(1, &postVbo);
  436. glBindBuffer(GL_ARRAY_BUFFER, postVbo);
  437. glVertexAttribPointer(0, 2, GL_FLOAT, 0, sizeof(float) * 4, (GLvoid*) 0);
  438. glEnableVertexAttribArray(0);
  439. glVertexAttribPointer(1, 2, GL_FLOAT, 0, sizeof(float) * 4, (GLvoid*) (sizeof(float) * 2));
  440. glEnableVertexAttribArray(1);
  441. float data[] =
  442. {
  443. -1.0f, 1.0f, 0.0f, 1.0f,
  444. -1.0f, -1.0f, 0.0f, 0.0f,
  445. 1.0f, -1.0f, 1.0f, 0.0f,
  446. -1.0f, 1.0f, 0.0f, 1.0f,
  447. 1.0f, -1.0f, 1.0f, 0.0f,
  448. 1.0f, 1.0f, 1.0f, 1.0f
  449. };
  450. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 24, data, GL_STATIC_DRAW);
  451. }
  452. void Engine::destroyPostData()
  453. {
  454. glDeleteVertexArrays(1, &postVba);
  455. glDeleteBuffers(1, &postVbo);
  456. }
  457. void Engine::setLineMode(bool mode)
  458. {
  459. lineMode = mode;
  460. }