Advertisement
Catachan

Untitled

Feb 22nd, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. //The issue is that the draw function is drawing only a black rectangle
  2. //to the window I generate.
  3. //I have verified that the array of Pixel objects has been filled
  4. //correctly, and remains filled correctly even after the program
  5. //advances past the texture creation.
  6.  
  7. class Pixel
  8. {
  9.     private:
  10.         unsigned char   r;  //The Red value
  11.         unsigned char   g;  //The Green value
  12.         unsigned char   b;  //The Blue value
  13.  
  14.     public:
  15.         Pixel(): r(0), g(0), b(0) {}
  16.         Pixel(char R, char G, char B): r(R), g(G), b(B) {}
  17.  
  18.         ~Pixel(){}
  19.        
  20.         void    setR(unsigned char R)   {r = R;}
  21.         void    setG(unsigned char G)   {g = G;}
  22.         void    setB(unsigned char B)   {b = B;}
  23.  
  24.         const   unsigned char   R() {return r;}
  25.         const   unsigned char   G() {return g;}
  26.         const   unsigned char   B() {return b;}
  27. };
  28.  
  29.  
  30. class Image
  31. {
  32.     private:
  33.         int     width;  //The width of the image
  34.         int     height; //The height of the image
  35.         Pixel   *   pix;    //List of the image pixels
  36.  
  37.     public:
  38.         Image();
  39.         Image(std::string);
  40.         ~Image()
  41.     {
  42.         delete pix;
  43.     }
  44.         Image(const Image&);
  45.  
  46.         Image& operator=(const Image&);
  47.  
  48.         void    readFile(std::ifstream&);   //Read the image into the pixels
  49.  
  50.         int getHeight()     {return height;}
  51.         int getWidth()  {return width;}
  52.  
  53.         const void draw();  //Draw the image to the screen
  54. };
  55.  
  56. const void Image::draw()
  57. {
  58.     GLuint tex = 0;
  59.     GLuint verts[][5] = {{0,0,0,0,0}, {width,0,0,1,0}, {0,height,0,0,1}, {width,height,0,1,1}};
  60.     glGenTextures(1, &tex);
  61.     glBindTexture(GL_TEXTURE_2D, tex);
  62.  
  63.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  64.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  65.  
  66.     // upload texture data
  67.     glTexImage2D(GL_TEXTURE_2D, 0, 3, /*GL_RGB,*/ width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pix);
  68.  
  69.     /*
  70.      * You only have to enable/disable when you are going to be drawing in a
  71.      * different mode on subsequent calls. Leaving in for good measure.
  72.      */
  73.     glEnableClientState(GL_VERTEX_ARRAY);
  74.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  75.  
  76.     // Point to vertex data and draw the strip.
  77.     glVertexPointer(3, GL_FLOAT, sizeof(float)*5, verts);
  78.     glTexCoordPointer(2, GL_FLOAT, sizeof(float)*5, verts+3);
  79.     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  80.  
  81.     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  82.     glDisableClientState(GL_VERTEX_ARRAY);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement