Einzelnen Beitrag anzeigen

Benutzerbild von ich2
ich2

Registriert seit: 7. Dez 2005
Ort: Würzburg
54 Beiträge
 
#1

OpenGL Texture -Problem

  Alt 10. Mär 2011, 12:09
Hallo zusammen...

Habe da ein kleines Problem mit Texturen in OpenGL.
Die Texturen werden geladen, aber dennoch nicht angezeigt...
vielleicht habe ich ja was übersehen...

also zuerst die ganzen Parameter initialisieren
Delphi-Quellcode:
function Init_OpenGL ( _handle: THandle; _width, _height: Word ): Boolean;
var
  TMUs: Integer;
begin

  try
    // init the camera
    Camera := Init_3D_TRS;
    Camera._tx := 0;
    Camera._ty := -0.7;
    Camera._tz := -7;

    // Setup OpenGL
    DC := GetDC ( _handle );
    RC := CreateRenderingContext ( DC,
                                   [ opDoubleBuffered ],
                                   32, // ColorBits
                                   24, // ZBits
                                   0, // StencilBits
                                   0, // AccumBits
                                   0, // AuxBuffer
                                   0 ); // Layer
    ActivateRenderingContext ( DC, RC );

    // Check, if the GFX-Card supports Multitexturing
    glGetIntegerv ( GL_MAX_TEXTURE_UNITS, @TMUs );
    if TMUs < 2 then begin
      console ( 'no support for multitexturing' );
    end;

    // Enable 2D Textures
    glEnable ( GL_TEXTURE_2D );
    // Enable Depth Test
    glEnable ( GL_DEPTH_TEST );
    // Depth Buffer Setup
    glClearDepth ( 1.0 );
    // Set Detph Function
    glDepthFunc ( GL_LEQUAL );
    // Qualitativ bessere Koordinaten Interpolation
    glHint ( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

    // Set Clear Color = black
    glClearColor ( 0, 0, 0, 0 );

    // set screen-size
    Resize_OpenGL ( _width, _height );

    // create font
    CreateFont ( 0.01 );

    // clean the texture-puffer
    Init_Textures;

    // create Quadric
    MySphere := gluNewQuadric();

    // Set OpenGL initialized
    OpenGL_Initialized := true;
    result := true;
  except
    OpenGL_Initialized := false;
    result := false;
    exit;
  end;
end; // <- Init_OpenGL
Delphi-Quellcode:
function Init_Textures: Boolean;
var
  Counter: Word;
begin
  try
    // init the texture-puffer
    for Counter := 1 to TEXTURESMAX do
      if Textures [ Counter ] <> nil then begin
        Textures [ Counter ].Free;
        Textures [ Counter ] := nil;
      end;

    result := true;
  except
    result := false;
    exit;
  end;
end; // <- Init_Textures
Das Array Textures sieht folgendermaßen aus...

textures: array [ 1..TEXTURESMAX ] of TGLBmp; Dann werden mal ein paar Texturen geladen...(alles 512x512 jpg-files)

Delphi-Quellcode:
  Load_Texture ( textures [ 1 ], EPath + '\media\yx_values_13080Hz_abs.bin.jpg' );//, true, true );
  Load_Texture ( textures [ 2 ], EPath + '\media\yz_values_13080Hz_abs.bin.jpg' );//, true, true );
  Load_Texture ( textures [ 3 ], EPath + '\media\zx_values_13080Hz_abs.bin.jpg' );//, true, true );

  Load_Texture ( textures [ 4 ], EPath + '\media\yx_values_13080Hz_im.bin.jpg' );//, true, true );
  Load_Texture ( textures [ 5 ], EPath + '\media\yz_values_13080Hz_im.bin.jpg' );//, true, true );
  Load_Texture ( textures [ 6 ], EPath + '\media\zx_values_13080Hz_im.bin.jpg' );//, true, true );

  Load_Texture ( textures [ 7 ], EPath + '\media\yx_values_13080Hz_re.bin.jpg' );//, true, true );
  Load_Texture ( textures [ 8 ], EPath + '\media\yz_values_13080Hz_re.bin.jpg' );//, true, true );
  Load_Texture ( textures [ 9 ], EPath + '\media\zx_values_13080Hz_re.bin.jpg' );//, true, true );
das Load_Texture sieht so aus:

Delphi-Quellcode:
function Load_Texture ( var _tex: TGLBmp; _filename: String; _s3tc: Boolean = false; _ani: Boolean = false ): Boolean; overload;
begin
  try
    if _tex <> nil then
      _tex.Free;

    _tex := TGLBmp.Create ( _filename );
    _tex.GenTexture ( _s3tc, _ani );

    result := true;
  except
    result := false;
    exit;
  end;
end; // <- Load_Texture
TGLBmp.Create ( _filename ); ...sollte funktionieren...da macht er nichts anderes als die texturedaten einlesen
GenTexture (); ...ist eigentlich auch fertig und sollte tun...aber naja...

Delphi-Quellcode:
procedure TGLBMP.GenTexture(pS3TC, pAni : Boolean);
var
 MaxAnisotropy : TGLUInt;
begin
  if Assigned(rgbBits) then begin
    // If a texture object has already been created, delete it
    if TextureID > 0 then
      glDeleteTextures(1, @TextureID);

    // Create a new OpenGL texture object
    glGenTextures(1, @TextureID);
    glBindTexture(GL_TEXTURE_2D, TextureID);

    // Set up parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texWrapS);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texWrapT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);

    // Zusatz : Anisotropisches Filtern
{!} if pAni then
{!}  begin
{!}  glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropy);
{!}  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MaxAnisotropy);
{!}  end
{!} else
{!}  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0);
    // MipMaps generieren, wenn möglich im komprimierten S3TC-Format
{!} if pS3TC then
{!}  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, PByte(rgbBits))
{!} else
{!}  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, PByte(rgbBits))
  end;
