Advertisement
Guest User

Texture loading test

a guest
Sep 4th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1.  
  2.  
  3. #include <cassert>
  4. #include <cstdio>
  5. #include <string>
  6. #include <iostream>
  7.  
  8. #if defined(__APPLE__) || defined(MACOSX)
  9. #include <OpenGL/gl.h>
  10. #include <GLUT/glut.h>
  11. #else
  12. #include <GL/gl.h>
  13. #include <GL/glut.h>
  14. #endif
  15.  
  16. GLuint textureId;
  17.  
  18. #define glCheck(call) ((call), checkError(__FILE__, __LINE__))
  19. void checkError(const std::string& file, unsigned int line)
  20. {
  21.     // Get the last error
  22.     GLenum errorCode = glGetError();
  23.    
  24.     if (errorCode != GL_NO_ERROR)
  25.     {
  26.         std::string error = "unknown error";
  27.         std::string description  = "no description";
  28.        
  29.         // Decode the error code
  30.         switch (errorCode)
  31.         {
  32.             case GL_INVALID_ENUM :
  33.             {
  34.                 error = "GL_INVALID_ENUM";
  35.                 description = "an unacceptable value has been specified for an enumerated argument";
  36.                 break;
  37.             }
  38.                
  39.             case GL_INVALID_VALUE :
  40.             {
  41.                 error = "GL_INVALID_VALUE";
  42.                 description = "a numeric argument is out of range";
  43.                 break;
  44.             }
  45.                
  46.             case GL_INVALID_OPERATION :
  47.             {
  48.                 error = "GL_INVALID_OPERATION";
  49.                 description = "the specified operation is not allowed in the current state";
  50.                 break;
  51.             }
  52.                
  53.             case GL_STACK_OVERFLOW :
  54.             {
  55.                 error = "GL_STACK_OVERFLOW";
  56.                 description = "this command would cause a stack overflow";
  57.                 break;
  58.             }
  59.                
  60.             case GL_STACK_UNDERFLOW :
  61.             {
  62.                 error = "GL_STACK_UNDERFLOW";
  63.                 description = "this command would cause a stack underflow";
  64.                 break;
  65.             }
  66.                
  67.             case GL_OUT_OF_MEMORY :
  68.             {
  69.                 error = "GL_OUT_OF_MEMORY";
  70.                 description = "there is not enough memory left to execute the command";
  71.                 break;
  72.             }
  73.                
  74.             case GL_INVALID_FRAMEBUFFER_OPERATION_EXT :
  75.             {
  76.                 error = "GL_INVALID_FRAMEBUFFER_OPERATION_EXT";
  77.                 description = "the object bound to FRAMEBUFFER_BINDING_EXT is not \"framebuffer complete\"";
  78.                 break;
  79.             }
  80.         }
  81.        
  82.         // Log the error
  83.         std::cout << "An internal OpenGL call failed in "
  84.         << file.substr(file.find_last_of("\\/") + 1) << " (" << line << ") : "
  85.         << error << ", " << description
  86.         << std::endl;
  87.     }
  88. }
  89.  
  90. void render(void)
  91. {
  92.     glCheck(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
  93.     glCheck(glLoadIdentity());
  94.    
  95.     glCheck(glBindTexture(GL_TEXTURE_2D, textureId));
  96.    
  97.     glCheck(glRotatef(1, 0, 0, 5));
  98.     glCheck(glPushMatrix());
  99.     glCheck(glTranslatef(0.1, 0.1, 0));
  100.     glCheck(glScalef(0.8, 0.8, 0.8));
  101.    
  102.     glCheck(glBegin(GL_QUADS));
  103.     {
  104.         glCheck(glTexCoord2f(0.0f, 0.0f));
  105.         glCheck(glVertex2f(-1.0f, -1.0f));  // Bottom Left Of The Texture and Quad
  106.        
  107.         glCheck(glTexCoord2f(1.0f, 0.0f));
  108.         glCheck(glVertex2f( 1.0f, -1.0f));  // Bottom Right Of The Texture and Quad
  109.        
  110.         glCheck(glTexCoord2f(1.0f, 1.0f));
  111.         glCheck(glVertex2f( 1.0f,  1.0f));  // Top Right Of The Texture and Quad
  112.        
  113.         glCheck(glTexCoord2f(0.0f, 1.0f));
  114.         glCheck(glVertex2f(-1.0f,  1.0f));  // Top Left Of The Texture and Quad
  115.     }
  116.     glCheck(glEnd());
  117.    
  118.     glCheck(glPopMatrix());
  119.    
  120.    
  121.     glutSwapBuffers();
  122. }
  123.  
  124. void setup()
  125. {
  126.     glCheck(glClearColor(1, 0, 0, 1));
  127.    
  128.     unsigned width = 128;
  129.     unsigned heigth = 128;
  130.     unsigned char *data = new unsigned char[width * heigth * 4];
  131.     assert(data != NULL);
  132.    
  133.     for (unsigned x = 0; x < width;x++)
  134.     {
  135.         for (unsigned y = 0; y < heigth;y++)
  136.         {
  137.             if ((y * width + x) % 2 == 0)
  138.             {
  139.                 data[(x * heigth + y) * 4 + 0] = 0;
  140.                 data[(x * heigth + y) * 4 + 1] = 0;
  141.                 data[(x * heigth + y) * 4 + 2] = 255;
  142.                 data[(x * heigth + y) * 4 + 3] = 255;
  143.             }
  144.             else
  145.             {
  146.                 data[(x * heigth + y) * 4 + 0] = 255;
  147.                 data[(x * heigth + y) * 4 + 1] = 0;
  148.                 data[(x * heigth + y) * 4 + 2] = 0;
  149.                 data[(x * heigth + y) * 4 + 3] = 255;
  150.             }
  151.         }
  152.     }
  153.    
  154.     glCheck(glEnable(GL_TEXTURE_2D));
  155.     glCheck(glGenTextures(1, &textureId));
  156.     glCheck(glBindTexture(GL_TEXTURE_2D, textureId));
  157.     glCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, data));
  158.    
  159.     glCheck(glMatrixMode(GL_MODELVIEW));
  160.     glCheck(glLoadIdentity());
  161. }
  162.  
  163. int main( int argc, char** argv )
  164. {
  165.     glutInit(&argc,argv);
  166.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
  167.    
  168.     glutInitWindowPosition( 20, 60 );
  169.     glutInitWindowSize( 360, 360 );
  170.     glutCreateWindow( "Texture test" );
  171.    
  172.     setup();
  173.     glutDisplayFunc(render);
  174.     glutMainLoop();
  175.    
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement