unit ogl3d;
interface
uses opengl,types3d;

procedure CreateTexture(var Where: TTexture; Clamp: Boolean);
procedure CreateTextureAlpha(var Where: TTexture; Clamp: Boolean);
function EmptyTexture(_Width_,_Height_: GLuint): GLuint;
procedure ViewOrtho(_Width_,_Height_: GLuint);
procedure ViewPerspective;

implementation

procedure CreateTexture(var Where: TTexture; Clamp: Boolean);
var TextureID: GLuint;
begin
  glGenTextures(1,TextureID); //vytvor 1 texturu
  glBindTexture(GL_TEXTURE_2D,TextureID); //zvol texturu
  glTexImage2D(GL_TEXTURE_2D,0,3,Where.Width,Where.Height,0,GL_RGB,GL_UNSIGNED_BYTE,Where.Data); //vytvor RGB texturu

  if Clamp then begin
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  end;

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //pri zmensovani pouzivaj linearny filter
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); //aj pri zvacsovani mozes

  Where.ID:=TextureID; //nastav id textury
end;

procedure CreateTextureAlpha(var Where: TTexture; Clamp: Boolean);
var TextureID: GLuint;
begin
  glGenTextures(1,TextureID); //vytvor 1 texturu
  glBindTexture(GL_TEXTURE_2D,TextureID); //zvol texturu
  glTexImage2D(GL_TEXTURE_2D,0,4,Where.Width,Where.Height,0,GL_RGBA,GL_UNSIGNED_BYTE,Where.Data); //vytvor RGB texturu

  if Clamp then begin
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  end;

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //pri zmensovani pouzivaj linearny filter
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); //aj pri zvacsovani mozes

  Where.ID:=TextureID; //nastav id textury
end;

function EmptyTexture(_Width_,_Height_: GLuint): GLuint;
var TextureID: GLuint;
    Data: PGLuint;
function AllocMem(Size: Cardinal): Pointer;
begin
  GetMem(Result, Size);
  FillChar(Result^, Size, 0);
end;
begin
  Data:=AllocMem(_Width_*_Height_*4*SizeOf(GLuint));
  glGenTextures(1,TextureID);
  glBindTexture(GL_TEXTURE_2D,TextureID);
  glTexImage2D(GL_TEXTURE_2D,0,4,_Width_,_Height_,0,GL_RGBA,GL_UNSIGNED_BYTE,Data);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  FreeMem(Data);
  Result:=TextureID;
end;

procedure ViewOrtho(_Width_,_Height_: GLuint); //zapne ortograficke zobrazenie
begin
  glMatrixMode(GL_PROJECTION);        //projekcna matica
  glPushMatrix();                     //ulozi maticu
  glLoadIdentity();                   //resetuje maticu
  glOrtho(0,_Width_,_Height_,0,-1,1); //pravouhla projekcia
  glMatrixMode(GL_MODELVIEW);         //modelview matica
  glPushMatrix();                     //ulozi maticu
  glLoadIdentity()                    //obnovi maticu
end;

procedure ViewPerspective; //obnovi perspektivne zobrazenie
begin
  glMatrixMode(GL_PROJECTION); //projekcna matica
  glPopMatrix();               //obnovi maticu
  glMatrixMode(GL_MODELVIEW);  //modelview matica
  glPopMatrix();               //obnovi maticu
end;

end.
