0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
| | #import "OpenGLMotionBlur.h"
#define ALPHA 0.6
#define TEX_W 512
#define TEX_H 512
@implementation OpenGLMotionBlur
- (id)init {
self = [super init];
alpha = ALPHA;
width = TEX_W;
height = TEX_H;
bitmapData = (unsigned char *)malloc( width*height*3*sizeof( unsigned char ) );
glGenTextures( 1, texName );
glBindTexture( GL_TEXTURE_2D, texName[0] );
glTexImage2D(
GL_TEXTURE_2D, 0 , GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData
);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
return self;
}
- (void)dealloc {
free( bitmapData );
[super dealloc];
}
- (void)setViewOrtho {
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
glOrtho( 0, 1, 0, 1, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
}
- (void)drawTexture {
glBindTexture( GL_TEXTURE_2D, texName[0] );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_BLEND );
glEnable( GL_TEXTURE_2D );
glColor4f( 1.0, 1.0, 1.0, alpha );
glBegin( GL_POLYGON );
glTexCoord2f( 0.0f, 0.0f );
glVertex2f( 0, 0 );
glTexCoord2f( 0.0f, 1.0f );
glVertex2f( 0, 1 );
glTexCoord2f( 1.0f, 1.0f );
glVertex2f( 1, 1 );
glTexCoord2f( 1.0f, 0.0f );
glVertex2f( 1, 0 );
glEnd();
}
- (void)start {
glViewport( 0, 0, width, height );
[self setViewOrtho];
[self drawTexture];
}
- (void)end {
glBindTexture( GL_TEXTURE_2D, texName[0] );
glCopyTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGB,
0, 0,
width, height,
0
);
}
- (void)draw {
[self setViewOrtho];
[self drawTexture];
}
- (unsigned char *)bitmapData {
return bitmapData;
}
- (void)setWidth:(int)w height:(int)h {
width = w;
height = h;
free( bitmapData );
bitmapData = (unsigned char *)malloc( width*height*3*sizeof( unsigned char ) );
glBindTexture( GL_TEXTURE_2D, texName[0] );
glTexImage2D(
GL_TEXTURE_2D, 0 , GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData
);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
}
- (int)width {
return width;
}
- (int)height {
return height;
}
- (void)setAlpha:(float)a {
alpha = a;
}
- (float)alpha {
return alpha;
}
@end
|