Advertisement
Guest User

Untitled

a guest
Aug 5th, 2010
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. /*
  2.  *  main.cpp
  3.  *  TestEnvGlut2
  4.  *
  5.  *  Created by Jos Timanta Tarigan on 8/5/10.
  6.  *  Copyright 2010 Linkoping University. All rights reserved.
  7.  *
  8.  */
  9.  
  10. /*
  11.  *  main.cpp
  12.  *  TestEnvGlut
  13.  *
  14.  *  Created by Jos Timanta Tarigan on 8/5/10.
  15.  *  Copyright 2010 Linkoping University. All rights reserved.
  16.  *
  17.  */
  18.  
  19. #include <GLUT/glut.h>
  20.  
  21. #include <FreeImage/FreeImage.h>
  22.  
  23. #include <stdio.h>    // for fopen, FILE & NULL
  24. #include <cstdlib>
  25. #include <iostream>
  26.  
  27.  
  28. #define PI 3.14159265
  29.  
  30.  
  31. int width = 100;
  32. int height = 100;
  33. int size = 10;
  34.  
  35. GLuint texname = 0;
  36.  
  37. ///Now the interesting code:
  38. void loadTexture(const char* textureFile){
  39.     FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(textureFile,0);//Automatocally detects the format(from over 20 formats!)
  40.     FIBITMAP* imagen = FreeImage_Load(formato, textureFile);
  41.    
  42.     FIBITMAP* temp = imagen;
  43.     imagen = FreeImage_ConvertTo32Bits(imagen);
  44.     FreeImage_Unload(temp);
  45.    
  46.     int w = FreeImage_GetWidth(imagen);
  47.     int h = FreeImage_GetHeight(imagen);
  48.     std::cout<<"The size of the image is: "<<textureFile<<" es "<<w<<"*"<<h<<std::endl; //Some debugging code
  49.    
  50.     GLubyte* textura = new GLubyte[4*w*h];
  51.     char* pixeles = (char*)FreeImage_GetBits(imagen);
  52.     //FreeImage loads in BGR format, so you need to swap some bytes(Or use GL_BGR).
  53.    
  54.     for(int j= 0; j<w*h; j++){
  55.         textura[j*4+0]= pixeles[j*4+2];
  56.         textura[j*4+1]= pixeles[j*4+1];
  57.         textura[j*4+2]= pixeles[j*4+0];
  58.         textura[j*4+3]= pixeles[j*4+3];
  59.         //cout<<j<<": "<<textura[j*4+0]<<"**"<<textura[j*4+1]<<"**"<<textura[j*4+2]<<"**"<<textura[j*4+3]<<endl;
  60.     }
  61.     std::cout << "finish" << std::endl;
  62.     //Now generate the OpenGL texture object
  63.     std::cerr << "what?" << std::endl;
  64.    
  65.     glGenTextures(1, &texname);
  66.     std::cerr << "asdf?" << std::endl;
  67.     glBindTexture(GL_TEXTURE_2D, texname);
  68.     std::cout << "wer?" << std::endl;
  69.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)textura );
  70.     std::cout << "is it?" << std::endl;
  71.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  72.    
  73.     GLenum huboError = glGetError();
  74.     if(huboError){
  75.        
  76.         std::cout<<"There was an error loading the texture"<<std::endl;
  77.     }  
  78.    
  79. }
  80.  
  81. int main ()
  82. {
  83.     loadTexture("2.bmp");
  84.    
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement