王
王心惟
Unregistered / Unconfirmed
GUEST, unregistred user!
本人在学习OpenGl时,按书上例子却无法得到正确的纹理(其实纹理根本未显示),代码
如下,盼高手答之!:->
procedure TForm1.minit;//建立内存纹理图案
var
i,j,c1,c2,c:integer;
r,g,b:byte;
begin
for i:=0 to 63 do
for j:=0 to 63 do
begin
if (i and $8)=0 then c1:=$ffff
else c1:=0;
if (j and $8)=0 then c2:=$ffff
else c2:=0;
c:=(c1 xor c2) and $ffff;
checkimage[i,j,0]:=Byte(c);
checkimage[i,j,1]:=Byte(c);
checkimage[i,j,2]:=Byte(c);
end;
end;
procedure TForm1.Initialize(width:GLsizei;height:GLsizei);//初始化
begin
glClearColor(0,0,0,0);
glClearDepth(1);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_DEPTH_TEST);
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
gluPerspective(45,1.0*width/(1.0*height),1,20);
minit;
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2d(GL_TEXTURE_2D,0,1,64,64,0,GL_RGB,GL_UNSIGNED_BYTE,@checkimage);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_BLEND);
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_DECAL);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
end;
procedure TForm1.DrawScene;//绘制
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glPushMatrix;
glTranslatef(0,0,-5);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(-2,-1,0);
glTexCoord2f(0,1);
glVertex3f(-2,1,0);
glTexCoord2f(1,1);
glVertex3f(0,1,0);
glTexCoord2f(1,0);
glVertex3f(0,-1,0);
glEnd;
glPopMatrix;
glFlush;
SwapBuffers(ghdc);
end;
procedure TForm1.FormPaint(Sender: TObject);//WM_ONPAINT
begin
DrawScene;
end;
如下,盼高手答之!:->
procedure TForm1.minit;//建立内存纹理图案
var
i,j,c1,c2,c:integer;
r,g,b:byte;
begin
for i:=0 to 63 do
for j:=0 to 63 do
begin
if (i and $8)=0 then c1:=$ffff
else c1:=0;
if (j and $8)=0 then c2:=$ffff
else c2:=0;
c:=(c1 xor c2) and $ffff;
checkimage[i,j,0]:=Byte(c);
checkimage[i,j,1]:=Byte(c);
checkimage[i,j,2]:=Byte(c);
end;
end;
procedure TForm1.Initialize(width:GLsizei;height:GLsizei);//初始化
begin
glClearColor(0,0,0,0);
glClearDepth(1);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_DEPTH_TEST);
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
gluPerspective(45,1.0*width/(1.0*height),1,20);
minit;
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2d(GL_TEXTURE_2D,0,1,64,64,0,GL_RGB,GL_UNSIGNED_BYTE,@checkimage);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_BLEND);
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_DECAL);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
end;
procedure TForm1.DrawScene;//绘制
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glPushMatrix;
glTranslatef(0,0,-5);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(-2,-1,0);
glTexCoord2f(0,1);
glVertex3f(-2,1,0);
glTexCoord2f(1,1);
glVertex3f(0,1,0);
glTexCoord2f(1,0);
glVertex3f(0,-1,0);
glEnd;
glPopMatrix;
glFlush;
SwapBuffers(ghdc);
end;
procedure TForm1.FormPaint(Sender: TObject);//WM_ONPAINT
begin
DrawScene;
end;