ulvis.paste.net

Paste Search Dynamic
Recent pastes
load png texture
  1. #ifdef _WIN32
  2. #include <GL/glut.h>
  3. #else
  4. #include <GLUT/glut.h>
  5. #endif
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <png.h>
  9. #include <iostream>
  10.  
  11. GLubyte *textureImage;
  12. float rotateX = 0;
  13. float rotateY = 0;
  14.  
  15. int mouseX;
  16. int mouseY;
  17.  
  18. bool loadPngImage(char *name, int &outWidth, int &outHeight, bool &outHasAlpha, GLubyte **outData) {
  19.     png_structp png_ptr;
  20.     png_infop info_ptr;
  21.     unsigned int sig_read = 0;
  22.     int color_type, interlace_type;
  23.     file *fp;
  24.  
  25.     if ((fp = fopen(name, "rb")) == null)
  26.         return false;
  27.  
  28.     /* Create and initialize the png_struct
  29.      * with the desired error handler
  30.      * functions.  If you want to use the
  31.      * default stderr and longjump method,
  32.      * you can supply NULL for the last
  33.      * three parameters.  We also supply the
  34.      * the compiler header file version, so
  35.      * that we know if the application
  36.      * was compiled with a compatible version
  37.      * of the library.  REQUIRED
  38.      */
  39.     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  40.                                      null, null, null);
  41.  
  42.     if (png_ptr == null) {
  43.         fclose(fp);
  44.         return false;
  45.     }
  46.  
  47.     /* Allocate/initialize the memory
  48.      * for image information.  REQUIRED. */
  49.     info_ptr = png_create_info_struct(png_ptr);
  50.     if (info_ptr == null) {
  51.         fclose(fp);
  52.         png_destroy_read_struct(&png_ptr, null, null);
  53.         return false;
  54.     }
  55.  
  56.     /* Set error handling if you are
  57.      * using the setjmp/longjmp method
  58.      * (this is the normal method of
  59.      * doing things with libpng).
  60.      * REQUIRED unless you  set up
  61.      * your own error handlers in
  62.      * the png_create_read_struct()
  63.      * earlier.
  64.      */
  65.     if (setjmp(png_jmpbuf(png_ptr))) {
  66.         /* Free all of the memory associated
  67.          * with the png_ptr and info_ptr */
  68.         png_destroy_read_struct(&png_ptr, &info_ptr, null);
  69.         fclose(fp);
  70.         /* If we get here, we had a
  71.          * problem reading the file */
  72.         return false;
  73.     }
  74.  
  75.     /* Set up the output control if
  76.      * you are using standard C streams */
  77.     png_init_io(png_ptr, fp);
  78.  
  79.     /* If we have already
  80.      * read some of the signature */
  81.     png_set_sig_bytes(png_ptr, sig_read);
  82.  
  83.     /*
  84.      * If you have enough memory to read
  85.      * in the entire image at once, and
  86.      * you need to specify only
  87.      * transforms that can be controlled
  88.      * with one of the PNG_TRANSFORM_*
  89.      * bits (this presently excludes
  90.      * dithering, filling, setting
  91.      * background, and doing gamma
  92.      * adjustment), then you can read the
  93.      * entire image (including pixels)
  94.      * into the info structure with this
  95.      * call
  96.      *
  97.      * PNG_TRANSFORM_STRIP_16 |
  98.      * PNG_TRANSFORM_PACKING  forces 8 bit
  99.      * PNG_TRANSFORM_EXPAND forces to
  100.      *  expand a palette into RGB
  101.      */
  102.     png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, null);
  103.  
  104.     png_uint_32 width, height;
  105.     int bit_depth;
  106.     png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
  107.                  &interlace_type, null, null);
  108.     outWidth = width;
  109.     outHeight = height;
  110.  
  111.     unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
  112.     *outData = (unsigned char*) malloc(row_bytes * outHeight);
  113.  
  114.     png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
  115.  
  116.     for (int i = 0; i < outHeight; i++) {
  117.         // note that png is ordered top to
  118.         // bottom, but OpenGL expect it bottom to top
  119.         // so the order or swapped
  120.         memcpy(*outData+(row_bytes * (outHeight-1-i)), row_pointers[i], row_bytes);
  121.     }
  122.  
  123.     /* Clean up after the read,
  124.      * and free any memory allocated */
  125.     png_destroy_read_struct(&png_ptr, &info_ptr, null);
  126.  
  127.     /* Close the file */
  128.     fclose(fp);
  129.  
  130.     /* That's it */
  131.     return true;
  132. }
  133.  
  134. void init(void) {
  135.     glClearColor(0.0, 0.0, 0.0, 0.0);
  136.     glEnable(GL_DEPTH_TEST);
  137.     // The following two lines enable semi transparent
  138.     glEnable(GL_BLEND);
  139.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  140.  
  141.     int width, height;
  142.     bool hasAlpha;
  143.     char filename[] = "logo.png";
  144.     bool success = loadPngImage(filename, width, height, hasAlpha, &textureImage);
  145.     if (!success) {
  146.         std::cout << "Unable to load png file" << std::endl;
  147.         return;
  148.     }
  149.     std::cout << "Image loaded " << width << " " << height << " alpha " << hasAlpha << std::endl;
  150.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  151.     glTexImage2D(GL_TEXTURE_2D, 0, hasAlpha ? 4 : 3, width,
  152.                  height, 0, hasAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,
  153.                  textureImage);
  154.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  155.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  156.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  157.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  158.     glEnable(GL_TEXTURE_2D);
  159.     glShadeModel(GL_FLAT);
  160. }
  161.  
  162. void display(void) {
  163.     glLoadIdentity();
  164.     glTranslatef(0.0, 0.0, -3.6);
  165.     glRotatef(rotateX, 0,1,0);
  166.     glRotatef(rotateY, 1,0,0);
  167.  
  168.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  169.     glBegin(GL_QUADS);
  170.     glTexCoord2f(0.0, 0.0);
  171.     glVertex3f(-2.0, -1.0, 0.0);
  172.     glTexCoord2f(0.0, 1.0);
  173.     glVertex3f(-2.0, 1.0, 0.0);
  174.     glTexCoord2f(1.0, 1.0);
  175.     glVertex3f(0.0, 1.0, 0.0);
  176.     glTexCoord2f(1.0, 0.0);
  177.     glVertex3f(0.0, -1.0, 0.0);
  178.  
  179.     glEnd();
  180.     glutSwapBuffers();
  181. }
  182.  
  183. void myReshape(int w, int h) {
  184.     glViewport(0, 0, w, h);
  185.     glMatrixMode(GL_PROJECTION);
  186.     glLoadIdentity();
  187.     gluPerspective(60.0, 1.0 * (GLfloat) w / (GLfloat) h, 1.0, 30.0);
  188.     glMatrixMode(GL_MODELVIEW);
  189. }
  190.  
  191. void mousePassive(int x, int y){
  192.     mouseX = x;
  193.     mouseY = y;
  194. }
  195.  
  196. void mouseMotion(int x, int y){
  197.     const float SPEED = 2;
  198.  
  199.     rotateX += (mouseX-x)/SPEED;
  200.     rotateY += (mouseY-y)/SPEED;
  201.     mousePassive(x, y);
  202.     glutPostRedisplay();
  203. }
  204.  
  205. int
  206. main(int argc, char** argv) {
  207.     glutInit(&argc, argv);
  208.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
  209.     glutCreateWindow("PNG texture");
  210.     glutMotionFunc(mouseMotion);
  211.     glutPassiveMotionFunc(mousePassive);
  212.     init();
  213.     glutReshapeFunc(myReshape);
  214.     glutDisplayFunc(display);
  215.     std::cout << "Use mouse drag to rotate." << std::endl;
  216.     glutMainLoop();
  217.     return 0;
  218. }
Parsed in 0.035 seconds