Advertisement
Guest User

Untitled

a guest
May 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <GL/glew.h>
  4. #define GLUT_DISABLE_ATEXIT_HACK
  5. #include <GL/glut.h>
  6. #pragma comment(lib, "glew32.lib")
  7. #define USE_MATH_DEFINES
  8. #include <math.h>
  9.  
  10. #define WINDSIZEX 500
  11. #define WINDSIZEY 500
  12.  
  13. static char* vsSource = "#version 120 \n\
  14. attribute vec4 aPosition; \n\
  15. attribute vec4 aColor; \n\
  16. varying vec4 vColor; \n\
  17. uniform mat4 uModel; \n\
  18. uniform mat4 uView; \n\
  19. uniform mat4 uProj; \n\
  20. uniform float uY_change;\n\
  21. void main(void) { \n\
  22. vec4 pos = aPosition;\n\
  23. pos.y = pos.y + uY_change;\n\
  24. gl_Position = uProj * uView * uModel * pos; \n\
  25. vColor = aColor; \n\
  26. }";
  27.  
  28. static char* fsSource = "#version 120 \n\
  29. varying vec4 vColor; \n\
  30. void main(void) { \n\
  31. gl_FragColor = vColor; \n\
  32. }";
  33.  
  34. GLfloat theta = 0;
  35. GLfloat y_change = 0;
  36. bool up = true;
  37. GLfloat resize1 = 0.1f;
  38.  
  39. GLuint vs = 0;
  40. GLuint fs = 0;
  41. GLuint prog = 0;
  42.  
  43. GLfloat* vertices1;
  44. GLushort* indices1;
  45.  
  46. GLfloat* vertices2;
  47. GLushort* indices2;
  48.  
  49.  
  50.  
  51. int count;
  52.  
  53. GLfloat scale = 1.0f;
  54. GLuint i_number = 0;
  55. GLuint v_number = 0;
  56.  
  57. GLfloat vertices[] = {
  58. -0.5, -0.5, -0.5, 1.0, // 0
  59. -0.5, -0.5, +0.5, 1.0, // 1
  60. -0.5, +0.5, -0.5, 1.0, // 2
  61. -0.5, +0.5, +0.5, 1.0, // 3
  62. +0.5, -0.5, -0.5, 1.0, // 4
  63. +0.5, -0.5, +0.5, 1.0, // 5
  64. +0.5, +0.5, -0.5, 1.0, // 6
  65. +0.5, +0.5, +0.5, 1.0, // 7
  66. };
  67.  
  68. GLfloat colors[] = {
  69. 0.5, 0.5, 0.5, 1.0, // black
  70. 0.5, 0.5, 1.0, 1.0, // blue
  71. 0.5, 1.0, 0.5, 1.0, // green
  72. 0.5, 1.0, 1.0, 1.0, // cyan
  73. 1.0, 0.5, 0.5, 1.0, // red
  74. 1.0, 0.5, 1.0, 1.0, // magenta
  75. 1.0, 1.0, 0.5, 1.0, // yellow
  76. 1.0, 1.0, 1.0, 1.0, // white
  77. };
  78.  
  79. GLushort indices[] = { // 36 points, 12 triangles
  80. 0, 4, 6,
  81. 6, 2, 0,
  82. 4, 5, 7,
  83. 7, 6, 4,
  84. 1, 3, 7,
  85. 7, 5, 1,
  86. 0, 2, 3,
  87. 3, 1, 0,
  88. 2, 6, 7,
  89. 7, 3, 2,
  90. 0, 1, 5,
  91. 5, 4, 0,
  92. };
  93.  
  94. GLuint vbo[1];
  95. GLuint vbo1;
  96.  
  97. char buf[1024];
  98.  
  99. GLfloat motion_start[3]; // start vector, (x, y, z)
  100. GLfloat motion_end[3]; // end vector, (x, y, z)
  101.  
  102. GLfloat matModel[16] = {
  103. 1.0, 0.0, 0.0, 0.0,
  104. 0.0, 1.0, 0.0, 0.0,
  105. 0.0, 0.0, 1.0, 0.0,
  106. 0.0, 0.0, 0.0, 1.0,
  107. };
  108. GLfloat matView[16] = {
  109. 1.0, 0.0, 0.0, 0.0,
  110. 0.0, 1.0, 0.0, 0.0,
  111. 0.0, 0.0, 1.0, 0.0,
  112. 0.0, 0.0, 0.0, 1.0,
  113. };
  114. GLfloat matProj[16] = {
  115. 1.0, 0.0, 0.0, 0.0,
  116. 0.0, 1.0, 0.0, 0.0,
  117. 0.0, 0.0, 1.0, 0.0,
  118. 0.0, 0.0, 0.0, 1.0,
  119. };
  120. GLfloat matA[16];
  121. GLfloat matB[16];
  122.  
  123. void matCopy(GLfloat a[16], GLfloat b[16]) { // a = b;
  124. a[0] = b[0]; a[4] = b[4]; a[8] = b[8]; a[12] = b[12];
  125. a[1] = b[1]; a[5] = b[5]; a[9] = b[9]; a[13] = b[13];
  126. a[2] = b[2]; a[6] = b[6]; a[10] = b[10]; a[14] = b[14];
  127. a[3] = b[3]; a[7] = b[7]; a[11] = b[11]; a[15] = b[15];
  128. }
  129.  
  130. void matIden(GLfloat a[16]) { // a = I
  131. a[0] = 1.0; a[4] = 0.0; a[8] = 0.0; a[12] = 0.0;
  132. a[1] = 0.0; a[5] = 1.0; a[9] = 0.0; a[13] = 0.0;
  133. a[2] = 0.0; a[6] = 0.0; a[10] = 1.0; a[14] = 0.0;
  134. a[3] = 0.0; a[7] = 0.0; a[11] = 0.0; a[15] = 1.0;
  135. }
  136.  
  137. void matMult(GLfloat c[16], GLfloat a[16], GLfloat b[16]) { // C = A * B
  138. c[0] = a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3];
  139. c[1] = a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3];
  140. c[2] = a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3];
  141. c[3] = a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3];
  142. //
  143. c[4] = a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7];
  144. c[5] = a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7];
  145. c[6] = a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7];
  146. c[7] = a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7];
  147. //
  148. c[8] = a[0] * b[8] + a[4] * b[9] + a[8] * b[10] + a[12] * b[11];
  149. c[9] = a[1] * b[8] + a[5] * b[9] + a[9] * b[10] + a[13] * b[11];
  150. c[10] = a[2] * b[8] + a[6] * b[9] + a[10] * b[10] + a[14] * b[11];
  151. c[11] = a[3] * b[8] + a[7] * b[9] + a[11] * b[10] + a[15] * b[11];
  152. //
  153. c[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14] + a[12] * b[15];
  154. c[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14] + a[13] * b[15];
  155. c[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
  156. c[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];
  157. }
  158.  
  159. void vecMult(GLfloat v[4], GLfloat m[16], GLfloat u[4]) { // v = M * u
  160. v[0] = m[0] * u[0] + m[4] * u[1] + m[8] * u[2] + m[12] * u[3];
  161. v[1] = m[1] * u[0] + m[5] * u[1] + m[9] * u[2] + m[13] * u[3];
  162. v[2] = m[2] * u[0] + m[6] * u[1] + m[10] * u[2] + m[14] * u[3];
  163. v[3] = m[3] * u[0] + m[7] * u[1] + m[11] * u[2] + m[15] * u[3];
  164. }
  165.  
  166. void myinit(void) {
  167. GLint status;
  168. GLuint loc;
  169. // vs: vertex shader
  170. vs = glCreateShader(GL_VERTEX_SHADER);
  171. glShaderSource(vs, 1, &vsSource, NULL);
  172. glCompileShader(vs); // compile to get .OBJ
  173. glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
  174. printf("vs compile status = %s\n", (status == GL_TRUE) ? "true" : "false");
  175. glGetShaderInfoLog(vs, sizeof(buf), NULL, buf);
  176. printf("vs log = [%s]\n", buf);
  177. // fs: fragment shader
  178. fs = glCreateShader(GL_FRAGMENT_SHADER);
  179. glShaderSource(fs, 1, &fsSource, NULL);
  180. glCompileShader(fs); // compile to get .OBJ
  181. glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
  182. printf("fs compile status = %s\n", (status == GL_TRUE) ? "true" : "false");
  183. glGetShaderInfoLog(fs, sizeof(buf), NULL, buf);
  184. printf("fs log = [%s]\n", buf);
  185. // prog: program
  186. prog = glCreateProgram();
  187. glAttachShader(prog, vs);
  188. glAttachShader(prog, fs);
  189. glLinkProgram(prog); // link to get .EXE
  190. glGetProgramiv(prog, GL_LINK_STATUS, &status);
  191. printf("program link status = %s\n", (status == GL_TRUE) ? "true" : "false");
  192. glGetProgramInfoLog(prog, sizeof(buf), NULL, buf);
  193. printf("link log = [%s]\n", buf);
  194. glValidateProgram(prog);
  195. glGetProgramiv(prog, GL_VALIDATE_STATUS, &status);
  196. printf("program validate status = %s\n", (status == GL_TRUE) ? "true" : "false");
  197. glGetProgramInfoLog(prog, sizeof(buf), NULL, buf);
  198. printf("validate log = [%s]\n", buf);
  199. glUseProgram(prog); // execute it !
  200. // VBO setting
  201. glGenBuffers(1, vbo);
  202. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  203. glBufferData(GL_ARRAY_BUFFER, 2 * 8 * 4 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
  204. glBufferSubData(GL_ARRAY_BUFFER, 0, 8 * 4 * sizeof(GLfloat), vertices);
  205. glBufferSubData(GL_ARRAY_BUFFER, 8 * 4 * sizeof(GLfloat), 8 * 4 * sizeof(GLfloat), colors);
  206. // provide the vertex attributes
  207. loc = glGetAttribLocation(prog, "aPosition");
  208. glEnableVertexAttribArray(loc);
  209. glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(0));
  210. // provide the color attributes
  211. loc = glGetAttribLocation(prog, "aColor");
  212. glEnableVertexAttribArray(loc);
  213. glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(8 * 4 * sizeof(GLfloat)));
  214. // depth buffer enabled
  215. glEnable(GL_DEPTH_TEST);
  216. glGenBuffers(1, &vbo1);
  217. glBindBuffer(GL_ARRAY_BUFFER, vbo1);
  218. glBufferData(GL_ARRAY_BUFFER, (v_number+4)*4*sizeof(GLfloat), vertices1, GL_STATIC_DRAW);
  219. }
  220.  
  221. void mykeyboard(unsigned char key, int x, int y) {
  222. switch (key) {
  223. case 27: // ESCAPE
  224. exit(0);
  225. break;
  226. }
  227. }
  228.  
  229. void myidle(void) {
  230. // nothing
  231.  
  232. }
  233.  
  234. void myreshape(int width, int height) {
  235. int shift;
  236. if (width >= height) {
  237. shift = (width - height) / 2;
  238. glViewport(shift, 0, height, height);
  239. } else {
  240. shift = (height - width) / 2;
  241. glViewport(0, shift, width, width);
  242. }
  243. }
  244.  
  245. void mydisplay(void) {
  246. GLuint loc;
  247. // clear
  248. glClearColor(0.3f, 0.3f, 0.3f, 1.0f); // gray
  249. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  250. // provide the matrices
  251. loc = glGetUniformLocation(prog, "uModel");
  252. glUniformMatrix4fv(loc, 1, GL_FALSE, matModel);
  253. loc = glGetUniformLocation(prog, "uView");
  254. glUniformMatrix4fv(loc, 1, GL_FALSE, matView);
  255. loc = glGetUniformLocation(prog, "uProj");
  256. glUniformMatrix4fv(loc, 1, GL_FALSE, matProj);
  257. loc = glGetUniformLocation(prog, "uY_change");
  258. glUniform1f(loc, y_change);
  259. // draw a triangle
  260. glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, indices);
  261. // flush all
  262. glFlush();
  263. glutSwapBuffers();
  264. }
  265.  
  266. void setViewMat(void) {
  267. GLfloat eye[3], at[3], up[3];
  268. GLfloat p[3], n[3], v[3], u[3];
  269. GLfloat l;
  270. GLfloat vec[4], uec[4];
  271. int i;
  272. eye[0] = sin(theta);
  273. eye[1] = 0;
  274. eye[2] = cos(theta);
  275. at[0] = at[1] = at[2] = 0;
  276. up[0] = up[2] = 0;
  277. up[1] = 1.0f;
  278. // p = eye
  279. p[0] = eye[0];
  280. p[1] = eye[1];
  281. p[2] = eye[2];
  282. // n = (at - eye)
  283. n[0] = at[0] - eye[0];
  284. n[1] = at[1] - eye[1];
  285. n[2] = at[2] - eye[2];
  286. l = sqrtf(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
  287. n[0] /= l; n[1] /= l; n[2] /= l;
  288. // u = up * n
  289. u[0] = up[1] * n[2] - up[2] * n[1];
  290. u[1] = up[2] * n[0] - up[0] * n[2];
  291. u[2] = up[0] * n[1] - up[1] * n[0];
  292. l = sqrtf(u[0] * u[0] + u[1] * u[1] + u[2] * u[2]);
  293. u[0] /= l; u[1] /= l; u[2] /= l;
  294. // v = n * u;
  295. v[0] = n[1] * u[2] - n[2] * u[1];
  296. v[1] = n[2] * u[0] - n[0] * u[2];
  297. v[2] = n[0] * u[1] - n[1] * u[0];
  298. l = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  299. v[0] /= l; v[1] /= l; v[2] /= l;
  300. //
  301. // view matrix
  302. matA[0] = u[0]; matA[4] = u[1]; matA[8] = u[2]; matA[12] = 0;
  303. matA[1] = v[0]; matA[5] = v[1]; matA[9] = v[2]; matA[13] = 0;
  304. matA[2] = n[0]; matA[6] = n[1]; matA[10] = n[2]; matA[14] = 0;
  305. matA[3] = 0.0; matA[7] = 0.0; matA[11] = 0.0; matA[15] = 1.0;
  306. matB[0] = 1; matB[4] = 0; matB[8] = 0; matB[12] = -p[0];
  307. matB[1] = 0; matB[5] = 1; matB[9] = 0; matB[13] = -p[1];
  308. matB[2] = 0; matB[6] = 0; matB[10] = 1; matB[14] = -p[2];
  309. matB[3] = 0.0; matB[7] = 0.0; matB[11] = 0.0; matB[15] = 1.0;
  310. matMult(matView, matA, matB);
  311. for (i = 0; i < 8; i++) {
  312. uec[0] = vertices[4 * i + 0];
  313. uec[1] = vertices[4 * i + 1];
  314. uec[2] = vertices[4 * i + 2];
  315. uec[3] = vertices[4 * i + 3];
  316. vecMult(vec, matView, uec);
  317. }
  318. fflush(stdout);
  319. }
  320.  
  321. void setProjMat(void) {
  322. GLfloat xmin, xmax;
  323. GLfloat ymin, ymax;
  324. GLfloat zmin, zmax;
  325. GLfloat vec[4], uec[4], wec[4];
  326. int i;
  327. // input
  328. xmin = -2;
  329. xmax = 2;
  330. ymin = -2;
  331. ymax = 2;
  332. zmin = -2;
  333. zmax = 2;
  334. // view matrix
  335. matProj[0] = 2.0F / (xmax - xmin); matProj[4] = 0.0; matProj[8] = 0.0;
  336. matProj[12] = -(xmax + xmin) / (xmax - xmin);
  337. matProj[1] = 0.0; matProj[5] = 2.0F / (ymax - ymin); matProj[9] = 0.0;
  338. matProj[13] = -(ymax + ymin) / (ymax - ymin);
  339. matProj[2] = 0.0; matProj[6] = 0.0; matProj[10] = 2.0F / (zmax - zmin);
  340. matProj[14] = -(zmax + zmin) / (zmax - zmin);
  341. matProj[3] = 0.0; matProj[7] = 0.0; matProj[11] = 0.0; matProj[15] = 1.0;
  342. for (i = 0; i < 8; i++) {
  343. uec[0] = vertices[4 * i + 0];
  344. uec[1] = vertices[4 * i + 1];
  345. uec[2] = vertices[4 * i + 2];
  346. uec[3] = vertices[4 * i + 3];
  347. vecMult(wec, matView, uec);
  348. vecMult(vec, matProj, wec);
  349. }
  350. }
  351.  
  352. void myRotate(int val)
  353. {
  354. theta += val * 0.0001F;
  355. y_change += up ? 0.05F : -0.05F;
  356. if(y_change >= 0.5 && up)
  357. up = false;
  358. else if(!up && y_change <= -0.5)
  359. up = true;
  360.  
  361. setViewMat();
  362. glutPostRedisplay();
  363. glutTimerFunc(16, myRotate, val);
  364. }
  365.  
  366.  
  367. int main(int argc, char* argv[]) {
  368. glutInit(&argc, argv);
  369. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  370. glutInitWindowSize(WINDSIZEX, WINDSIZEY);
  371. glutInitWindowPosition(0, 0);
  372. glutCreateWindow("simple");
  373. glutDisplayFunc(mydisplay);
  374. glutKeyboardFunc(mykeyboard);
  375. glutReshapeFunc(myreshape);
  376. glutIdleFunc(myidle);
  377. glutTimerFunc(16, myRotate, 100);
  378. glewInit();
  379. myinit();
  380. setViewMat();
  381. setProjMat();
  382. glutMainLoop();
  383. return 0;
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement