Guest User

Untitled

a guest
Nov 16th, 2013
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GLFW/glfw3.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <GL/gl.h>
  5. #include <time.h>
  6. #include <SOIL/SOIL.h>
  7. int n=1;
  8. int myargc;
  9. char **myargv;
  10. int X = 2000;
  11. int Y = 2000;
  12. int C = 3;
  13. int x,y;
  14. void *picture;
  15. GLFWwindow *window;
  16. GLchar *fragmentsource, *vertexsource;
  17. GLuint vertexshader, fragmentshader;
  18. GLuint shaderprogram;
  19. GLuint image;
  20. GLuint sampler;
  21. GLuint texture;
  22. GLuint vao, vbo[1], UV[1];
  23.  
  24. struct vec2 {
  25. GLfloat point[2];
  26. };
  27. struct vec3 {
  28. GLdouble point[3];
  29. };
  30.  
  31. const struct vec3 flat[4] = {
  32. {{ -1.0, 1.0, 0.0 }},
  33. {{ 1.0, 1.0, 0.0 }},
  34. {{ 1.0, -1.0, 0.0 }},
  35. {{ -1.0, -1.0, 0.0 }}
  36. };
  37. const struct vec2 UVmap[4] = {
  38. {{ 0.0, 0.0 }},
  39. {{ 1.0, 0.0 }},
  40. {{ 1.0, 1.0 }},
  41. {{ 0.0, 1.0 }}
  42. };
  43.  
  44. /* A simple function that will read a file into an allocated char pointer buffer */
  45. char* filetobuf(char *file) {
  46. FILE *fptr;
  47. long length;
  48. char *buf;
  49.  
  50. fptr = fopen(file, "r"); /* Open file for reading */
  51. if (!fptr) /* Return NULL on failure */
  52. return NULL;
  53. fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */
  54. length = ftell(fptr); /* Find out how many bytes into the file we are */
  55. buf = (char*)malloc(length + 1); /* Allocate a buffer for the entire length of the file plus a null terminator */
  56. fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file */
  57. fread(buf, length, 1, fptr); /* Read the contents of the file in to the buffer */
  58. fclose(fptr); /* Close the file */
  59. buf[length] = 0; /* Null terminator */
  60.  
  61. return buf; /* Return the buffer */
  62. }
  63.  
  64. static void error_callback(int error, const char *description) {
  65. fputs(description, stderr);
  66. }
  67.  
  68. static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  69. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  70. glfwSetWindowShouldClose(window, GL_TRUE);
  71. else if (action == GLFW_PRESS) {
  72. if (key == GLFW_KEY_RIGHT) {
  73. if (n==myargc-1)
  74. n=0;
  75. n++;
  76. }
  77. else if (key == GLFW_KEY_LEFT) {
  78. if (n==1)
  79. n=myargc;
  80. n--;
  81. }
  82. else goto nothing;
  83. picture = SOIL_load_image(myargv[n], &X, &Y, &C, SOIL_LOAD_RGB);
  84. glfwSetWindowSize(window, X,Y);
  85. glfwGetFramebufferSize(window, &x, &y);
  86. glViewport(0, 0, x, y);
  87. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, X, Y, 0, GL_RGB, GL_UNSIGNED_BYTE, picture);
  88. SOIL_free_image_data(picture);
  89. nothing:;
  90. }
  91. }
  92.  
  93. void framebuffer_size_callback(window, x, y) {
  94. glViewport(0, 0, x, y);
  95. }
  96.  
  97. void init_glfw() {
  98. // povezati error callback provo za svaki slucaj
  99. glfwSetErrorCallback(error_callback);
  100. if (!glfwInit())
  101. exit(EXIT_FAILURE);
  102. }
  103.  
  104. void create_window() {
  105. picture = SOIL_load_image(myargv[1], &X, &Y, &C, SOIL_LOAD_RGB);
  106. if (!picture) {
  107. glfwTerminate();
  108. exit(EXIT_FAILURE);
  109. }
  110. window = glfwCreateWindow(X, Y, "Simple example", NULL, NULL);
  111. if (!window) {
  112. glfwTerminate();
  113. exit(EXIT_FAILURE);
  114. }
  115.  
  116. glfwMakeContextCurrent(window);
  117.  
  118. glfwSetKeyCallback(window, key_callback);
  119. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  120.  
  121. glfwGetWindowSize (window, &x, &y);
  122. glViewport(0, 0, x, y);
  123.  
  124. glEnable(GL_DEPTH_TEST);
  125. glDepthFunc(GL_LESS);
  126. glEnable(GL_TEXTURE_2D);
  127. }
  128.  
  129. void opengl() {
  130. glGenVertexArrays(1, &vao);
  131. glBindVertexArray(vao);
  132.  
  133. glGenBuffers(1, vbo);
  134. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  135. glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(struct vec3), flat, GL_STATIC_DRAW);
  136. glVertexAttribPointer((GLuint) 0, 3, GL_DOUBLE, GL_FALSE, sizeof(struct vec3), 0);
  137. glEnableVertexAttribArray(0);
  138.  
  139. glGenBuffers(1, UV);
  140. glBindBuffer(GL_ARRAY_BUFFER, UV);
  141. glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(struct vec2), UVmap, GL_STATIC_DRAW);
  142. glVertexAttribPointer((GLuint) 1, 2, GL_FLOAT, GL_FALSE, sizeof(struct vec2), 0);
  143. glEnableVertexAttribArray(1);
  144.  
  145.  
  146. vertexsource = filetobuf("vertexshader.vert");
  147. fragmentsource = filetobuf("fragmentshader.frag");
  148.  
  149. vertexshader = glCreateShader(GL_VERTEX_SHADER);
  150. fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
  151.  
  152. glShaderSource(vertexshader, 1, (const GLchar **)&vertexsource, 0);
  153. glShaderSource(fragmentshader, 1, (const GLchar **)&fragmentsource, 0);
  154.  
  155. glCompileShader(vertexshader);
  156. glCompileShader(fragmentshader);
  157.  
  158. shaderprogram = glCreateProgram();
  159. glAttachShader(shaderprogram, vertexshader);
  160. glAttachShader(shaderprogram, fragmentshader);
  161. glLinkProgram(shaderprogram);
  162.  
  163. glUseProgram(shaderprogram);
  164.  
  165.  
  166. glGenSamplers(1, &sampler);
  167. glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  168. glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  169. glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
  170.  
  171.  
  172. glGenTextures(1, &texture);
  173. glBindTexture(GL_TEXTURE_2D, texture);
  174. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  175. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  176.  
  177. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, X, Y, 0, GL_RGB, GL_UNSIGNED_BYTE, picture);
  178.  
  179. SOIL_free_image_data(picture);
  180. }
  181.  
  182. int main(int argc, char** argv) {
  183. myargc = argc;
  184. myargv = argv;
  185.  
  186. init_glfw();
  187. create_window();
  188. opengl();
  189.  
  190. int i=1;
  191.  
  192. while (!glfwWindowShouldClose(window)) {
  193. struct timespec tps, tpe;
  194.  
  195. glClearColor(1.0, 0.0, 0.0, 1.0); // pozadinu u crveno, za debug
  196. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  197.  
  198. glDrawArrays(GL_QUADS, 0, 4); // u ovom slucaju QUAD umjesto trokuta
  199.  
  200. glfwSwapBuffers(window);
  201.  
  202.  
  203. usleep(10000);
  204.  
  205.  
  206. if (i==0) {
  207. if (clock_gettime(CLOCK_MONOTONIC, &tpe) != 0) {
  208. perror("clock_gettime");
  209. return -1;
  210. }
  211. printf("%lu s, %lu ns %lu fps \n", tpe.tv_sec-tps.tv_sec, tpe.tv_nsec-tps.tv_nsec, 1000000000/(tpe.tv_nsec-tps.tv_nsec));
  212. i=1000;
  213. }
  214. i--;
  215. clock_gettime(CLOCK_MONOTONIC, &tps);
  216.  
  217.  
  218. glfwPollEvents();
  219. }
  220.  
  221.  
  222. glfwDestroyWindow(window);
  223. glfwTerminate();
  224. exit(EXIT_SUCCESS);
  225. }
RAW Paste Data