Advertisement
Guest User

Untitled

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