Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #include "GL/freeglut.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct{ GLubyte r, g, b; } PIXEL;
  6.  
  7. PIXEL *imagineBMP;
  8. GLint width, height;
  9.  
  10. PIXEL *ReadBMP(char *nume, int *ncol, int *nlin)
  11. {
  12. int i, j, w, h, data_offset, r, lglin, tcompr;
  13. long dofs;
  14. short nbpp;
  15. char buf[20];
  16. PIXEL *img;
  17. GLubyte t;
  18. FILE *f;
  19. if ((f = fopen(nume, "rb")) == NULL)
  20. {
  21. fprintf(stderr, "ReadBMP --eroare deschidere fisier\n");
  22. exit(1);
  23. }
  24. else
  25. {
  26. fprintf(stderr, "Fisierul a fost deschis cu succes!");
  27. }
  28. fread(buf, 1, 2, f);
  29. buf[2] = '\0';
  30. fseek(f, 28L, SEEK_SET);
  31. fread(&nbpp, sizeof(short), 1, f);
  32. fseek(f, 30L, SEEK_SET);
  33. fread(&tcompr, sizeof(int), 1, f);
  34. if (strcmp(buf, "BM") != 0 || nbpp != 24 || tcompr != 0)
  35. {
  36. //se citeste un fisier BMP necomprimat
  37. fprintf(stderr, "ReadBMP -- format BMP incorect\n");
  38. exit(1);
  39. }
  40.  
  41. fseek(f, 18L, SEEK_SET);
  42. fread(&w, sizeof(int), 1, f); //citesc numarul de coloane
  43. fread(&h, sizeof(int), 1, f); //citesc numarul de linii
  44. img = (PIXEL *)malloc(h*w*sizeof(PIXEL));
  45. fseek(f, 10L, SEEK_SET); //cauta deplasamentul zonei de date
  46. fread(&data_offset, sizeof(int), 1, f); //citesc deplas. zonei de
  47. // date fata de inceputul fisierului
  48. lglin = w * 3;
  49. if (r = lglin % 4) lglin += 4 - r;
  50. for (dofs = data_offset, i = 0; i < h; dofs += lglin, i++)
  51. {
  52. fseek(f, dofs, SEEK_SET);
  53. fread(&img[i*w], sizeof(PIXEL), w, f);
  54. for (j = 0; j < w; j++) //schimba red/blue
  55. {
  56. t = img[i*w + j].r;
  57. img[i*w + j].r = img[i*w + j].b;
  58. img[i*w + j].b = t;
  59. }
  60. }
  61. *nlin = h;
  62. *ncol = w;
  63. fclose(f);
  64. return img;
  65. }
  66.  
  67.  
  68.  
  69. PIXEL *image;
  70. static GLuint texName[1];
  71.  
  72.  
  73. void Plane()
  74. {
  75. glBegin(GL_QUADS);
  76. glVertex3d(0, 0, 1);
  77. glTexCoord2f(0.0, 0.0);
  78. glVertex3d(3, 0, 1);
  79. glTexCoord2f(1.0, 0.0);
  80. glVertex3d(3, 3,-1);
  81. glTexCoord2f(1.0, 1.0);
  82. glVertex3d(0, 3, -1);
  83. glTexCoord2f(0.0, 1.0);
  84. glEnd();
  85. }
  86.  
  87.  
  88.  
  89. void init(void)
  90. {
  91. glClearColor(1, 1, 1, 0);
  92. //texturare
  93. image = ReadBMP("bricks.bmp", &width, &height);
  94.  
  95. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  96. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  97. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  98. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  99. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  100. glEnable(GL_TEXTURE_2D);
  101. }
  102.  
  103. void display(void)
  104. {
  105. glClear(GL_COLOR_BUFFER_BIT);
  106. glPushMatrix();
  107. glTranslatef(-1.5, -1.5, -5);
  108. Plane();
  109. glPopMatrix();
  110. glutSwapBuffers();
  111. }
  112.  
  113. void reshape(int w, int h)
  114. {
  115. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  116. glMatrixMode(GL_PROJECTION);
  117. glLoadIdentity();
  118. gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.1, 100);
  119. glMatrixMode(GL_TEXTURE);
  120. glLoadIdentity();
  121. glScaled(2, 2, 2);
  122. glMatrixMode(GL_MODELVIEW);
  123. glLoadIdentity();
  124. }
  125.  
  126. int main(int argc, char **argv)
  127. {
  128. glutInit(&argc, argv);
  129. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  130. glutInitWindowPosition(200, 200);
  131. glutInitWindowSize(600, 600);
  132. glutCreateWindow("SPG OpenGL");
  133.  
  134. init();
  135. glutDisplayFunc(display);
  136. glutReshapeFunc(reshape);
  137.  
  138. glutMainLoop();
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement