gliv-1.1.3
[gliv.git] / one_texture.c
blob8a5469330d894f64b256cef785997ec579145961
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * See the COPYING file for license information.
18 * Guillaume Chazarain <booh@altern.org>
21 /*****************************
22 * Texture for small images. *
23 *****************************/
25 #include <math.h>
26 #include "gliv.h"
28 /* Draws the unique rectangle. */
29 static void list_one(gliv_image * im)
31 GLfloat x0, y0;
32 GLfloat xi, yi;
33 GLfloat xmax, ymax;
35 im->list = glGenLists(1);
36 glNewList(im->list, GL_COMPILE);
38 /* (x0;y0) : origin of the image. */
39 x0 = -(im->width / 2);
40 y0 = -(im->height / 2);
42 /* Size of the displayed image. */
43 xi = im->width;
44 yi = im->height;
46 /* Size of the interesting part of the texture. */
47 xmax = (GLfloat) (im->width - .5) / im->gl_width;
48 ymax = (GLfloat) (im->height - .5) / im->gl_height;
50 glBindTexture(GL_TEXTURE_2D, im->textures[0]);
52 /* Draw the rectangle. */
53 glBegin(GL_QUADS);
55 glTexCoord2i(0, 0);
56 glVertex2f(x0, y0);
58 glTexCoord2f(xmax, 0.0);
59 glVertex2f(x0 + xi, y0);
61 glTexCoord2f(xmax, ymax);
62 glVertex2f(x0 + xi, y0 + yi);
64 glTexCoord2f(0.0, ymax);
65 glVertex2f(x0, y0 + yi);
67 glEnd();
68 glEndList();
72 * Returns the lowest power of two bigger than the argument.
73 * We can't avoid linking with -lm so we use it.
75 static guint p2(guint p)
77 guint exponent;
79 exponent = ceil(log((float) p) / M_LN2);
81 return (guint) pow(2.0, (float) exponent);
84 /* Creates one texture with powers of two as width and height. */
85 void make_one_texture(gliv_image * im)
87 DATA32 *ptr;
89 im->gl_width = p2(im->width);
90 im->gl_height = p2(im->height);
92 im->nb_width = im->nb_height = 1;
94 make_texture_image(im);
96 imlib_context_set_image(im->gl_im);
97 ptr = imlib_image_get_data_for_reading_only();
99 glBindTexture(GL_TEXTURE_2D, im->textures[0]);
100 texture_parameter();
101 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, im->gl_width, im->gl_height,
102 0, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
104 list_one(im);