Vis 2 Demo  1.0
Technical illustration type real-time rendering of geometry
 All Classes Namespaces Files Functions Variables Typedefs Macros
Texture.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "Texture.h"
3 
4 using namespace vis2;
5 
6 Texture::Texture(const std::string& _str_path)
7  : image_data_handle(0), texture_handle(0), texture_type(VIS2_TEX_TYPE_RGB), texture_width(0), texture_height(0)
8 {
9  // generate the texture handle
10  glGenTextures(1,&texture_handle);
11  // bind to a texture unit on the GK
12  glActiveTexture(GL_TEXTURE0);
13  glBindTexture(GL_TEXTURE_2D,texture_handle);
14 
15  // load texture form file
16  loadTextureFromFile(_str_path);
17 
18  // copy image data to the GK
19  this->texture_width = ilGetInteger(IL_IMAGE_WIDTH);
20  this->texture_height = ilGetInteger(IL_IMAGE_HEIGHT);
21  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGB, GL_UNSIGNED_BYTE,ilGetData());
22 
23  // set its parameters
24  glGenerateMipmap(GL_TEXTURE_2D);
25  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
26  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
27 
28 }
29 
30 Texture::Texture(const unsigned int _width, const unsigned int _height, const unsigned int _type)
31  : image_data_handle(0), texture_handle(0), texture_type(VIS2_TEX_TYPE_RGB), texture_width(_width), texture_height(_height)
32 {
33  // generate the texture handle
34  glGenTextures(1,&texture_handle);
35  // bind
36  glBindTexture(GL_TEXTURE_2D,texture_handle);
37 
38  // define texture format (target, mipmap level, internal format, width, height, border, enum format, type, data)
39  switch (_type)
40  {
41  case VIS2_TEX_TYPE_RGB:
42  {
43  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, _width, _height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
45  }
46  break;
47  case VIS2_TEX_TYPE_RGBA:
48  {
49  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
51  }
52  break;
54  {
55  // glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, _width, _height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
56  glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, _width, _height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
58  }
59  break;
60  default:
61  break;
62  }
63 
64  // set its parameters
65  glGenerateMipmap(GL_TEXTURE_2D);
66  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // of vital importance for FBO textures!
67  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // of vital importance for FBO textures!
68  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // if not set, causes errors on NVIDIA drivers
69  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // if not set, causes errors on NVIDIA drivers
70 
71 }
72 
74 {
75  if (image_data_handle != 0)
76  ilDeleteImages(1,&image_data_handle);
77  if (texture_handle != 0)
78  glDeleteTextures(1,&texture_handle);
79 }
80 
81 void Texture::textureBind(const int _unit)
82 {
83  glActiveTexture(GL_TEXTURE0 + _unit);
84  glBindTexture(GL_TEXTURE_2D, texture_handle);
85 }
86 
87 void Texture::textureBind2FBO(const int _unit)
88 {
89  // bind texture to FBO (fbo target, fbo attachment, texture target, texture handle, mipmap level)
90  switch (this->texture_type)
91  {
92  case VIS2_TEX_TYPE_RGB:
93  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + _unit, GL_TEXTURE_2D, this->texture_handle, 0);
94  break;
95  case VIS2_TEX_TYPE_RGBA:
96  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + _unit, GL_TEXTURE_2D, this->texture_handle, 0);
97  break;
99  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, this->texture_handle, 0);
100  break;
101  default:
102  break;
103  }
104 }
105 
106 void Texture::loadTextureFromFile(const std::string _file_path)
107 {
108  // uses DevIL
109  // generate an image name and bind it
110  ilGenImages(1,&image_data_handle);
111  ilBindImage(image_data_handle);
112  // load image data
113  std::cout << "Loading texture from file \"" << _file_path << "\" ." << std::endl;
114  if (ilLoadImage((const ILstring)_file_path.c_str()) != IL_TRUE){
115  std::cout << "Failed to load texture file \"" << _file_path << "\" !" << std::endl;
116  system("PAUSE");
117  exit(-1);
118  }
119  // std::cout << "texture w / h " << ilGetInteger(IL_IMAGE_WIDTH) << " / " << ilGetInteger(IL_IMAGE_HEIGHT) << std::endl;
120 }