380 |
380 |
381 SetTextureParameters(true); |
381 SetTextureParameters(true); |
382 end; |
382 end; |
383 |
383 |
384 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
384 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
385 var modeIntFormat: LongInt; |
385 var tw, th, x, y: Longword; |
386 modeFormat: LongInt; |
386 tmpp: pointer; |
387 tw, th, x, y: Longword; |
387 fromP4, toP4: PLongWordArray; |
388 tmpp: pointer; |
|
389 fromP4, toP4: PLongWordArray; |
|
390 fromP1, toP1: PByteArray; |
|
391 begin |
388 begin |
392 new(Surface2Tex); |
389 new(Surface2Tex); |
393 Surface2Tex^.w:= surf^.w; |
390 Surface2Tex^.w:= surf^.w; |
394 Surface2Tex^.h:= surf^.h; |
391 Surface2Tex^.h:= surf^.h; |
395 |
392 |
396 if (surf^.format^.BytesPerPixel = 3) then |
393 if (surf^.format^.BytesPerPixel <> 4) then |
397 begin |
394 begin |
398 modeIntFormat:= GL_RGB; |
395 TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true); |
399 modeFormat:= GL_RGB; |
396 Surface2Tex^.id:= 0; |
400 end |
397 exit |
401 else |
398 end; |
402 if (surf^.format^.BytesPerPixel = 4) then |
399 |
403 begin |
|
404 modeIntFormat:= GL_RGBA; |
|
405 modeFormat:= GL_RGBA; |
|
406 end |
|
407 else |
|
408 begin |
|
409 TryDo(false, 'Surface2Tex: BytesPerPixel not in [3, 4]', true); |
|
410 Surface2Tex^.id:= 0; |
|
411 exit |
|
412 end; |
|
413 |
400 |
414 glGenTextures(1, @Surface2Tex^.id); |
401 glGenTextures(1, @Surface2Tex^.id); |
415 |
402 |
416 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
403 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
417 |
404 |
418 if SDL_MustLock(surf) then |
405 if SDL_MustLock(surf) then |
419 SDLTry(SDL_LockSurface(surf) >= 0, true); |
406 SDLTry(SDL_LockSurface(surf) >= 0, true); |
420 |
407 |
421 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
408 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
422 begin |
409 begin |
423 tw:= toPowerOf2(Surf^.w); |
410 tw:= toPowerOf2(Surf^.w); |
424 th:= toPowerOf2(Surf^.h); |
411 th:= toPowerOf2(Surf^.h); |
425 |
412 |
426 Surface2Tex^.rx:= Surf^.w / tw; |
413 Surface2Tex^.rx:= Surf^.w / tw; |
427 Surface2Tex^.ry:= Surf^.h / th; |
414 Surface2Tex^.ry:= Surf^.h / th; |
428 |
415 |
429 GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
416 GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
430 |
417 |
431 if surf^.format^.BytesPerPixel = 4 then |
|
432 begin |
|
433 fromP4:= Surf^.pixels; |
418 fromP4:= Surf^.pixels; |
434 toP4:= tmpp; |
419 toP4:= tmpp; |
435 |
420 |
436 for y:= 0 to Pred(Surf^.h) do |
421 for y:= 0 to Pred(Surf^.h) do |
437 begin |
422 begin |
438 for x:= 0 to Pred(Surf^.w) do |
423 for x:= 0 to Pred(Surf^.w) do toP4^[x]:= fromP4^[x]; |
439 toP4^[x]:= fromP4^[x]; |
424 for x:= Surf^.w to Pred(tw) do toP4^[x]:= 0; |
440 for x:= Surf^.w to Pred(tw) do |
|
441 toP4^[x]:= 0; |
|
442 toP4:= @(toP4^[tw]); |
425 toP4:= @(toP4^[tw]); |
443 fromP4:= @(fromP4^[Surf^.pitch div 4]); |
426 fromP4:= @(fromP4^[Surf^.pitch div 4]); |
444 end; |
427 end; |
445 |
428 |
446 for y:= Surf^.h to Pred(th) do |
429 for y:= Surf^.h to Pred(th) do |
447 begin |
430 begin |
448 for x:= 0 to Pred(tw) do |
431 for x:= 0 to Pred(tw) do toP4^[x]:= 0; |
449 toP4^[x]:= 0; |
|
450 toP4:= @(toP4^[tw]); |
432 toP4:= @(toP4^[tw]); |
451 end; |
|
452 end |
|
453 else |
|
454 begin |
|
455 fromP1:= Surf^.pixels; |
|
456 toP1:= tmpp; |
|
457 |
|
458 for y:= 0 to Pred(Surf^.h) do |
|
459 begin |
|
460 for x:= 0 to Pred(Surf^.w) do |
|
461 begin |
|
462 toP1^[x * 3]:= fromP1^[x * 3]; |
|
463 toP1^[x * 3 + 1]:= fromP1^[x * 3 + 1]; |
|
464 toP1^[x * 3 + 2]:= fromP1^[x * 3 + 2]; |
|
465 end; |
|
466 for x:= Surf^.w to Pred(tw) do |
|
467 begin |
|
468 toP1^[x * 3]:= 0; |
|
469 toP1^[x * 3 + 1]:= 0; |
|
470 toP1^[x * 3 + 2]:= 0; |
|
471 end; |
|
472 toP1:= @(toP1^[tw * 3]); |
|
473 fromP1:= @(fromP1^[Surf^.pitch]); |
|
474 end; |
|
475 |
|
476 for y:= Surf^.h to Pred(th) do |
|
477 begin |
|
478 for x:= 0 to Pred(tw) do |
|
479 begin |
|
480 toP1^[x * 3]:= 0; |
|
481 toP1^[x * 3 + 1]:= 0; |
|
482 toP1^[x * 3 + 2]:= 0; |
|
483 end; |
|
484 toP1:= @(toP1^[tw * 3]); |
|
485 end; |
|
486 end; |
433 end; |
487 |
434 |
488 // legacy resizing function |
435 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
489 // gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE, Surf^.pixels, tw, th, GL_UNSIGNED_BYTE, tmpp); |
|
490 |
|
491 glTexImage2D(GL_TEXTURE_2D, 0, modeIntFormat, tw, th, 0, modeFormat, GL_UNSIGNED_BYTE, tmpp); |
|
492 |
436 |
493 FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
437 FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
494 end else |
438 end |
495 begin |
439 else |
|
440 begin |
496 Surface2Tex^.rx:= 1.0; |
441 Surface2Tex^.rx:= 1.0; |
497 Surface2Tex^.ry:= 1.0; |
442 Surface2Tex^.ry:= 1.0; |
498 glTexImage2D(GL_TEXTURE_2D, 0, modeIntFormat, surf^.w, surf^.h, 0, modeFormat, GL_UNSIGNED_BYTE, surf^.pixels); |
443 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
499 end; |
444 end; |
500 |
445 |
501 ResetVertexArrays(Surface2Tex); |
446 ResetVertexArrays(Surface2Tex); |
502 |
447 |
503 if SDL_MustLock(surf) then |
448 if SDL_MustLock(surf) then |
504 SDL_UnlockSurface(surf); |
449 SDL_UnlockSurface(surf); |