oculus2_psprite

view src/main.c @ 13:4c08bc24ef0a

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 28 Oct 2014 06:57:31 +0200
parents 92acd335620e 256d8fcf02f1
children 8a80bb153582
line source
1 /* Very simple OculusSDK OpenGL usage example.
2 *
3 * Uses SDL2 (www.libsdl.org) for event handling and OpenGL context management.
4 * Uses GLEW (glew.sourceforge.net) for OpenGL extension wrangling.
5 *
6 * Author: John Tsiombikas <nuclear@member.fsf.org>
7 * This code is in the public domain. Do whatever you like with it.
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <assert.h>
12 #include <SDL2/SDL.h>
13 #include <GL/glew.h>
14 #include <X11/Xlib.h>
15 #include <GL/glx.h>
17 #ifdef WIN32
18 #define OVR_OS_WIN32
19 #elif defined(__APPLE__)
20 #define OVR_OS_MAC
21 #else
22 #define OVR_OS_LINUX
23 #endif
25 #include <OVR_CAPI.h>
26 #include <OVR_CAPI_GL.h>
28 int init(void);
29 void cleanup(void);
30 void toggle_hmd_fullscreen(void);
31 void display(void);
32 void draw_scene(void);
33 void draw_box(float xsz, float ysz, float zsz, float norm_sign);
34 void update_rtarg(int width, int height);
35 int handle_event(SDL_Event *ev);
36 int key_event(int key, int state);
37 void reshape(int x, int y);
38 unsigned int next_pow2(unsigned int x);
39 void quat_to_matrix(const float *quat, float *mat);
40 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1);
42 /* forward declaration to avoid including non-public headers of libovr */
43 OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enable);
45 static SDL_Window *win;
46 static SDL_GLContext ctx;
47 static int win_width, win_height;
49 static unsigned int fbo, fb_tex, fb_depth;
50 static int fb_width, fb_height;
51 static int fb_tex_width, fb_tex_height;
53 static ovrHmd hmd;
54 static ovrSizei eyeres[2];
55 static ovrEyeRenderDesc eye_rdesc[2];
56 static ovrGLTexture fb_ovr_tex[2];
58 static unsigned int chess_tex;
61 int main(int argc, char **argv)
62 {
63 if(init() == -1) {
64 return 1;
65 }
67 for(;;) {
68 SDL_Event ev;
69 while(SDL_PollEvent(&ev)) {
70 if(handle_event(&ev) == -1) {
71 goto done;
72 }
73 }
74 display();
75 }
77 done:
78 cleanup();
79 return 0;
80 }
83 int init(void)
84 {
85 int i, x, y;
86 unsigned int flags, dcaps;
87 union ovrGLConfig glcfg;
89 /* libovr must be initialized before we create the OpenGL context */
90 ovr_Initialize();
92 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
94 x = y = SDL_WINDOWPOS_UNDEFINED;
95 flags = SDL_WINDOW_OPENGL;
96 if(!(win = SDL_CreateWindow("press 'f' to move to the HMD", x, y, 1280, 800, flags))) {
97 fprintf(stderr, "failed to create window\n");
98 return -1;
99 }
100 if(!(ctx = SDL_GL_CreateContext(win))) {
101 fprintf(stderr, "failed to create OpenGL context\n");
102 return -1;
103 }
105 glewInit();
107 if(!(hmd = ovrHmd_Create(0))) {
108 fprintf(stderr, "failed to open Oculus HMD, falling back to virtual debug HMD\n");
109 if(!(hmd = ovrHmd_CreateDebug(ovrHmd_DK2))) {
110 fprintf(stderr, "failed to create virtual debug HMD\n");
111 return -1;
112 }
113 }
114 printf("initialized HMD: %s - %s\n", hmd->Manufacturer, hmd->ProductName);
116 /* resize our window to match the HMD resolution */
117 SDL_SetWindowSize(win, hmd->Resolution.w, hmd->Resolution.h);
118 SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
119 win_width = hmd->Resolution.w;
120 win_height = hmd->Resolution.h;
122 /* enable position and rotation tracking */
123 ovrHmd_ConfigureTracking(hmd, ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position, 0);
124 /* retrieve the optimal render target resolution for each eye */
125 eyeres[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, hmd->DefaultEyeFov[0], 1.0);
126 eyeres[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, hmd->DefaultEyeFov[1], 1.0);
128 /* and create a single render target texture to encompass both eyes */
129 fb_width = eyeres[0].w + eyeres[1].w;
130 fb_height = eyeres[0].h > eyeres[1].h ? eyeres[0].h : eyeres[1].h;
131 update_rtarg(fb_width, fb_height);
133 /* fill in the ovrGLTexture structures that describe our render target texture */
134 for(i=0; i<2; i++) {
135 fb_ovr_tex[i].OGL.Header.API = ovrRenderAPI_OpenGL;
136 fb_ovr_tex[i].OGL.Header.TextureSize.w = fb_tex_width;
137 fb_ovr_tex[i].OGL.Header.TextureSize.h = fb_tex_height;
138 /* this next field is the only one that differs between the two eyes */
139 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.x = i == 0 ? 0 : fb_width / 2.0;
140 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.y = fb_tex_height - fb_height;
141 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.w = fb_width / 2.0;
142 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.h = fb_height;
143 fb_ovr_tex[i].OGL.TexId = fb_tex; /* both eyes will use the same texture id */
144 }
146 /* fill in the ovrGLConfig structure needed by the SDK to draw our stereo pair
147 * to the actual HMD display (SDK-distortion mode)
148 */
149 memset(&glcfg, 0, sizeof glcfg);
150 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
151 glcfg.OGL.Header.RTSize = hmd->Resolution;
152 glcfg.OGL.Header.Multisample = 1;
154 #ifdef WIN32
155 glcfg.OGL.Window = GetActiveWindow();
156 glcfg.OGL.DC = wglGetCurrentDC();
157 #else
158 glcfg.OGL.Disp = glXGetCurrentDisplay();
159 glcfg.OGL.Win = glXGetCurrentDrawable();
160 #endif
162 if(hmd->HmdCaps & ovrHmdCap_ExtendDesktop) {
163 printf("running in \"extended desktop\" mode\n");
164 } else {
165 /* to sucessfully draw to the HMD display in "direct-hmd" mode, we have to
166 * call ovrHmd_AttachToWindow
167 * XXX: this doesn't work properly yet due to bugs in the oculus 0.4.1 sdk/driver
168 */
169 #ifdef WIN32
170 ovrHmd_AttachToWindow(hmd, glcfg.OGL.Window, 0, 0);
171 #else
172 ovrHmd_AttachToWindow(hmd, (void*)glcfg.OGL.Win, 0, 0);
173 #endif
174 printf("running in \"direct-hmd\" mode\n");
175 }
177 /* enable low-persistence display and dynamic prediction for lattency compensation */
178 ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
180 /* configure SDK-rendering and enable chromatic abberation correction, vignetting, and
181 * timewrap, which shifts the image before drawing to counter any lattency between the call
182 * to ovrHmd_GetEyePose and ovrHmd_EndFrame.
183 */
184 dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp |
185 ovrDistortionCap_Overdrive;
186 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, hmd->DefaultEyeFov, eye_rdesc)) {
187 fprintf(stderr, "failed to configure distortion renderer\n");
188 }
190 /* disable the retarded "health and safety warning" */
191 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
193 glEnable(GL_DEPTH_TEST);
194 glEnable(GL_CULL_FACE);
195 glEnable(GL_LIGHTING);
196 glEnable(GL_LIGHT0);
197 glEnable(GL_LIGHT1);
198 glEnable(GL_NORMALIZE);
200 glClearColor(0.1, 0.1, 0.1, 1);
202 chess_tex = gen_chess_tex(1.0, 0.7, 0.4, 0.4, 0.7, 1.0);
203 return 0;
204 }
206 void cleanup(void)
207 {
208 if(hmd) {
209 ovrHmd_Destroy(hmd);
210 }
211 ovr_Shutdown();
213 SDL_Quit();
214 }
216 void toggle_hmd_fullscreen(void)
217 {
218 static int fullscr, prev_x, prev_y;
219 fullscr = !fullscr;
221 if(fullscr) {
222 /* going fullscreen on the rift. save current window position, and move it
223 * to the rift's part of the desktop before going fullscreen
224 */
225 SDL_GetWindowPosition(win, &prev_x, &prev_y);
226 SDL_SetWindowPosition(win, hmd->WindowsPos.x, hmd->WindowsPos.y);
227 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
228 } else {
229 /* return to windowed mode and move the window back to its original position */
230 SDL_SetWindowFullscreen(win, 0);
231 SDL_SetWindowPosition(win, prev_x, prev_y);
232 }
233 }
235 void display(void)
236 {
237 int i;
238 ovrMatrix4f proj;
239 ovrPosef pose[2];
240 float rot_mat[16];
242 /* the drawing starts with a call to ovrHmd_BeginFrame */
243 ovrHmd_BeginFrame(hmd, 0);
245 /* start drawing onto our texture render target */
246 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
247 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
249 /* for each eye ... */
250 for(i=0; i<2; i++) {
251 ovrEyeType eye = hmd->EyeRenderOrder[i];
253 /* -- viewport transformation --
254 * setup the viewport to draw in the left half of the framebuffer when we're
255 * rendering the left eye's view (0, 0, width/2, height), and in the right half
256 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
257 */
258 glViewport(eye == ovrEye_Left ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
260 /* -- projection transformation --
261 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
262 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
263 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
264 */
265 proj = ovrMatrix4f_Projection(hmd->DefaultEyeFov[eye], 0.5, 500.0, 1);
266 glMatrixMode(GL_PROJECTION);
267 glLoadTransposeMatrixf(proj.M[0]);
269 /* -- view/camera transformation --
270 * we need to construct a view matrix by combining all the information provided by the oculus
271 * SDK, about the position and orientation of the user's head in the world.
272 */
273 /* TODO: use ovrHmd_GetEyePoses out of the loop instead */
274 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye);
275 glMatrixMode(GL_MODELVIEW);
276 glLoadIdentity();
277 glTranslatef(eye_rdesc[eye].HmdToEyeViewOffset.x,
278 eye_rdesc[eye].HmdToEyeViewOffset.y,
279 eye_rdesc[eye].HmdToEyeViewOffset.z);
280 /* retrieve the orientation quaternion and convert it to a rotation matrix */
281 quat_to_matrix(&pose[eye].Orientation.x, rot_mat);
282 glMultMatrixf(rot_mat);
283 /* translate the view matrix with the positional tracking */
284 glTranslatef(-pose[eye].Position.x, -pose[eye].Position.y, -pose[eye].Position.z);
285 /* move the camera to the eye level of the user */
286 glTranslatef(0, -ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, 1.65), 0);
288 /* finally draw the scene for this eye */
289 draw_scene();
290 }
292 /* after drawing both eyes into the texture render target, revert to drawing directly to the
293 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
294 * compensated for lens distortion and chromatic abberation onto the HMD screen.
295 */
296 glBindFramebuffer(GL_FRAMEBUFFER, 0);
297 glViewport(0, 0, win_width, win_height);
299 ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
301 assert(glGetError() == GL_NO_ERROR);
302 }
304 void draw_scene(void)
305 {
306 int i;
307 float grey[] = {0.8, 0.8, 0.8, 1};
308 float col[] = {0, 0, 0, 1};
309 float lpos[][4] = {
310 {-8, 2, 10, 1},
311 {0, 15, 0, 1}
312 };
313 float lcol[][4] = {
314 {0.8, 0.8, 0.8, 1},
315 {0.4, 0.3, 0.3, 1}
316 };
318 for(i=0; i<2; i++) {
319 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
320 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
321 }
323 glMatrixMode(GL_MODELVIEW);
325 glPushMatrix();
326 glTranslatef(0, 10, 0);
327 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
328 glBindTexture(GL_TEXTURE_2D, chess_tex);
329 glEnable(GL_TEXTURE_2D);
330 draw_box(30, 20, 30, -1.0);
331 glDisable(GL_TEXTURE_2D);
332 glPopMatrix();
334 for(i=0; i<4; i++) {
335 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
336 glPushMatrix();
337 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
338 draw_box(0.5, 2, 0.5, 1.0);
339 glPopMatrix();
341 col[0] = i & 1 ? 1.0 : 0.3;
342 col[1] = i == 0 ? 1.0 : 0.3;
343 col[2] = i & 2 ? 1.0 : 0.3;
344 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
346 glPushMatrix();
347 if(i & 1) {
348 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
349 } else {
350 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
351 }
352 draw_box(0.5, 0.5, 0.5, 1.0);
353 glPopMatrix();
354 }
356 col[0] = 1;
357 col[1] = 1;
358 col[2] = 0.4;
359 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
360 draw_box(0.05, 1.2, 6, 1.0);
361 draw_box(6, 1.2, 0.05, 1.0);
362 }
364 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
365 {
366 glMatrixMode(GL_MODELVIEW);
367 glPushMatrix();
368 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
370 if(norm_sign < 0.0) {
371 glFrontFace(GL_CW);
372 }
374 glBegin(GL_QUADS);
375 glNormal3f(0, 0, 1 * norm_sign);
376 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
377 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
378 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
379 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
380 glNormal3f(1 * norm_sign, 0, 0);
381 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
382 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
383 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
384 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
385 glNormal3f(0, 0, -1 * norm_sign);
386 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
387 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
388 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
389 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
390 glNormal3f(-1 * norm_sign, 0, 0);
391 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
392 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
393 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
394 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
395 glEnd();
396 glBegin(GL_TRIANGLE_FAN);
397 glNormal3f(0, 1 * norm_sign, 0);
398 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
399 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
400 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
401 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
402 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
403 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
404 glEnd();
405 glBegin(GL_TRIANGLE_FAN);
406 glNormal3f(0, -1 * norm_sign, 0);
407 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
408 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
409 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
410 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
411 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
412 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
413 glEnd();
415 glFrontFace(GL_CCW);
416 glPopMatrix();
417 }
419 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
420 void update_rtarg(int width, int height)
421 {
422 if(!fbo) {
423 /* if fbo does not exist, then nothing does... create every opengl object */
424 glGenFramebuffers(1, &fbo);
425 glGenTextures(1, &fb_tex);
426 glGenRenderbuffers(1, &fb_depth);
428 glBindTexture(GL_TEXTURE_2D, fb_tex);
429 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
430 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
431 }
433 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
435 /* calculate the next power of two in both dimensions and use that as a texture size */
436 fb_tex_width = next_pow2(width);
437 fb_tex_height = next_pow2(height);
439 /* create and attach the texture that will be used as a color buffer */
440 glBindTexture(GL_TEXTURE_2D, fb_tex);
441 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
442 GL_RGBA, GL_UNSIGNED_BYTE, 0);
443 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
445 /* create and attach the renderbuffer that will serve as our z-buffer */
446 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
447 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
448 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
450 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
451 fprintf(stderr, "incomplete framebuffer!\n");
452 }
454 glBindFramebuffer(GL_FRAMEBUFFER, 0);
455 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
456 }
458 int handle_event(SDL_Event *ev)
459 {
460 switch(ev->type) {
461 case SDL_QUIT:
462 return -1;
464 case SDL_KEYDOWN:
465 case SDL_KEYUP:
466 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
467 return -1;
468 }
469 break;
471 default:
472 break;
473 }
475 return 0;
476 }
478 int key_event(int key, int state)
479 {
480 if(state) {
481 /*
482 ovrHSWDisplayState hsw;
483 ovrHmd_GetHSWDisplayState(hmd, &hsw);
484 if(hsw.Displayed) {
485 ovrHmd_DismissHSWDisplay(hmd);
486 }
487 */
489 switch(key) {
490 case 27:
491 return -1;
493 case ' ':
494 /* allow the user to recenter by pressing space */
495 ovrHmd_RecenterPose(hmd);
496 break;
498 case 'f':
499 /* press f to move the window to the HMD */
500 toggle_hmd_fullscreen();
501 break;
503 default:
504 break;
505 }
506 }
507 return 0;
508 }
510 unsigned int next_pow2(unsigned int x)
511 {
512 x -= 1;
513 x |= x >> 1;
514 x |= x >> 2;
515 x |= x >> 4;
516 x |= x >> 8;
517 x |= x >> 16;
518 return x + 1;
519 }
521 /* convert a quaternion to a rotation matrix */
522 void quat_to_matrix(const float *quat, float *mat)
523 {
524 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
525 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
526 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
527 mat[12] = 0.0f;
529 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
530 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
531 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
532 mat[13] = 0.0f;
534 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
535 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
536 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
537 mat[14] = 0.0f;
539 mat[3] = mat[7] = mat[11] = 0.0f;
540 mat[15] = 1.0f;
541 }
543 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
544 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
545 {
546 int i, j;
547 unsigned int tex;
548 unsigned char img[8 * 8 * 3];
549 unsigned char *pix = img;
551 for(i=0; i<8; i++) {
552 for(j=0; j<8; j++) {
553 int black = (i & 1) == (j & 1);
554 pix[0] = (black ? r0 : r1) * 255;
555 pix[1] = (black ? g0 : g1) * 255;
556 pix[2] = (black ? b0 : b1) * 255;
557 pix += 3;
558 }
559 }
561 glGenTextures(1, &tex);
562 glBindTexture(GL_TEXTURE_2D, tex);
563 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
564 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
565 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
567 return tex;
568 }