Sitelet https://github.com/google/fplbase/commit/335a31a8988e83862b51e06f8a4b6df7f205f7d8
Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit 335a31a

Browse files
author
Wouter van Oortmerssen
committed
Added support for turning mipmaps off.
Also allowing luminance textures to be updated (needed in font renderer). Change-Id: Icd07fcd42f169d66b0c62c134edadc8248add79b Tested: on Linux.
1 parent 50cdba3 commit 335a31a

4 files changed

Lines changed: 38 additions & 11 deletions

File tree

‎include/fplbase/material.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Texture : public AsyncResource {
5858
size_(mathfu::kZeros2i),
5959
uv_(mathfu::vec4(0.0f, 0.0f, 1.0f, 1.0f)),
6060
has_alpha_(false),
61+
mipmaps_(true),
6162
desired_(kFormatAuto) {}
6263
Texture(Renderer &renderer)
6364
: AsyncResource(""),
@@ -66,12 +67,14 @@ class Texture : public AsyncResource {
6667
size_(mathfu::kZeros2i),
6768
uv_(mathfu::vec4(0.0f, 0.0f, 1.0f, 1.0f)),
6869
has_alpha_(false),
70+
mipmaps_(true),
6971
desired_(kFormatAuto) {}
7072
~Texture() { Delete(); }
7173

7274
virtual void Load();
73-
virtual void LoadFromMemory(const uint8_t *data, const mathfu::vec2i size,
74-
const TextureFormat format, const bool has_alpha);
75+
virtual void LoadFromMemory(const uint8_t *data, const mathfu::vec2i &size,
76+
TextureFormat format, bool has_alpha,
77+
bool mipmaps);
7578
virtual void Finalize();
7679

7780
void Set(size_t unit);
@@ -96,6 +99,7 @@ class Texture : public AsyncResource {
9699
mathfu::vec2i size_;
97100
mathfu::vec4 uv_;
98101
bool has_alpha_;
102+
bool mipmaps_;
99103
TextureFormat desired_;
100104
};
101105

‎include/fplbase/renderer.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,14 @@ class Renderer {
7979
// Create a texture from a memory buffer containing xsize * ysize RGBA pixels.
8080
// Return 0 if not a power of two in size.
8181
TextureHandle CreateTexture(const uint8_t *buffer, const vec2i &size,
82-
bool has_alpha,
82+
bool has_alpha, bool mipmaps = true,
8383
TextureFormat desired = kFormatAuto);
8484

85+
// Update (part of) the current texture with new pixel data.
86+
// For now, must always update at least entire rows.
87+
void UpdateTexture(TextureFormat format, int xoffset, int yoffset, int width,
88+
int height, const void *data);
89+
8590
// Unpacks a memory buffer containing a TGA format file.
8691
// May only be uncompressed RGB or RGBA data, Y-flipped or not.
8792
// Returns RGBA array of returned dimensions or nullptr if the

‎src/material.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ void Texture::Load() {
3737
}
3838
}
3939

40-
void Texture::LoadFromMemory(const uint8_t *data, const vec2i size,
41-
const TextureFormat format, const bool has_alpha) {
40+
void Texture::LoadFromMemory(const uint8_t *data, const vec2i &size,
41+
TextureFormat format, bool has_alpha,
42+
bool mipmaps) {
4243
size_ = size;
4344
has_alpha_ = has_alpha;
4445
desired_ = format;
45-
id_ = renderer_->CreateTexture(data, size_, has_alpha_, desired_);
46+
mipmaps_ = mipmaps;
47+
id_ = renderer_->CreateTexture(data, size_, has_alpha_, mipmaps_, desired_);
4648
}
4749

4850
void Texture::Finalize() {
4951
if (data_) {
50-
id_ = renderer_->CreateTexture(data_, size_, has_alpha_, desired_);
52+
id_ = renderer_->CreateTexture(data_, size_, has_alpha_, mipmaps_, desired_);
5153
free(data_);
5254
data_ = nullptr;
5355
}

‎src/renderer.cpp‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ uint16_t *Renderer::Convert888To565(const uint8_t *buffer, const vec2i &size) {
285285
}
286286

287287
GLuint Renderer::CreateTexture(const uint8_t *buffer, const vec2i &size,
288-
bool has_alpha, TextureFormat desired) {
288+
bool has_alpha, bool mipmaps,
289+
TextureFormat desired) {
289290
int area = size.x() * size.y();
290291
if (area & (area - 1)) {
291292
LogError(kError,
@@ -303,7 +304,7 @@ GLuint Renderer::CreateTexture(const uint8_t *buffer, const vec2i &size,
303304
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
304305
GL_CALL(
305306
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
306-
GL_LINEAR_MIPMAP_NEAREST /*GL_LINEAR_MIPMAP_LINEAR*/));
307+
mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
307308
if (desired == kFormatAuto) desired = has_alpha ? kFormat5551 : kFormat565;
308309
switch (desired) {
309310
case kFormat5551: {
@@ -353,12 +354,27 @@ GLuint Renderer::CreateTexture(const uint8_t *buffer, const vec2i &size,
353354
break;
354355
}
355356
default:
356-
assert(0);
357+
assert(false);
357358
}
358-
GL_CALL(glGenerateMipmap(GL_TEXTURE_2D));
359+
if (mipmaps) GL_CALL(glGenerateMipmap(GL_TEXTURE_2D));
359360
return texture_id;
360361
}
361362

363+
void Renderer::UpdateTexture(TextureFormat format, int xoffset, int yoffset,
364+
int width, int height, const void *data) {
365+
// In OpenGL ES2.0, width and pitch of the src buffer needs to match. So
366+
// that we are updating entire row at once.
367+
// TODO: Optimize glTexSubImage2D call in ES3.0 capable platform.
368+
switch (format) {
369+
case kFormatLuminance:
370+
GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, width,
371+
height, GL_LUMINANCE, GL_UNSIGNED_BYTE, data));
372+
break;
373+
default:
374+
assert(false); // TODO: not implemented.
375+
}
376+
}
377+
362378
uint8_t *Renderer::UnpackTGA(const void *tga_buf, vec2i *dimensions,
363379
bool *has_alpha) {
364380
struct TGA {

0 commit comments

Comments
 (0)