esAux4.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. --------------------------------------------------
  3. James William Fletcher (github.com/mrbid)
  4. February 2023 - esAux4.h v4.0
  5. --------------------------------------------------
  6. A pretty good color converter: https://www.easyrgb.com/en/convert.php
  7. Lambertian fragment shaders make a difference, but only if you normalise the
  8. vertNorm in the fragment shader. Most of the time you won't notice the difference.
  9. Requires:
  10. - vec.h: https://gist.github.com/mrbid/77a92019e1ab8b86109bf103166bd04e
  11. - mat.h: https://gist.github.com/mrbid/cbc69ec9d99b0fda44204975fcbeae7c
  12. Modified for Tuxocide
  13. */
  14. #ifndef AUX_H
  15. #define AUX_H
  16. //#define VERTEX_SHADE // uncomment for vertex shaded, default is pixel shaded
  17. #include "vec.h"
  18. #include "mat.h"
  19. //*************************************
  20. // STRUCTURES
  21. //*************************************
  22. typedef struct
  23. {
  24. GLuint vid; // Vertex Array Buffer ID
  25. GLuint iid; // Index Array Buffer ID
  26. GLuint cid; // Colour Array Buffer ID
  27. GLuint nid; // Normal Array Buffer ID
  28. GLuint tid; // TexCoord Array Buffer ID
  29. } ESModel;
  30. //*************************************
  31. // UTILITY
  32. //*************************************
  33. GLuint esRand(const GLuint min, const GLuint max);
  34. GLfloat esRandFloat(const GLfloat min, const GLfloat max);
  35. void esBind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage);
  36. void esRebind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage);
  37. void esBindModel(ESModel* model, const GLfloat* vertices, const GLsizei vertlen, const GLushort* indices, const GLsizei indlen);
  38. GLuint esLoadTexture(const GLuint w, const GLuint h, const unsigned char* data, const GLuint linear);
  39. GLuint esLoadTextureWrapped(const GLuint w, const GLuint h, const unsigned char* data);
  40. GLuint esLoadTextureA(const GLuint w, const GLuint h, const unsigned char* data, const GLuint linear);
  41. //*************************************
  42. // SHADER
  43. //*************************************
  44. GLuint debugShader(GLuint shader_program);
  45. void makeAllShaders();
  46. void makeFullbright();
  47. void makeFullbright1();
  48. void makeLambert();
  49. void makeLambert1();
  50. void makeLambert2();
  51. void makeLambert3();
  52. void makeFullbrightT();
  53. void makeLambertT();
  54. // no texture
  55. void shadeFullbright(GLint* position, GLint* projection, GLint* modelview, GLint* color, GLint* opacity); // solid color + no shading
  56. void shadeFullbright1(GLint* position, GLint* projection, GLint* modelview, GLint* color, GLint* opacity); // colors + no shading
  57. void shadeLambert(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* color, GLint* opacity); // solid color + no normals
  58. void shadeLambert1(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity); // solid color + normals
  59. void shadeLambert2(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* color, GLint* opacity); // colors + no normals
  60. void shadeLambert3(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity); // colors + normals
  61. // textured
  62. void shadeFullbrightT(GLint* position, GLint* projection, GLint* modelview, GLint* texcoord, GLint* sampler);
  63. void shadeLambertT(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* texcoord, GLint* sampler, GLint* opacity);
  64. //*************************************
  65. // UTILITY CODE
  66. //*************************************
  67. GLuint esRand(const GLuint min, const GLuint max)
  68. {
  69. return (rand()%(max+1-min))+min;
  70. }
  71. GLfloat esRandFloat(const GLfloat min, const GLfloat max)
  72. {
  73. static GLfloat rrndmax = 1.f/(GLfloat)RAND_MAX;
  74. return (((GLfloat)rand()) * rrndmax) * (max-min) + min;
  75. }
  76. void esBind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage)
  77. {
  78. glGenBuffers(1, buffer);
  79. glBindBuffer(target, *buffer);
  80. glBufferData(target, datalen, data, usage);
  81. }
  82. void esRebind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage)
  83. {
  84. glBindBuffer(target, *buffer);
  85. glBufferData(target, datalen, data, usage);
  86. }
  87. void esBindModel(ESModel* model, const GLfloat* vertices, const GLsizei vertlen, const GLushort* indices, const GLsizei indlen)
  88. {
  89. esBind(GL_ARRAY_BUFFER, &model->vid, vertices, vertlen * sizeof(GLfloat) * 3, GL_STATIC_DRAW);
  90. esBind(GL_ELEMENT_ARRAY_BUFFER, &model->iid, indices, indlen * sizeof(GLushort), GL_STATIC_DRAW);
  91. }
  92. GLuint esLoadTexture(const GLuint w, const GLuint h, const unsigned char* data, const GLuint linear)
  93. {
  94. // https://registry.khronos.org/OpenGL-Refpages/es3/html/glTexParameter.xhtml
  95. // https://docs.gl/es2/glTexParameter
  96. GLuint textureId;
  97. // Use tightly packed data
  98. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  99. // Generate a texture object
  100. glGenTextures(1, &textureId);
  101. // Bind the texture object
  102. glBindTexture(GL_TEXTURE_2D, textureId);
  103. // Load the texture
  104. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  105. // Set the filtering mode
  106. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  107. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  108. if(linear == 1)
  109. {
  110. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  111. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  112. }
  113. else
  114. {
  115. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  116. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  117. }
  118. return textureId;
  119. }
  120. GLuint esLoadTextureA(const GLuint w, const GLuint h, const unsigned char* data, const GLuint linear)
  121. {
  122. GLuint textureId;
  123. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  124. glGenTextures(1, &textureId);
  125. glBindTexture(GL_TEXTURE_2D, textureId);
  126. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  127. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  129. if(linear == 1)
  130. {
  131. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  132. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  133. }
  134. else
  135. {
  136. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  137. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  138. }
  139. return textureId;
  140. }
  141. GLuint esLoadTextureWrapped(const GLuint w, const GLuint h, const unsigned char* data)
  142. {
  143. GLuint textureId;
  144. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  145. glGenTextures(1, &textureId);
  146. glBindTexture(GL_TEXTURE_2D, textureId);
  147. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  148. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  149. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  150. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  151. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  152. return textureId;
  153. }
  154. #ifdef GL_DEBUG
  155. // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDebugMessageControl.xhtml
  156. // https://registry.khronos.org/OpenGL-Refpages/es3/html/glDebugMessageControl.xhtml
  157. // OpenGL ES 3.2 or OpenGL 4.3 and above only.
  158. void GLAPIENTRY MessageCallback( GLenum source,
  159. GLenum type,
  160. GLuint id,
  161. GLenum severity,
  162. GLsizei length,
  163. const GLchar* message,
  164. const void* userParam )
  165. {
  166. printf("GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
  167. ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
  168. type, severity, message );
  169. }
  170. void esDebug(const GLuint state)
  171. {
  172. if(state == 1)
  173. {
  174. glEnable(GL_DEBUG_OUTPUT);
  175. glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
  176. glDebugMessageCallback(MessageCallback, 0);
  177. glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, NULL, GL_FALSE);
  178. }
  179. else
  180. {
  181. glDisable(GL_DEBUG_OUTPUT);
  182. glDisable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
  183. }
  184. }
  185. #endif
  186. GLuint debugShader(GLuint shader_program)
  187. {
  188. GLint linked;
  189. glGetProgramiv(shader_program, GL_LINK_STATUS, &linked);
  190. if(linked == GL_FALSE)
  191. {
  192. GLint infoLen = 0;
  193. glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &infoLen);
  194. if(infoLen > 1)
  195. {
  196. char* infoLog = malloc(sizeof(char) * infoLen);
  197. if(infoLog != NULL)
  198. {
  199. glGetProgramInfoLog(shader_program, infoLen, NULL, infoLog);
  200. printf("!!! error linking shader !!!\n%s\n", infoLog);
  201. free(infoLog);
  202. }
  203. }
  204. else
  205. {
  206. printf("!!! failed to link shader with no returned debug output !!!\n");
  207. }
  208. glDeleteProgram(shader_program);
  209. return linked;
  210. }
  211. return linked;
  212. }
  213. //*************************************
  214. // SHADER CODE
  215. //*************************************
  216. const GLchar* vt0 =
  217. "#version 100\n"
  218. "uniform mat4 modelview;\n"
  219. "uniform mat4 projection;\n"
  220. "attribute vec4 position;\n"
  221. "attribute vec2 texcoord;\n"
  222. "varying vec2 vtc;\n"
  223. "void main()\n"
  224. "{\n"
  225. "vtc = texcoord;\n"
  226. "gl_Position = projection * modelview * position;\n"
  227. "}\n";
  228. const GLchar* ft0 =
  229. "#version 100\n"
  230. "precision highp float;\n"
  231. "varying vec2 vtc;\n"
  232. "uniform sampler2D tex;\n"
  233. "void main()\n"
  234. "{\n"
  235. "gl_FragColor = texture2D(tex, vtc);\n"
  236. "}\n";
  237. //
  238. const GLchar* v0 =
  239. "#version 100\n"
  240. "uniform mat4 modelview;\n"
  241. "uniform mat4 projection;\n"
  242. "uniform vec3 color;\n"
  243. "uniform float opacity;\n"
  244. "attribute vec4 position;\n"
  245. "varying vec4 fragcolor;\n"
  246. "void main()\n"
  247. "{\n"
  248. "fragcolor = vec4(color, opacity);\n"
  249. "gl_Position = projection * modelview * position;\n"
  250. "}\n";
  251. const GLchar* f0 =
  252. "#version 100\n"
  253. "precision highp float;\n"
  254. "varying vec4 fragcolor;\n"
  255. "void main()\n"
  256. "{\n"
  257. "gl_FragColor = fragcolor;\n"
  258. "}\n";
  259. //
  260. const GLchar* v01 =
  261. "#version 100\n"
  262. "uniform mat4 modelview;\n"
  263. "uniform mat4 projection;\n"
  264. "attribute vec3 color;\n"
  265. "uniform float opacity;\n"
  266. "attribute vec4 position;\n"
  267. "varying vec3 vertCol;\n"
  268. "varying float vertOpa;\n"
  269. "void main()\n"
  270. "{\n"
  271. "vertCol = color;\n"
  272. "vertOpa = opacity;\n"
  273. "gl_Position = projection * modelview * position;\n"
  274. "}\n";
  275. const GLchar* f01 =
  276. "#version 100\n"
  277. "precision highp float;\n"
  278. "varying vec3 vertCol;\n"
  279. "varying float vertOpa;\n"
  280. "void main()\n"
  281. "{\n"
  282. "gl_FragColor = vec4(vertCol, vertOpa);\n"
  283. "}\n";
  284. //
  285. #ifdef VERTEX_SHADE
  286. // solid color + no normals
  287. const GLchar* v1 =
  288. "#version 100\n"
  289. "uniform mat4 modelview;\n"
  290. "uniform mat4 projection;\n"
  291. "uniform vec3 color;\n"
  292. "uniform float opacity;\n"
  293. "uniform vec3 lightpos;\n"
  294. "attribute vec4 position;\n"
  295. "varying vec4 fragcolor;\n"
  296. "void main()\n"
  297. "{\n"
  298. "vec4 vertPos4 = modelview * position;\n"
  299. "vec3 vertNorm = normalize(vec3(modelview * vec4(normalize(position.xyz), 0.0)));\n"
  300. "vec3 lightDir = normalize(lightpos - (vertPos4.xyz / vertPos4.w));\n"
  301. "fragcolor = vec4((color * 0.148) + max(dot(lightDir, vertNorm), 0.0) * color, opacity);\n"
  302. "gl_Position = projection * vertPos4;\n"
  303. "}\n";
  304. // solid color + normal array
  305. const GLchar* v11 =
  306. "#version 100\n"
  307. "uniform mat4 modelview;\n"
  308. "uniform mat4 projection;\n"
  309. "uniform vec3 color;\n"
  310. "uniform float opacity;\n"
  311. "uniform vec3 lightpos;\n"
  312. "attribute vec4 position;\n"
  313. "attribute vec3 normal;\n"
  314. "varying vec4 fragcolor;\n"
  315. "void main()\n"
  316. "{\n"
  317. "vec4 vertPos4 = modelview * position;\n"
  318. "vec3 vertNorm = normalize(vec3(modelview * vec4(normal, 0.0)));\n"
  319. "vec3 lightDir = normalize(lightpos - (vertPos4.xyz / vertPos4.w));\n"
  320. "fragcolor = vec4((color * 0.148) + max(dot(lightDir, vertNorm), 0.0) * color, opacity);\n"
  321. "gl_Position = projection * vertPos4;\n"
  322. "}\n";
  323. // color array + no normals
  324. const GLchar* v12 =
  325. "#version 100\n"
  326. "uniform mat4 modelview;\n"
  327. "uniform mat4 projection;\n"
  328. "uniform float opacity;\n"
  329. "uniform vec3 lightpos;\n"
  330. "attribute vec4 position;\n"
  331. "attribute vec3 color;\n"
  332. "varying vec4 fragcolor;\n"
  333. "void main()\n"
  334. "{\n"
  335. "vec4 vertPos4 = modelview * position;\n"
  336. "vec3 vertNorm = normalize(vec3(modelview * vec4(normalize(position.xyz), 0.0)));\n"
  337. "vec3 lightDir = normalize(lightpos - (vertPos4.xyz / vertPos4.w));\n"
  338. "fragcolor = vec4((color * 0.148) + max(dot(lightDir, vertNorm), 0.0) * color, opacity);\n"
  339. "gl_Position = projection * vertPos4;\n"
  340. "}\n";
  341. // color array + normal array
  342. const GLchar* v13 =
  343. "#version 100\n"
  344. "uniform mat4 modelview;\n"
  345. "uniform mat4 projection;\n"
  346. "uniform float opacity;\n"
  347. "uniform vec3 lightpos;\n"
  348. "attribute vec4 position;\n"
  349. "attribute vec3 normal;\n"
  350. "attribute vec3 color;\n"
  351. "varying vec4 fragcolor;\n"
  352. "void main()\n"
  353. "{\n"
  354. "vec4 vertPos4 = modelview * position;\n"
  355. "vec3 vertNorm = normalize(vec3(modelview * vec4(normal, 0.0)));\n"
  356. "vec3 lightDir = normalize(lightpos - (vertPos4.xyz / vertPos4.w));\n"
  357. "fragcolor = vec4((color * 0.148) + max(dot(lightDir, vertNorm), 0.0) * color, opacity);\n"
  358. //"fragcolor = vec4((color * 0.33) + max(dot(lightDir, vertNorm), 0.0) * color, opacity);\n"
  359. "gl_Position = projection * vertPos4;\n"
  360. "}\n";
  361. const GLchar* f1 =
  362. "#version 100\n"
  363. "precision highp float;\n"
  364. "varying vec4 fragcolor;\n"
  365. "void main()\n"
  366. "{\n"
  367. "gl_FragColor = fragcolor;\n"
  368. "}\n";
  369. // texture + normal array
  370. const GLchar* v14 =
  371. "#version 100\n"
  372. "uniform mat4 modelview;\n"
  373. "uniform mat4 projection;\n"
  374. "uniform float opacity;\n"
  375. "uniform vec3 lightpos;\n"
  376. "attribute vec4 position;\n"
  377. "attribute vec3 normal;\n"
  378. "attribute vec2 texcoord;\n"
  379. "varying float lambertian;\n"
  380. "varying float vertOpa;\n"
  381. "varying vec2 vtc;\n"
  382. "void main()\n"
  383. "{\n"
  384. "vtc = texcoord;\n"
  385. "vec4 vertPos4 = modelview * position;\n"
  386. "vec3 vertPos = vertPos4.xyz / vertPos4.w;\n"
  387. "vec3 vertNorm = vec3(modelview * vec4(normal, 0.0));\n"
  388. "vec3 lightDir = normalize(lightpos - vertPos);\n"
  389. "lambertian = max(dot(lightDir, normalize(vertNorm)), 0.0);\n"
  390. "vertOpa = opacity;\n"
  391. "gl_Position = projection * vertPos4;\n"
  392. "}\n";
  393. const GLchar* f14 =
  394. "#version 100\n"
  395. "precision highp float;\n"
  396. "varying float lambertian;\n"
  397. "varying float vertOpa;\n"
  398. "varying vec2 vtc;\n"
  399. "uniform sampler2D tex;\n"
  400. "void main()\n"
  401. "{\n"
  402. "vec4 tcol = texture2D(tex, vtc);\n"
  403. "gl_FragColor = vec4((tcol.xyz * 0.148) + lambertian*tcol.xyz, vertOpa);\n"
  404. "}\n";
  405. #else
  406. // solid color + no normals
  407. const GLchar* v1 =
  408. "#version 100\n"
  409. "uniform mat4 modelview;\n"
  410. "uniform mat4 projection;\n"
  411. "uniform vec3 color;\n"
  412. "uniform float opacity;\n"
  413. "uniform vec3 lightpos;\n"
  414. "attribute vec4 position;\n"
  415. "varying vec3 vertPos;\n"
  416. "varying vec3 vertNorm;\n"
  417. "varying vec3 vertCol;\n"
  418. "varying float vertOpa;\n"
  419. "varying vec3 vlightPos;\n"
  420. "void main()\n"
  421. "{\n"
  422. "vec4 vertPos4 = modelview * position;\n"
  423. "vertPos = vertPos4.xyz / vertPos4.w;\n"
  424. "vertNorm = vec3(modelview * vec4(normalize(position.xyz), 0.0));\n"
  425. "vertCol = color;\n"
  426. "vertOpa = opacity;\n"
  427. "vlightPos = lightpos;\n"
  428. "gl_Position = projection * vertPos4;\n"
  429. "}\n";
  430. // solid color + normal array
  431. const GLchar* v11 =
  432. "#version 100\n"
  433. "uniform mat4 modelview;\n"
  434. "uniform mat4 projection;\n"
  435. "uniform vec3 color;\n"
  436. "uniform float opacity;\n"
  437. "uniform vec3 lightpos;\n"
  438. "attribute vec4 position;\n"
  439. "attribute vec3 normal;\n"
  440. "varying vec3 vertPos;\n"
  441. "varying vec3 vertNorm;\n"
  442. "varying vec3 vertCol;\n"
  443. "varying float vertOpa;\n"
  444. "varying vec3 vlightPos;\n"
  445. "void main()\n"
  446. "{\n"
  447. "vec4 vertPos4 = modelview * position;\n"
  448. "vertPos = vertPos4.xyz / vertPos4.w;\n"
  449. "vertNorm = vec3(modelview * vec4(normal, 0.0));\n"
  450. "vertCol = color;\n"
  451. "vertOpa = opacity;\n"
  452. "vlightPos = lightpos;\n"
  453. "gl_Position = projection * vertPos4;\n"
  454. "}\n";
  455. // color array + no normals
  456. const GLchar* v12 =
  457. "#version 100\n"
  458. "uniform mat4 modelview;\n"
  459. "uniform mat4 projection;\n"
  460. "uniform float opacity;\n"
  461. "uniform vec3 lightpos;\n"
  462. "attribute vec4 position;\n"
  463. "attribute vec3 color;\n"
  464. "varying vec3 vertPos;\n"
  465. "varying vec3 vertNorm;\n"
  466. "varying vec3 vertCol;\n"
  467. "varying float vertOpa;\n"
  468. "varying vec3 vlightPos;\n"
  469. "void main()\n"
  470. "{\n"
  471. "vec4 vertPos4 = modelview * position;\n"
  472. "vertPos = vertPos4.xyz / vertPos4.w;\n"
  473. "vertNorm = vec3(modelview * vec4(normalize(position.xyz), 0.0));\n"
  474. "vertCol = color;\n"
  475. "vertOpa = opacity;\n"
  476. "vlightPos = lightpos;\n"
  477. "gl_Position = projection * vertPos4;\n"
  478. "}\n";
  479. // color array + normal array
  480. const GLchar* v13 =
  481. "#version 100\n"
  482. "uniform mat4 modelview;\n"
  483. "uniform mat4 projection;\n"
  484. "uniform float opacity;\n"
  485. "uniform vec3 lightpos;\n"
  486. "attribute vec4 position;\n"
  487. "attribute vec3 normal;\n"
  488. "attribute vec3 color;\n"
  489. "varying vec3 vertPos;\n"
  490. "varying vec3 vertNorm;\n"
  491. "varying vec3 vertCol;\n"
  492. "varying float vertOpa;\n"
  493. "varying vec3 vlightPos;\n"
  494. "void main()\n"
  495. "{\n"
  496. "vec4 vertPos4 = modelview * position;\n"
  497. "vertPos = vertPos4.xyz / vertPos4.w;\n"
  498. "vertNorm = vec3(modelview * vec4(normal, 0.0));\n"
  499. "vertCol = color;\n"
  500. "vertOpa = opacity;\n"
  501. "vlightPos = lightpos;\n"
  502. "gl_Position = projection * vertPos4;\n"
  503. "}\n";
  504. const GLchar* f1 =
  505. "#version 100\n"
  506. "precision highp float;\n"
  507. "varying vec3 vertPos;\n"
  508. "varying vec3 vertNorm;\n"
  509. "varying vec3 vertCol;\n"
  510. "varying float vertOpa;\n"
  511. "varying vec3 vlightPos;\n"
  512. "void main()\n"
  513. "{\n"
  514. "vec3 ambientColor = vertCol * 0.648;\n"
  515. //"vec3 ambientColor = vertCol * 0.548;\n" // hax
  516. "vec3 lightDir = normalize(vlightPos - vertPos);\n"
  517. "float lambertian = max(dot(lightDir, normalize(vertNorm)), 0.0);\n"
  518. //"float lambertian = min(max(dot(lightDir, normalize(vertNorm)), 0.0), 0.26);\n" // hax
  519. "gl_FragColor = vec4(ambientColor + lambertian*vertCol, vertOpa);\n"
  520. "}\n";
  521. // texture + normal array
  522. const GLchar* v14 =
  523. "#version 100\n"
  524. "uniform mat4 modelview;\n"
  525. "uniform mat4 projection;\n"
  526. "uniform float opacity;\n"
  527. "uniform vec3 lightpos;\n"
  528. "attribute vec4 position;\n"
  529. "attribute vec3 normal;\n"
  530. "attribute vec2 texcoord;\n"
  531. "varying vec3 vertPos;\n"
  532. "varying vec3 vertNorm;\n"
  533. "varying float vertOpa;\n"
  534. "varying vec3 vlightPos;\n"
  535. "varying vec2 vtc;\n"
  536. "void main()\n"
  537. "{\n"
  538. "vtc = texcoord;\n"
  539. "vec4 vertPos4 = modelview * position;\n"
  540. "vertPos = vertPos4.xyz / vertPos4.w;\n"
  541. "vertNorm = vec3(modelview * vec4(normal, 0.0));\n"
  542. "vertOpa = opacity;\n"
  543. "vlightPos = lightpos;\n"
  544. "gl_Position = projection * vertPos4;\n"
  545. "}\n";
  546. const GLchar* f14 =
  547. "#version 100\n"
  548. "precision highp float;\n"
  549. "varying vec3 vertPos;\n"
  550. "varying vec3 vertNorm;\n"
  551. "varying float vertOpa;\n"
  552. "varying vec3 vlightPos;\n"
  553. "varying vec2 vtc;\n"
  554. "uniform sampler2D tex;\n"
  555. "void main()\n"
  556. "{\n"
  557. "vec4 tcol = texture2D(tex, vtc);\n"
  558. "vec3 ambientColor = tcol.xyz * 0.148;\n"
  559. "vec3 lightDir = normalize(vlightPos - vertPos);\n"
  560. "float lambertian = max(dot(lightDir, normalize(vertNorm)), 0.0);\n"
  561. "gl_FragColor = vec4(ambientColor + lambertian*tcol.xyz, vertOpa);\n"
  562. //"gl_FragColor = vec4((ambientColor + lambertian*tcol.xyz) * clamp(1.0 - (length(vertPos)*0.09), 0.0, 1.0), vertOpa);\n"
  563. "}\n";
  564. #endif
  565. //
  566. GLuint shdFullbrightT;
  567. GLint shdFullbrightT_position;
  568. GLint shdFullbrightT_projection;
  569. GLint shdFullbrightT_modelview;
  570. GLint shdFullbrightT_texcoord;
  571. GLint shdFullbrightT_sampler;
  572. GLuint shdFullbright;
  573. GLint shdFullbright_position;
  574. GLint shdFullbright_projection;
  575. GLint shdFullbright_modelview;
  576. GLint shdFullbright_color;
  577. GLint shdFullbright_opacity;
  578. GLuint shdFullbright1;
  579. GLint shdFullbright1_position;
  580. GLint shdFullbright1_projection;
  581. GLint shdFullbright1_modelview;
  582. GLint shdFullbright1_color;
  583. GLint shdFullbright1_opacity;
  584. GLuint shdLambert;
  585. GLint shdLambert_position;
  586. GLint shdLambert_projection;
  587. GLint shdLambert_modelview;
  588. GLint shdLambert_lightpos;
  589. GLint shdLambert_color;
  590. GLint shdLambert_opacity;
  591. GLuint shdLambert1;
  592. GLint shdLambert1_position;
  593. GLint shdLambert1_projection;
  594. GLint shdLambert1_modelview;
  595. GLint shdLambert1_lightpos;
  596. GLint shdLambert1_color;
  597. GLint shdLambert1_normal;
  598. GLint shdLambert1_opacity;
  599. GLuint shdLambert2;
  600. GLint shdLambert2_position;
  601. GLint shdLambert2_projection;
  602. GLint shdLambert2_modelview;
  603. GLint shdLambert2_lightpos;
  604. GLint shdLambert2_color;
  605. GLint shdLambert2_opacity;
  606. GLuint shdLambert3;
  607. GLint shdLambert3_position;
  608. GLint shdLambert3_projection;
  609. GLint shdLambert3_modelview;
  610. GLint shdLambert3_lightpos;
  611. GLint shdLambert3_color;
  612. GLint shdLambert3_normal;
  613. GLint shdLambert3_opacity;
  614. GLuint shdLambertT;
  615. GLint shdLambertT_position;
  616. GLint shdLambertT_projection;
  617. GLint shdLambertT_modelview;
  618. GLint shdLambertT_lightpos;
  619. GLint shdLambertT_texcoord;
  620. GLint shdLambertT_sampler;
  621. GLint shdLambertT_normal;
  622. GLint shdLambertT_opacity;
  623. //
  624. void makeFullbrightT()
  625. {
  626. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  627. glShaderSource(vertexShader, 1, &vt0, NULL);
  628. glCompileShader(vertexShader);
  629. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  630. glShaderSource(fragmentShader, 1, &ft0, NULL);
  631. glCompileShader(fragmentShader);
  632. shdFullbrightT = glCreateProgram();
  633. glAttachShader(shdFullbrightT, vertexShader);
  634. glAttachShader(shdFullbrightT, fragmentShader);
  635. glLinkProgram(shdFullbrightT);
  636. if(debugShader(shdFullbrightT) == GL_FALSE){return;}
  637. shdFullbrightT_position = glGetAttribLocation(shdFullbrightT, "position");
  638. shdFullbrightT_texcoord = glGetAttribLocation(shdFullbrightT, "texcoord");
  639. shdFullbrightT_projection = glGetUniformLocation(shdFullbrightT, "projection");
  640. shdFullbrightT_modelview = glGetUniformLocation(shdFullbrightT, "modelview");
  641. shdFullbrightT_sampler = glGetUniformLocation(shdFullbrightT, "tex");
  642. }
  643. void makeFullbright()
  644. {
  645. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  646. glShaderSource(vertexShader, 1, &v0, NULL);
  647. glCompileShader(vertexShader);
  648. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  649. glShaderSource(fragmentShader, 1, &f0, NULL);
  650. glCompileShader(fragmentShader);
  651. shdFullbright = glCreateProgram();
  652. glAttachShader(shdFullbright, vertexShader);
  653. glAttachShader(shdFullbright, fragmentShader);
  654. glLinkProgram(shdFullbright);
  655. if(debugShader(shdFullbright) == GL_FALSE){return;}
  656. shdFullbright_position = glGetAttribLocation(shdFullbright, "position");
  657. shdFullbright_projection = glGetUniformLocation(shdFullbright, "projection");
  658. shdFullbright_modelview = glGetUniformLocation(shdFullbright, "modelview");
  659. shdFullbright_color = glGetUniformLocation(shdFullbright, "color");
  660. shdFullbright_opacity = glGetUniformLocation(shdFullbright, "opacity");
  661. }
  662. void makeFullbright1()
  663. {
  664. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  665. glShaderSource(vertexShader, 1, &v01, NULL);
  666. glCompileShader(vertexShader);
  667. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  668. glShaderSource(fragmentShader, 1, &f01, NULL);
  669. glCompileShader(fragmentShader);
  670. shdFullbright1 = glCreateProgram();
  671. glAttachShader(shdFullbright1, vertexShader);
  672. glAttachShader(shdFullbright1, fragmentShader);
  673. glLinkProgram(shdFullbright1);
  674. if(debugShader(shdFullbright1) == GL_FALSE){return;}
  675. shdFullbright1_position = glGetAttribLocation(shdFullbright1, "position");
  676. shdFullbright1_color = glGetAttribLocation(shdFullbright1, "color");
  677. shdFullbright1_projection = glGetUniformLocation(shdFullbright1, "projection");
  678. shdFullbright1_modelview = glGetUniformLocation(shdFullbright1, "modelview");
  679. shdFullbright1_opacity = glGetUniformLocation(shdFullbright1, "opacity");
  680. }
  681. void makeLambert()
  682. {
  683. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  684. glShaderSource(vertexShader, 1, &v1, NULL);
  685. glCompileShader(vertexShader);
  686. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  687. glShaderSource(fragmentShader, 1, &f1, NULL);
  688. glCompileShader(fragmentShader);
  689. shdLambert = glCreateProgram();
  690. glAttachShader(shdLambert, vertexShader);
  691. glAttachShader(shdLambert, fragmentShader);
  692. glLinkProgram(shdLambert);
  693. if(debugShader(shdLambert) == GL_FALSE){return;}
  694. shdLambert_position = glGetAttribLocation(shdLambert, "position");
  695. shdLambert_projection = glGetUniformLocation(shdLambert, "projection");
  696. shdLambert_modelview = glGetUniformLocation(shdLambert, "modelview");
  697. shdLambert_lightpos = glGetUniformLocation(shdLambert, "lightpos");
  698. shdLambert_color = glGetUniformLocation(shdLambert, "color");
  699. shdLambert_opacity = glGetUniformLocation(shdLambert, "opacity");
  700. }
  701. void makeLambert1()
  702. {
  703. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  704. glShaderSource(vertexShader, 1, &v11, NULL);
  705. glCompileShader(vertexShader);
  706. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  707. glShaderSource(fragmentShader, 1, &f1, NULL);
  708. glCompileShader(fragmentShader);
  709. shdLambert1 = glCreateProgram();
  710. glAttachShader(shdLambert1, vertexShader);
  711. glAttachShader(shdLambert1, fragmentShader);
  712. glLinkProgram(shdLambert1);
  713. if(debugShader(shdLambert1) == GL_FALSE){return;}
  714. shdLambert1_position = glGetAttribLocation(shdLambert1, "position");
  715. shdLambert1_normal = glGetAttribLocation(shdLambert1, "normal");
  716. shdLambert1_projection = glGetUniformLocation(shdLambert1, "projection");
  717. shdLambert1_modelview = glGetUniformLocation(shdLambert1, "modelview");
  718. shdLambert1_lightpos = glGetUniformLocation(shdLambert1, "lightpos");
  719. shdLambert1_color = glGetUniformLocation(shdLambert1, "color");
  720. shdLambert1_opacity = glGetUniformLocation(shdLambert1, "opacity");
  721. }
  722. void makeLambert2()
  723. {
  724. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  725. glShaderSource(vertexShader, 1, &v12, NULL);
  726. glCompileShader(vertexShader);
  727. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  728. glShaderSource(fragmentShader, 1, &f1, NULL);
  729. glCompileShader(fragmentShader);
  730. shdLambert2 = glCreateProgram();
  731. glAttachShader(shdLambert2, vertexShader);
  732. glAttachShader(shdLambert2, fragmentShader);
  733. glLinkProgram(shdLambert2);
  734. if(debugShader(shdLambert2) == GL_FALSE){return;}
  735. shdLambert2_position = glGetAttribLocation(shdLambert2, "position");
  736. shdLambert2_color = glGetAttribLocation(shdLambert2, "color");
  737. shdLambert2_projection = glGetUniformLocation(shdLambert2, "projection");
  738. shdLambert2_modelview = glGetUniformLocation(shdLambert2, "modelview");
  739. shdLambert2_lightpos = glGetUniformLocation(shdLambert2, "lightpos");
  740. shdLambert2_opacity = glGetUniformLocation(shdLambert2, "opacity");
  741. }
  742. void makeLambert3()
  743. {
  744. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  745. glShaderSource(vertexShader, 1, &v13, NULL);
  746. glCompileShader(vertexShader);
  747. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  748. glShaderSource(fragmentShader, 1, &f1, NULL);
  749. glCompileShader(fragmentShader);
  750. shdLambert3 = glCreateProgram();
  751. glAttachShader(shdLambert3, vertexShader);
  752. glAttachShader(shdLambert3, fragmentShader);
  753. glLinkProgram(shdLambert3);
  754. if(debugShader(shdLambert3) == GL_FALSE){return;}
  755. shdLambert3_position = glGetAttribLocation(shdLambert3, "position");
  756. shdLambert3_normal = glGetAttribLocation(shdLambert3, "normal");
  757. shdLambert3_color = glGetAttribLocation(shdLambert3, "color");
  758. shdLambert3_projection = glGetUniformLocation(shdLambert3, "projection");
  759. shdLambert3_modelview = glGetUniformLocation(shdLambert3, "modelview");
  760. shdLambert3_lightpos = glGetUniformLocation(shdLambert3, "lightpos");
  761. shdLambert3_opacity = glGetUniformLocation(shdLambert3, "opacity");
  762. }
  763. void makeLambertT()
  764. {
  765. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  766. glShaderSource(vertexShader, 1, &v14, NULL);
  767. glCompileShader(vertexShader);
  768. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  769. glShaderSource(fragmentShader, 1, &f14, NULL);
  770. glCompileShader(fragmentShader);
  771. shdLambertT = glCreateProgram();
  772. glAttachShader(shdLambertT, vertexShader);
  773. glAttachShader(shdLambertT, fragmentShader);
  774. glLinkProgram(shdLambertT);
  775. if(debugShader(shdLambertT) == GL_FALSE){return;}
  776. shdLambertT_position = glGetAttribLocation(shdLambertT, "position");
  777. shdLambertT_normal = glGetAttribLocation(shdLambertT, "normal");
  778. shdLambertT_texcoord = glGetAttribLocation(shdLambertT, "texcoord");
  779. shdLambertT_projection = glGetUniformLocation(shdLambertT, "projection");
  780. shdLambertT_modelview = glGetUniformLocation(shdLambertT, "modelview");
  781. shdLambertT_lightpos = glGetUniformLocation(shdLambertT, "lightpos");
  782. shdLambertT_opacity = glGetUniformLocation(shdLambertT, "opacity");
  783. shdLambertT_sampler = glGetUniformLocation(shdLambertT, "tex");
  784. }
  785. void makeAllShaders()
  786. {
  787. makeFullbright();
  788. makeFullbright1();
  789. makeLambert();
  790. makeLambert1();
  791. makeLambert2();
  792. makeLambert3();
  793. makeFullbrightT();
  794. makeLambertT();
  795. }
  796. void shadeFullbrightT(GLint* position, GLint* projection, GLint* modelview, GLint* texcoord, GLint* sampler)
  797. {
  798. *position = shdFullbrightT_position;
  799. *projection = shdFullbrightT_projection;
  800. *modelview = shdFullbrightT_modelview;
  801. *texcoord = shdFullbrightT_texcoord;
  802. *sampler = shdFullbrightT_sampler;
  803. glUseProgram(shdFullbrightT);
  804. }
  805. void shadeFullbright(GLint* position, GLint* projection, GLint* modelview, GLint* color, GLint* opacity)
  806. {
  807. *position = shdFullbright_position;
  808. *projection = shdFullbright_projection;
  809. *modelview = shdFullbright_modelview;
  810. *color = shdFullbright_color;
  811. *opacity = shdFullbright_opacity;
  812. glUseProgram(shdFullbright);
  813. }
  814. void shadeFullbright1(GLint* position, GLint* projection, GLint* modelview, GLint* color, GLint* opacity)
  815. {
  816. *position = shdFullbright1_position;
  817. *projection = shdFullbright1_projection;
  818. *modelview = shdFullbright1_modelview;
  819. *color = shdFullbright1_color;
  820. *opacity = shdFullbright1_opacity;
  821. glUseProgram(shdFullbright1);
  822. }
  823. void shadeLambert(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* color, GLint* opacity)
  824. {
  825. *position = shdLambert_position;
  826. *projection = shdLambert_projection;
  827. *modelview = shdLambert_modelview;
  828. *lightpos = shdLambert_lightpos;
  829. *color = shdLambert_color;
  830. *opacity = shdLambert_opacity;
  831. glUseProgram(shdLambert);
  832. }
  833. void shadeLambert1(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity)
  834. {
  835. *position = shdLambert1_position;
  836. *projection = shdLambert1_projection;
  837. *modelview = shdLambert1_modelview;
  838. *lightpos = shdLambert1_lightpos;
  839. *color = shdLambert1_color;
  840. *normal = shdLambert1_normal;
  841. *opacity = shdLambert1_opacity;
  842. glUseProgram(shdLambert1);
  843. }
  844. void shadeLambert2(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* color, GLint* opacity)
  845. {
  846. *position = shdLambert2_position;
  847. *projection = shdLambert2_projection;
  848. *modelview = shdLambert2_modelview;
  849. *lightpos = shdLambert2_lightpos;
  850. *color = shdLambert2_color;
  851. *opacity = shdLambert2_opacity;
  852. glUseProgram(shdLambert2);
  853. }
  854. void shadeLambert3(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity)
  855. {
  856. *position = shdLambert3_position;
  857. *projection = shdLambert3_projection;
  858. *modelview = shdLambert3_modelview;
  859. *lightpos = shdLambert3_lightpos;
  860. *color = shdLambert3_color;
  861. *normal = shdLambert3_normal;
  862. *opacity = shdLambert3_opacity;
  863. glUseProgram(shdLambert3);
  864. }
  865. void shadeLambertT(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* texcoord, GLint* sampler, GLint* opacity)
  866. {
  867. *position = shdLambertT_position;
  868. *projection = shdLambertT_projection;
  869. *modelview = shdLambertT_modelview;
  870. *lightpos = shdLambertT_lightpos;
  871. *texcoord = shdLambertT_texcoord;
  872. *sampler = shdLambertT_sampler;
  873. *normal = shdLambertT_normal;
  874. *opacity = shdLambertT_opacity;
  875. glUseProgram(shdLambertT);
  876. }
  877. #endif