Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 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[3];
  7. GLint slices=16;
  8. GLint stacks=16;
  9. float density = 0.5f;
  10. struct Image {
  11. unsigned long sizeX;
  12. unsigned long sizeY;
  13. char *data;
  14. };
  15. typedef struct Image Image;
  16.  
  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("satin.bmp");
  104. if(image1 == NULL)
  105. {
  106. printf("Image was not returned from loadTexture\n");
  107. exit(0);
  108. }
  109.  
  110. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  111. // Create Texture
  112. glGenTextures(3, texture);
  113. glBindTexture(GL_TEXTURE_2D, texture[0]);
  114. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  115. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  116. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  117. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  118.  
  119. //TO DO Task 3
  120. //add texture from code stored in tex_brick
  121. glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0,
  122. GL_RGB, GL_UNSIGNED_BYTE, image1->data);
  123. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  124.  
  125. free(image1);
  126.  
  127. //TO DO Task 3
  128. glBindTexture(GL_TEXTURE_2D, texture[1]);
  129. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  130. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  131. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  132. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  133. glTexImage2D(GL_TEXTURE_2D, 0, 3, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_brick);
  134.  
  135. Image *image2 = loadTexture("image_name.bmp");
  136. if (image2 == NULL)
  137. {
  138. printf("Image was not returned from loadTexture\n");
  139. exit(0);
  140. }
  141.  
  142. glBindTexture(GL_TEXTURE_2D, texture[2]);
  143. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  144. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  146. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  147. glTexImage2D(GL_TEXTURE_2D, 0, 3, image2->sizeX, image2->sizeY, 0,
  148. GL_RGB, GL_UNSIGNED_BYTE, image2->data);
  149. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  150.  
  151.  
  152. glEnable(GL_TEXTURE_2D);
  153. glShadeModel(GL_FLAT);
  154. }
  155.  
  156. void init_fog(void)
  157. {
  158. glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Fog mode
  159. glFogfv(GL_FOG_COLOR, fogColor); // Set fog color
  160. glFogf(GL_FOG_DENSITY, density); // Density of fog
  161. glHint(GL_FOG_HINT, GL_DONT_CARE); // kind of creation - pixel or vertex
  162. glFogf(GL_FOG_START, 1.0f); // length from begin
  163. glFogf(GL_FOG_END, 5.0f); // length to end
  164. glEnable(GL_FOG); // Enable GL_FOG
  165. }
  166.  
  167. void myinit(void)
  168. {
  169. glClearColor (0.5, 0.5, 0.5, 1.0);
  170.  
  171. //TO DO Task 2
  172. init_texture();
  173. }
  174.  
  175. void display_cylinder(void)
  176. {
  177. GLUquadricObj *cylinder = gluNewQuadric();
  178. glBindTexture(GL_TEXTURE_2D, texture[2]);
  179. gluQuadricTexture(cylinder, GL_TRUE);
  180. gluQuadricDrawStyle(cylinder, GLU_FILL);
  181. glPolygonMode(GL_FRONT, GL_FILL);
  182. gluQuadricNormals(cylinder, GLU_SMOOTH);
  183. gluCylinder(cylinder, 1.0, 0.0, 1.0, 20, 100);
  184.  
  185. }
  186.  
  187. void display_textures(void)
  188. {
  189.  
  190. glBindTexture(GL_TEXTURE_2D, texture[0]);
  191. glPushMatrix();
  192. //TO DO Task 3
  193. glTranslatef(-3, 0, 0);
  194. glutSolidTeapot(1.0);
  195. glPopMatrix();
  196.  
  197. //TO DO Task 3
  198. glBindTexture(GL_TEXTURE_2D, texture[1]);
  199. glPushMatrix();
  200. //TO DO Task 3
  201. glTranslatef(3, 0, 0);
  202. glutSolidTeapot(1.0);
  203. glPopMatrix();
  204.  
  205. glBindTexture(GL_TEXTURE_2D, texture[2]);
  206. glPushMatrix();
  207. display_cylinder();
  208. glPopMatrix();
  209. }
  210.  
  211. void display(void)
  212. {
  213. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  214. //TO DO Task 1
  215. //glutWireTeapot(1.0);
  216.  
  217. //TO DO Task 2
  218. display_textures();
  219.  
  220. //TO DO Task 5
  221. //display_cylinder();
  222.  
  223. glutSwapBuffers();
  224. }
  225.  
  226. void myReshape(int w, int h)
  227. {
  228. glViewport(0, 0, w, h);
  229. glMatrixMode(GL_PROJECTION);
  230. glLoadIdentity();
  231. gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0);
  232. glMatrixMode(GL_MODELVIEW);
  233. glLoadIdentity();
  234. glTranslatef(0.0, 0.0, -3.6);
  235. }
  236.  
  237. void keyboard (unsigned char key, int x, int y)
  238. {
  239. //TO DO Task 1
  240. switch (key) {
  241. case 'w':
  242. glRotatef(10, 1, 0, 0);
  243. break;
  244. case 's':
  245. glRotatef(-10, 1, 0, 0);
  246. break;
  247. case 'a':
  248. glRotatef(10, 0, 1, 0);
  249. break;
  250. case 'd':
  251. glRotatef(-10, 0, 1, 0);
  252. break;
  253. case 'q':
  254. glRotatef(10, 0, 0, 1);
  255. break;
  256. case 'e':
  257. glRotatef(-10, 0, 0, 1);
  258. break;
  259.  
  260. case 'f':
  261. init_fog();
  262. break;
  263. case ' ':
  264. fogfilter++;
  265. if (fogfilter > 2)
  266. fogfilter = 0;
  267. break;
  268. case '+':
  269. density += 0.1f;
  270. if (density > 1.0f) {
  271. density = 0.0f;
  272. }
  273. break;
  274. case '-':
  275. density -= 0.1f;
  276. if (density < 0.0f) {
  277. density = 1.0f;
  278. }
  279. break;
  280. }
  281.  
  282. //TO DO Task 4
  283.  
  284. glutPostRedisplay();
  285. }
  286.  
  287. int main(int argc, char** argv)
  288. {
  289. glutInit(&argc, argv);
  290. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  291. glutCreateWindow("Shaders");
  292. myinit(); //initialization of objects
  293.  
  294. glutReshapeFunc (myReshape); //processing during reshape
  295. glutDisplayFunc(display); //processing when displaying objects
  296. glutKeyboardFunc(keyboard); //keyboard subroutine
  297. glutMainLoop();
  298.  
  299. return 0;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement