- 在线时间
- 0 小时
- 最后登录
- 2005-9-21
- 注册时间
- 2004-4-27
- 听众数
- 1
- 收听数
- 0
- 能力
- 0 分
- 体力
- 1027 点
- 威望
- 0 点
- 阅读权限
- 40
- 积分
- 385
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 153
- 主题
- 43
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   28.33% 该用户从未签到
国际赛参赛者
 |
< >这个类使用了Glu.h头文件.
, ]4 k9 T" O( Q" s* R看代码吧!</P>
, L' q# ` O9 J# _. N$ \( @9 w< >//头文件6 G/ y+ B* R! i* \+ a& w7 f
#ifndef TEXTURE_H
' Z. p4 ]7 |9 M3 G9 F7 v, a. @#define TEXTURE_H</P>5 s# o" O* g9 E
< >class Texture
4 J, [" \4 G0 I2 @, Z# ~, |{
3 @2 Y, [/ v# qprivate:/ z$ Q" l" w2 r
unsigned int m_Index;; H* W% l3 z* k n
bool m_Empty;
& ~. [; Y! R: V5 y' V' S3 @ float m_Width;
8 ^* R$ r& K6 h- k6 ]/ c5 Y7 b1 o float m_Height;
9 b3 a+ V, d* k- A, ^, `public:6 k* N( ^4 y& ]1 `& i' E
Texture();2 W8 M& J2 b d/ C# m2 m
~Texture();</P>
" V: H4 h3 y5 J8 ^) D6 W< > void Clear();4 h$ j- ?; y) U. }/ I
void LoadFile(char*fn);/ F( X$ I; u& S$ @! Z
void Draw(float x,float y);
9 t# e+ v- r+ I};</P>
# ^7 _, P; j2 z' `$ ~2 G2 ]< >#endif//TEXTURE_H
/ C* m6 q/ W& D. ^
' |. U' P# J/ P5 k; i8 t
' c1 [' d% k4 K$ ~! D7 y! {1 X1 W2 @0 [; s0 Y! o
//CPP文件# x5 _! \1 \" l5 _8 j' E3 e0 f
#include "Texture.h"
1 {9 @8 L, K2 v/ x" e3 b8 w#include <windows.h>
& e3 |& a9 X" C* m8 n) U, V; ]#include <gl/gl.h>
% N1 l. U' f& W#include <gl/glaux.h></P>
. g& z. }. k7 a/ o3 \7 c P8 J9 i< >#pragma comment(lib,"glaux.lib")</P>$ j0 B6 i9 ]0 f2 y* [
< >Texture::Texture()
" \" k7 h T: K! `{3 W; S0 l w6 w/ k6 j: F R
m_Empty=true;% p3 q0 w) G4 t) ?2 U/ F
}</P>
1 }3 C: k% u7 t2 l# i2 G6 f< >Texture::~Texture()
8 a. ~( u$ ]5 { p{
/ _0 ^7 R- E7 x$ }* f# s Clear();; F% b& e5 e6 b) M
}</P>0 R7 @9 ^2 R, A$ m8 k, }
< >void Texture: oadFile(char*fn)& ?5 W! H. N' ~+ Q% n8 b+ O
{
8 e* R; t( i; H$ Z AUX_RGBImageRec*image;6 {, e7 K4 s6 F. H4 N; [3 d& q
image=NULL;</P>
* G; U9 _: \- l I( k< > image=auxDIBImageLoad(fn);</P>4 r) ^1 C3 |" b+ E1 l# n% Q T
< > if(image!=NULL)
, R8 k3 U+ I; }4 b C, B5 L e8 t {
. a c6 i, h. Y- S glGenTextures(1,&m_Index);
# a& m: m7 a) ~! _; |, [ glBindTexture(GL_TEXTURE_2D,m_Index);
( D2 a4 q1 g. K0 H( w; U+ f. D; E glTexImage2D(GL_TEXTURE_2D,0,3,image->sizeX,image->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE, P& N, J4 n% x3 Z$ i. t) q* I
image->data);5 d, o0 u1 P: B
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); a) ~0 R* F: v5 O* e
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);</P>- d: K" g, j1 x* g! m
< > m_Width=image->sizeX;" K3 E& \. ~1 t3 P% E
m_Height=image->sizeY;</P>
, V# j9 w* R4 d) B! |* `< > if(image)/ E# l& i3 r9 M7 {. O
{
$ A' }/ ]' E' K6 y s2 L9 R& P# r if(image->data)# Q, ^; i w% f, r7 S! O
free(image->data);</P>) j7 Z. }) U A6 v# Z" C- f& g" ~; U5 ?
< > free(image);
) W6 A5 r1 h+ H# C7 l0 C+ \3 v }</P>" l% O8 B; D. V2 A+ E
< > m_Empty=false;! _+ N2 l% O0 W
}9 Y1 Y- ]2 E( x( c }
}</P>
2 x4 l+ r' b, g4 y1 q/ T< >void Texture: raw(float x,float y)' m8 c- n& Y1 l2 U
{* a0 b: P% U8 d9 ?: z: o/ [
glEnable(GL_TEXTURE_2D);
: X$ B7 h/ o7 m8 Q2 ]! i glBindTexture(GL_TEXTURE_2D,m_Index);</P>
7 g+ `3 t" _: `' _< > glPushMatrix();5 ^2 v$ J# h9 }0 A e, O5 |3 J
glTranslatef(x,y,0);
- t5 {8 Y0 }1 o$ ?! S$ B, k2 D' _ glBegin(GL_QUADS);& `- @0 T0 v' G( r
glTexCoord2f(0,1);glVertex2f(0 ,0);/ u q0 x; U( @1 {6 R$ F: |/ c
glTexCoord2f(0,0);glVertex2f(0 ,m_Height);2 z1 ~# ?2 y8 F
glTexCoord2f(1,0);glVertex2f(m_Width,m_Height); T3 q% R+ Z7 }. \# U
glTexCoord2f(1,1);glVertex2f(m_Width,0);& N1 }, ~$ O& s" w
glEnd();4 J$ [- D2 |7 _8 ?8 [( ~
glPopMatrix();9 K! x' l l6 s5 Z; ~& r' k
glDisable(GL_TEXTURE_2D);
: P6 J K4 a* H4 x* `}</P>* G) s2 Z! I1 {
< >void Texture::Clear()
! B$ q1 b' N: {! B( D% T7 R{
4 i: `5 N# j% j! B' L! M7 l4 S if(m_Empty)return;</P>, Z/ i- l. b( i. i. f
< > glDeleteTextures(1,&m_Index);</P>* ?+ L* b7 A3 k: E' o6 ` v8 g4 m
< > m_Empty=true;/ S8 j8 e M7 Q" \7 _8 S9 e
}) v: X3 F6 u* S4 D7 X- S
* a( c8 A$ ^ b2 `$ \5 ~' i; n* f" ^$ ^! C/ }# m
% f% ~8 f; `# \- A% f使用也很简单..直接Texture t;</P>- { X1 W4 h- o$ R
< >t.LoadFile(FileName);</P>
" e0 p* E5 _! f2 v< >然后在Draw里,t.draw(x,y);</P>
3 ^. r. E$ p8 ^1 c< >就可以了.
5 R- A, a" t. I9 e9 X
( m# p) @' ?& Q以下是效果图:</P> |
zan
|