end;
...so zu guter letzt die Renderei...
Delphi-Quellcode:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if not begin_Render then exit;

  // Switch to 2D mode
  OpenGL_2D;

// Show_Line2D ( Vector2D ( 1, 1 ), Vector2D ( 200, 10 ), FloatColorAlpha ( 0, 0, 1, 1 ) );


  // Switch to 3D mode
  OpenGL_3D;
  inc ( count );

  set_globalambient ( 0.5, 0.5, 0.5, 1 );


  Show_TexturedQuad3D ( Get_3D_TRS ( 0, 0, 0, 0, 0, 0, 2, 2, 2 ),
                        FloatColorAlpha ( 1, 1, 1, 0.7 ), 3*textureindex-2 );
  Show_TexturedQuad3D ( Get_3D_TRS ( 0, 0, 0, 0, 90, 0, 2, 2, 2 ),
                        FloatColorAlpha ( 1, 1, 1, 0.7 ), 3*textureindex-1 );
  Show_TexturedQuad3D ( Get_3D_TRS ( 0, 0, 0, 90, 0, 0, 2, 2, 2 ),
                        FloatColorAlpha ( 1, 1, 1, 0.7 ), 3*textureindex-0 );



  end_Render;
end;
Delphi-Quellcode:
function begin_Render: Boolean;
begin
  if not OpenGL_Initialized then begin
    result := false;
    exit
  end;

  // Clearscreen
  glClear ( GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT );
  glViewport ( 0, 0, Screen_Width, Screen_Height );
  glMatrixMode ( GL_MODELVIEW );
  glLoadIdentity;

  // set to camera-view
  Compute_Camera;

  result := true;
end; // <- begin_Render

procedure OpenGL_2D;
begin
  glMatrixMode ( GL_PROJECTION );
  glPushMatrix (); // Store The Projection Matrix
  glLoadIdentity (); // Reset The Projection Matrix
  glOrtho ( 0, Screen_width, 0, Screen_height, -1, 1 ); // Set Up An Ortho Screen
  glMatrixMode ( GL_MODELVIEW );
  glPushMatrix (); // Store old Modelview Matrix
  glLoadIdentity (); // Reset The Modelview Matrix
end; // <- go2d

procedure OpenGL_3D;
begin
  glMatrixMode ( GL_PROJECTION );
  glPopMatrix (); // Restore old Projection Matrix
  glMatrixMode ( GL_MODELVIEW );
  glPopMatrix (); // Restore old Projection Matrix
end; // <- exit2d

procedure end_Render;
begin
  // Redraw screen
  SwapBuffers ( dc );
end; // <- end_Render

procedure Show_TexturedQuad3D ( _3D_TRS: T3D_TRS;
                                Color: TFloatColorAlpha;
                                _tex_Index: Word ); overload;
begin
  if _tex_Index < 1 then
    Show_TexturedQuad3D ( _3D_TRS, Color, nil )
  else
    Show_TexturedQuad3D ( _3D_TRS, Color, Textures [ _tex_Index ] );
end; // <- Show_TexturedQuad3D

procedure Show_TexturedQuad3D ( _3D_TRS: T3D_TRS;
                                Color: TFloatColorAlpha;
                                _tex: TGLBmp ); overload;
var
  C1, C2, C3, C4: TVector3D;
begin
  C1 := Vector3D ( -1, 1, 0 );
  C2 := Vector3D ( 1, 1, 0 );
  C3 := Vector3D ( 1, -1, 0 );
  C4 := Vector3D ( -1, -1, 0 );

  glEnable ( GL_BLEND );
  glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

  glPushMatrix;

  glTranslatef ( _3D_TRS._tx, _3D_TRS._ty, _3D_TRS._tz );

  glRotatef ( _3D_TRS._rx, 1, 0, 0 );
  glRotatef ( _3D_TRS._ry, 0, 1, 0 );
  glRotatef ( _3D_TRS._rz, 0, 0, 1 );

  glScalef ( _3D_TRS._sx, _3D_TRS._sy, _3D_TRS._sz );

  if _tex <> nil then
    _tex.Bind
  else
    glBindTexture ( GL_TEXTURE_2D, 0 );
 
  glcolor4f ( Color.r, Color.g, Color.b, Color.a );

  glbegin ( GL_QUADS );
    glTexCoord2f ( 0, 1 );
    glvertex3f ( C1.x, C1.y, C1.z );
    glTexCoord2f ( 1, 1 );
    glvertex3f ( C2.x, C2.y, C2.z );
    glTexCoord2f ( 1, 0 );
    glvertex3f ( C3.x, C3.y, C3.z );
    glTexCoord2f ( 0, 0 );
    glvertex3f ( C4.x, C4.y, C4.z );
  glend;

  glPopMatrix;

  glDisable ( GL_BLEND );
end; // <- Show_TexturedQuad3D
...so weit so gut...

grüße
Angehängte Dateien
Dateityp: rar Test04.rar (1,12 MB, 14x aufgerufen)
Wissen ist Macht. Das ändert aber so gut wie nichts an der Übermacht der Dummheit.
  Mit Zitat antworten Zitat