Salut,
Je debute sous openGl et sous delphi, mais je pense avoir compris le principe general des deux.
Je travaille sous delphi 2006.
J'ai déja fait des pg pour débuter qui m'affichent des formes (triangle, carré) avec des couleurs ou des dégradé, et j'aimerais passé aux textures à partir d'une image.
J'ai fait un bout de code mais je ne vois rien et je ne sais pas d'ou ca peut venir.

Si quelqu'un a le courage d'y regarder, je le remercie d'avance.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
var
  Form1: TForm1;
  Tex:GLuint;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
var
    PixelFormat    :TPixelFormatDescriptor;
    cPixelFormat   :Integer;
    DoubleBuffer   :Boolean;
begin
     DoubleBuffer := false;
     FillChar( PixelFormat, SizeOf(PixelFormat), 0 );
     With PixelFormat Do
     Begin
          nSize      := Sizeof(TPixelFormatDescriptor);
          If DoubleBuffer Then
             dwFlags    := PFD_DOUBLEBUFFER or PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL
          Else
              dwFlags    := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
          iLayerType := PFD_MAIN_PLANE;
          iPixelType := PFD_TYPE_RGBA;
          nVersion   := 1;
          cColorBits := 16;
          CdepthBits := 16;
     End;
     glClearColor( 0.0, 0.0, 0.0, 0.0 );
     Loadtexture(ExtractFilePath(ParamStr(0))+'texture.bmp',Tex);
end;
Loadtexture(ExtractFilePath(ParamStr(0))+'texture.bmp',Tex);
fait parti d'un autre fichier que j'ai inclus.
Je détaillerai cette fonction plus bas.
ensuite vient l'évenement FormPaint qui permet d'afficher mon carré.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
procedure TForm1.FormPaint(Sender: TObject);
begin
 
Glclear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glClearColor( 0.0, 0.0, 0.0, 0.0 );
 
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
 
gluLookAt( 0.0 , 0.0 , 20.0,
           0.0 , 0.0 , 0.0,
           0.0 , 1.0 , 0.0 );
 
glPushMatrix();
glBindTexture( GL_TEXTURE_2D, Tex );
 
glBegin( GL_QUADS );
glNormal3f( 0.0, 0.0, 1.0 );
glTexCoord2f( 01, 01 );glVertex3f( -2.0, 2.0, 0.0 );
glTexCoord2f( 01, 0 );glVertex3f( -2.0,-2.0, 0.0 );
glTexCoord2f( 0, 0 );glVertex3f(  2.0,-2.0, 0.0 );
glTexCoord2f( 0, 01 );glVertex3f(  2.0, 2.0, 0.0 );
glEnd();
glPopMatrix();
glFlush;
SwapBuffers( Canvas.Handle );
 
end;
et enfin, l'évenement resize "basique"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
procedure TForm1.FormResize(Sender: TObject);
begin
     {Empêcher les division par ZERO}
     If ClientHeight = 0 Then
        ClientHeight := 1;
 
     {Spécification de l'espace disponible sur le contrôle fenêtré.}
     glViewport( 0, 0, ClientWidth, ClientHeight );
     {Rendre la matrice de project active pour définir un nouveau champs de
      vision}
     glMatrixMode( GL_PROJECTION );
     glLoadIdentity();
 
     {Création d'une nouvelle matrice de projection.}
     gluPerspective( 45, ClientWidth / ClientHeight, 0.1, 500.0 );
 
     {Rafraîchir le contenu de la fenêtre}
     Form1.Invalidate;
end;
 
end.
Comme promis, la fonction LoadTexture, mais je ne pense pas que le probleme vienne de là.
Pour dire vrai, cette partie de code n'est pas de moi.
SwapRGB permet de passer du format RGB au format GBR
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
function LoadTexture(Filename: String; var Texture : GLuint) : Boolean;
var
  FileHeader: BITMAPFILEHEADER;
  InfoHeader: BITMAPINFOHEADER;
  Palette: array of RGBQUAD;
  BitmapFile: THandle;
  BitmapLength: LongWord;
  PaletteLength: LongWord;
  ReadBytes: LongWord;
  Width, Height : Integer;
  pData : Pointer;
 
 
begin
  result :=FALSE;
    BitmapFile := CreateFile(PChar(Filename), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
    if (BitmapFile = INVALID_HANDLE_VALUE) then begin
      MessageBox(0, PChar('Error opening ' + Filename), PChar('BMP Unit'), MB_OK);
      Exit;
    end;
 
    ReadFile(BitmapFile, FileHeader, SizeOf(FileHeader), ReadBytes, nil);
    ReadFile(BitmapFile, InfoHeader, SizeOf(InfoHeader), ReadBytes, nil);
 
    PaletteLength := InfoHeader.biClrUsed;
    SetLength(Palette, PaletteLength);
    ReadFile(BitmapFile, Palette, PaletteLength, ReadBytes, nil);
    if (ReadBytes <> PaletteLength) then begin
      MessageBox(0, PChar('Error reading palette'), PChar('BMP Unit'), MB_OK);
      Exit;
    end;
 
    Width  := InfoHeader.biWidth;
    Height := InfoHeader.biHeight;
    BitmapLength := InfoHeader.biSizeImage;
    if BitmapLength = 0 then
    BitmapLength := Width * Height * InfoHeader.biBitCount Div 8;
 
    GetMem(pData, BitmapLength);
    ReadFile(BitmapFile, pData^, BitmapLength, ReadBytes, nil);
    if (ReadBytes <> BitmapLength) then begin
      MessageBox(0, PChar('Error reading bitmap data'), PChar('BMP Unit'), MB_OK);
      Exit;
    end;
    CloseHandle(BitmapFile);
 
  SwapRGB(pData, Width*Height);
 
  Texture :=CreateTexture(Width, Height, GL_RGB, pData);
  FreeMem(pData);
  result :=TRUE;
end;
Celui qui trouve, je lui fait un bisou....c'est promis