src/texture.cpp in rays-0.1.28 vs src/texture.cpp in rays-0.1.29
- old
+ new
@@ -80,42 +80,10 @@
int n = 1;
while (n < num) n *= 2;
return n;
}
- static void
- setup_texture (Texture::Data* self, const void* pixels = NULL)
- {
- assert(self && !self->has_id());
-
- if (self->context)
- invalid_state_error(__FILE__, __LINE__);
-
- self->context = OpenGL_get_context();
- if (!self->context)
- opengl_error(__FILE__, __LINE__);
-
- glGenTextures(1, &self->id);
- glBindTexture(GL_TEXTURE_2D, self->id);
- if (glIsTexture(self->id) == GL_FALSE)
- opengl_error(__FILE__, __LINE__, "failed to create texture.");
-
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);
-
- GLenum format, type;
- ColorSpace_get_gl_format_and_type(&format, &type, self->color_space);
-
- glTexImage2D(
- GL_TEXTURE_2D, 0, format, self->width_pow2, self->height_pow2, 0,
- format, type, pixels);
-
- OpenGL_check_error(__FILE__, __LINE__);
- }
-
template <int BytesPerPixel>
static inline void
copy_pixel (uchar* dest, const uchar* src)
{
*dest = *src;
@@ -177,11 +145,13 @@
}
static void
copy_bitmap (Bitmap* dest, const Bitmap& src)
{
- if (!dest || !src)
+ assert(dest);
+
+ if (!src)
argument_error(__FILE__, __LINE__);
int width = std::min(src.width(), dest->width());
int height = std::min(src.height(), dest->height());
int src_Bpp = src.color_space().Bpp();
@@ -194,55 +164,95 @@
dest->at<uchar>(0, y), dest_Bpp,
src. at<uchar>(0, y), src_Bpp);
}
}
-
- Texture::Texture ()
+ static std::shared_ptr<Bitmap>
+ resize_bitmap (const Bitmap& bitmap, int width, int height)
{
+ std::shared_ptr<Bitmap> bmp(
+ new Bitmap(width, height, bitmap.color_space()));
+ if (!*bmp)
+ rays_error(__FILE__, __LINE__);
+
+ copy_bitmap(bmp.get(), bitmap);
+ return bmp;
}
- Texture::Texture (int width, int height, const ColorSpace& cs)
+ static void
+ setup_texture (
+ Texture::Data* self, int width, int height, const ColorSpace& cs,
+ const Bitmap* bitmap = NULL)
{
- if (width <= 0 || height <= 0 || !cs)
- argument_error(__FILE__, __LINE__);
+ assert(self && !self->has_id());
+ if (self->context)
+ invalid_state_error(__FILE__, __LINE__);
+
+ self->context = OpenGL_get_context();
+ if (!self->context)
+ opengl_error(__FILE__, __LINE__);
+
+ glGenTextures(1, &self->id);
+ glBindTexture(GL_TEXTURE_2D, self->id);
+ if (glIsTexture(self->id) == GL_FALSE)
+ opengl_error(__FILE__, __LINE__, "failed to create texture.");
+
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);
+
+ GLenum format, type;
+ ColorSpace_get_gl_format_and_type(&format, &type, cs);
+
+ // create non-power-of-two texture
+ glTexImage2D(
+ GL_TEXTURE_2D, 0, format, width, height, 0, format, type,
+ bitmap ? bitmap->pixels() : NULL);
+ if (OpenGL_has_error())
+ {
+ // create power-of-two texture
+ int width_pow2 = min_pow2(width);
+ int height_pow2 = min_pow2(height);
+
+ glTexImage2D(
+ GL_TEXTURE_2D, 0, format, width_pow2, height_pow2, 0, format, type,
+ bitmap ? resize_bitmap(*bitmap, width_pow2, height_pow2)->pixels() : NULL);
+ OpenGL_check_error(__FILE__, __LINE__);
+
+ self->width_pow2 = width_pow2;
+ self->height_pow2 = height_pow2;
+ }
+
self->width = width;
self->height = height;
- self->width_pow2 = min_pow2(width);
- self->height_pow2 = min_pow2(height);
self->color_space = cs;
self->modified = true;
+ }
- setup_texture(self.get());
+
+ Texture::Texture ()
+ {
}
+ Texture::Texture (int width, int height, const ColorSpace& cs)
+ {
+ if (width <= 0 || height <= 0 || !cs)
+ argument_error(__FILE__, __LINE__);
+
+ setup_texture(self.get(), width, height, cs);
+ }
+
Texture::Texture (const Bitmap& bitmap)
{
if (!bitmap)
argument_error(__FILE__, __LINE__);
- self->width = bitmap.width();
- self->height = bitmap.height();
- self->width_pow2 = min_pow2(self->width);
- self->height_pow2 = min_pow2(self->height);
- self->color_space = bitmap.color_space();
- self->modified = true;
-
- Bitmap bmp = bitmap;
- if (
- self->width_pow2 != self->width ||
- self->height_pow2 != self->height)
- {
- bmp = Bitmap(self->width_pow2, self->height_pow2, self->color_space);
- if (!bmp)
- rays_error(__FILE__, __LINE__);
-
- copy_bitmap(&bmp, bitmap);
- }
-
- setup_texture(self.get(), bmp.pixels());
+ setup_texture(
+ self.get(), bitmap.width(), bitmap.height(), bitmap.color_space(),
+ &bitmap);
}
Texture::~Texture ()
{
}
@@ -254,11 +264,12 @@
}
int
Texture::reserved_width () const
{
- return self->width_pow2;
+ int w = self->width_pow2;
+ return w > 0 ? w : self->width;
}
int
Texture::height () const
{
@@ -266,10 +277,11 @@
}
int
Texture::reserved_height () const
{
- return self->height_pow2;
+ int h = self->height_pow2;
+ return h > 0 ? h : self->height;
}
const ColorSpace&
Texture::color_space () const
{