[43] | 1 | // Copyright (C) 2012-2013 Kornel Kisielewicz
|
---|
| 2 | // This file is part of NV Libraries.
|
---|
| 3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 4 |
|
---|
| 5 | #include "nv/gl/gl_texture2d.hh"
|
---|
| 6 |
|
---|
| 7 | #include "nv/gl/gl_enum.hh"
|
---|
| 8 | #include "nv/lib/gl.hh"
|
---|
| 9 |
|
---|
| 10 | using namespace nv;
|
---|
| 11 |
|
---|
[70] | 12 | nv::gl_texture2d::gl_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
|
---|
[49] | 13 | : texture2d( size, aformat, adatatype, asampler ), m_name()
|
---|
[43] | 14 | {
|
---|
| 15 | glBindTexture( GL_TEXTURE_2D, m_name.get_value() );
|
---|
| 16 |
|
---|
[160] | 17 | // Detect if mipmapping was requested
|
---|
| 18 | if (( asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST ) ||
|
---|
| 19 | ( asampler.filter_max != sampler::LINEAR && asampler.filter_max != sampler::NEAREST ))
|
---|
| 20 | {
|
---|
| 21 | glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
[121] | 24 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)nv::sampler_filter_to_enum( m_sampler.filter_min ) );
|
---|
| 25 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)nv::sampler_filter_to_enum( m_sampler.filter_max ) );
|
---|
| 26 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (int)nv::sampler_wrap_to_enum( m_sampler.wrap_s) );
|
---|
| 27 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (int)nv::sampler_wrap_to_enum( m_sampler.wrap_t) );
|
---|
[43] | 28 |
|
---|
| 29 | glBindTexture( GL_TEXTURE_2D, 0 );
|
---|
| 30 |
|
---|
| 31 | if (data)
|
---|
| 32 | {
|
---|
| 33 | assign(data);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | void nv::gl_texture2d::assign( void* data )
|
---|
| 38 | {
|
---|
| 39 | glBindTexture( GL_TEXTURE_2D, m_name.get_value() );
|
---|
[121] | 40 | glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(m_format), m_size.x, m_size.y, 0, nv::image_format_to_enum(m_format), nv::datatype_to_gl_enum(m_datatype), data );
|
---|
[43] | 41 | glBindTexture( GL_TEXTURE_2D, 0 );
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[121] | 44 | void nv::gl_texture2d::bind( size_t slot )
|
---|
[43] | 45 | {
|
---|
| 46 | glActiveTexture( GL_TEXTURE0 + slot );
|
---|
| 47 | glBindTexture( GL_TEXTURE_2D, m_name.get_value() );
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | void nv::gl_texture2d::unbind()
|
---|
| 51 | {
|
---|
| 52 | glBindTexture( GL_TEXTURE_2D, 0 );
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | bool nv::gl_texture2d::is_valid() const
|
---|
| 56 | {
|
---|
| 57 | return m_name.is_valid();
|
---|
| 58 | }
|
---|