/*Build3DMipmaps(components, width, height, depth, format, type, pixels) -> error (userdata)*/ static int luaglu_build_3d_mipmaps(lua_State *L) { lua_pushnumber(L, gluBuild3DMipmaps(GL_TEXTURE_3D, (GLint)luaL_checkinteger(L, 1), luaL_checkinteger(L, 2), luaL_checkinteger(L, 3), luaL_checkinteger(L, 4), luagl_get_gl_enum(L, 5), luagl_get_gl_enum(L, 6), luagl_checkuserdata(L, 7))); return 1; }
bool Texture::Create( const Texture::Properties& properties, const void* ptr) { Destroy(); SetProperties(properties); if(IsValid()) return true; GLuint handle = properties.Handle; if(handle < 1) glGenTextures(1, &handle); if(handle < 1) return false; m_Properties.Handle = handle; // glActiveTextureARB(m_Properties.TextureUnit); // Texture::CheckStatus(m_Properties.Name, "Activating texture unit"); glBindTexture(m_Properties.Target, handle); Texture::CheckStatus(m_Properties.Name, "Binding texture"); if(!m_Properties.Internal) m_Properties.Internal = GL_RGBA; if(!m_Properties.Format) m_Properties.Format = m_Properties.Internal; if(!m_Properties.DataType) m_Properties.DataType = GL_UNSIGNED_BYTE; if(m_Properties.Internal == GL_DEPTH_COMPONENT) { int iDepth = 8; glGetIntegerv(GL_DEPTH_BITS, &iDepth); if(iDepth == 16) m_Properties.Internal = GL_DEPTH_COMPONENT16; else if(iDepth == 24) m_Properties.Internal = GL_DEPTH_COMPONENT24; else if(iDepth == 32) m_Properties.Internal = GL_DEPTH_COMPONENT32; } if(m_Properties.Target == GL_TEXTURE_3D) { if(m_Properties.UseMipMaps && ptr) { if(!m_Properties.MinFilterMode) m_Properties.MinFilterMode = GL_LINEAR_MIPMAP_NEAREST; if(!m_Properties.MagFilterMode) m_Properties.MagFilterMode = GL_LINEAR_MIPMAP_LINEAR; gluBuild3DMipmaps(m_Properties.Target, m_Properties.Internal, m_Properties.Width, m_Properties.Height, m_Properties.Depth, m_Properties.Format, m_Properties.DataType, ptr ); } else { if(!m_Properties.MinFilterMode) m_Properties.MinFilterMode = GL_LINEAR; if(!m_Properties.MagFilterMode) m_Properties.MagFilterMode = GL_LINEAR; glTexImage3D(m_Properties.Target, 0, m_Properties.Internal, m_Properties.Width, m_Properties.Height, m_Properties.Depth, 0, m_Properties.Format, m_Properties.DataType, ptr); } } else if(m_Properties.Target == GL_TEXTURE_2D || GL_TEXTURE_RECTANGLE_ARB) { if(m_Properties.UseMipMaps && ptr) { if(!m_Properties.MinFilterMode) m_Properties.MinFilterMode = GL_LINEAR_MIPMAP_NEAREST; if(!m_Properties.MagFilterMode) m_Properties.MagFilterMode = GL_LINEAR_MIPMAP_LINEAR; gluBuild2DMipmaps(m_Properties.Target, m_Properties.Internal, m_Properties.Width, m_Properties.Height, m_Properties.Format, m_Properties.DataType, ptr ); } else { if(!m_Properties.MinFilterMode) m_Properties.MinFilterMode = GL_LINEAR; if(!m_Properties.MagFilterMode) m_Properties.MagFilterMode = GL_LINEAR; glTexImage2D(m_Properties.Target, 0, m_Properties.Internal, m_Properties.Width, m_Properties.Height, 0, m_Properties.Format, m_Properties.DataType, ptr); } } glTexParameteri(m_Properties.Target, GL_TEXTURE_MIN_FILTER, m_Properties.MinFilterMode); glTexParameteri(m_Properties.Target, GL_TEXTURE_MAG_FILTER, m_Properties.MagFilterMode); Texture::CheckStatus(m_Properties.Name, "Allocating texture"); glBindTexture(m_Properties.Target, 0); Texture::CheckStatus(m_Properties.Name, "Unbinding texture"); // glActiveTextureARB(GL_TEXTURE0); // Texture::CheckStatus(m_Properties.Name, "Disabling texture unit"); return true; }
void Texture::setup_texture_data( int dim, Image::ColorEncoding encoding_in, Memory::pointer base_mem_in, Texture::FilteringMode mode, int width, int height, int depth) { const int static_buffer_size = 1024*1024 ; static Memory::byte static_buffer[static_buffer_size * 4] ; Memory::pointer dynamic_buffer = nil ; Memory::pointer base_mem = base_mem_in ; Image::ColorEncoding encoding = encoding_in ; mipmap_ = (mode == Texture::MIPMAP) ; linear_filter_ = (mode != Texture::NO_FILTERING) ; if(height == 0) { height++ ; } if(depth == 0) { depth++ ; } int size = width * height * depth ; if(encode_indexed_to_rgb_ && (encoding == Image::GRAY || encoding == Image::INDEXED)) { if(dim == 3 && encoding == Image::GRAY) { } else { encoding = Image::RGBA ; if(size <= static_buffer_size) { base_mem = static_buffer ; } else { dynamic_buffer = new Memory::byte[size*4] ; base_mem = dynamic_buffer ; } indexed_to_rgb(size, base_mem_in, Image::RGBA, base_mem) ; } } switch(dim) { case 1: switch(encoding) { case Image::GRAY : case Image::INDEXED : gluBuild1DMipmaps( GL_TEXTURE_1D, GL_RGBA, width, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, base_mem ) ; break ; case Image::RGB : gluBuild1DMipmaps( GL_TEXTURE_1D, GL_RGB, width, GL_RGB, GL_UNSIGNED_BYTE, base_mem ) ; break ; case Image::RGBA : gluBuild1DMipmaps( GL_TEXTURE_1D, GL_RGBA, width, GL_RGBA, GL_UNSIGNED_BYTE, base_mem ) ; break ; case Image::FLOAT32 : { Memory::pointer colormapped_texture_values = new Memory::byte[4 * width]; float32_to_rgb(size, base_mem_in, Image::RGBA, colormapped_texture_values); gluBuild1DMipmaps( GL_TEXTURE_1D, GL_RGBA, width, GL_RGBA, GL_UNSIGNED_BYTE, colormapped_texture_values ) ; delete[] colormapped_texture_values; break ; } default: { bool implemented = false ; ogf_assert(implemented) ; } } break ; case 2: switch(encoding) { case Image::GRAY : case Image::INDEXED : if(mipmap_) { gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, width, height, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, base_mem ) ; } else { glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, base_mem ) ; } break ; case Image::RGB : if(mipmap_) { gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, base_mem ) ; } else { glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, base_mem ) ; } break ; case Image::RGBA : if(mipmap_) { gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, base_mem ) ; } else { glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, base_mem ) ; } break ; case Image::RGB_FLOAT32: glTexImage2D( GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGB32_NV, width, height, 0, GL_RGB, GL_FLOAT, base_mem ) ; break ; case Image::RGBA_FLOAT32: glTexImage2D( GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, width, height, 0, GL_RGBA, GL_FLOAT, base_mem ) ; break ; case Image::FLOAT32 : { Memory::pointer colormapped_texture_values = new Memory::byte[4 * width * height]; float32_to_rgb(size, base_mem_in, Image::RGBA, colormapped_texture_values); if(mipmap_) { gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, colormapped_texture_values ) ; } else { glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, colormapped_texture_values ) ; } delete[] colormapped_texture_values; break ; } default: { bool implemented = false ; ogf_assert(implemented) ; } break ; } break ; case 3: // Note: gluBuild3DMipmaps does not seem to exist under Windows #ifdef WIN32 mipmap_ = false ; #endif switch(encoding) { case Image::GRAY : if(mipmap_) { #ifndef WIN32 gluBuild3DMipmaps( GL_TEXTURE_3D, GL_LUMINANCE, width, height, depth, GL_LUMINANCE, GL_UNSIGNED_BYTE, base_mem ) ; #endif } else { glTexImage3DEXT( GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height, depth, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, base_mem ) ; } break ; case Image::INDEXED : if(mipmap_) { #ifndef WIN32 gluBuild3DMipmaps( GL_TEXTURE_3D, GL_RGBA, width, height, depth, GL_RGB, GL_UNSIGNED_BYTE, base_mem ) ; #endif } else { glTexImage3DEXT( GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_UNSIGNED_BYTE, base_mem ) ; } break ; case Image::RGB : if(mipmap_) { #ifndef WIN32 gluBuild3DMipmaps( GL_TEXTURE_3D, GL_RGB, width, height, depth, GL_RGB, GL_UNSIGNED_BYTE, base_mem ) ; #endif } else { glTexImage3DEXT( GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_UNSIGNED_BYTE, base_mem ) ; } break ; case Image::RGBA : if(mipmap_) { #ifndef WIN32 gluBuild3DMipmaps( GL_TEXTURE_3D, GL_RGBA, width, height, depth, GL_RGBA, GL_UNSIGNED_BYTE, base_mem ) ; #endif } else { glTexImage3DEXT( GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, base_mem ) ; } break ; case Image::FLOAT32 : { Memory::pointer colormapped_texture_values = new Memory::byte[4 * width * height * depth]; float32_to_rgb(size, base_mem_in, Image::RGBA, colormapped_texture_values); if(mipmap_) { #ifndef WIN32 gluBuild3DMipmaps( GL_TEXTURE_3D, GL_RGBA, width, height, depth, GL_RGBA, GL_UNSIGNED_BYTE, colormapped_texture_values ) ; #endif } else { glTexImage3DEXT( GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, colormapped_texture_values ) ; } delete[] colormapped_texture_values; break ; } default: { bool implemented = false ; ogf_assert(implemented) ; } } break ; } delete[] dynamic_buffer ; }
GLuint volume_create_texture(Image const * volume, int const gradients) { Image_Format format = volume->format; Image const * palette = volume->palette; GLenum const type = format.type; GLenum internal_format, external_format; unsigned char * data; if (gradients && type == GL_UNSIGNED_BYTE) { data = create_gradients(volume); external_format = GL_RGBA; internal_format = GL_RGBA; } else if (palette) { download_palette(palette); data = (unsigned char *) volume->pixels; external_format = GL_COLOR_INDEX; internal_format = palette->format.format; /* GL_RGBA, GL_LUMINANCE_ALPHA, or GL_INTENSITY */ } else { data = (unsigned char *) volume->pixels; external_format = GL_LUMINANCE; internal_format = GL_INTENSITY; } GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_3D, texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); #ifdef MIPMAPS gluBuild3DMipmaps(GL_TEXTURE_3D, internal_format, format.size.x, format.size.y, format.size.z, external_format, type, data); #else glTexImage3D(GL_TEXTURE_3D, 0, internal_format, format.size.x, format.size.y, format.size.z, 0, external_format, type, data); #endif GLenum const error = glGetError(); error_check(error, "failed to download texture"); glPixelTransferi(GL_MAP_COLOR, GL_FALSE); // static GLint const texture_wrap = GL_CLAMP_TO_EDGE; // static GLint const texture_wrap = GL_MIRRORED_REPEAT; static GLint const texture_wrap = GL_REPEAT; glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, texture_wrap); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, texture_wrap); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, texture_wrap); return texture; }