OpenGL v Delphi4



Lesklé předměty.


Příklad a zdroják ke stažení (339k)



V příkladu používám 'Sphere Environment Mapping' a 'Multitexturing'. Jedna textura je textura předmětu (mramor), druhá textura je textura okolí, která pomocí 'Sphere Environment Mapping'u je stále na stejném místě, to znamená, že nereaguje na pohyb předmětu a vytváří dojem lesklého povrchu.



'Sphere Environment Mapping' se inicializuje pomocí dvou příkazů.

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); 
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);



Tyto příkazy jsem napsal do procedury

procedure InitTexture;
var a:word;
    f:TGluint;
begin
     UniPicture:=TPicture.Create;
     for a:=1 to 256 do begin
         glGenTextures(1, @f);TextureBin[a]:=f;
     end;
     for a:=1 to 256 do begin
         with texture[a] do begin
            rgbacolor:=nil;
            rgbcolor:=nil;
            ColorSystem:=Gl_RGB;

            glBindTexture(GL_TEXTURE_2D,TextureBin[a]);
            glTexParameterf(GL_Texture_2D,GL_Texture_Wrap_S,gl_repeat);
            glTexParameterf(GL_Texture_2D,GL_Texture_Wrap_T,gl_repeat);
            glTexParameterf(GL_Texture_2D,GL_Texture_Mag_Filter,GL_Linear);
            glTexParameterf(GL_Texture_2D,GL_Texture_Min_Filter,GL_Linear);
            glTexEnvf(GL_Texture_Env, GL_Texture_Env_Mode, GL_MODULATE);

            glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);                 // Set The Texture Generation Mode For S To Sphere Mapping
            glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);                 // Set The Texture Generation Mode For T To Sphere Mapping

         end;
     end;
end;



Pak jen stačí před načtením textury 'Sphere Environment Mapping' zapnout
 
glEnable(GL_TEXTURE_GEN_S);               
glEnable(GL_TEXTURE_GEN_T);               

a je to.

Tyto příkazy jsem napsal do procedury

procedure DrawSphere;
var
  i,j: integer;
