Welcome to mirror list, hosted at ThFree Co, Russian Federation.

texture.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 84a37232d929ca50c4a670cea4f185ac0e56f703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "drape/texture.hpp"

#include "drape/glfunctions.hpp"
#include "drape/glextensions_list.hpp"

#include "base/math.hpp"

#define ASSERT_ID ASSERT(GetID() != -1, ())

namespace dp
{

Texture::ResourceInfo::ResourceInfo(m2::RectF const & texRect)
  : m_texRect(texRect) {}

m2::RectF const & Texture::ResourceInfo::GetTexRect() const
{
  return m_texRect;
}

//////////////////////////////////////////////////////////////////

Texture::Texture()
  : m_textureID(-1)
  , m_width(0)
  , m_height(0)
  , m_format(dp::UNSPECIFIED)
{
}

Texture::~Texture()
{
  if (m_textureID != -1)
    GLFunctions::glDeleteTexture(m_textureID);
}

void Texture::Create(uint32_t width, uint32_t height, TextureFormat format)
{
  Create(width, height, format, MakeStackRefPointer<void>(NULL));
}

void Texture::Create(uint32_t width, uint32_t height, TextureFormat format, RefPointer<void> data)
{
  m_format = format;
  m_width = width;
  m_height = height;
  if (!GLExtensionsList::Instance().IsSupported(GLExtensionsList::TextureNPOT))
  {
    m_width = my::NextPowOf2(width);
    m_height = my::NextPowOf2(height);
  }

  m_textureID = GLFunctions::glGenTexture();
  GLFunctions::glBindTexture(m_textureID);

  glConst layout;
  glConst pixelType;
  UnpackFormat(format, layout, pixelType);

  GLFunctions::glTexImage2D(m_width, m_height, layout, pixelType, data.GetRaw());
  SetFilterParams(gl_const::GLLinear, gl_const::GLLinear);
  SetWrapMode(gl_const::GLClampToEdge, gl_const::GLClampToEdge);
}

void Texture::SetFilterParams(glConst minFilter, glConst magFilter)
{
  ASSERT_ID;
  GLFunctions::glTexParameter(gl_const::GLMinFilter, minFilter);
  GLFunctions::glTexParameter(gl_const::GLMagFilter, magFilter);
}

void Texture::SetWrapMode(glConst sMode, glConst tMode)
{
  ASSERT_ID;
  GLFunctions::glTexParameter(gl_const::GLWrapS, sMode);
  GLFunctions::glTexParameter(gl_const::GLWrapT, tMode);
}

void Texture::UploadData(uint32_t x, uint32_t y, uint32_t width, uint32_t height,
                         TextureFormat format, RefPointer<void> data)
{
  ASSERT_ID;
  ASSERT(format == m_format, ());
  glConst layout;
  glConst pixelType;

  UnpackFormat(format, layout, pixelType);

  GLFunctions::glTexSubImage2D(x, y, width, height, layout, pixelType, data.GetRaw());
}

TextureFormat Texture::GetFormat() const
{
  return m_format;
}

uint32_t Texture::GetWidth() const
{
  ASSERT_ID;
  return m_width;
}

uint32_t Texture::GetHeight() const
{
  ASSERT_ID;
  return m_height;
}

float Texture::GetS(uint32_t x) const
{
  ASSERT_ID;
  return x / (float)m_width;
}

float Texture::GetT(uint32_t y) const
{
  ASSERT_ID;
  return y / (float)m_height;
}

void Texture::Bind() const
{
  ASSERT_ID;
  GLFunctions::glBindTexture(GetID());
}

uint32_t Texture::GetMaxTextureSize()
{
  return GLFunctions::glGetInteger(gl_const::GLMaxTextureSize);
}

void Texture::UnpackFormat(TextureFormat format, glConst & layout, glConst & pixelType)
{
  bool requiredFormat = GLExtensionsList::Instance().IsSupported(GLExtensionsList::RequiredInternalFormat);
  switch (format) {
  case RGBA8:
    layout = requiredFormat ? gl_const::GLRGBA8 : gl_const::GLRGBA;
    pixelType = gl_const::GL8BitOnChannel;
    break;
  case RGBA4:
    layout = requiredFormat ? gl_const::GLRGBA4 : gl_const::GLRGBA;
    pixelType = gl_const::GL4BitOnChannel;
    break;
  case ALPHA:
    layout = requiredFormat ? gl_const::GLAlpha8 : gl_const::GLAlpha;
    pixelType = gl_const::GL8BitOnChannel;
    break;
  default:
    ASSERT(false, ());
    break;
  }
}

int32_t Texture::GetID() const
{
  return m_textureID;
}

} // namespace dp