Guest User

Untitled

a guest
Oct 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <windows.h>
  2. #include <GL/gl.h>
  3. #include <GL/glut.h>
  4. #include <math.h>
  5. #include "Camera.h"
  6. #include "Vector3.h"
  7. #include <iostream>
  8. #include <assert.h>
  9. #include <fstream>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include "textures.h"
  14.  
  15. using namespace std;
  16.  
  17. fstream inf;
  18.  
  19. ushort getShort() //helper function
  20. { //BMP format uses little-endian integer types
  21. // get a 2-byte integer stored in little-endian form
  22. char ic;
  23. ushort ip;
  24. inf.get(ic); ip = ic; //first byte is little one
  25. inf.get(ic); ip |= ((ushort)ic << 8); // or in high order byte
  26. return ip;
  27. }
  28.  
  29. //<<<<<<<<<<<<<<<<<<<< getLong >>>>>>>>>>>>>>>>>>>
  30. ulong getLong() //helper function
  31. { //BMP format uses little-endian integer types
  32. // get a 4-byte integer stored in little-endian form
  33. ulong ip = 0;
  34. char ic = 0;
  35. unsigned char uc = ic;
  36. inf.get(ic); uc = ic; ip = uc;
  37. inf.get(ic); uc = ic; ip |=((ulong)uc << 8);
  38. inf.get(ic); uc = ic; ip |=((ulong)uc << 16);
  39. inf.get(ic); uc = ic; ip |=((ulong)uc << 24);
  40. return ip;
  41. }
  42.  
  43. void RGBpixmap::SetTexColor(float rin, float gin, float bin)
  44. {
  45. // Set multipliers to set the color of the parameterized textures
  46. gg = gin;
  47. gr = rin;
  48. gb = bin;
  49. }
  50.  
  51. int RGBpixmap:: readBMPFile(char *fname)
  52. {
  53. // Read into memory an mRGB image from an uncompressed BMP file.
  54. // return 0 on failure, 1 on success
  55. inf.open(fname, ios::in|ios::binary); //read binary char's
  56. if(!inf){ cout << " can't open file: " << fname << endl; return 0;}
  57. int k, row, col, numPadBytes, nBytesInRow;
  58. // read the file header information
  59. char ch1, ch2;
  60. inf.get(ch1); inf.get(ch2); //type: always 'BM'
  61. ulong fileSize = getLong();
  62. ushort reserved1 = getShort(); // always 0
  63. ushort reserved2= getShort(); // always 0
  64. ulong offBits = getLong(); // offset to image - unreliable
  65. ulong headerSize = getLong(); // always 40
  66. ulong numCols = getLong(); // number of columns in image
  67. ulong numRows = getLong(); // number of rows in image
  68. ushort planes= getShort(); // always 1
  69. ushort bitsPerPixel= getShort(); //8 or 24; allow 24 here
  70. ulong compression = getLong(); // must be 0 for uncompressed
  71. ulong imageSize = getLong(); // total bytes in image
  72. ulong xPels = getLong(); // always 0
  73. ulong yPels = getLong(); // always 0
  74. ulong numLUTentries = getLong(); // 256 for 8 bit, otherwise 0
  75. ulong impColors = getLong(); // always 0
  76. if(bitsPerPixel != 24)
  77. { // error - must be a 24 bit uncompressed image
  78. cout << "not a 24 bit/pixelimage, or is compressed!\n";
  79. inf.close(); return 0;
  80. }
  81. //add bytes at end of each row so total # is a multiple of 4
  82. // round up 3*numCols to next mult. of 4
  83. nBytesInRow = ((3 * numCols + 3)/4) * 4;
  84. numPadBytes = nBytesInRow - 3 * numCols; // need this many
  85. nRows = numRows; // set class's data members
  86. nCols = numCols;
  87. pixel = new mRGB[nRows * nCols]; //make space for array
  88. if(!pixel) return 0; // out of memory!
  89. long count = 0;
  90. char dum;
  91. for(row = 0; row < nRows; row++) // read pixel values
  92. {
  93. for(col = 0; col < nCols; col++)
  94. {
  95. char r,g,b;
  96. inf.get(b); inf.get(g); inf.get(r); //read bytes
  97. pixel[count].r = r; //place them in colors
  98. pixel[count].g = g;
  99. pixel[count++].b = b;
  100. }
  101. for(k = 0; k < numPadBytes ; k++) //skip pad bytes at row's end
  102. inf >> dum;
  103. }
  104. inf.close(); return 1; // success
  105. }
  106.  
  107. void RGBpixmap::SetTexture(GLuint textureName)
  108. {
  109. // Set texture parameters to initial values
  110. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  111. // Bind 2D textures, based on input name
  112. glBindTexture(GL_TEXTURE_2D, textureName);
  113. // Set wrapping and interpolation
  114. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  115. glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT);
  116. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  117. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  118. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->nCols, this->nRows, 0, GL_RGB, GL_UNSIGNED_BYTE, this->pixel);
  119. }
Add Comment
Please, Sign In to add comment