begin
      glPushMatrix;
    //Draw Triangles
    if SphereTriangles then begin
      glActiveTextureARB(GL_TEXTURE0_ARB);
      glEnable(GL_TEXTURE_GEN_S);               // Enable Texture Coord Generation For S ( NEW )
      glEnable(GL_TEXTURE_GEN_T);               // Enable Texture Coord Generation For T ( NEW )
      glBindTexture(GL_TEXTURE_2D, TextureBin[3]);
      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_Replace);
      glEnable(GL_TEXTURE_2D);

      glActiveTextureARB(GL_TEXTURE1_ARB);
      glDisable(GL_TEXTURE_GEN_S);              // Disable Texture Coord Generation For S ( NEW )
      glDisable(GL_TEXTURE_GEN_T);              // Disable Texture Coord Generation For S ( NEW )
      glBindTexture(GL_TEXTURE_2D, TextureBin[4]);
      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_Modulate);
      glEnable(GL_TEXTURE_2D);

      glBegin(GL_TRIANGLES);
      for i := 1 to (SpFace div 2)-2 do begin
        for j := 1 to SpFace do begin
          if j <> SpFace then begin
            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord1[i,j].x,SpCoord1[i,j].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord1[i,j].x,SpCoord1[i,j].y);
            glNormal3f(SpNormal1[i,j].x,SpNormal1[i,j].y,SpNormal1[i,j].z);
            glVertex3f(SpPoint[i,j].x,SpPoint[i,j].y,SpPoint[i,j].z);

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord2[i,j+1].x,SpCoord2[i,j+1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord2[i,j+1].x,SpCoord2[i,j+1].y);
            glNormal3f(SpNormal2[i,j+1].x,SpNormal2[i,j+1].y,SpNormal2[i,j+1].z);
            glVertex3f(SpPoint[i,j+1].x,SpPoint[i,j+1].y,SpPoint[i,j+1].z);

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord3[i+1,j].x,SpCoord3[i+1,j].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord3[i+1,j].x,SpCoord3[i+1,j].y);
            glNormal3f(SpNormal3[i+1,j].x,SpNormal3[i+1,j].y,SpNormal3[i+1,j].z);
            glVertex3f(SpPoint[i+1,j].x,SpPoint[i+1,j].y,SpPoint[i+1,j].z);

            ///////////////////////////////////////////////////////////////////

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord2[i,j+1].x,SpCoord2[i,j+1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord2[i,j+1].x,SpCoord2[i,j+1].y);
            glNormal3f(SpNormal2[i,j+1].x,SpNormal2[i,j+1].y,SpNormal2[i,j+1].z);
            glVertex3f(SpPoint[i,j+1].x,SpPoint[i,j+1].y,SpPoint[i,j+1].z);

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord3[i+1,j].x,SpCoord3[i+1,j].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord3[i+1,j].x,SpCoord3[i+1,j].y);
            glNormal3f(SpNormal3[i+1,j].x,SpNormal3[i+1,j].y,SpNormal3[i+1,j].z);
            glVertex3f(SpPoint[i+1,j].x,SpPoint[i+1,j].y,SpPoint[i+1,j].z);

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord4[i+1,j+1].x,SpCoord4[i+1,j+1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord4[i+1,j+1].x,SpCoord4[i+1,j+1].y);
            glNormal3f(SpNormal4[i+1,j+1].x,SpNormal4[i+1,j+1].y,SpNormal4[i+1,j+1].z);
            glVertex3f(SpPoint[i+1,j+1].x,SpPoint[i+1,j+1].y,SpPoint[i+1,j+1].z);

          end else begin
            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord1[i,j].x,SpCoord1[i,j].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord1[i,j].x,SpCoord1[i,j].y);
            glNormal3f(SpNormal1[i,j].x,SpNormal1[i,j].y,SpNormal1[i,j].z);
            glVertex3f(SpPoint[i,j].x,SpPoint[i,j].y,SpPoint[i,j].z);

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord2[i,1].x,SpCoord2[i,1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord2[i,1].x,SpCoord2[i,1].y);
            glNormal3f(SpNormal2[i,1].x,SpNormal2[i,1].y,SpNormal2[i,1].z);
            glVertex3f(SpPoint[i,1].x,SpPoint[i,1].y,SpPoint[i,1].z);

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord3[i+1,j].x,SpCoord3[i+1,j].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord3[i+1,j].x,SpCoord3[i+1,j].y);
            glNormal3f(SpNormal3[i+1,j].x,SpNormal3[i+1,j].y,SpNormal3[i+1,j].z);
            glVertex3f(SpPoint[i+1,j].x,SpPoint[i+1,j].y,SpPoint[i+1,j].z);

            ///////////////////////////////////////////////////////////////////

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord2[i,1].x,SpCoord2[i,1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord2[i,1].x,SpCoord2[i,1].y);
            glNormal3f(SpNormal2[i,1].x,SpNormal2[i,1].y,SpNormal2[i,1].z);
            glVertex3f(SpPoint[i,1].x,SpPoint[i,1].y,SpPoint[i,1].z);

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord3[i+1,j].x,SpCoord3[i+1,j].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord3[i+1,j].x,SpCoord3[i+1,j].y);
            glNormal3f(SpNormal3[i+1,j].x,SpNormal3[i+1,j].y,SpNormal3[i+1,j].z);
            glVertex3f(SpPoint[i+1,j].x,SpPoint[i+1,j].y,SpPoint[i+1,j].z);

            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord4[i+1,1].x,SpCoord4[i+1,1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord4[i+1,1].x,SpCoord4[i+1,1].y);
            glNormal3f(SpNormal4[i+1,1].x,SpNormal4[i+1,1].y,SpNormal4[i+1,1].z);
            glVertex3f(SpPoint[i+1,1].x,SpPoint[i+1,1].y,SpPoint[i+1,1].z);
          end;
        end;

      end;
      glEnd;

      //first point
      glBegin(GL_TRIANGLES);
        for j := 1 to SpFace do begin
          glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord1[0,j].x,SpCoord1[0,j].y);
          glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord1[0,j].x,SpCoord1[0,j].y);
          glNormal3f(SpNormal1[0,j].x,SpNormal1[0,j].y,SpNormal1[0,j].z);
          glVertex3f(SpPoint[0,j].x,SpPoint[0,j].y,SpPoint[0,j].z);

          glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord3[1,j].x,SpCoord3[1,j].y);
          glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord3[1,j].x,SpCoord3[1,j].y);
          glNormal3f(SpNormal3[1,j].x,SpNormal3[1,j].y,SpNormal3[1,j].z);
          glVertex3f(SpPoint[1,j].x,SpPoint[1,j].y,SpPoint[1,j].z);
          if j <> SpFace then begin
            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord4[1,j+1].x,SpCoord4[1,j+1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord4[1,j+1].x,SpCoord4[1,j+1].y);
            glNormal3f(SpNormal4[1,j+1].x,SpNormal4[1,j+1].y,SpNormal4[1,j+1].z);
            glVertex3f(SpPoint[1,j+1].x,SpPoint[1,j+1].y,SpPoint[1,j+1].z);
          end else begin
            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord4[1,1].x,SpCoord4[1,1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord4[1,1].x,SpCoord4[1,1].y);
            glNormal3f(SpNormal4[1,1].x,SpNormal4[1,1].y,SpNormal4[1,1].z);
            glVertex3f(SpPoint[1,1].x,SpPoint[1,1].y,SpPoint[1,1].z);
          end;
        end;
      glEnd;

      //last point
      glBegin(GL_TRIANGLES);
        for j := 1 to SpFace do begin
          glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord3[(SpFace div 2)+1,j].x,SpCoord3[(SpFace div 2)+1,j].y);
          glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord3[(SpFace div 2)+1,j].x,SpCoord3[(SpFace div 2)+1,j].y);
          glNormal3f(SpNormal3[(SpFace div 2),j].x,SpNormal3[(SpFace div 2),j].y,SpNormal3[(SpFace div 2),j].z);
          glVertex3f(SpPoint[(SpFace div 2),j].x,SpPoint[(SpFace div 2),j].y,SpPoint[(SpFace div 2),j].z);

          glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord1[(SpFace div 2),j].x,SpCoord1[(SpFace div 2),j].y);
          glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord1[(SpFace div 2),j].x,SpCoord1[(SpFace div 2),j].y);
          glNormal3f(SpNormal1[(SpFace div 2)-1,j].x,SpNormal1[(SpFace div 2)-1,j].y,SpNormal1[(SpFace div 2)-1,j].z);
          glVertex3f(SpPoint[(SpFace div 2)-1,j].x,SpPoint[(SpFace div 2)-1,j].y,SpPoint[(SpFace div 2)-1,j].z);
          if j <> SpFace then begin
            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord2[(SpFace div 2),j+1].x,SpCoord2[(SpFace div 2),j+1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord2[(SpFace div 2),j+1].x,SpCoord2[(SpFace div 2),j+1].y);
            glNormal3f(SpNormal2[(SpFace div 2)-1,j+1].x,SpNormal2[(SpFace div 2)-1,j+1].y,SpNormal2[(SpFace div 2)-1,j+1].z);
            glVertex3f(SpPoint[(SpFace div 2)-1,j+1].x,SpPoint[(SpFace div 2)-1,j+1].y,SpPoint[(SpFace div 2)-1,j+1].z);
          end else begin
            glMultiTexCoord2fARB(GL_TEXTURE0_ARB,SpCoord2[(SpFace div 2),1].x,SpCoord2[(SpFace div 2),1].y);
            glMultiTexCoord2fARB(GL_TEXTURE1_ARB,SpCoord2[(SpFace div 2),1].x,SpCoord2[(SpFace div 2),1].y);
            glNormal3f(SpNormal2[(SpFace div 2)-1,1].x,SpNormal2[(SpFace div 2)-1,1].y,SpNormal2[(SpFace div 2)-1,1].z);
            glVertex3f(SpPoint[(SpFace div 2)-1,1].x,SpPoint[(SpFace div 2)-1,1].y,SpPoint[(SpFace div 2)-1,1].z);
          end;
        end;
      glEnd;
      glDisable(GL_BLEND);
      glEnable(GL_DEPTH_TEST);
    end;
      glPopMatrix;
  end;

