




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
【原創(chuàng)】OPGLES加載PNG圖片為紋理 因為BMP圖片在顯示的時候周邊有比較難看的鋸齒圖,OPGLES基本上都是使用對BMP加載紋理圖的做法,很少加載PNG紋理圖,今天在某國外網(wǎng)站看到這么一段使用OPGL加載PNG紋理圖代碼,摘錄下來。 論壇上有人試驗成功,但是我使用libpng庫的時候,用png_read_info讀取png圖片信息時出錯,正在調(diào)試中。大家討論討論。 通過該鏈接可以下載庫和源碼http://www.tenik.co.jp/~adachi/wince/zlibce/index.html
源碼如下:
1
/*
2
*
png.c
--
png
texture
loader
3
*
last
modification:
aug.
14,
2007
4
*
5
*
Copyright
(c)
2005-2007
David
HENRY
6
*
7
*
Permission
is
hereby
granted,
free
of
charge,
to
any
person
8
*
obtaining
a
copy
of
this
software
and
associated
documentation
9
*
files
(the
"Software"),
to
deal
in
the
Software
without
10
*
restriction,
including
without
limitation
the
rights
to
use,
11
*
copy,
modify,
merge,
publish,
distribute,
sublicense,
and/or
12
*
sell
copies
of
the
Software,
and
to
permit
persons
to
whom
the
13
*
Software
is
furnished
to
do
so,
subject
to
the
following
conditions:
14
*
15
*
The
above
copyright
notice
and
this
permission
notice
shall
be
16
*
included
in
all
copies
or
substantial
portions
of
the
Software.
17
*
18
*
THE
SOFTWARE
IS
PROVIDED
"AS
IS",
WITHOUT
WARRANTY
OF
ANY
KIND,
19
*
EXPRESS
OR
IMPLIED,
INCLUDING
BUT
NOT
LIMITED
TO
THE
WARRANTIES
OF
20
*
MERCHANTABILITY,
FITNESS
FOR
A
PARTICULAR
PURPOSE
AND
21
*
NONINFRINGEMENT.
22
*
IN
NO
EVENT
SHALL
THE
AUTHORS
OR
COPYRIGHT
HOLDERS
BE
LIABLE
FOR
23
*
ANY
CLAIM,
DAMAGES
OR
OTHER
LIABILITY,
WHETHER
IN
AN
ACTION
OF
24
*
CONTRACT,
TORT
OR
OTHERWISE,
ARISING
FROM,
OUT
OF
OR
IN
CONNECTION
25
*
WITH
THE
SOFTWARE
OR
THE
USE
OR
OTHER
DEALINGS
IN
THE
SOFTWARE.
26
*
27
*
gcc
-Wall
-ansi
-lGL
-lGLU
-lglut
28
*
`libpng12-config
--cflags
--libs`
png.c
-o
png
29
*/
30
31
#include
<GL/glut.h>
32
#include
<stdio.h>
33
#include
<stdlib.h>
34
#include
<string.h>
35
36
#include
<png.h>
37
38
/*
Microsoft
Visual
C++
*/
39
#ifdef
_MSC_VER
40
#pragma
comment
(lib,
"libpng.lib")
41
#pragma
comment
(lib,
"zlib.lib")
42
#pragma
comment
(linker,
"/nodefaultlib:libc")
43
#endif
/*
_MSC_VER
*/
44
45
/*
OpenGL
texture
info
*/
46
struct
gl_texture_t
47
{
48
GLsizei
width;
49
GLsizei
height;
50
51
GLenum
format;
52
GLint
internalFormat;
53
GLuint
id;
54
55
GLubyte
*texels;
56
};
57
58
/*
Texture
id
for
the
demo
*/
59
GLuint
texId;
60
61
62
static
void
63
GetPNGtextureInfo
(int
color_type,
struct
gl_texture_t
*texinfo)
64
{
65
switch
(color_type)
66
{
67
case
PNG_COLOR_TYPE_GRAY:
68
texinfo->format
=
GL_LUMINANCE;
69
texinfo->internalFormat
=
1;
70
break;
71
72
case
PNG_COLOR_TYPE_GRAY_ALPHA:
73
texinfo->format
=
GL_LUMINANCE_ALPHA;
74
texinfo->internalFormat
=
2;
75
break;
76
77
case
PNG_COLOR_TYPE_RGB:
78
texinfo->format
=
GL_RGB;
79
texinfo->internalFormat
=
3;
80
break;
81
82
case
PNG_COLOR_TYPE_RGB_ALPHA:
83
texinfo->format
=
GL_RGBA;
84
texinfo->internalFormat
=
4;
85
break;
86
87
default:
88
/*
Badness
*/
89
break;
90
}
91
}
92
93
static
struct
gl_texture_t
*
94
ReadPNGFromFile
(const
char
*filename)
95
{
96
struct
gl_texture_t
*texinfo;
97
png_byte
magic[8];
98
png_structp
png_ptr;
99
png_infop
info_ptr;
100
int
bit_depth,
color_type;
101
FILE
*fp
=
NULL;
102
png_bytep
*row_pointers
=
NULL;
103
png_uint_32
w,
h;
104
int
i;
105
106
/*
Open
image
file
*/
107
fp
=
fopen
(filename,
"rb");
108
if
(!fp)
109
{
110
fprintf
(stderr,
"error:
couldn't
open
\"%s\"!\n",
filename);
111
return
NULL;
112
}
113
114
/*
Read
magic
number
*/
115
fread
(magic,
1,
sizeof
(magic),
fp);
116
117
/*
Check
for
valid
magic
number
*/
118
if
(!png_check_sig
(magic,
sizeof
(magic)))
119
{
120
fprintf
(stderr,
"error:
\"%s\"
is
not
a
valid
PNG
image!\n",
121
filename);
122
fclose
(fp);
123
return
NULL;
124
}
125
126
/*
Create
a
png
read
struct
*/
127
png_ptr
=
png_create_read_struct
128
(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
129
if
(!png_ptr)
130
{
131
fclose
(fp);
132
return
NULL;
133
}
134
135
/*
Create
a
png
info
struct
*/
136
info_ptr
=
png_create_info_struct
(png_ptr);
137
if
(!info_ptr)
138
{
139
fclose
(fp);
140
png_destroy_read_struct
(&png_ptr,
NULL,
NULL);
141
return
NULL;
142
}
143
144
/*
Create
our
OpenGL
texture
object
*/
145
texinfo
=
(struct
gl_texture_t
*)
146
malloc
(sizeof
(struct
gl_texture_t));
147
148
/*
Initialize
the
setjmp
for
returning
properly
after
a
libpng
149
error
occured
*/
150
if
(setjmp
(png_jmpbuf
(png_ptr)))
151
{
152
fclose
(fp);
153
png_destroy_read_struct
(&png_ptr,
&info_ptr,
NULL);
154
155
if
(row_pointers)
156
free
(row_pointers);
157
158
if
(texinfo)
159
{
160
if
(texinfo->texels)
161
free
(texinfo->texels);
162
163
free
(texinfo);
164
}
165
166
return
NULL;
167
}
168
169
/*
Setup
libpng
for
using
standard
C
fread()
function
170
with
our
FILE
pointer
*/
171
png_init_io
(png_ptr,
fp);
172
173
/*
Tell
libpng
that
we
have
already
read
the
magic
number
*/
174
png_set_sig_bytes
(png_ptr,
sizeof
(magic));
175
176
/*
Read
png
info
*/
177
png_read_info
(png_ptr,
info_ptr);
178
179
/*
Get
some
usefull
information
from
header
*/
180
bit_depth
=
png_get_bit_depth
(png_ptr,
info_ptr);
181
color_type
=
png_get_color_type
(png_ptr,
info_ptr);
182
183
/*
Convert
index
color
images
to
RGB
images
*/
184
if
(color_type
==
PNG_COLOR_TYPE_PALETTE)
185
png_set_palette_to_rgb
(png_ptr);
186
187
/*
Convert
1-2-4
bits
grayscale
images
to
8
bits
188
grayscale.
*/
189
if
(color_type
==
PNG_COLOR_TYPE_GRAY
&&
bit_depth
<
8)
190
png_set_gray_1_2_4_to_8
(png_ptr);
191
192
if
(png_get_valid
(png_ptr,
info_ptr,
PNG_INFO_tRNS))
193
png_set_tRNS_to_alpha
(png_ptr);
194
195
if
(bit_depth
==
16)
196
png_set_strip_16
(png_ptr);
197
else
if
(bit_depth
<
8)
198
png_set_packing
(png_ptr);
199
200
/*
Update
info
structure
to
apply
transformations
*/
201
png_read_update_info
(png_ptr,
info_ptr);
202
203
/*
Retrieve
updated
information
*/
204
png_get_IHDR
(png_ptr,
info_ptr,
&w,
&h,
&bit_depth,
205
&color_type,
NULL,
NULL,
NULL);
206
texinfo->width
=
w;
207
texinfo->height
=
h;
208
209
/*
Get
image
format
and
components
per
pixel
*/
210
GetPNGtextureInfo
(color_type,
texinfo);
211
212
/*
We
can
now
allocate
memory
for
storing
pixel
data
*/
213
texinfo->texels
=
(GLubyte
*)malloc
(sizeof
(GLubyte)
*
texinfo->width
214
*
texinfo->height
*
texinfo->internalFormat);
215
216
/*
Setup
a
pointer
array.
Each
one
points
at
the
begening
of
a
row.
*/
217
row_pointers
=
(png_bytep
*)malloc
(sizeof
(png_bytep)
*
texinfo->height);
218
219
for
(i
=
0;
i
<
texinfo->height;
++i)
220
{
221
row_pointers[i]
=
(png_bytep)(texinfo->texels
+
222
((texinfo->height
-
(i
+
1))
*
texinfo->width
*
texinfo->internalFormat));
223
}
224
225
/*
Read
pixel
data
using
row
pointers
*/
226
png_read_image
(png_ptr,
row_pointers);
227
228
/*
Finish
decompression
and
release
memory
*/
229
png_read_end
(png_ptr,
NULL);
230
png_destroy_read_struct
(&png_ptr,
&info_ptr,
NULL);
231
232
/*
We
don't
need
row
pointers
anymore
*/
233
free
(row_pointers);
234
235
fclose
(fp);
236
return
texinfo;
237
}
238
239
GLuint
240
loadPNGTexture
(const
char
*filename)
241
{
242
struct
gl_texture_t
*png_tex
=
NULL;
243
GLuint
tex_id
=
0;
244
245
png_tex
=
ReadPNGFromFile
(filename);
246
247
if
(png_tex
&&
png_tex->texels)
248
{
249
/*
Generate
texture
*/
250
glGenTextures
(1,
&png_tex->id);
251
glBindTexture
(GL_TEXTURE_2D,
png_tex->id);
252
253
/*
Setup
some
parameters
for
texture
filters
and
mipmapping
*/
254
glTexParameteri
(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
255
glTexParameteri
(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
256
257
#if
0
258
glTexImage2D
(GL_TEXTURE_2D,
0,
png_tex->internalFormat,
259
png_tex->width,
png_tex->height,
0,
png_tex->format,
260
GL_UNSIGNED_BYTE,
png_tex->texels);
261
#else
262
gluBuild2DMipmaps
(GL_TEXTURE_2D,
png_tex->internalFormat,
263
png_tex->width,
png_tex->height,
264
png_tex->format,
GL_UNSIGNED_BYTE,
png_tex->texels);
265
#endif
266
267
tex_id
=
png_tex->id;
268
269
/*
OpenGL
has
its
own
copy
of
texture
data
*/
270
free
(png_tex->texels);
271
free
(png_tex);
272
}
273
274
return
tex_id;
275
}
276
277
static
void
278
cleanup
()
279
{
280
glDeleteTextures
(1,
&texId);
281
}
282
283
static
void
284
init
(const
char
*filename)
285
{
286
/*
Initialize
OpenGL
*/
287
glClearColor
(1,
0.8f,
0.8f,0.0f);
288
glShadeModel
(GL_SMOOTH);
289
290
glEnable
(GL_DEPTH_TEST);
291
292
glEnable
(GL_BLEND);
293
glBlendFunc
(GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA);
294
295
/*
Load
PNG
texture
from
file
*/
296
texId
=
loadPNGTexture
(filename);
297
if
(!texId)
298
exit
(EXIT_FAILURE);
299
}
300
301
static
void
302
reshape
(int
w,
int
h)
303
{
304
if
(h
==
0)
305
h
=
1;
306
glViewport
(0,
0,
(GLsizei)w,
(GLsizei)h);
307
308
glMatrixMode
(GL_PROJECTION);
309
glLoadIdentity
();
310
gluPerspective
(45.0,
w/(GLdouble)h,
0.1,
1000.0);
311
312
glMatrixMode
(GL_MODELVIEW);
313
glLoadIdentity
();
314
315
glutPostRedisplay
();
316
}
317
318
static
void
319
display
()
320
{
321
glClear
(GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT);
322
glLoadIdentity
();
323
324
glEnable
(GL_TEXTURE_2D);
325
glBindTexture
(GL_TEXTURE_2D,
texId);
326
327
/*
Draw
textured
quad
*/
328
glTranslatef
(0.0,
0.0,
-5.0);
329
glBegin
(GL_QUADS);
330
glTexCoord2f
(0.0f,
0.0f);
331
glVertex3f
(-1.0f,
-1.0f,
0.0f);
332
333
glTexCoord2f
(1.0f,
0.0f);
334
glVertex3f
(1.0f,
-1.0f,
0.0f);
335
336
glTexCoord2f
(1.0f,
1.0f);
337
glVertex3f
(1.0f,
1.0f
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 重慶師范大學《酒水與酒吧管理》2023-2024學年第二學期期末試卷
- 寧夏大學新華學院《植物細胞工程》2023-2024學年第二學期期末試卷
- 濟南大學《管理研究方法導讀》2023-2024學年第二學期期末試卷
- 湖北工業(yè)大學《中學思想政治教育學科教育學》2023-2024學年第二學期期末試卷
- 天津體育職業(yè)學院《勘查地球物理方法及應用》2023-2024學年第二學期期末試卷
- 新疆機電職業(yè)技術學院《現(xiàn)場總線技術》2023-2024學年第二學期期末試卷
- 忻州職業(yè)技術學院《戰(zhàn)略與公司管理》2023-2024學年第二學期期末試卷
- 呼倫貝爾職業(yè)技術學院《鄉(xiāng)土中國》2023-2024學年第二學期期末試卷
- 合作辦學合同的請示-文書模板
- 云南藝術學院文華學院《數(shù)字交互設計》2023-2024學年第二學期期末試卷
- 期末 (試題) -2024-2025學年人教PEP版英語五年級上冊
- 專題17 物質(zhì)結(jié)構(gòu)與性質(zhì)綜合題-五年(2020-2024)高考化學真題分類匯編(解析版)
- 語文學習任務群的解讀及設計要領
- 2024年山東省高考生物試卷真題(含答案解析)
- 光伏發(fā)電站項目安全技術交底資料
- 富血小板血漿(PRP)臨床實踐與病例分享課件
- 光伏工程施工組織設計
- 《護理科研》課件
- 人教版(2024新版)八年級上冊物理《開啟科學探索之旅》教學設計
- 年產(chǎn)1萬噸的二氧化碳捕集及資源化利用全流程示范項目可行性研究報告模板-立項拿地
- 部編版語文四年級下冊第六單元大單元作業(yè)設計
評論
0/150
提交評論