kanciastopantalones

30 03

Mar 30th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. #include <windows.h>
  2. #include "GL/glut.h"
  3. #include "GL/gl.h"
  4. #include <stdio.h>
  5.  
  6. GLuint texture[2];
  7. GLint slices=16;
  8. GLint stacks=16;
  9. struct Image {
  10. unsigned long sizeX;
  11. unsigned long sizeY;
  12. char *data;
  13. };
  14. typedef struct Image Image;
  15.  
  16. // texture brick
  17. GLubyte const c=255;
  18. GLubyte const b=51;
  19. GLubyte const w=150;
  20. GLubyte tex_brick[8][8][3] = {
  21. { {c,b,b},{c,b,b},{c,b,b},{w,w,w},{c,b,b},{c,b,b},{c,b,b},{c,b,b} },
  22. { {c,b,b},{c,b,b},{c,b,b},{w,w,w},{c,b,b},{c,b,b},{c,b,b},{c,b,b} },
  23. { {c,b,b},{c,b,b},{c,b,b},{w,w,w},{c,b,b},{c,b,b},{c,b,b},{c,b,b} },
  24. { {w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w} },
  25. { {c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{w,w,w} },
  26. { {c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{w,w,w} },
  27. { {c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{w,w,w} },
  28. { {w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w} }
  29. };
  30.  
  31. GLuint filter; // Which filter
  32. GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // 3 types of fog
  33. GLuint fogfilter= 0; // Which filter use
  34. GLfloat fogColor[4]= {0.5, 0.5, 0.5, 1.0}; // Color of gof
  35. GLfloat fogDensity = 0.55f;
  36. bool fogOn = false;
  37.  
  38. int ImageLoad(char *filename, Image *image) {
  39. FILE *file;
  40. unsigned long size; // size of the image in bytes.
  41. unsigned long i; // standard counter.
  42.  
  43. char temp; // temporary color storage for
  44. // make sure the file is there.
  45. if ((file = fopen(filename, "rb"))==NULL)
  46. {
  47. printf("File Not Found : %s\n",filename);
  48. return 0;
  49. }
  50. // seek through the bmp header, up to the width/height:
  51. fseek(file, 18, SEEK_CUR);
  52. // read the width and height
  53. if ((fread(&image->sizeX, 4, 1, file) != 1)
  54. || (fread(&image->sizeY, 4, 1, file) != 1)) {
  55. printf("Error reading width or height from %s.\n", filename);
  56. return 0;
  57. }
  58.  
  59. // calculate the size (assuming 24 bits or 3 bytes per pixel).
  60. size = image->sizeX * image->sizeY * 3;
  61.  
  62. // seek past the rest of the bitmap header.
  63. fseek(file, 24, SEEK_CUR);
  64. // read the data.
  65. image->data = (char *) malloc(size);
  66. if (image->data == NULL) {
  67. printf("Error in allocating memory");
  68. return 0;
  69. }
  70. if ((i = fread(image->data, size, 1, file)) != 1) {
  71. printf("Error reading image data from %s.\n", filename);
  72. return 0;
  73. }
  74. for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
  75. temp = image->data[i];
  76. image->data[i] = image->data[i+2];
  77. image->data[i+2] = temp;
  78. }
  79. fclose(file);
  80. return 1;
  81. }
  82.  
  83. Image * loadTexture(char *filename)
  84. {
  85. Image *image1;
  86. // allocate space for texture
  87. image1 = (Image *) malloc(sizeof(Image));
  88. if (image1 == NULL) {
  89. printf("Error allocating space for image");
  90. exit(0);
  91. }
  92. //pic.bmp is a 64x64 picture
  93. if (!ImageLoad(filename, image1)) {
  94. exit(1);
  95. }
  96. return image1;
  97. }
  98.  
  99. void init_texture(void)
  100. {
  101. glEnable(GL_DEPTH_TEST);
  102. glDepthFunc(GL_LESS);
  103.  
  104. Image *image1 = loadTexture("satin.bmp");
  105. if(image1 == NULL)
  106. {
  107. printf("Image was not returned from loadTexture\n");
  108. exit(0);
  109. }
  110.  
  111. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  112. // Create Texture
  113. glGenTextures(2, texture);
  114. glBindTexture(GL_TEXTURE_2D, texture[0]);
  115. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  116. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  117. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  118. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  119.  
  120. //TO DO Task 3
  121. //add texture from code stored in tex_brick
  122.  
  123. glTexImage2D(GL_TEXTURE_2D, 0, 3, 8, 8, 0,
  124. GL_RGB, GL_UNSIGNED_BYTE, tex_brick);
  125. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  126.  
  127. //TO DO Task 3
  128. //add second texture into texture[1] read from file
  129.  
  130. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  131. // Create Texture
  132. //glGenTextures(2, texture);
  133. glBindTexture(GL_TEXTURE_2D, texture[1]);
  134. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  135. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  136. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  137. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  138.  
  139. glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0,
  140. GL_RGB, GL_UNSIGNED_BYTE, image1->data);
  141. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  142.  
  143. free(image1);
  144.  
  145.  
  146. glEnable(GL_TEXTURE_2D);
  147. glShadeModel(GL_FLAT);
  148. }
  149.  
  150. void init_fog(void)
  151. {
  152. glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Fog mode
  153. glFogfv(GL_FOG_COLOR, fogColor); // Set fog color
  154. glFogf(GL_FOG_DENSITY, fogDensity); // Density of fog
  155. glHint(GL_FOG_HINT, GL_DONT_CARE); // kind of creation - pixel or vertex
  156. glFogf(GL_FOG_START, 1.0f); // length from begin
  157. glFogf(GL_FOG_END, 5.0f); // length to end
  158. if(fogOn)
  159. glEnable(GL_FOG); // Enable GL_FOG
  160. else
  161. glDisable(GL_FOG);
  162. }
  163.  
  164. void myinit(void)
  165. {
  166. glClearColor (0.5, 0.5, 0.5, 1.0);
  167.  
  168. //TO DO Task 2
  169. init_texture();
  170. }
  171.  
  172. void display_textures(void)
  173. {
  174.  
  175. glBindTexture(GL_TEXTURE_2D, texture[0]);
  176. glPushMatrix ();
  177. //TO DO Task 3
  178.  
  179. glTranslatef (3, 0, 0);
  180. glutSolidTeapot(1.0);
  181. glPopMatrix ();
  182.  
  183. //TO DO Task 3
  184. glBindTexture(GL_TEXTURE_2D, texture[1]);
  185. glPushMatrix ();
  186. //TO DO Task 3
  187.  
  188. glTranslatef (-3, 0, 0);
  189. glutSolidTeapot(1.0);
  190. glPopMatrix ();
  191.  
  192. }
  193.  
  194. void display_cylinder(void)
  195. {
  196. GLUquadricObj *cylinder = gluNewQuadric();
  197. glBindTexture(GL_TEXTURE_2D, texture[0]);
  198. gluQuadricTexture(cylinder, GL_TRUE);
  199. gluQuadricDrawStyle(cylinder, GLU_FILL);
  200. glPolygonMode(GL_FRONT, GL_FILL);
  201. gluQuadricNormals(cylinder, GLU_SMOOTH);
  202. gluCylinder(cylinder, 1.0, 0.0, 1.0, 20, 100);
  203.  
  204. }
  205.  
  206. void display(void)
  207. {
  208. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  209. //TO DO Task 1
  210. //glutWireTeapot(1.0);
  211.  
  212. //TO DO Task 2
  213. display_textures();
  214.  
  215. //TO DO Task 5
  216. display_cylinder();
  217.  
  218. glutSwapBuffers();
  219. }
  220.  
  221. void myReshape(int w, int h)
  222. {
  223. glViewport(0, 0, w, h);
  224. glMatrixMode(GL_PROJECTION);
  225. glLoadIdentity();
  226. gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0);
  227. glMatrixMode(GL_MODELVIEW);
  228. glLoadIdentity();
  229. glTranslatef(0.0, 0.0, -3.6);
  230. }
  231.  
  232. void keyboard (unsigned char key, int x, int y)
  233. {
  234. //TO DO Task 1
  235. //rotate of object
  236. switch(key)
  237. {
  238. case 'a':
  239. glRotatef(10, 0, 1, 0);
  240. break;
  241. case 's':
  242. glRotatef(10, 1, 0, 0);
  243. break;
  244. case 'd':
  245. glRotatef(-10, 0, 1, 0);
  246. break;
  247. case 'w':
  248. glRotatef(-10, 1, 0, 0);
  249. break;
  250. case 'q':
  251. glRotatef(10, 0, 0, 1);
  252. break;
  253. case 'e':
  254. glRotatef(-10, 0, 0, 1);
  255. break;
  256. //TO DO Task 4
  257. //init fog
  258. case 'f':
  259. fogOn = !fogOn;
  260. init_fog();
  261. break;
  262. case ' ':
  263. fogfilter = fogfilter >= 2 ? 0 : ++fogfilter;
  264. init_fog();
  265. break;
  266. case '+':
  267. fogDensity += 0.1f;
  268. init_fog();
  269.  
  270. break;
  271. case '-':
  272. fogDensity = fogDensity <= 0 ? 0 : fogDensity - 0.1f;
  273. init_fog();
  274. break;
  275. }
  276.  
  277.  
  278. glutPostRedisplay();
  279. }
  280.  
  281. int main(int argc, char** argv)
  282. {
  283. glutInit(&argc, argv);
  284. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  285. glutCreateWindow("Shaders");
  286. myinit(); //initialization of objects
  287.  
  288. glutReshapeFunc (myReshape); //processing during reshape
  289. glutDisplayFunc(display); //processing when displaying objects
  290. glutKeyboardFunc(keyboard); //keyboard subroutine
  291. glutMainLoop();
  292.  
  293. return 0;
  294. }
Add Comment
Please, Sign In to add comment