Ticket #7970: fb2opengl.2.h

File fb2opengl.2.h, 8.7 KB (added by SF/luke_br, 22 years ago)

Framebuffer to OpenGL texture conversion

Line 
1/* FrameBuffer renderer in an OpenGL texture
2 Andre Souza <asouza@olinux.com.br> */
3
4#include <GL/gl.h>
5#include <SDL/SDL.h>
6#include <stdlib.h>
7#include <string.h>
8
9/* FLAGS */
10#define FB2GL_FS 1 /* FULLSCREEN */
11#define FB2GL_RGBA 2 /* Use RGBA (else use palette) */
12#define FB2GL_320 4 /* 320x256 texture (else use 256x256) */
13#define FB2GL_AUDIO 8 /* Activate SDL Audio */
14#define FB2GL_PITCH 16 /* On fb2l_update, use pitch (else bytes per pixel) */
15#define FB2GL_EXPAND 32 /* Create a RGB fb with the color lookup table*/
16
17/* Framebuffer for 8 bpp */
18unsigned char ogl_fb[256][256];
19unsigned char ogl_fbb[256][64];
20/* Framebuffer for RGBA */
21unsigned char ogl_fb1[256][256][4];
22unsigned char ogl_fb2[256][256][4];
23/* Texture(s) */
24GLuint texture;
25GLuint textureb;
26/* Display list */
27GLuint dlist;
28/* Color Table (256 colors, RGB) */
29char ogl_ctable[256][3];
30/* Use RGBA? */
31char fb2gl_RGBA=0;
32/* If so, expand the 8 bit fb to RGB? */
33char fb2gl_expand=0;
34/* 320x256? */
35char fb2gl_t320=0;
36/* Use pitch? Else, bytes per pixel */
37char use_pitch=0;
38/* Methods */
39void fb2gl_maketex();
40void fb2gl_makedlist(int xf, int yf);
41void fb2gl_display();
42
43/* Public */
44SDL_Surface *screen;
45void fb2gl_init(int width, int height, int xfix, int yfix, char flags);
46void fb2gl_update(void *fb, int width, int height, int pitch, int xskip, int yskip);
47void fb2gl_palette(int index, int r, int g, int b);
48void fb2gl_set_palette(int first, int ncolors);
49
50
51void fb2gl_maketex()
52{
53 glGenTextures(0,&texture);
54 glBindTexture(GL_TEXTURE_2D,texture);
55
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60
61 if (fb2gl_RGBA) {
62 glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,256,256,0,GL_RGBA, GL_UNSIGNED_BYTE, ogl_fb1);
63 }
64 else {
65 glTexImage2D(GL_TEXTURE_2D,0,GL_COLOR_INDEX,256,256,0,GL_COLOR_INDEX, GL_UNSIGNED_BYTE, ogl_fb);
66 }
67
68 if (fb2gl_t320) {
69 glGenTextures(1,&textureb);
70 glBindTexture(GL_TEXTURE_2D,textureb);
71
72 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
76
77 if (fb2gl_RGBA) {
78 glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,64,256,0,GL_RGBA,
79 GL_UNSIGNED_BYTE, ogl_fb2);
80 }
81 else {
82 glTexImage2D(GL_TEXTURE_2D,0,GL_COLOR_INDEX,64,256,0,GL_COLOR_INDEX,
83 GL_UNSIGNED_BYTE, ogl_fbb);
84 }
85 }
86
87}
88
89void fb2gl_makedlist(int xf, int yf)
90{
91 double xfix=(double)xf/128; /* 128 = 256/2 (half texture => 0.0 to 1.0) */
92 double yfix=(double)yf/128;
93 double texend = (double)615/1024; /* End of 256x256 (from -1.0 to 1.0) */
94
95 dlist=glGenLists(1);
96 glNewList(dlist,GL_COMPILE);
97
98 glEnable(GL_TEXTURE_2D);
99
100 glBindTexture(GL_TEXTURE_2D, texture);
101
102 if (!fb2gl_t320) { /* Normal 256x256 */
103 glBegin(GL_QUADS);
104 glTexCoord2f(0.0,1.0); glVertex2f(-1.0,-1.0-yfix); /* upper left */
105 glTexCoord2f(0.0,0.0); glVertex2f(-1.0,1.0); /* lower left */
106 glTexCoord2f(1.0,0.0); glVertex2f(1.0+xfix,1.0); /* lower right */
107 glTexCoord2f(1.0,1.0); glVertex2f(1.0+xfix,-1.0-yfix); /* upper right */
108 glEnd();
109 }
110 else { /* 320x256 */
111
112 /* First, the 256x256 texture */
113 glBegin(GL_QUADS);
114 glTexCoord2f(0.0,1.0); glVertex2f(-1.0,-1.0-yfix); /* upper left */
115 glTexCoord2f(0.0,0.0); glVertex2f(-1.0,1.0); /* lower left */
116 glTexCoord2f(1.0,0.0); glVertex2f(texend+xfix,1.0); /* lower right */
117 glTexCoord2f(1.0,1.0); glVertex2f(texend+xfix,-1.0-yfix); /* upper right*/
118 glEnd();
119
120 /* 64x256 */
121 glBindTexture(GL_TEXTURE_2D, textureb);
122
123 glBegin(GL_QUADS);
124 glTexCoord2f(0.0,1.0); glVertex2f(texend+xfix,-1.0-yfix); /* upper left
125*/
126 glTexCoord2f(0.0,0.0); glVertex2f(texend+xfix,1.0); /* lower left */
127 glTexCoord2f(1.0,0.0); glVertex2f(1.0+xfix,1.0); /* lower right */
128 glTexCoord2f(1.0,1.0); glVertex2f(1.0+xfix,-1.0-yfix); /* upper right */
129 glEnd();
130 }
131
132 glDisable(GL_TEXTURE_2D);
133
134 glEndList();
135}
136
137void fb2gl_init(int width, int height, int xfix, int yfix, char flags)
138{
139 char fs=0;
140 char audio=0;
141 char gl_ext[4096];
142
143 /* Test the flags */
144 if (flags & FB2GL_FS) fs=1; else fs=0;
145 if (flags & FB2GL_RGBA) fb2gl_RGBA=1; else fb2gl_RGBA=0;
146 if (flags & FB2GL_320) fb2gl_t320=1; else fb2gl_t320=0;
147 if (flags & FB2GL_AUDIO) audio=1; else audio=0;
148 if (flags & FB2GL_PITCH) use_pitch=1; else use_pitch=0;
149 if (flags & FB2GL_EXPAND) fb2gl_expand=1; else fb2gl_expand=0;
150/*
151 if (audio) SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
152 else SDL_Init(SDL_INIT_VIDEO);
153
154 atexit(SDL_Quit);
155*/
156
157
158 if (fs) {
159 screen = SDL_SetVideoMode(width, height, 0, SDL_HWPALETTE | SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_FULLSCREEN);
160 }
161 else {
162 screen = SDL_SetVideoMode(width, height, 0, SDL_HWPALETTE | SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER);
163 }
164
165 if (!screen) {
166 fprintf(stderr, "Couldn't start video res %dx%d\n", width, height);
167 }
168
169 if (!fb2gl_RGBA) {
170 /* Paletted Texture Extension */
171 strcpy(gl_ext, (char *)glGetString(GL_EXTENSIONS));
172
173 if ( strstr( gl_ext , "GL_EXT_paletted_texture") )
174 glEnable(GL_EXT_paletted_texture);
175 else {
176 printf("Your OpenGL implementation doesn't support paletted texture\n");
177 exit(0);
178 }
179 }
180
181 glEnable(GL_DITHER);
182
183 fb2gl_maketex();
184
185 fb2gl_makedlist(xfix, yfix);
186
187}
188
189void fb2gl_display()
190{
191 glCallList(dlist);
192 SDL_GL_SwapBuffers();
193}
194
195/* Changed the way xskip and yskip work for use with scummvm (shaking) */
196void fb2gl_update(void *fb, int w, int h, int pitch, int xskip, int yskip) {
197 unsigned char *fb1=(unsigned char *)fb;
198 int x,y,scr_pitch,byte=0;
199
200 if (use_pitch) scr_pitch=pitch;
201 else {
202 scr_pitch=w*pitch;
203 byte = pitch; /* Bytes perl pixel (for RGBA mode) */
204 }
205
206 if (fb2gl_RGBA) {
207 if (fb2gl_expand) { /* Expand the 8 bit fb into a RGB fb */
208 for (y=yskip; y<h; y++) {
209 for (x=xskip; x<w; x++) {
210 if (x<256) {
211 ogl_fb1[y][x][0] = ogl_ctable[*(fb1+x)][0];
212 ogl_fb1[y][x][1] = ogl_ctable[*(fb1+x)][1];
213 ogl_fb1[y][x][2] = ogl_ctable[*(fb1+x)][2];
214 /* ogl_fb1[y-yskip][x-xskip][3]=255; */
215 }
216 else {
217 ogl_fb2[y][x-256][0] = ogl_ctable[*(fb1+x)][0];
218 ogl_fb2[y][x-256][1] = ogl_ctable[*(fb1+x)][1];
219 ogl_fb2[y][x-256][2] = ogl_ctable[*(fb1+x)][2];
220/* ogl_fb2[y-yskip][x-256][3]=255; */
221 }
222 }
223 fb1 += scr_pitch;
224 }
225 }
226 else { /* No expansion */
227 for (y=yskip; y<h; y++) {
228 for (x=xskip; x<w; x++) {
229 if (x<256) {
230 ogl_fb1[y-yskip][x-xskip][0] = *(fb1+(x*byte));
231 ogl_fb1[y-yskip][x-xskip][1] = *(fb1+(x*byte)+1);
232 ogl_fb1[y-yskip][x-xskip][2] = *(fb1+(x*byte)+2);
233 ogl_fb1[y-yskip][x-xskip][3]=255;
234 }
235 else {
236 ogl_fb2[y-yskip][x-256][0] = *(fb1+(x*byte));
237 ogl_fb2[y-yskip][x-256][1] = *(fb1+(x*byte)+1);
238 ogl_fb2[y-yskip][x-256][2] = *(fb1+(x*byte)+2);
239 ogl_fb2[y-yskip][x-256][3]=255;
240 }
241 }
242 fb1 += scr_pitch;
243 }
244 }
245
246 /* Update 256x256 texture */
247 glBindTexture(GL_TEXTURE_2D,texture);
248 glFlush();
249 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,256,256,GL_RGBA,
250 GL_UNSIGNED_BYTE,ogl_fb1);
251
252 if (fb2gl_t320) {
253 /* Update 64x256 texture */
254 glBindTexture(GL_TEXTURE_2D,textureb);
255 glFlush();
256 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,64,256,GL_RGBA,
257 GL_UNSIGNED_BYTE,ogl_fb2);
258 }
259
260 }
261 else { /* non RGBA */
262
263 for (y=0; y<h; y++)
264 for (x=0; x<w; x++) {
265 if (x<256) {
266 ogl_fb[ y ][ x ] = *(fb1 + (y)*scr_pitch + x);
267 }
268 else {
269 ogl_fbb[ y ][ x - 256 ] = *(fb1 + y*scr_pitch + x);
270 }
271 }
272
273 /* Update 256x256 texture */
274 glBindTexture(GL_TEXTURE_2D,texture);
275 glTexSubImage2D(GL_TEXTURE_2D,0,xskip,yskip,256-xskip,256-yskip,GL_COLOR_INDEX, GL_UNSIGNED_BYTE,ogl_fb);
276
277 if (fb2gl_t320) {
278 /* Update 64x256 texture */
279 glBindTexture(GL_TEXTURE_2D,textureb);
280 glTexSubImage2D(GL_TEXTURE_2D,0,xskip,yskip,64-xskip,256-yskip,GL_COLOR_INDEX, GL_UNSIGNED_BYTE,ogl_fbb);
281 }
282
283 }
284
285 fb2gl_display();
286
287}
288
289void fb2gl_palette(int i, int r, int g, int b) {
290 ogl_ctable[i][0]=r;
291 ogl_ctable[i][1]=g;
292 ogl_ctable[i][2]=b;
293}
294
295void fb2gl_set_palette(int f, int n) {
296 char temp[256][3];
297 int i;
298
299 if (!fb2gl_expand)
300 {
301
302 glBindTexture(GL_TEXTURE_2D,texture);
303/* glColorTable(GL_TEXTURE_2D,GL_RGB,256,GL_RGB,GL_UNSIGNED_BYTE,ogl_ctable);*/
304 glGetColorTable(GL_TEXTURE_2D,GL_RGB,GL_UNSIGNED_BYTE,&temp);
305
306 for (i=f; i<n; i++) {
307 temp[i][0] = ogl_ctable[i][0];
308 temp[i][1] = ogl_ctable[i][1];
309 temp[i][2] = ogl_ctable[i][2];
310 }
311
312 glColorTable(GL_TEXTURE_2D,GL_RGB,256,GL_RGB,GL_UNSIGNED_BYTE,&temp);
313
314 if (fb2gl_t320) {
315 glBindTexture(GL_TEXTURE_2D,textureb);
316/* glColorTable(GL_TEXTURE_2D,GL_RGB,256,GL_RGB,GL_UNSIGNED_BYTE,ogl_ctable);*/
317 glColorTable(GL_TEXTURE_2D,GL_RGB,256,GL_RGB,GL_UNSIGNED_BYTE,&temp);
318 }
319
320 }
321}