Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.54 KB | None | 0 0
  1. // NOTE- TEXTURE LOADING DISABLED (uses a dummy procedural tex. see "getTexture", replace it..
  2. // compile & run on linux:
  3. // g++ torusdemosdl.cpp -std=c++1y -lGLU -lXext -lGL -lSDL2 -lrt -lm -lpthread -o torusdemosdl && ./torusdemosdl
  4.  
  5. #include <math.h>
  6. #include <string.h>
  7. #define PI M_PI
  8. #include <vector>
  9. #include <array>
  10.  
  11. #include <SDL2/SDL.h>
  12.  
  13. #define GL_GLEXT_PROTOTYPES 1
  14. //#include <SDL2/SDL_opengles2.h>
  15. #include <SDL2/SDL_opengl.h>
  16.  
  17. using namespace std;
  18. #ifndef GFX_LOGI
  19. #define GFX_LOGI(X,...)
  20. #endif
  21.  
  22. #define GFX_ERROR(X) printf("error");
  23.  
  24. enum
  25. { VertexAttrIndex_pos,VertexAttrIndex_color,VertexAttrIndex_norm,VertexAttrIndex_tex0,VertexAttrIndex_tex1,VertexAttrIndex_count
  26. };
  27.  
  28.  
  29.  
  30. struct TestVertex
  31. {
  32. array<float,3> pos;
  33. array<float,4> color;
  34. array<float,3> norm;
  35. array<float,2> tex0;
  36. };
  37.  
  38.  
  39. struct Mesh
  40. {
  41. typedef int IndexType;
  42. enum {IndexSize = sizeof(IndexType) };
  43. int vertexSize;
  44. typedef ::TestVertex Vertex;
  45. int vbo;
  46. int ibo;
  47. int numVertices,numIndices;
  48. };
  49.  
  50. int g_Texture[5];
  51. int g_ShaderProgram,g_PixelShader,g_VertexShader;
  52.  
  53.  
  54. inline auto SetMatrixProjectionFrustumDemo(float m[16], float left, float right, float bottom, float top, float fnear, float ffar)->void
  55. {
  56. auto a=(right+left)/(right-left);
  57. auto b=(top+bottom)/(top-bottom);
  58. auto c=-(ffar+fnear)/(ffar-fnear);
  59. auto d=-(2*ffar*fnear/(ffar-fnear));
  60. #define STORE4(D,I, X,Y,Z,W) (D)[0+I]=X; (D)[4+I]=Y; (D)[8+I]=Z; (D)[12+I]=W;
  61. STORE4(m,0, 2.f*fnear/(right-left), 0.f,0.f,0.f);
  62. STORE4(m,1, 0.f, 2.f*fnear/(top-bottom), 0.f,0.f);
  63. STORE4(m,2, a,b,c,-1.f);
  64. STORE4(m,3, 0.f,0.f,d,0.f);
  65. #undef STORE4
  66. }
  67.  
  68.  
  69.  
  70. int
  71. GFX_CreateAndCompileShader(int shaderType, const char* source)
  72. {
  73. int shader = glCreateShader(shaderType);
  74. const char* pSrc[1]={source};
  75. int length[1] = {(int)strlen(source)};
  76. glShaderSource(shader, 1,pSrc,length);
  77. glCompileShader(shader);
  78. int status;glGetShaderiv(shader,GL_COMPILE_STATUS,&status);
  79. if (status==GL_FALSE)
  80. {
  81. char log[512]; int len;
  82. glGetShaderInfoLog(shader, 512,&len, log);
  83. printf("\n%s\n", source, log);
  84. printf("compile shader[%d] failed: \n%s\n", shader, log);
  85. exit (0);
  86. }
  87. else printf("create shader[%d] - compile suceeded\n", shader);
  88. return shader;
  89. }
  90.  
  91. struct ShaderProg;
  92. struct VertexShader
  93. {
  94. const char* name;
  95. const char* source;
  96. VertexShader* next;
  97. VertexShader* nextOfProg;
  98. ShaderProg* progsUsingMe;
  99. };
  100. struct PixelShader
  101. {
  102. const char* name;
  103. const char* source;
  104. PixelShader* next;
  105. PixelShader* nextOfProg;
  106. ShaderProg* progsUsingMe;
  107. };
  108. struct ShaderProg
  109. {
  110. ShaderProg* next;
  111. ShaderProg* vertexShader;
  112. PixelShader* pixelShader;
  113. // VertexAttr vta;
  114. // UniformTable uniforms;
  115. };
  116.  
  117. // Paired pixel and vertex shaders.
  118.  
  119. PixelShader* g_PixelShaders;
  120. VertexShader* g_VertexShaders;
  121. ShaderProg* g_ShaderProgs;
  122.  
  123. struct ShaderProgRet {
  124. int pixel_shader;
  125. int vertex_shader;
  126. int program;
  127. };
  128.  
  129. ShaderProgRet GFX_CreateShaderProgram(
  130. const char* pixelShaderSource,
  131. const char* vertexShaderSource,
  132. int* pixelShaderOut,
  133. int* vertexShaderOut,
  134. int* programOut)
  135. {
  136. *vertexShaderOut = GFX_CreateAndCompileShader(GL_VERTEX_SHADER, vertexShaderSource);
  137. *pixelShaderOut = GFX_CreateAndCompileShader(GL_FRAGMENT_SHADER, pixelShaderSource);
  138. int prog = glCreateProgram();
  139. // assign attribute names before linking
  140.  
  141. glAttachShader(prog, *pixelShaderOut);
  142. glAttachShader(prog, *vertexShaderOut);
  143.  
  144.  
  145. printf("linking verteshader[%d], pixelshader[%d] to program[%d]\n", *vertexShaderOut, *pixelShaderOut, prog);
  146. glLinkProgram(prog);
  147.  
  148. int err;
  149. glGetProgramiv(prog,GL_LINK_STATUS,&err);
  150. if (err==GL_INVALID_VALUE || err==GL_INVALID_OPERATION) {
  151. char buffer[1024];int len;
  152. glGetProgramInfoLog(*programOut,1024,&len,buffer);
  153. printf("link program failed:");
  154. printf("%s", buffer);
  155. } else printf("link program status %d\n", err);
  156. *programOut = prog;
  157. return ShaderProgRet{*pixelShaderOut,*vertexShaderOut,prog};
  158. }
  159.  
  160. //TODO: split into default uniforms, default vertex, default vertex-shader-out
  161.  
  162. #define PS_VS_INTERFACE0 \
  163. "varying vec4 v_pos;\n" \
  164. "varying vec4 v_color;\n" \
  165. "varying vec3 v_norm;\n" \
  166. "varying vec2 v_tex0;\n" \
  167. "varying vec3 v_tex1;\n" \
  168. "varying vec4 v_tangent;\n"\
  169. "varying vec4 v_binormal;\n"
  170.  
  171.  
  172. #define PS_VERTEX_FORMAT0 \
  173. "layout(location=0) in vec3 a_pos;\n" \
  174. "layout(location=1) in vec4 a_color;\n" \
  175. "layout(location=2) in vec3 a_norm;\n" \
  176. "layout(location=3) in vec2 a_tex0;\n" \
  177.  
  178. #define SHADER_UNIFORMS \
  179. "layout(location=0) uniform mat4 uMatProj;\n" \
  180. "layout(location=4) uniform mat4 uMatModelView;\n" \
  181. "layout(location=8) uniform vec4 uAmbient;\n" \
  182. "layout(location=9) uniform vec4 uDiffuseDX;\n" \
  183. "layout(location=10) uniform vec4 uDiffuseDY;\n" \
  184. "layout(location=11) uniform vec4 uDiffuseDZ;\n" \
  185. "layout(location=12) uniform vec4 uFogColor;\n" \
  186. "layout(location=13) uniform vec4 uFogFalloff;\n" \
  187. "layout(location=14) uniform vec4 uSpecularDir;\n" \
  188. "layout(location=15) uniform float uSpecularPower;\n" \
  189. "layout(location=16) uniform vec4 uSpecularColor;\n" \
  190. "layout(location=17) uniform vec4 uLightPos;\n" \
  191. "layout(location=18) uniform vec4 uLightColor;\n" \
  192. "layout(location=19) uniform vec4 uLightFalloff;\n" \
  193. "layout(location=20) uniform sampler2D uTex0;" \
  194. "layout(location=21) uniform sampler2D uTex1;" \
  195.  
  196.  
  197. const char g_VS_Default[]=
  198. "#version 330\n"
  199. "#extension GL_ARB_explicit_uniform_location : require\n"
  200. "#define varying out\n"
  201. PS_VERTEX_FORMAT0
  202. PS_VS_INTERFACE0
  203. SHADER_UNIFORMS
  204. "void main() {\n"
  205. " vec4 epos = uMatModelView * vec4(a_pos.xyz,1.0);\n"
  206. " vec3 enorm = (uMatModelView * vec4(a_norm.xyz,0.0)).xyz;\n"
  207. " vec4 spos=uMatProj * epos;\n"
  208. " gl_Position = spos;\n"
  209. " v_pos = epos;\n"
  210. " v_color = a_color;\n"
  211. " v_tex0 = a_tex0;\n"
  212. " v_tex1 = a_pos.xyz;\n"
  213. " v_norm = enorm;\n"
  214. "}";
  215.  
  216. const char g_VS_PassThru[]=
  217. "#version 330\n"
  218. "#extension GL_ARB_explicit_uniform_location : require\n"
  219. "#define varying out\n"
  220. PS_VERTEX_FORMAT0
  221. PS_VS_INTERFACE0
  222. SHADER_UNIFORMS
  223. "void main() {\n"
  224. " vec4 epos = uMatModelView * vec4(a_pos.xyz,1.0);\n"
  225. " vec3 enorm = (uMatModelView * vec4(a_norm.xyz,0.0)).xyz;\n"
  226. " vec4 spos=uMatProj * epos;\n"
  227. " gl_Position = vec4(a_pos.xyz,1.0);\n"
  228. " v_pos = epos;\n"
  229. " v_color = a_color;\n"
  230. " v_tex0 = a_tex0;\n"
  231. " v_tex1 = a_pos.xyz;\n"
  232. " v_norm = enorm;\n"
  233. "}";
  234.  
  235. /*
  236. cases:
  237. VSO:
  238. static scene
  239. animation,3bone
  240. PS:
  241. 2textures
  242. 3textures
  243. */
  244.  
  245. const char g_PS_Alpha[]=
  246. "#version 330\n"
  247. "#extension GL_ARB_explicit_uniform_location : require\n"
  248. "#define varying in\n"
  249. PS_VS_INTERFACE0
  250. SHADER_UNIFORMS
  251.  
  252. "vec4 applyFog(vec3 pos, vec4 color){\n"
  253. " return mix(color,uFogColor, clamp(-uFogFalloff.x-pos.z*uFogFalloff.y,0.0,1.0));\n"
  254. "}\n"
  255. "vec4 pointlight(vec3 pos, vec3 norm,vec3 lpos, vec4 color, vec4 falloff) {\n"
  256. " vec3 dv=lpos-pos;\n"
  257. " float d2=sqrt(dot(dv,dv));\n"
  258. " float f=clamp( 1.0-(d2/falloff.x),0.0,1.0);\n"
  259. " vec3 lv=normalize(dv);\n"
  260. " return clamp(dot(lv,norm),0.0,1.0) * f*color;\n"
  261. "}\n"
  262. "void main() { \n"
  263. " float inva=(v_color.w),a=(1-v_color.w);\n"
  264. " vec4 t0=texture2D(uTex0, v_tex0);\n"
  265. " vec4 t1=texture2D(uTex1, v_tex0);\n"
  266. " float a0=t0.x*0.4+t0.y*0.6+t0.z*0.25;\n"
  267. " float a1=t1.x*0.4+t1.y*0.6+t1.z*0.25;\n"
  268. " float highlight=max(0.f,dot(v_norm,uSpecularDir.xyz));"
  269. " highlight=(highlight*highlight);highlight=highlight*highlight;"
  270. " vec4 surfaceColor=mix(t0,t1,v_color.w);\n"
  271. " vec4 surfaceSpec=clamp(4*(surfaceColor-vec4(0.5,0.5,0.5,0.0)), vec4(0.0,0.0,0.0,0.0),vec4(1.0,1.0,1.0,1.0));\n"
  272. " vec4 spec=highlight*uSpecularColor*surfaceSpec;\n"
  273. " vec4 diff=uAmbient+v_norm.x*uDiffuseDX+v_norm.y*uDiffuseDY+v_norm.z*uDiffuseDZ;\n"
  274. " float lx=0.5,ly=0.5;\n"
  275. " diff+=pointlight(v_pos.xyz,v_norm.xyz, vec3(lx,ly,-1.0), vec4(1.0,0.0,0.0,0.0),vec4(1.0,0.0,0.0,0.0));\n"
  276. " diff+=pointlight(v_pos.xyz,v_norm.xyz, vec3(lx,-ly,-1.0), vec4(0.0,1.0,0.0,0.0),vec4(1.0,0.0,0.0,0.0));\n"
  277. " diff+=pointlight(v_pos.xyz,v_norm.xyz, vec3(-lx,-ly,-1.0), vec4(0.0,0.0,1.0,0.0),vec4(1.0,0.0,0.0,0.0));\n"
  278. " diff+=pointlight(v_pos.xyz,v_norm.xyz, vec3(-lx,ly,-1.0), vec4(0.5,0.0,0.5,0.0),vec4(1.0,0.0,0.0,0.0));\n"
  279. " gl_FragColor = applyFog(v_pos.xyz,surfaceColor*diff*vec4(v_color.xyz,0.0)*2.f+spec);\n"
  280. "}";
  281.  
  282. const char g_PS_Add[]=
  283. "#version 330\n"
  284. "#extension GL_ARB_explicit_uniform_location : require\n"
  285. "#define varying in\n"
  286. PS_VS_INTERFACE0
  287. SHADER_UNIFORMS
  288. "void main() { \n"
  289. " float inva=(v_color.w),a=(1-v_color.w);\n"
  290. " vec4 t0=texture2D(s_Tex0, v_tex0);\n"
  291. " vec4 t1=texture2D(s_Tex1, v_tex0);\n"
  292. " float a0=t0.x*0.4+t0.y*0.6+t0.z*0.25;\n"
  293. " float a1=t1.x*0.4+t1.y*0.6+t1.z*0.25;\n"
  294. " float highlight=max(0.f,dot(v_norm,uSpecularDir.xyz));\n"
  295. " highlight=(highlight*highlight);highlight=highlight*highlight;\n"
  296. " vec4 surfaceColor=t0+(t1-vec4(0.5f,0.5f,0.5f,0.0f))*v_color.w;\n"
  297. " vec4 surfaceSpec=clamp(4*(surfaceColor-Vec4(0.5,0.5,0.5,0.0)), vec4(0.0,0.0,0.0,0.0),vec4(1.0,1.0,1.0,1.0));\n"
  298. " vec4 spec=highlight*uSpecularColor*surfaceSpec;\n"
  299. " vec4 diff=uAmbient+vso_norm.x*uDiffuseDX+vso_norm.y*uDiffuseDY+vso_norm.z*uDiffuseDZ;"
  300. " gl_FragColor =surfaceColor*diff*Vec4(v_color.xyz,0.0)*2.f+spec;\n"
  301. "}";
  302.  
  303.  
  304.  
  305. /*
  306. enum vertexMode
  307. {
  308. singleBone
  309. matrixPalette
  310. };
  311. // just do everything this way for the moment
  312. enum PixelMode
  313. {
  314. vertexAlphaBlend
  315. }
  316. */
  317.  
  318. void CreateShaders()
  319. {
  320. GFX_CreateShaderProgram(g_PS_Alpha, g_VS_Default, &g_VertexShader,&g_PixelShader, &g_ShaderProgram);
  321. }
  322.  
  323. struct GridMesh : Mesh
  324. {
  325. GridMesh(int usize, int vsize);
  326. };
  327.  
  328.  
  329. /*
  330. // number of shaders:-
  331. lights 1-4 x
  332. skinning 1,3 x
  333. texturecoords x
  334.  
  335. pixel shaders:-
  336. simple
  337. 2layer
  338. 3layer alpha blend
  339. 3layer sea-blend
  340.  
  341. GetShader(numLights, numTexcoords, numBones);
  342. */
  343.  
  344. #define SETARRAY2(dst, v0,v1) { dst[0]=v0; dst[1]=v1;}
  345. #define SETARRAY3(dst, v0,v1,v2) { dst[0]=v0; dst[1]=v1; dst[2]=v2;}
  346.  
  347. void
  348. FillTorusVertices(TestVertex* dv, int numU,int numV)
  349. {
  350. int i,j;
  351. float fi=0.f, fj=0.f, dfi=1.f/(float)(numU-1), dfj=1.f/(float)(numV-1);
  352.  
  353. float rx=0.05f,ry=rx*0.33f;
  354. const float pi=3.14159265,pi2=pi*2.f;
  355. for (fj=0.f,j=0; j<numV; j++,fj+=dfj)
  356. {
  357. for (fi=0.f,i=0; i<numU; i++,fi+=dfi, dv++)
  358. {
  359. int dvi=i+j*numU;
  360. float cx = sin(2.f*pi*fi);
  361. float sx = cos(2.f*pi*fi);
  362. float sy = sin(2.f*pi*fj);
  363. float cy = cos(2.f*pi*fj);
  364. SETARRAY3(dv->norm,(sy)*cx,(sy)*sx,cy)
  365. SETARRAY3(dv->pos, (rx+sy*ry)*cx, (rx+sy*ry)*sx, ry*cy);
  366. // printf("vertex[i]=%.3f %.3f %.3f\n",dvi,dv->pos0],dv->pos[1],dv->pos[2]);
  367. SETARRAY2(dv->tex0, fi*8.f,fj*2.f);
  368. auto outf= (sy*0.125f+(1.f-0.125f));
  369. // dv->color=0xffffffff;
  370. dv->color[0]=outf*((sin(fi*pi2*6.f)+sin(fj*pi2*1.f))*0.5f*0.05f+0.5f);
  371. dv->color[1]=outf*((sin(fi*pi2*5.f)+sin(fj*pi2*2.f))*0.5f*0.05f+0.5f);
  372. dv->color[2]=outf*((sin(fi*pi2*4.f)+sin(fj*pi2*3.f))*0.5f*0.05f+0.5f);
  373. dv->color[3]=((i&7)<4)?1.f:0.f;
  374. }
  375. }
  376. }
  377. template<typename T>
  378. auto CreateBuffer(const T* data,size_t num_elems, int bufferType)->int {
  379. GLuint id;
  380. size_t size = num_elems*sizeof(T);
  381. glGenBuffers(1,&id);
  382. GFX_LOGI("buffer created %d size=%d\n",id, (int)size);
  383. glBindBuffer(bufferType, id);
  384.  
  385. glBufferData(bufferType, size, data, GL_STATIC_DRAW);
  386. auto err=glGetError();
  387. if (err!=GL_NO_ERROR) {
  388. if (err==GL_OUT_OF_MEMORY){
  389. GFX_LOGI("out of memory attempting to alloc %lu bytes\n",size);
  390. }
  391. GFX_ERROR(err);
  392. }
  393.  
  394. glBindBuffer(bufferType, 0);
  395. return id;
  396. }
  397.  
  398. GridMesh::GridMesh(int numU, int numV)
  399. {
  400. auto m=this;
  401. m->numVertices=numU*numV;
  402. m->numIndices = (numV-1)*(2*numU+2);
  403. vector<TestVertex> vertices; vertices.resize(numU*numV);
  404. m->vertexSize = sizeof(Vertex);
  405. int i,j;
  406. FillTorusVertices(&vertices[0], numU,numV);
  407.  
  408. vector<IndexType> indices; indices.resize(m->numIndices);
  409. // per strip..
  410. int dvi=0,vi=0;
  411. for (j=0; j<(numV-1); j++)
  412. {
  413. // per quad
  414. indices[dvi++]= j*numU+0; // start with degenerate
  415. for (i=0; i<numU; i++)
  416. {
  417. indices[dvi++] = j*numU + i;
  418. indices[dvi++] = (j+1)*numU + i;
  419. }
  420. indices[dvi]= indices[dvi-1]; // end with degenerate
  421. }
  422. printf("index debug%d %d %d %d\n",m->IndexSize, m->numIndices, dvi, (int)sizeof(IndexType)*dvi);
  423. m->vbo = CreateBuffer(&vertices[0],m->numVertices, GL_ARRAY_BUFFER);
  424. m->ibo = CreateBuffer(&indices[0],m->numIndices, GL_ELEMENT_ARRAY_BUFFER);
  425.  
  426. }
  427.  
  428. void TestGl_Idle();
  429.  
  430. float angle=0.f;
  431. GridMesh* g_pGridMesh;
  432.  
  433.  
  434. void RenderGridMeshFromClientMem(GridMesh* msh)
  435. {
  436. int i;
  437. int clientState[3]={GL_VERTEX_ARRAY,GL_COLOR_ARRAY,GL_TEXTURE_COORD_ARRAY};
  438. for (i=0; i<3; i++) glEnableClientState(clientState[i]);
  439. glBindBuffer(GL_ARRAY_BUFFER, 0);
  440. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  441.  
  442. std::vector<TestVertex> vert; vert.resize(16*16);
  443.  
  444. FillTorusVertices(&vert[0],16,16);
  445.  
  446. int stride=sizeof(vert[0]);
  447. TestVertex* baseVertex=&vert[0];
  448. glVertexPointer(3, GL_FLOAT, stride, (void*) &baseVertex->pos);
  449. glColorPointer(4,GL_UNSIGNED_BYTE, stride, (void*) &baseVertex->color);
  450. glTexCoordPointer(2, GL_FLOAT, stride, (void*) &baseVertex->tex0);
  451.  
  452. glDrawArrays(GL_POINTS, 0, 16*16);
  453.  
  454. for (i=0; i<3; i++) glDisableClientState(clientState[i]);
  455. }
  456.  
  457.  
  458. void RenderGridMeshFromBuffer(GridMesh* msh)
  459. {
  460. int i;
  461. int clientState[3]={GL_VERTEX_ARRAY,GL_COLOR_ARRAY,GL_TEXTURE_COORD_ARRAY};
  462. for (i=0; i<3; i++) glEnableClientState(clientState[i]);
  463.  
  464. glBindBuffer(GL_ARRAY_BUFFER, msh->vbo);
  465. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, msh->ibo);
  466.  
  467.  
  468.  
  469. int stride=sizeof(TestVertex);
  470. TestVertex* baseVertex=(TestVertex*)0;
  471. glVertexPointer(3, GL_FLOAT, stride, (void*) &baseVertex->pos);
  472. glColorPointer(4,GL_FLOAT, stride, (void*) &baseVertex->color);
  473. glTexCoordPointer(2, GL_FLOAT, stride, (void*) &baseVertex->tex0);
  474.  
  475. glDrawElements(GL_TRIANGLE_STRIP, msh->numIndices, GL_UNSIGNED_INT,0);
  476.  
  477. for (i=0; i<3; i++) glDisableClientState(clientState[i]);
  478. }
  479.  
  480. inline void MatrixStoreIdentity4x4Demo(float m[16]){
  481. int i; for (i=0; i<16; i++) m[i]=0.f;
  482. for (i=0; i<4; i++) { m[i+i*4]=1.f;}
  483. }
  484. inline void MatrixMul4x4Demo(float result[16],const float a[16],const float b[16]) {
  485. int i,j,k;
  486. for (i=0; i<4; i++) {
  487. for (j=0; j<4; j++) {
  488. float sum=0.f;
  489. for (k=0; k<4; k++) {
  490. sum+=a[j*4+k]*b[k*4+i];
  491. }
  492. result[j*4+i]=sum;
  493. }
  494. }
  495. }
  496.  
  497.  
  498. enum {
  499. SU_MatProj = 0,
  500. SU_MatModelView = 4,
  501. SU_Ambient = 8,
  502. SU_DiffuseDX = 9,
  503. SU_DiffuseDY = 10,
  504. SU_DiffuseDZ = 11,
  505. SU_FogColor = 12,
  506. SU_FogFalloff = 13,
  507. SU_SpecularDir = 14,
  508. SU_SpecularPower = 15,
  509. SU_SpecularColor = 16,
  510. SU_LightPos = 17,
  511. SU_LightColor = 18,
  512. SU_LightFalloff = 19,
  513. SU_Tex0 = 21,
  514. SU_Tex1 = 22,
  515. };
  516. struct Vector4f {float x,y,z,w;};
  517. void SetUniform(int index, const Vector4f& v) {
  518. glUniform4f(index,v.x,v.y,v.z,v.w);
  519. }
  520.  
  521. Vector4f g_FogColor={0.25f,0.5f,0.5f,1.f};
  522. void RenderGridMeshShader(GridMesh* msh,int t0,int t1)
  523. {
  524.  
  525. int i;
  526. glBindBuffer(GL_ARRAY_BUFFER, msh->vbo);
  527. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, msh->ibo);
  528.  
  529. int stride=sizeof(TestVertex);
  530. TestVertex* baseVertex=(TestVertex*)0;
  531. glEnableVertexAttribArray(VertexAttrIndex_pos);
  532. glEnableVertexAttribArray(VertexAttrIndex_color);
  533. glEnableVertexAttribArray(VertexAttrIndex_tex0);
  534. glEnableVertexAttribArray(VertexAttrIndex_norm);
  535.  
  536. glUniform1i(SU_Tex0, 0);
  537. glUniform1i(SU_Tex1, 1);
  538. // specular highlight, only one supported
  539. SetUniform(SU_SpecularDir, Vector4f{0.0,0.707f,0.707f,0.f});
  540. SetUniform(SU_SpecularColor, Vector4f{1.f,0.75f,0.5f,0.f});
  541. // describe a spherical harmonic - essentially a 4x4 matrix transforming normal vector into a color
  542. // aproximatly it's an ambient color and variation of color along x,y,z axes.
  543. // you can approximate environment lights this way.
  544. SetUniform(SU_Ambient, Vector4f{0.25f,0.25f,0.25f,1.f});
  545. SetUniform(SU_DiffuseDX, Vector4f{0.0f,0.0f,0.25f,1.f});
  546. SetUniform(SU_DiffuseDY, Vector4f{0.5f,0.5f,0.5f,1.f});
  547. SetUniform(SU_DiffuseDZ, Vector4f{0.25f,0.0f,0.0f,1.f});
  548. // simple depth based fog
  549. SetUniform(SU_FogColor, g_FogColor);
  550. SetUniform(SU_FogFalloff, Vector4f{0.5f,0.25f,0.0f,0.f});
  551.  
  552. glActiveTexture(GL_TEXTURE0+0); glBindTexture(GL_TEXTURE_2D, t0);
  553. glActiveTexture(GL_TEXTURE0+1); glBindTexture(GL_TEXTURE_2D, t1);
  554.  
  555. // describe the vertex layout in the vertex buffer..
  556. glVertexAttribPointer(VertexAttrIndex_pos, 3,GL_FLOAT, GL_FALSE, stride, (void*) &baseVertex->pos);
  557. glVertexAttribPointer(VertexAttrIndex_color, 4,GL_FLOAT, GL_FALSE, stride, (void*) &baseVertex->color);
  558. glVertexAttribPointer(VertexAttrIndex_tex0, 2, GL_FLOAT, GL_FALSE, stride, (void*) &baseVertex->tex0);
  559. glVertexAttribPointer(VertexAttrIndex_norm, 3, GL_FLOAT, GL_FALSE, stride, (void*) &baseVertex->norm);
  560.  
  561. glDrawElements(GL_TRIANGLE_STRIP, msh->numIndices, GL_UNSIGNED_INT,0);
  562.  
  563. }
  564.  
  565.  
  566. void
  567. DrawImmediateQuadXY()
  568. {
  569. glBegin(GL_TRIANGLE_STRIP);
  570.  
  571. glColor3f(1.f,0.f,0.f);
  572. glVertex3f(-1.f,-1.f,0.f);
  573.  
  574. glColor3f(0.f,1.f,0.f);
  575. glVertex3f(1.f,-1.f,0.f);
  576.  
  577. glColor3f(0.f,0.f,1.f);
  578. glVertex3f(-1.f,1.f,0.f);
  579.  
  580. glColor3f(0.f,1.f,0.f);
  581. glVertex3f(1.f,1.f,0.f);
  582. glEnd();
  583. }
  584.  
  585. float g_angle=0.f;
  586. int g_frame=0;
  587.  
  588. void
  589. MatrixRotate(float m[16], int axis, float angle) {
  590. MatrixStoreIdentity4x4Demo(m);
  591. static int axis0[3]={1,0,0};
  592. static int axis1[3]={2,2,1};
  593. int i0=axis0[axis];
  594. int i1=axis1[axis];
  595. float s= sin(angle),c=cos(angle);
  596. m[i0*4+i0]=c;
  597. m[i0*4+i1]=s;
  598. m[i1*4+i0]=-s;
  599. m[i1*4+i1]=c;
  600. }
  601. void
  602. MatrixTranslate(float m[16], float x, float y, float z) {
  603. MatrixStoreIdentity4x4Demo(m);
  604. m[12]=x;
  605. m[13]=y;
  606. m[14]=z;
  607. }
  608. int g_NumTorus = 4096;
  609. void ShaderTest_Render()
  610. {
  611.  
  612. g_angle+=0.0025f;
  613. glClearColor(g_FogColor.x,g_FogColor.y,g_FogColor.z,g_FogColor.w);
  614. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  615. float matI[16],matMVP[16], matMV[16], matP[16], matRotX[16],matRotY[16],matTrans[16], matRotXY[16];
  616. MatrixStoreIdentity4x4Demo(matI);
  617. SetMatrixProjectionFrustumDemo(matP,-0.5f,0.5f,-0.5f,0.5f,0.5f,5.f);
  618. int i;
  619. float r0 = 0.5f;
  620. float r1 = 0.1f;
  621. float sda=0.1f;
  622. float a0=g_angle*1.1f+0.1f, a1=g_angle*1.09f+1.5f;
  623. float a2=g_angle*1.05f+0.5f, a3=g_angle*1.11f;
  624. float a4=g_angle*1.11f+0.7f, a5=g_angle*1.105f;
  625. float da0=2*PI*0.071f*sda;
  626. float da1=2*PI*0.042f*sda;
  627. float da2=2*PI*0.081f*sda;
  628. float da3=2*PI*0.091f*sda;
  629. float da4=2*PI*0.153f*sda;
  630. float da5=2*PI*0.1621f*sda;
  631.  
  632. for (i=0; i<g_NumTorus; i++,a0+=da0,a1+=da1,a2+=da2,a3+=da3,a4+=da4,a5+=da5)
  633. {
  634. MatrixTranslate(matTrans, cos(a0)*r0+cos(a3)*r1 , cos(a1)*r0+cos(a4)*r1, cos(a2)*r0+cos(a5)*r1 -2.f*r0);
  635. MatrixRotate(matRotX, 0, a0);
  636. MatrixRotate(matRotY, 1, a1*0.245f);
  637. MatrixMul4x4Demo(matRotXY, matRotX, matRotY);
  638. MatrixMul4x4Demo(matMV, matRotXY, matTrans);
  639. MatrixMul4x4Demo(matMVP, matP,matMV);
  640.  
  641. glUseProgram(g_ShaderProgram);
  642. glUniformMatrix4fvARB(SU_MatProj, 1, GL_FALSE, matP);
  643. glUniformMatrix4fvARB(SU_MatModelView, 1, GL_FALSE, matMV);
  644.  
  645. RenderGridMeshShader(g_pGridMesh,g_Texture[1+(i&3)],g_Texture[1+((1+i)&3)]);
  646. }
  647. glFlush();
  648. g_frame++;
  649. }
  650.  
  651. void CreateTexture() {
  652. // static_assert(sizeof(GLuint)==sizeof(int));
  653. glGenTextures(1,(GLuint*)&g_Texture[0]);
  654. glBindTexture(GL_TEXTURE_2D,g_Texture[0]);
  655. glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
  656. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  657. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  658. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  659. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  660.  
  661. int usize=256,vsize=256;
  662. void* buffer = malloc(usize*vsize*4);
  663. int i,j;
  664. int* dst=(int*) buffer;
  665. for (j=0; j<vsize; j++) {
  666. for (i=0; i<usize;i++) {
  667. dst[i+j*usize] = i+j*256+255*256*256;
  668. }
  669. }
  670. int m;
  671. for (m=0; m<8; m++) {
  672. glTexImage2D(GL_TEXTURE_2D, m, GL_RGB, usize,vsize, 0, GL_RGB, GL_UNSIGNED_BYTE,buffer);
  673. }
  674. free(buffer);
  675. glBindTexture(GL_TEXTURE_2D,0);
  676. g_Texture[1]=g_Texture[0];
  677. g_Texture[2]=g_Texture[0];
  678. g_Texture[3]=g_Texture[0];
  679. g_Texture[4]=g_Texture[0];
  680. /*
  681. g_Texture[1] = getTexture("data/rocktile.tga");
  682. g_Texture[4] = getTexture("data/pebbles_texture.tga");
  683. g_Texture[3] = getTexture("data/grass.tga");
  684. g_Texture[2] = getTexture("data/cliffs.tga");
  685. */
  686.  
  687. }
  688.  
  689. // Shader sources
  690. const GLchar* triangle_vertexSource =
  691. "attribute vec4 position; \n"
  692. "void main() \n"
  693. "{ \n"
  694. " gl_Position = vec4(position.xyz, 1.0); \n"
  695. "} \n";
  696. const GLchar* triangle_fragmentSource =
  697. "precision mediump float;\n"
  698. "void main() \n"
  699. "{ \n"
  700. " gl_FragColor = vec4 (1.0, 1.0, 1.0, 1.0 );\n"
  701. "}\n";
  702.  
  703. GLfloat g_triangle_vertices[] = {0.0f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f};
  704. GLuint g_triangle_vao;
  705. GLuint g_triangle_vbo;
  706.  
  707. int g_triangle_shaderProgram;
  708. void init_triangle() {
  709.  
  710. glGenVertexArrays(1, &g_triangle_vao);
  711. glBindVertexArray(g_triangle_vao);
  712.  
  713. // Create a Vertex Buffer Object and copy the vertex data to it
  714. glGenBuffers(1, &g_triangle_vbo);
  715.  
  716.  
  717. glBindBuffer(GL_ARRAY_BUFFER, g_triangle_vbo);
  718. glBufferData(GL_ARRAY_BUFFER, sizeof(g_triangle_vertices), g_triangle_vertices, GL_STATIC_DRAW);
  719. printf("1\n");
  720.  
  721.  
  722. // Create and compile the vertex shader
  723. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  724. glShaderSource(vertexShader, 1, &triangle_vertexSource, NULL);
  725. glCompileShader(vertexShader);
  726.  
  727. // Create and compile the fragment shader
  728. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  729. glShaderSource(fragmentShader, 1, &triangle_fragmentSource, NULL);
  730. glCompileShader(fragmentShader);
  731.  
  732. // Link the vertex and fragment shader into a shader program
  733. g_triangle_shaderProgram = glCreateProgram();
  734. glAttachShader(g_triangle_shaderProgram, vertexShader);
  735. glAttachShader(g_triangle_shaderProgram, fragmentShader);
  736. // glBindFragDataLocation(shaderProgram, 0, "outColor");
  737. glLinkProgram(g_triangle_shaderProgram);
  738. }
  739. void draw_triangle(){
  740.  
  741. glUseProgram(g_triangle_shaderProgram);
  742. GLint posAttrib = glGetAttribLocation(g_triangle_shaderProgram, "position");
  743. glBindVertexArray(g_triangle_vao);
  744. glEnableVertexAttribArray(posAttrib);
  745. glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  746. glDrawArrays(GL_TRIANGLES, 0, 3);
  747. }
  748.  
  749.  
  750. int main(int argc, const char** argv)
  751. {
  752.  
  753. printf("hello SDL world\n");
  754.  
  755. SDL_Init(SDL_INIT_EVERYTHING);
  756. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  757. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  758.  
  759. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  760. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  761.  
  762. SDL_GL_SetSwapInterval(0);
  763. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  764. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  765. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  766. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  767.  
  768.  
  769. auto wnd(
  770. SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  771. 1024, 1024, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN));
  772. auto glc = SDL_GL_CreateContext(wnd);
  773. SDL_ShowWindow(wnd);
  774.  
  775. CreateShaders();
  776. CreateTexture();
  777.  
  778. glDrawBuffer(GL_BACK);
  779. glEnable(GL_DEPTH_TEST);
  780. g_pGridMesh = new GridMesh(16,16);
  781. init_triangle();
  782.  
  783. int frame=0;
  784. while (1){
  785. SDL_Event e;
  786. while(SDL_PollEvent(&e))
  787. {
  788. if(e.type == SDL_QUIT) std::terminate();
  789. }
  790.  
  791.  
  792.  
  793. // draw the torus lisajous
  794. ShaderTest_Render();
  795.  
  796. // Draw a single triangle (simpler demo)
  797. // Specify the layout of the vertex data
  798.  
  799. draw_triangle();
  800.  
  801.  
  802. printf("frame=%d",frame);
  803. frame++;
  804.  
  805. SDL_GL_SwapWindow(wnd);
  806. }
  807.  
  808. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement