All pastes #95240 Raw Edit

Untitled

public text v1 · immutable
#95240 ·published 2006-07-21 21:11 UTC
rendered paste body
unit SDL_GL;

{$MODE OBJFPC}
interface
uses Sdl, Gl, sdl_image;

function SDL_GL_LoadTexture(surface: PSDL_Surface ; texcoord: PGLFloat): GLuint;
function load_texture(filename: pchar; t: integer): integer;



implementation

function power_of_two(input: integer): Integer;
var
  value: integer = 1;
begin
  while ( value < input ) do value := value shl 1;
  Result := value;
end;

function SDL_GL_LoadTexture(surface: PSDL_Surface ; texcoord: PGLFloat): GLuint;
var
  texture: GLuint;
  w, h: integer;
  image: PSDL_Surface;
  area: TSDL_Rect;
  saved_flags: LongWord;
  saved_alpha: byte;
begin
    //* Use the surface width and height expanded to powers of 2 */
    w := power_of_two(surface^.w);
    h := power_of_two(surface^.h);
    texcoord[0] := 0.0;//f;         /* Min X */
    texcoord[1] := 0.0;//f;         /* Min Y */
    texcoord[2] := GLfloat(surface^.w) / w;  //* Max X */
    texcoord[3] := GLfloat(surface^.h) / h;  //* Max Y */

    image := SDL_CreateRGBSurface(
            SDL_SWSURFACE,
            w, h,
            32,
{$IFDEF ENDIAN_Little}///* OpenGL RGBA masks */
            $000000FF,
            $0000FF00,
            $00FF0000,
            $FF000000
{$ELSE}
            $FF000000,
            $00FF0000,
            $0000FF00,
            $000000FF
{$ENDIF}
               );
    if ( image = nil ) then exit(0);

    //* Save the alpha blending attributes */
    saved_flags := surface^.flags and (SDL_SRCALPHA or SDL_RLEACCELOK);
   saved_alpha := 0;//surface^.format^.alpha;
   // if ( (saved_flags and SDL_SRCALPHA) = SDL_SRCALPHA ) then
    //  SDL_SetAlpha(surface, 0, 0);


    //* Copy the surface into the GL texture image */
    area.x := 0;
    area.y := 0;
    area.w := surface^.w;
    area.h := surface^.h;
    SDL_BlitSurface(surface, @area, image, @area);

    //* Restore the alpha blending attributes */
    //if ( (saved_flags and SDL_SRCALPHA) = SDL_SRCALPHA ) then
    //  SDL_SetAlpha(surface, saved_flags, saved_alpha);
    
    //* Create an OpenGL texture for the image */
    glGenTextures(1, @texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D,
             0,
             GL_RGBA,
             w, h,
             0,
             GL_RGBA,
             GL_UNSIGNED_BYTE,
             image^.pixels);
    SDL_FreeSurface(image); //* No longer needed */

    Result := texture;
end;

function load_texture(filename: pchar; t: integer): integer;
var
  tex: PSDL_Surface =nil;
  monkey: GLuint;
begin
    //exit;
	tex := IMG_Load(filename);
	if (tex = Nil) then begin
	//	fprintf(stderr, "error: IMG_Load() failed\n");
	//	return -1;
        end;

	//* We shouldn't need to lock it, but whatever */
	if (SDL_MUSTLOCK(tex)) then
		SDL_LockSurface(tex);

		//glActiveTextureARB_ptr(GL_TEXTURE0_ARB);
		glEnable(GL_TEXTURE_2D);
		glGenTextures(1, @monkey);
		glBindTexture(GL_TEXTURE_2D, monkey);


	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	{if(t <> 2) then
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
	else
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);}

//if (tex^.format^.BytesPerPixel = 4) then
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex^.w, tex^.h, 0,
		     GL_RGBA, GL_UNSIGNED_BYTE, tex^.pixels)
//else
//		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex^.w, tex^.h, 0,
//			     GL_RGB, GL_UNSIGNED_BYTE, tex^.pixels);

	;if (SDL_MUSTLOCK(tex)) then
		SDL_UnlockSurface(tex);

	SDL_FreeSurface(tex);

	//if(glGetError())
		//return -1;

	//return 0;
 result := monkey
end;
end.