esAux2.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. --------------------------------------------------
  3. James William Fletcher (github.com/mrbid)
  4. October 2021 - esAux2.h v2.0
  5. --------------------------------------------------
  6. Requires:
  7. - vec.h: https://gist.github.com/mrbid/77a92019e1ab8b86109bf103166bd04e
  8. - mat.h: https://gist.github.com/mrbid/cbc69ec9d99b0fda44204975fcbeae7c
  9. v2.0:
  10. - added support for fullbright texture mapping
  11. - added esRebind()
  12. v1.0:
  13. The shaders are the product of a little help from:
  14. http://www.cs.toronto.edu/~jacobson/phong-demo/
  15. https://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/code/WebGLShaderLightMat/ShaderLightMat.html
  16. https://www.gsn-lib.org/docs/nodes/ShaderPluginNode.php
  17. https://www.khronos.org/registry/OpenGL/specs/es/2.0/GLSL_ES_Specification_1.00.pdf
  18. https://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf
  19. https://www.khronos.org/registry/OpenGL-Refpages/es2.0/
  20. The phong uses BlinnPhong by default, if you wish to use
  21. vanilla phong just specify the following definition in
  22. your source file: #define REGULAR_PHONG
  23. */
  24. #ifndef AUX_H
  25. #define AUX_H
  26. #include "vec.h"
  27. #include "mat.h"
  28. //*************************************
  29. // MATRIX
  30. //*************************************
  31. // structures
  32. typedef struct
  33. {
  34. GLuint vid; // Vertex Array Buffer ID
  35. GLuint iid; // Index Array Buffer ID
  36. GLuint cid; // Colour Array Buffer ID
  37. GLuint nid; // Normal Array Buffer ID
  38. GLuint tid; // TexCoord Array Buffer ID
  39. } ESModel;
  40. //*************************************
  41. // UTILITY
  42. //*************************************
  43. GLuint esRand(const GLuint min, const GLuint max);
  44. GLfloat esRandFloat(const GLfloat min, const GLfloat max);
  45. void esBind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage);
  46. void esRebind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage);
  47. void esBindModel(ESModel* model, const GLfloat* vertices, const GLsizei vertlen, const GLushort* indices, const GLsizei indlen);
  48. GLuint esLoadTexture(const GLuint w, const GLuint h, const unsigned char* data);
  49. GLuint esLoadTextureA(const GLuint w, const GLuint h, const unsigned char* data);
  50. //*************************************
  51. // SHADER
  52. //*************************************
  53. void makeAllShaders();
  54. void makeFullbright();
  55. void makeLambert();
  56. void makeLambert1();
  57. void makeLambert2();
  58. void makeLambert3();
  59. void makePhong();
  60. void makePhong1();
  61. void makePhong2();
  62. void makePhong3();
  63. void shadeFullbrightT(GLint* position, GLint* projection, GLint* modelview, GLint* texcoord, GLint* sampler); // texture + no shading
  64. void shadeFullbright(GLint* position, GLint* projection, GLint* modelview, GLint* color, GLint* opacity); // solid color + no shading
  65. void shadeLambert(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* color, GLint* opacity); // solid color + no normals
  66. void shadeLambert1(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity); // solid color + normals
  67. void shadeLambert2(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* color, GLint* opacity); // colors + no normals
  68. void shadeLambert3(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity); // colors + normals
  69. void shadePhong(GLint* position, GLint* projection, GLint* modelview, GLint* normalmat, GLint* lightpos, GLint* color, GLint* opacity); // solid color + no normals
  70. void shadePhong1(GLint* position, GLint* projection, GLint* modelview, GLint* normalmat, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity); // solid color + normals
  71. void shadePhong2(GLint* position, GLint* projection, GLint* modelview, GLint* normalmat, GLint* lightpos, GLint* color, GLint* opacity); // colors + no normals
  72. void shadePhong3(GLint* position, GLint* projection, GLint* modelview, GLint* normalmat, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity); // colors + normals
  73. //*************************************
  74. // UTILITY CODE
  75. //*************************************
  76. GLuint esRand(const GLuint min, const GLuint max)
  77. {
  78. return (rand()%(max+1-min))+min;
  79. }
  80. GLfloat esRandFloat(const GLfloat min, const GLfloat max)
  81. {
  82. static GLfloat rrndmax = 1.f/(GLfloat)RAND_MAX;
  83. return ( (((GLfloat)rand()) * rrndmax) * (max-min) ) + min;
  84. }
  85. void esBind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage)
  86. {
  87. glGenBuffers(1, buffer);
  88. glBindBuffer(target, *buffer);
  89. glBufferData(target, datalen, data, usage);
  90. }
  91. void esRebind(const GLenum target, GLuint* buffer, const void* data, const GLsizeiptr datalen, const GLenum usage)
  92. {
  93. glBindBuffer(target, *buffer);
  94. glBufferData(target, datalen, data, usage);
  95. }
  96. void esBindModel(ESModel* model, const GLfloat* vertices, const GLsizei vertlen, const GLushort* indices, const GLsizei indlen)
  97. {
  98. esBind(GL_ARRAY_BUFFER, &model->vid, vertices, vertlen * sizeof(GLfloat) * 3, GL_STATIC_DRAW);
  99. esBind(GL_ELEMENT_ARRAY_BUFFER, &model->iid, indices, indlen * sizeof(GLushort), GL_STATIC_DRAW);
  100. }
  101. GLuint esLoadTexture(const GLuint w, const GLuint h, const unsigned char* data)
  102. {
  103. GLuint textureId;
  104. // Use tightly packed data
  105. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  106. // Generate a texture object
  107. glGenTextures(1, &textureId);
  108. // Bind the texture object
  109. glBindTexture(GL_TEXTURE_2D, textureId);
  110. // Load the texture
  111. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  112. // Set the filtering mode
  113. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  114. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  115. return textureId;
  116. }
  117. GLuint esLoadTextureA(const GLuint w, const GLuint h, const unsigned char* data)
  118. {
  119. GLuint textureId;
  120. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  121. glGenTextures(1, &textureId);
  122. glBindTexture(GL_TEXTURE_2D, textureId);
  123. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  124. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  125. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  126. return textureId;
  127. }
  128. //*************************************
  129. // SHADER CODE
  130. //*************************************
  131. const GLchar* vt0 =
  132. "#version 100\n"
  133. "uniform mat4 modelview;\n"
  134. "uniform mat4 projection;\n"
  135. "attribute vec4 position;\n"
  136. "attribute vec2 texcoord;\n"
  137. "varying vec2 vtc;\n"
  138. "void main()\n"
  139. "{\n"
  140. "vtc = texcoord;\n"
  141. "gl_Position = projection * modelview * position;\n"
  142. "}\n";
  143. const GLchar* ft0 =
  144. "#version 100\n"
  145. "precision mediump float;\n"
  146. "varying vec2 vtc;\n"
  147. "uniform sampler2D tex;\n"
  148. "void main()\n"
  149. "{\n"
  150. "gl_FragColor = texture2D(tex, vtc);\n"
  151. "}\n";
  152. //
  153. const GLchar* v0 =
  154. "#version 100\n"
  155. "uniform mat4 modelview;\n"
  156. "uniform mat4 projection;\n"
  157. "uniform vec3 color;\n"
  158. "uniform float opacity;\n"
  159. "attribute vec4 position;\n"
  160. "varying vec3 vertCol;\n"
  161. "varying float vertOpa;\n"
  162. "void main()\n"
  163. "{\n"
  164. "vertCol = color;\n"
  165. "vertOpa = opacity;\n"
  166. "gl_Position = projection * modelview * position;\n"
  167. "}\n";
  168. const GLchar* f0 =
  169. "#version 100\n"
  170. "precision mediump float;\n"
  171. "varying vec3 vertCol;\n"
  172. "varying float vertOpa;\n"
  173. "void main()\n"
  174. "{\n"
  175. "gl_FragColor = vec4(vertCol, vertOpa);\n"
  176. "}\n";
  177. //
  178. // solid color + no normals
  179. const GLchar* v1 =
  180. "#version 100\n"
  181. "uniform mat4 modelview;\n"
  182. "uniform mat4 projection;\n"
  183. "uniform vec3 color;\n"
  184. "uniform float opacity;\n"
  185. "uniform vec3 lightpos;\n"
  186. "attribute vec4 position;\n"
  187. "varying vec3 vertPos;\n"
  188. "varying vec3 vertNorm;\n"
  189. "varying vec3 vertCol;\n"
  190. "varying float vertOpa;\n"
  191. "varying vec3 vlightPos;\n"
  192. "void main()\n"
  193. "{\n"
  194. "vec4 vertPos4 = modelview * position;\n"
  195. "vertPos = vec3(vertPos4) / vertPos4.w;\n"
  196. "vertNorm = vec3(modelview * vec4(position.xyz, 0.0));\n"
  197. "vertCol = color;\n"
  198. "vertOpa = opacity;\n"
  199. "vlightPos = lightpos;\n"
  200. "gl_Position = projection * modelview * position;\n"
  201. "}\n";
  202. // solid color + normal array
  203. const GLchar* v11 =
  204. "#version 100\n"
  205. "uniform mat4 modelview;\n"
  206. "uniform mat4 projection;\n"
  207. "uniform vec3 color;\n"
  208. "uniform float opacity;\n"
  209. "uniform vec3 lightpos;\n"
  210. "attribute vec4 position;\n"
  211. "attribute vec3 normal;\n"
  212. "varying vec3 vertPos;\n"
  213. "varying vec3 vertNorm;\n"
  214. "varying vec3 vertCol;\n"
  215. "varying float vertOpa;\n"
  216. "varying vec3 vlightPos;\n"
  217. "void main()\n"
  218. "{\n"
  219. "vec4 vertPos4 = modelview * position;\n"
  220. "vertPos = vec3(vertPos4) / vertPos4.w;\n"
  221. "vertNorm = vec3(modelview * vec4(normal.xyz, 0.0));\n"
  222. "vertCol = color;\n"
  223. "vertOpa = opacity;\n"
  224. "vlightPos = lightpos;\n"
  225. "gl_Position = projection * modelview * position;\n"
  226. "}\n";
  227. // color array + normal array
  228. const GLchar* v12 =
  229. "#version 100\n"
  230. "uniform mat4 modelview;\n"
  231. "uniform mat4 projection;\n"
  232. "uniform float opacity;\n"
  233. "uniform vec3 lightpos;\n"
  234. "attribute vec4 position;\n"
  235. "attribute vec3 normal;\n"
  236. "attribute vec3 color;\n"
  237. "varying vec3 vertPos;\n"
  238. "varying vec3 vertNorm;\n"
  239. "varying vec3 vertCol;\n"
  240. "varying float vertOpa;\n"
  241. "varying vec3 vlightPos;\n"
  242. "void main()\n"
  243. "{\n"
  244. "vec4 vertPos4 = modelview * position;\n"
  245. "vertPos = vec3(vertPos4) / vertPos4.w;\n"
  246. "vertNorm = vec3(modelview * vec4(normal.xyz, 0.0));\n"
  247. "vertCol = color;\n"
  248. "vertOpa = opacity;\n"
  249. "vlightPos = lightpos;\n"
  250. "gl_Position = projection * modelview * position;\n"
  251. "}\n";
  252. // color array + no normals
  253. const GLchar* v13 =
  254. "#version 100\n"
  255. "uniform mat4 modelview;\n"
  256. "uniform mat4 projection;\n"
  257. "uniform float opacity;\n"
  258. "uniform vec3 lightpos;\n"
  259. "attribute vec4 position;\n"
  260. "attribute vec3 color;\n"
  261. "varying vec3 vertPos;\n"
  262. "varying vec3 vertNorm;\n"
  263. "varying vec3 vertCol;\n"
  264. "varying float vertOpa;\n"
  265. "varying vec3 vlightPos;\n"
  266. "void main()\n"
  267. "{\n"
  268. "vec4 vertPos4 = modelview * position;\n"
  269. "vertPos = vec3(vertPos4) / vertPos4.w;\n"
  270. "vertNorm = vec3(modelview * vec4(position.xyz, 0.0));\n"
  271. "vertCol = color;\n"
  272. "vertOpa = opacity;\n"
  273. "vlightPos = lightpos;\n"
  274. "gl_Position = projection * modelview * position;\n"
  275. "}\n";
  276. const GLchar* f1 =
  277. "#version 100\n"
  278. "precision mediump float;\n"
  279. "varying vec3 vertPos;\n"
  280. "varying vec3 vertNorm;\n"
  281. "varying vec3 vertCol;\n"
  282. "varying float vertOpa;\n"
  283. "varying vec3 vlightPos;\n"
  284. //"float rand(vec2 co){return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);}\n"
  285. "void main()\n"
  286. "{\n"
  287. //"vec3 ambientColor = vertCol * 0.148*sin(vertNorm);\n" // lol
  288. "vec3 ambientColor = vertCol * 0.148;\n"
  289. "vec3 diffuseColor = vertCol;\n"
  290. "vec3 normal = normalize(vertNorm);\n"
  291. "vec3 lightDir = normalize(vlightPos - vertPos);\n"
  292. "float lambertian = max(dot(lightDir,normal), 0.0);\n"
  293. "gl_FragColor = vec4(ambientColor + lambertian*diffuseColor, vertOpa);\n"
  294. "}\n";
  295. const GLchar* v2 =
  296. "#version 100\n"
  297. "uniform mat4 modelview;\n"
  298. "uniform mat4 projection;\n"
  299. "uniform mat4 normalmat;\n"
  300. "uniform float opacity;\n"
  301. "uniform vec3 lightpos;\n"
  302. "uniform vec3 color;\n"
  303. "attribute vec4 position;\n"
  304. "varying vec3 normalInterp;\n"
  305. "varying vec3 vertPos;\n"
  306. "varying vec3 vertCol;\n"
  307. "varying float vertOpa;\n"
  308. "varying vec3 vlightPos;\n"
  309. "void main()\n"
  310. "{\n"
  311. "vec4 vertPos4 = modelview * position;\n"
  312. "vertPos = vec3(vertPos4) / vertPos4.w;\n"
  313. "vertCol = color;\n"
  314. "vertOpa = opacity;\n"
  315. "vlightPos = lightpos;\n"
  316. "normalInterp = vec3(normalmat * vec4(position.xyz, 0.0));\n"
  317. "gl_Position = projection * modelview * position;\n"
  318. "}\n";
  319. const GLchar* v21 =
  320. "#version 100\n"
  321. "uniform mat4 modelview;\n"
  322. "uniform mat4 projection;\n"
  323. "uniform mat4 normalmat;\n"
  324. "uniform float opacity;\n"
  325. "uniform vec3 lightpos;\n"
  326. "uniform vec3 color;\n"
  327. "attribute vec4 position;\n"
  328. "attribute vec3 normal;\n"
  329. "varying vec3 normalInterp;\n"
  330. "varying vec3 vertPos;\n"
  331. "varying vec3 vertCol;\n"
  332. "varying float vertOpa;\n"
  333. "varying vec3 vlightPos;\n"
  334. "void main()\n"
  335. "{\n"
  336. "vec4 vertPos4 = modelview * position;\n"
  337. "vertPos = vec3(vertPos4) / vertPos4.w;\n"
  338. "vertCol = color;\n"
  339. "vertOpa = opacity;\n"
  340. "vlightPos = lightpos;\n"
  341. "normalInterp = vec3(normalmat * vec4(normal.xyz, 0.0));\n"
  342. "gl_Position = projection * modelview * position;\n"
  343. "}\n";
  344. const GLchar* v22 =
  345. "#version 100\n"
  346. "uniform mat4 modelview;\n"
  347. "uniform mat4 projection;\n"
  348. "uniform mat4 normalmat;\n"
  349. "uniform float opacity;\n"
  350. "uniform vec3 lightpos;\n"
  351. "attribute vec4 position;\n"
  352. "attribute vec3 color;\n"
  353. "varying vec3 normalInterp;\n"
  354. "varying vec3 vertPos;\n"
  355. "varying vec3 vertCol;\n"
  356. "varying float vertOpa;\n"
  357. "varying vec3 vlightPos;\n"
  358. "void main()\n"
  359. "{\n"
  360. "vec4 vertPos4 = modelview * position;\n"
  361. "vertPos = vec3(vertPos4) / vertPos4.w;\n"
  362. "vertCol = color;\n"
  363. "vertOpa = opacity;\n"
  364. "vlightPos = lightpos;\n"
  365. "normalInterp = vec3(normalmat * vec4(position.xyz, 0.0));\n"
  366. "gl_Position = projection * modelview * position;\n"
  367. "}\n";
  368. const GLchar* v23 =
  369. "#version 100\n"
  370. "uniform mat4 modelview;\n"
  371. "uniform mat4 projection;\n"
  372. "uniform mat4 normalmat;\n"
  373. "uniform float opacity;\n"
  374. "uniform vec3 lightpos;\n"
  375. "attribute vec4 position;\n"
  376. "attribute vec3 normal;\n"
  377. "attribute vec3 color;\n"
  378. "varying vec3 normalInterp;\n"
  379. "varying vec3 vertPos;\n"
  380. "varying vec3 vertCol;\n"
  381. "varying float vertOpa;\n"
  382. "varying vec3 vlightPos;\n"
  383. "void main()\n"
  384. "{\n"
  385. "vec4 vertPos4 = modelview * position;\n"
  386. "vertPos = vec3(vertPos4) / vertPos4.w;\n"
  387. "vertCol = color;\n"
  388. "vertOpa = opacity;\n"
  389. "vlightPos = lightpos;\n"
  390. "normalInterp = vec3(normalmat * vec4(normal.xyz, 0.0));\n"
  391. "gl_Position = projection * modelview * position;\n"
  392. "}\n";
  393. const GLchar* f2 =
  394. "#version 100\n"
  395. "precision mediump float;\n"
  396. "varying vec3 normalInterp;\n"
  397. "varying vec3 vertPos;\n"
  398. "varying vec3 vertCol;\n"
  399. "varying float vertOpa;\n"
  400. "varying vec3 vlightPos;\n"
  401. "void main()\n"
  402. "{\n"
  403. "vec3 ambientColor = vertCol * 0.14;\n"
  404. "vec3 diffuseColor = vertCol;\n"
  405. "vec3 specColor = vec3(1.0, 1.0, 1.0);\n"
  406. "float specAmount = 4.0;\n"
  407. "vec3 normal = normalize(normalInterp);\n"
  408. "vec3 lightDir = normalize(vlightPos - vertPos);\n"
  409. "vec3 viewDir = normalize(-vertPos);\n"
  410. #ifdef REGULAR_PHONG
  411. "vec3 reflectDir = reflect(-lightDir, normal);\n"
  412. #else
  413. "vec3 halfDir = normalize(viewDir + lightDir);\n"
  414. #endif
  415. "float lumosity = dot(lightDir, normal);\n" // [0] you can max this or
  416. "vec3 specular = diffuseColor;\n"
  417. "if(lumosity > 0.0)\n"
  418. "{\n"
  419. #ifdef REGULAR_PHONG
  420. "float specAngle = max(dot(reflectDir, viewDir), 0.0);\n"
  421. #else
  422. "float specAngle = max(dot(halfDir, normal), 0.0);\n"
  423. #endif
  424. "specular += pow(specAngle, specAmount) * specColor;\n"
  425. "}\n"
  426. "gl_FragColor = vec4(ambientColor + max(specular * lumosity, 0.0), vertOpa);\n" // [0] .. you can max this
  427. "}\n";
  428. //
  429. GLuint shdFullbrightT;
  430. GLint shdFullbrightT_position;
  431. GLint shdFullbrightT_projection;
  432. GLint shdFullbrightT_modelview;
  433. GLint shdFullbrightT_texcoord;
  434. GLint shdFullbrightT_sampler;
  435. GLuint shdFullbright;
  436. GLint shdFullbright_position;
  437. GLint shdFullbright_projection;
  438. GLint shdFullbright_modelview;
  439. GLint shdFullbright_color;
  440. GLint shdFullbright_opacity;
  441. GLuint shdLambert;
  442. GLint shdLambert_position;
  443. GLint shdLambert_projection;
  444. GLint shdLambert_modelview;
  445. GLint shdLambert_lightpos;
  446. GLint shdLambert_color;
  447. GLint shdLambert_opacity;
  448. GLuint shdLambert1;
  449. GLint shdLambert1_position;
  450. GLint shdLambert1_projection;
  451. GLint shdLambert1_modelview;
  452. GLint shdLambert1_lightpos;
  453. GLint shdLambert1_color;
  454. GLint shdLambert1_normal;
  455. GLint shdLambert1_opacity;
  456. GLuint shdLambert2;
  457. GLint shdLambert2_position;
  458. GLint shdLambert2_projection;
  459. GLint shdLambert2_modelview;
  460. GLint shdLambert2_lightpos;
  461. GLint shdLambert2_color;
  462. GLint shdLambert2_normal;
  463. GLint shdLambert2_opacity;
  464. GLuint shdLambert3;
  465. GLint shdLambert3_position;
  466. GLint shdLambert3_projection;
  467. GLint shdLambert3_modelview;
  468. GLint shdLambert3_lightpos;
  469. GLint shdLambert3_color;
  470. GLint shdLambert3_opacity;
  471. GLuint shdPhong;
  472. GLint shdPhong_position;
  473. GLint shdPhong_projection;
  474. GLint shdPhong_modelview;
  475. GLint shdPhong_normalmat;
  476. GLint shdPhong_lightpos;
  477. GLint shdPhong_color;
  478. GLint shdPhong_opacity;
  479. GLuint shdPhong1;
  480. GLint shdPhong1_position;
  481. GLint shdPhong1_projection;
  482. GLint shdPhong1_modelview;
  483. GLint shdPhong1_normalmat;
  484. GLint shdPhong1_lightpos;
  485. GLint shdPhong1_color;
  486. GLint shdPhong1_normal;
  487. GLint shdPhong1_opacity;
  488. GLuint shdPhong2;
  489. GLint shdPhong2_position;
  490. GLint shdPhong2_projection;
  491. GLint shdPhong2_modelview;
  492. GLint shdPhong2_normalmat;
  493. GLint shdPhong2_lightpos;
  494. GLint shdPhong2_color;
  495. GLint shdPhong2_opacity;
  496. GLuint shdPhong3;
  497. GLint shdPhong3_position;
  498. GLint shdPhong3_projection;
  499. GLint shdPhong3_modelview;
  500. GLint shdPhong3_normalmat;
  501. GLint shdPhong3_lightpos;
  502. GLint shdPhong3_color;
  503. GLint shdPhong3_normal;
  504. GLint shdPhong3_opacity;
  505. //
  506. void makeFullbrightT()
  507. {
  508. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  509. glShaderSource(vertexShader, 1, &vt0, NULL);
  510. glCompileShader(vertexShader);
  511. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  512. glShaderSource(fragmentShader, 1, &ft0, NULL);
  513. glCompileShader(fragmentShader);
  514. shdFullbrightT = glCreateProgram();
  515. glAttachShader(shdFullbrightT, vertexShader);
  516. glAttachShader(shdFullbrightT, fragmentShader);
  517. glLinkProgram(shdFullbrightT);
  518. shdFullbrightT_position = glGetAttribLocation(shdFullbrightT, "position");
  519. shdFullbrightT_texcoord = glGetAttribLocation(shdFullbrightT, "texcoord");
  520. shdFullbrightT_projection = glGetUniformLocation(shdFullbrightT, "projection");
  521. shdFullbrightT_modelview = glGetUniformLocation(shdFullbrightT, "modelview");
  522. shdFullbrightT_sampler = glGetUniformLocation(shdFullbrightT, "tex");
  523. }
  524. void makeFullbright()
  525. {
  526. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  527. glShaderSource(vertexShader, 1, &v0, NULL);
  528. glCompileShader(vertexShader);
  529. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  530. glShaderSource(fragmentShader, 1, &f0, NULL);
  531. glCompileShader(fragmentShader);
  532. shdFullbright = glCreateProgram();
  533. glAttachShader(shdFullbright, vertexShader);
  534. glAttachShader(shdFullbright, fragmentShader);
  535. glLinkProgram(shdFullbright);
  536. shdFullbright_position = glGetAttribLocation(shdFullbright, "position");
  537. shdFullbright_projection = glGetUniformLocation(shdFullbright, "projection");
  538. shdFullbright_modelview = glGetUniformLocation(shdFullbright, "modelview");
  539. shdFullbright_color = glGetUniformLocation(shdFullbright, "color");
  540. shdFullbright_opacity = glGetUniformLocation(shdFullbright, "opacity");
  541. }
  542. void makeLambert()
  543. {
  544. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  545. glShaderSource(vertexShader, 1, &v1, NULL);
  546. glCompileShader(vertexShader);
  547. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  548. glShaderSource(fragmentShader, 1, &f1, NULL);
  549. glCompileShader(fragmentShader);
  550. shdLambert = glCreateProgram();
  551. glAttachShader(shdLambert, vertexShader);
  552. glAttachShader(shdLambert, fragmentShader);
  553. glLinkProgram(shdLambert);
  554. shdLambert_position = glGetAttribLocation(shdLambert, "position");
  555. shdLambert_projection = glGetUniformLocation(shdLambert, "projection");
  556. shdLambert_modelview = glGetUniformLocation(shdLambert, "modelview");
  557. shdLambert_lightpos = glGetUniformLocation(shdLambert, "lightpos");
  558. shdLambert_color = glGetUniformLocation(shdLambert, "color");
  559. shdLambert_opacity = glGetUniformLocation(shdLambert, "opacity");
  560. }
  561. void makeLambert1()
  562. {
  563. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  564. glShaderSource(vertexShader, 1, &v11, NULL);
  565. glCompileShader(vertexShader);
  566. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  567. glShaderSource(fragmentShader, 1, &f1, NULL);
  568. glCompileShader(fragmentShader);
  569. shdLambert1 = glCreateProgram();
  570. glAttachShader(shdLambert1, vertexShader);
  571. glAttachShader(shdLambert1, fragmentShader);
  572. glLinkProgram(shdLambert1);
  573. shdLambert1_position = glGetAttribLocation(shdLambert1, "position");
  574. shdLambert1_normal = glGetAttribLocation(shdLambert1, "normal");
  575. shdLambert1_projection = glGetUniformLocation(shdLambert1, "projection");
  576. shdLambert1_modelview = glGetUniformLocation(shdLambert1, "modelview");
  577. shdLambert1_lightpos = glGetUniformLocation(shdLambert1, "lightpos");
  578. shdLambert1_color = glGetUniformLocation(shdLambert1, "color");
  579. shdLambert1_opacity = glGetUniformLocation(shdLambert1, "opacity");
  580. }
  581. void makeLambert2()
  582. {
  583. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  584. glShaderSource(vertexShader, 1, &v1, NULL);
  585. glCompileShader(vertexShader);
  586. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  587. glShaderSource(fragmentShader, 1, &f1, NULL);
  588. glCompileShader(fragmentShader);
  589. shdLambert3 = glCreateProgram();
  590. glAttachShader(shdLambert3, vertexShader);
  591. glAttachShader(shdLambert3, fragmentShader);
  592. glLinkProgram(shdLambert3);
  593. shdLambert3_position = glGetAttribLocation(shdLambert3, "position");
  594. shdLambert3_color = glGetAttribLocation(shdLambert3, "color");
  595. shdLambert3_projection = glGetUniformLocation(shdLambert3, "projection");
  596. shdLambert3_modelview = glGetUniformLocation(shdLambert3, "modelview");
  597. shdLambert3_lightpos = glGetUniformLocation(shdLambert3, "lightpos");
  598. shdLambert3_opacity = glGetUniformLocation(shdLambert3, "opacity");
  599. }
  600. void makeLambert3()
  601. {
  602. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  603. glShaderSource(vertexShader, 1, &v12, NULL);
  604. glCompileShader(vertexShader);
  605. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  606. glShaderSource(fragmentShader, 1, &f1, NULL);
  607. glCompileShader(fragmentShader);
  608. shdLambert2 = glCreateProgram();
  609. glAttachShader(shdLambert2, vertexShader);
  610. glAttachShader(shdLambert2, fragmentShader);
  611. glLinkProgram(shdLambert2);
  612. shdLambert2_position = glGetAttribLocation(shdLambert2, "position");
  613. shdLambert2_normal = glGetAttribLocation(shdLambert2, "normal");
  614. shdLambert2_color = glGetAttribLocation(shdLambert2, "color");
  615. shdLambert2_projection = glGetUniformLocation(shdLambert2, "projection");
  616. shdLambert2_modelview = glGetUniformLocation(shdLambert2, "modelview");
  617. shdLambert2_lightpos = glGetUniformLocation(shdLambert2, "lightpos");
  618. shdLambert2_opacity = glGetUniformLocation(shdLambert2, "opacity");
  619. }
  620. void makePhong()
  621. {
  622. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  623. glShaderSource(vertexShader, 1, &v2, NULL);
  624. glCompileShader(vertexShader);
  625. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  626. glShaderSource(fragmentShader, 1, &f2, NULL);
  627. glCompileShader(fragmentShader);
  628. shdPhong = glCreateProgram();
  629. glAttachShader(shdPhong, vertexShader);
  630. glAttachShader(shdPhong, fragmentShader);
  631. glLinkProgram(shdPhong);
  632. shdPhong_position = glGetAttribLocation(shdPhong, "position");
  633. shdPhong_projection = glGetUniformLocation(shdPhong, "projection");
  634. shdPhong_modelview = glGetUniformLocation(shdPhong, "modelview");
  635. shdPhong_normalmat = glGetUniformLocation(shdPhong, "normalmat");
  636. shdPhong_lightpos = glGetUniformLocation(shdPhong, "lightpos");
  637. shdPhong_opacity = glGetUniformLocation(shdPhong, "opacity");
  638. shdPhong_color = glGetUniformLocation(shdPhong, "color");
  639. }
  640. void makePhong1()
  641. {
  642. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  643. glShaderSource(vertexShader, 1, &v21, NULL);
  644. glCompileShader(vertexShader);
  645. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  646. glShaderSource(fragmentShader, 1, &f2, NULL);
  647. glCompileShader(fragmentShader);
  648. shdPhong1 = glCreateProgram();
  649. glAttachShader(shdPhong1, vertexShader);
  650. glAttachShader(shdPhong1, fragmentShader);
  651. glLinkProgram(shdPhong1);
  652. shdPhong1_position = glGetAttribLocation(shdPhong1, "position");
  653. shdPhong1_normal = glGetAttribLocation(shdPhong1, "normal");
  654. shdPhong1_projection = glGetUniformLocation(shdPhong1, "projection");
  655. shdPhong1_modelview = glGetUniformLocation(shdPhong1, "modelview");
  656. shdPhong1_normalmat = glGetUniformLocation(shdPhong1, "normalmat");
  657. shdPhong1_lightpos = glGetUniformLocation(shdPhong1, "lightpos");
  658. shdPhong1_opacity = glGetUniformLocation(shdPhong1, "opacity");
  659. shdPhong1_color = glGetUniformLocation(shdPhong1, "color");
  660. }
  661. void makePhong2()
  662. {
  663. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  664. glShaderSource(vertexShader, 1, &v22, NULL);
  665. glCompileShader(vertexShader);
  666. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  667. glShaderSource(fragmentShader, 1, &f2, NULL);
  668. glCompileShader(fragmentShader);
  669. shdPhong2 = glCreateProgram();
  670. glAttachShader(shdPhong2, vertexShader);
  671. glAttachShader(shdPhong2, fragmentShader);
  672. glLinkProgram(shdPhong2);
  673. shdPhong2_position = glGetAttribLocation(shdPhong2, "position");
  674. shdPhong2_color = glGetAttribLocation(shdPhong2, "color");
  675. shdPhong2_projection = glGetUniformLocation(shdPhong2, "projection");
  676. shdPhong2_modelview = glGetUniformLocation(shdPhong2, "modelview");
  677. shdPhong2_normalmat = glGetUniformLocation(shdPhong2, "normalmat");
  678. shdPhong2_lightpos = glGetUniformLocation(shdPhong2, "lightpos");
  679. shdPhong2_opacity = glGetUniformLocation(shdPhong2, "opacity");
  680. }
  681. void makePhong3()
  682. {
  683. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  684. glShaderSource(vertexShader, 1, &v23, NULL);
  685. glCompileShader(vertexShader);
  686. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  687. glShaderSource(fragmentShader, 1, &f2, NULL);
  688. glCompileShader(fragmentShader);
  689. shdPhong3 = glCreateProgram();
  690. glAttachShader(shdPhong3, vertexShader);
  691. glAttachShader(shdPhong3, fragmentShader);
  692. glLinkProgram(shdPhong3);
  693. shdPhong3_position = glGetAttribLocation(shdPhong3, "position");
  694. shdPhong3_color = glGetAttribLocation(shdPhong3, "color");
  695. shdPhong3_normal = glGetAttribLocation(shdPhong3, "normal");
  696. shdPhong3_projection = glGetUniformLocation(shdPhong3, "projection");
  697. shdPhong3_modelview = glGetUniformLocation(shdPhong3, "modelview");
  698. shdPhong3_normalmat = glGetUniformLocation(shdPhong3, "normalmat");
  699. shdPhong3_lightpos = glGetUniformLocation(shdPhong3, "lightpos");
  700. shdPhong3_opacity = glGetUniformLocation(shdPhong3, "opacity");
  701. }
  702. void makeAllShaders()
  703. {
  704. makeFullbrightT();
  705. makeFullbright();
  706. makeLambert();
  707. makeLambert1();
  708. makeLambert2();
  709. makeLambert3();
  710. makePhong();
  711. makePhong1();
  712. makePhong2();
  713. makePhong3();
  714. }
  715. void shadeFullbrightT(GLint* position, GLint* projection, GLint* modelview, GLint* texcoord, GLint* sampler)
  716. {
  717. *position = shdFullbrightT_position;
  718. *projection = shdFullbrightT_projection;
  719. *modelview = shdFullbrightT_modelview;
  720. *texcoord = shdFullbrightT_texcoord;
  721. *sampler = shdFullbrightT_sampler;
  722. glUseProgram(shdFullbrightT);
  723. }
  724. void shadeFullbright(GLint* position, GLint* projection, GLint* modelview, GLint* color, GLint* opacity)
  725. {
  726. *position = shdFullbright_position;
  727. *projection = shdFullbright_projection;
  728. *modelview = shdFullbright_modelview;
  729. *color = shdFullbright_color;
  730. *opacity = shdFullbright_opacity;
  731. glUseProgram(shdFullbright);
  732. }
  733. void shadeLambert(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* color, GLint* opacity)
  734. {
  735. *position = shdLambert_position;
  736. *projection = shdLambert_projection;
  737. *modelview = shdLambert_modelview;
  738. *lightpos = shdLambert_lightpos;
  739. *color = shdLambert_color;
  740. *opacity = shdLambert_opacity;
  741. glUseProgram(shdLambert);
  742. }
  743. void shadeLambert1(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity)
  744. {
  745. *position = shdLambert1_position;
  746. *projection = shdLambert1_projection;
  747. *modelview = shdLambert1_modelview;
  748. *lightpos = shdLambert1_lightpos;
  749. *color = shdLambert1_color;
  750. *normal = shdLambert1_normal;
  751. *opacity = shdLambert1_opacity;
  752. glUseProgram(shdLambert1);
  753. }
  754. // notice: swapped this from 2 to 3
  755. void shadeLambert3(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity)
  756. {
  757. *position = shdLambert2_position;
  758. *projection = shdLambert2_projection;
  759. *modelview = shdLambert2_modelview;
  760. *lightpos = shdLambert2_lightpos;
  761. *color = shdLambert2_color;
  762. *normal = shdLambert2_normal;
  763. *opacity = shdLambert2_opacity;
  764. glUseProgram(shdLambert2);
  765. }
  766. // notice: swapped this from 3 to 2
  767. void shadeLambert2(GLint* position, GLint* projection, GLint* modelview, GLint* lightpos, GLint* color, GLint* opacity)
  768. {
  769. *position = shdLambert3_position;
  770. *projection = shdLambert3_projection;
  771. *modelview = shdLambert3_modelview;
  772. *lightpos = shdLambert3_lightpos;
  773. *color = shdLambert3_color;
  774. *opacity = shdLambert3_opacity;
  775. glUseProgram(shdLambert3);
  776. }
  777. void shadePhong(GLint* position, GLint* projection, GLint* modelview, GLint* normalmat, GLint* lightpos, GLint* color, GLint* opacity)
  778. {
  779. *position = shdPhong_position;
  780. *projection = shdPhong_projection;
  781. *modelview = shdPhong_modelview;
  782. *normalmat = shdPhong_normalmat;
  783. *lightpos = shdPhong_lightpos;
  784. *color = shdPhong_color;
  785. *opacity = shdPhong_opacity;
  786. glUseProgram(shdPhong);
  787. }
  788. void shadePhong1(GLint* position, GLint* projection, GLint* modelview, GLint* normalmat, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity)
  789. {
  790. *position = shdPhong1_position;
  791. *projection = shdPhong1_projection;
  792. *modelview = shdPhong1_modelview;
  793. *normalmat = shdPhong1_normalmat;
  794. *lightpos = shdPhong1_lightpos;
  795. *color = shdPhong1_color;
  796. *normal = shdPhong1_normal;
  797. *opacity = shdPhong1_opacity;
  798. glUseProgram(shdPhong1);
  799. }
  800. void shadePhong2(GLint* position, GLint* projection, GLint* modelview, GLint* normalmat, GLint* lightpos, GLint* color, GLint* opacity)
  801. {
  802. *position = shdPhong2_position;
  803. *projection = shdPhong2_projection;
  804. *modelview = shdPhong2_modelview;
  805. *normalmat = shdPhong2_normalmat;
  806. *lightpos = shdPhong2_lightpos;
  807. *color = shdPhong2_color;
  808. *opacity = shdPhong2_opacity;
  809. glUseProgram(shdPhong2);
  810. }
  811. void shadePhong3(GLint* position, GLint* projection, GLint* modelview, GLint* normalmat, GLint* lightpos, GLint* normal, GLint* color, GLint* opacity)
  812. {
  813. *position = shdPhong3_position;
  814. *projection = shdPhong3_projection;
  815. *modelview = shdPhong3_modelview;
  816. *normalmat = shdPhong3_normalmat;
  817. *lightpos = shdPhong3_lightpos;
  818. *color = shdPhong3_color;
  819. *normal = shdPhong3_normal;
  820. *opacity = shdPhong3_opacity;
  821. glUseProgram(shdPhong3);
  822. }
  823. #endif