1 | /*
|
---|
2 | glh - is a platform-indepenedent C++ OpenGL helper library
|
---|
3 |
|
---|
4 |
|
---|
5 | Copyright (c) 2000 Cass Everitt
|
---|
6 | Copyright (c) 2000, 2001 NVIDIA Corporation
|
---|
7 | All rights reserved.
|
---|
8 |
|
---|
9 | Redistribution and use in source and binary forms, with or
|
---|
10 | without modification, are permitted provided that the following
|
---|
11 | conditions are met:
|
---|
12 |
|
---|
13 | * Redistributions of source code must retain the above
|
---|
14 | copyright notice, this list of conditions and the following
|
---|
15 | disclaimer.
|
---|
16 |
|
---|
17 | * Redistributions in binary form must reproduce the above
|
---|
18 | copyright notice, this list of conditions and the following
|
---|
19 | disclaimer in the documentation and/or other materials
|
---|
20 | provided with the distribution.
|
---|
21 |
|
---|
22 | * The names of contributors to this software may not be used
|
---|
23 | to endorse or promote products derived from this software
|
---|
24 | without specific prior written permission.
|
---|
25 |
|
---|
26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
29 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
30 | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
31 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
---|
32 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
33 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
---|
34 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
35 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
---|
36 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
37 | POSSIBILITY OF SUCH DAMAGE.
|
---|
38 |
|
---|
39 |
|
---|
40 | Cass Everitt - cass@r3.nu
|
---|
41 | */
|
---|
42 |
|
---|
43 | /*
|
---|
44 | gluBuild2DMipmaps cannot build mipmaps for textures whose
|
---|
45 | "format" it does not recognize. This is primarily because
|
---|
46 | it infers the number of components and how to average them
|
---|
47 | from the format. This helper eliminates that problem by
|
---|
48 | factoring out that functionality.
|
---|
49 |
|
---|
50 | Cass Everitt
|
---|
51 | 1-9-01
|
---|
52 |
|
---|
53 | */
|
---|
54 |
|
---|
55 | #ifndef GLH_MIPMAPS_H
|
---|
56 | #define GLH_MIPMAPS_H
|
---|
57 |
|
---|
58 | #ifdef _WIN32
|
---|
59 | # include <windows.h>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #ifdef MACOS
|
---|
63 | #include <OpenGL/gl.h>
|
---|
64 | #else
|
---|
65 | #include <GL/gl.h>
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | namespace glh
|
---|
69 | {
|
---|
70 |
|
---|
71 | template <class T>
|
---|
72 | class tex_indexer2
|
---|
73 | {
|
---|
74 | public:
|
---|
75 | tex_indexer2(int width, int height, int tuple_size, T * data) :
|
---|
76 | w(width), h(height), n(tuple_size), d(data) {}
|
---|
77 |
|
---|
78 | T * operator()(int i, int j)
|
---|
79 | { return d + n * (w * j + i); }
|
---|
80 | private:
|
---|
81 | int w, h, n;
|
---|
82 | T * d;
|
---|
83 | };
|
---|
84 |
|
---|
85 | template <class T>
|
---|
86 | struct generic_filter
|
---|
87 | {
|
---|
88 | typedef T element_type;
|
---|
89 |
|
---|
90 | generic_filter(int tuplesize, GLenum gltype) :
|
---|
91 | gl_type(gltype), tuple_size(tuplesize) {}
|
---|
92 |
|
---|
93 | void average( T * out,
|
---|
94 | const T * a, const T * b,
|
---|
95 | const T * c, const T * d)
|
---|
96 | {
|
---|
97 | for(int i=0; i < tuple_size; i++)
|
---|
98 | {
|
---|
99 | double in = double(a[i]) + double(b[i]) + double(c[i]) + double(d[i]);
|
---|
100 | in /= 4;
|
---|
101 | out[i] = T(in);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | const GLenum gl_type;
|
---|
106 | const int tuple_size;
|
---|
107 | };
|
---|
108 |
|
---|
109 | // fixme: supports non-square textures!
|
---|
110 | template <class F>
|
---|
111 | void build_2D_mipmaps( GLenum target, GLenum internal_format,
|
---|
112 | GLsizei w, GLsizei h, GLenum format,
|
---|
113 | F filter, const void * vdata)
|
---|
114 | {
|
---|
115 |
|
---|
116 | typedef typename F::element_type DataType;
|
---|
117 | const DataType * in_data = (const DataType *)vdata;
|
---|
118 | DataType * data = new DataType [w * h * filter.tuple_size];
|
---|
119 |
|
---|
120 | glTexImage2D(target, 0, internal_format, w, h, 0, format, filter.gl_type, (const DataType *)vdata);
|
---|
121 |
|
---|
122 | int level = 1;
|
---|
123 | if( w >= 2 ) w /= 2;
|
---|
124 | if( h >= 2 ) h /= 2;
|
---|
125 | bool done = false;
|
---|
126 | while(! done)
|
---|
127 | {
|
---|
128 | tex_indexer2<const DataType> bg(w*2, h*2, filter.tuple_size, in_data);
|
---|
129 | tex_indexer2<DataType> sm(w , h , filter.tuple_size, data);
|
---|
130 | for(int j=0; j < h; j++)
|
---|
131 | {
|
---|
132 | int J = j * 2;
|
---|
133 | for(int i=0; i < w; i++)
|
---|
134 | {
|
---|
135 | int I = i*2;
|
---|
136 | filter.average( sm(i,j),
|
---|
137 | bg(I , J ), bg(I+1, J ),
|
---|
138 | bg(I , J+1), bg(I+1, J+1));
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | glTexImage2D(target, level, internal_format, w, h, 0, format, filter.gl_type, data);
|
---|
143 |
|
---|
144 | if(w == 1 && h == 1) done = true;
|
---|
145 |
|
---|
146 | if( w >= 2 ) w /= 2;
|
---|
147 | if( h >= 2 ) h /= 2;
|
---|
148 | level++;
|
---|
149 | in_data = data;
|
---|
150 | }
|
---|
151 |
|
---|
152 | delete [] data;
|
---|
153 | }
|
---|
154 |
|
---|
155 | }
|
---|
156 |
|
---|
157 | #endif
|
---|