Tato procedura vykreslí předmět (kámen).

Zdroják pro vykreslení předmětu je stejný jako zdroják pro výpočet koule , akorát při výpočtu bodů koule, jsem pozměnil pár hodnot.

procedure InitSphere;  {Calculate Sphere Points}
var
  rVert, rHoriz: array [0..100] of double;
  Angle: double;
  i,j: integer;
begin

   Angle := 360 / SpFace;

   for i := 1 to (SpFace div 2)-1 do begin
     rVert[i] := cos((i*Angle)*(pi/180))* SpRadius;
     rHoriz[i] := sin((i*Angle)*(pi/180))* SpRadius;

     for j := 1 to SpFace do begin
       SpPoint[i,j].x := cos((j*Angle)*(pi/180)) * rHoriz[i]*2;    //Změna *2
       SpPoint[i,j].y := rVert[i];
       SpPoint[i,j].z := -sin((j*Angle)*(pi/180)) * rHoriz[i]/2;   //Změna /2

       //first point
       SpPoint[0,j].x := 0;
       SpPoint[0,j].y := SpRadius;
       SpPoint[0,j].z := 0;

       //last point
       SpPoint[SpFace div 2,j].x := 0;
       SpPoint[SpFace div 2,j].y := -SpRadius;
       SpPoint[SpFace div 2,j].z := 0;

     end;
   end;

   SphereTexCoord;
   SphereNormals;
   SphereSmoothNormals;
end;



Celej zdroják je tady
Env_f



Načítání 3D objektů ze souboru (*.dxf) (Autocad, 3D MAX...) 3D Fonty, výpočet FPS
Home