summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Graunke <kenneth@whitecape.org>2010-10-27 20:34:46 -0700
committerKenneth Graunke <kenneth@whitecape.org>2010-10-27 20:34:46 -0700
commit0051ef626f3f44d0afe48ca1ae3fc554d8401b85 (patch)
treeb2c9235e40a3c3f6455d2b1f09a436a9fee77aea
parent3ba5993596bb196073379d3070f49ea97128854b (diff)
Add support code for loading textures from disk.
Signed-off-by: Ian Romanick <idr@freedesktop.org>
-rwxr-xr-xconfigure.ac4
-rwxr-xr-xsrc/Makefile.am2
-rw-r--r--src/texture_load.cpp253
-rw-r--r--src/texture_load.h37
4 files changed, 293 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac
index 4e7de98..b223b10 100755
--- a/configure.ac
+++ b/configure.ac
@@ -30,8 +30,8 @@ AC_HEADER_STDC
# SDL is required for this application. Find its header files and
# libraries. Make sure they exist.
-CPPFLAGS="$CPPFLAGS `sdl-config --cflags`"
-LIBS="$LIBS `sdl-config --libs`"
+CPPFLAGS="$CPPFLAGS `pkg-config --cflags sdl SDL_image`"
+LIBS="$LIBS `pkg-config --libs sdl SDL_image`"
AC_CHECK_HEADER(SDL.h, [], [exit 1])
diff --git a/src/Makefile.am b/src/Makefile.am
index c363b8d..9905146 100755
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -21,7 +21,7 @@
# USE OR OTHER DEALINGS IN THE SOFTWARE.
bin_PROGRAMS = camera_demo
-camera_demo_SOURCES = main.cpp bezier.cpp model_consumer.cpp
+camera_demo_SOURCES = main.cpp bezier.cpp model_consumer.cpp texture_load.cpp
AM_CXXFLAGS= $(GLU3_CFLAGS)
camera_demo_LDADD = $(GLU3_LIBS)
diff --git a/src/texture_load.cpp b/src/texture_load.cpp
new file mode 100644
index 0000000..e802020
--- /dev/null
+++ b/src/texture_load.cpp
@@ -0,0 +1,253 @@
+/*
+ * Copyright © 2007, 2008 Ian D. Romanick
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef _WIN32
+# define WIN32_LEAN_AND_MEAN
+# define _USE_MATH_DEFINES
+# include <windows.h>
+#endif
+
+#include <GL/glew.h>
+
+#include "texture_load.h"
+
+
+/**
+ * Calculate the ceil of the log-base-2
+ */
+static unsigned
+ceil_log2(unsigned v)
+{
+ unsigned power = 0;
+
+ if (v == 0) {
+ return 0;
+ }
+
+ while ((v >> (power + 1)) != 0) {
+ power++;
+ }
+
+ if ((1U << power) < v) {
+ power++;
+ }
+
+ return power;
+}
+
+
+struct known_color_format {
+ uint8_t shift;
+ uint8_t bits;
+};
+
+
+struct known_pixel_format {
+ unsigned bits_per_pixel;
+ struct known_color_format red;
+ struct known_color_format green;
+ struct known_color_format blue;
+ struct known_color_format alpha;
+
+ GLenum format;
+ GLenum type;
+};
+
+
+static const struct known_pixel_format format_table[] = {
+ /* bpp red green blue alpha */
+ { 32, {0, 8}, {8, 8}, {16, 8}, {24, 8}, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8 },
+ { 32, {24, 8}, {16, 8}, {8, 8}, {0, 8}, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV },
+ { 32, {16, 8}, {8, 8}, {0, 8}, {24, 8}, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8 },
+ { 32, {8, 8}, {16, 8}, {24, 8}, {0, 8}, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV },
+
+ { 24, {0, 8}, {8, 8}, {16, 8}, {0, 0}, GL_RGB, GL_UNSIGNED_BYTE },
+ { 24, {16, 8}, {8, 8}, {0, 8}, {0, 0}, GL_BGR, GL_UNSIGNED_BYTE },
+
+ { 16, {0, 5}, {5, 6}, {11, 5}, {0, 0}, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 },
+ { 16, {11, 5}, {5, 6}, {0, 5}, {0, 0}, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV },
+
+ { 16, {0, 5}, {5, 5}, {10, 5}, {15, 1}, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 },
+ { 16, {11, 5}, {6, 5}, {1, 5}, {0, 1}, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV },
+ { 16, {10, 5}, {5, 5}, {0, 5}, {15, 1}, GL_BGRA, GL_UNSIGNED_SHORT_5_5_5_1 },
+ { 16, {1, 5}, {6, 5}, {11, 5}, {0, 1}, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV },
+
+ { 16, {0, 4}, {4, 4}, {8, 4}, {12, 4}, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 },
+ { 16, {12, 4}, {8, 4}, {4, 4}, {0, 4}, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV },
+ { 16, {8, 4}, {4, 4}, {0, 4}, {12, 4}, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4 },
+ { 16, {4, 4}, {8, 4}, {12, 4}, {0, 4}, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV },
+
+ { 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 0, 0 },
+};
+
+
+bool
+texture_image(unsigned target, unsigned level,
+ unsigned internal_format, SDL_Surface *img,
+ bool require_power_of_two)
+{
+ SDL_Surface *img_copy = img;
+
+ /* There are a few common surface formats that can easily be handled
+ * natively. Determine whether or not the loaded image matches one
+ * of those formats. If not, convert it to one of them.
+ *
+ * If the image is not a power of two and require_power_of_two is set,
+ * the match test automatically fails. In addition, we have to use
+ * the more expensive SDL_BlitSurface function to do the format
+ * conversion and resize.
+ */
+ const unsigned rounded_up_width = 1U << ceil_log2(img->w);
+ const unsigned rounded_up_height = 1U << ceil_log2(img->h);
+
+ if (require_power_of_two
+ && ((rounded_up_width != (unsigned)img->w)
+ || (rounded_up_height != (unsigned)img->h))) {
+ const Uint32 alpha_mask = (img->format->Amask != 0)
+ ? 0xff000000 : 0;
+ const int bpp = (img->format->Amask != 0) ? 32 : 24;
+ SDL_Rect src_rect = {
+ 0, 0, img->w, img->h
+ };
+ SDL_Rect dest_rect = {
+ 0, 0, rounded_up_width, rounded_up_height
+ };
+
+ SDL_Surface *img_copy = SDL_CreateRGBSurface(SDL_SWSURFACE,
+ rounded_up_width,
+ rounded_up_height,
+ bpp,
+ 0x000000ff,
+ 0x0000ff00,
+ 0x00ff0000,
+ alpha_mask);
+ if (img_copy == NULL) {
+ return false;
+ }
+
+ SDL_BlitSurface(img, &src_rect, img_copy, &dest_rect);
+ }
+
+
+ GLenum format = 0;
+ GLenum type = 0;
+ const SDL_PixelFormat *const fmt = img_copy->format;
+ if (fmt->BitsPerPixel > 8) {
+ const unsigned red_bits =
+ ceil_log2((fmt->Rmask >> fmt->Rshift) + 1);
+ const unsigned green_bits =
+ ceil_log2((fmt->Gmask >> fmt->Gshift) + 1);
+ const unsigned blue_bits =
+ ceil_log2((fmt->Bmask >> fmt->Bshift) + 1);
+ const unsigned alpha_bits =
+ ceil_log2((fmt->Amask >> fmt->Ashift) + 1);
+
+ for (unsigned i = 0; format_table[i].bits_per_pixel != 0; i++) {
+ if ((format_table[i].bits_per_pixel == fmt->BitsPerPixel)
+ && (format_table[i].red.shift == fmt->Rshift)
+ && (format_table[i].green.shift == fmt->Gshift)
+ && (format_table[i].blue.shift == fmt->Bshift)
+ && (format_table[i].alpha.shift == fmt->Ashift)
+ && (format_table[i].red.bits == red_bits)
+ && (format_table[i].green.bits == green_bits)
+ && (format_table[i].blue.bits == blue_bits)
+ && (format_table[i].alpha.bits == alpha_bits)) {
+ format = format_table[i].format;
+ type = format_table[i].type;
+ }
+ }
+ }
+
+ if (format == 0) {
+ const Uint32 alpha_mask = (fmt->Amask != 0)
+ ? 0xff000000 : 0;
+ const Uint32 alpha_shift = (fmt->Amask != 0) ? 24 : 0;
+ const Uint8 bpp = (fmt->Amask != 0) ? 4 : 3;
+
+ SDL_PixelFormat prefered_format = {
+ NULL,
+ 8 * bpp,
+ bpp,
+ 0, 0, 0, 0,
+ 0, 8, 16, alpha_shift,
+ 0x000000ff, 0x0000ff00, 0x00ff0000, alpha_mask,
+ 0, 0
+ };
+
+ img_copy = SDL_ConvertSurface(img, & prefered_format,
+ SDL_SWSURFACE);
+ if (img_copy == NULL) {
+ return false;
+ }
+ }
+
+
+
+ /* Determine the length of each scanline in pixels.
+ */
+ const unsigned row_length = img_copy->pitch
+ / img_copy->format->BytesPerPixel;
+ const unsigned alignment = (img_copy->pitch + 1)
+ - (row_length * img_copy->format->BytesPerPixel);
+
+ GLint old_row_length;
+ GLint old_alignment;
+
+ glGetIntegerv(GL_UNPACK_ROW_LENGTH, &old_row_length);
+ glGetIntegerv(GL_UNPACK_ALIGNMENT, & old_alignment);
+
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
+
+ glTexImage2D(target, level, internal_format,
+ img_copy->w, img_copy->h, 0, format, GL_UNSIGNED_BYTE,
+ img_copy->pixels);
+
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, old_row_length);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, old_alignment);
+
+ if (img_copy != img) {
+ SDL_FreeSurface(img);
+ }
+
+ return true;
+}
+
+
+bool
+texture_image(unsigned target, unsigned level,
+ unsigned internal_format, const char *filename,
+ bool require_power_of_two)
+{
+ SDL_Surface *img = IMG_Load(filename);
+
+ if (img == NULL) {
+ return false;
+ }
+
+ const bool ok = texture_image(target, level, internal_format, img,
+ require_power_of_two);
+
+ SDL_FreeSurface(img);
+ return ok;
+}
diff --git a/src/texture_load.h b/src/texture_load.h
new file mode 100644
index 0000000..9539746
--- /dev/null
+++ b/src/texture_load.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright © 2007, 2008 Ian D. Romanick
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef TEXTURE_LOAD_H
+#define TEXTURE_LOAD_H
+
+#include <SDL_image.h>
+
+bool texture_image(unsigned target, unsigned level,
+ unsigned internal_format, SDL_Surface *img,
+ bool require_power_of_two);
+
+bool texture_image(unsigned target, unsigned level,
+ unsigned internal_format, const char *filename,
+ bool require_power_of_two);
+
+#endif