質問させてください。
OpenGLでテクスチャマッピングを実現したくunitにGL.pasを追加して
数サイトからのサンプルを参考に作ってみたもののうまくマッピングできません。
どこに問題があるのか皆さんお助けを…。
以下ソース抜粋(少々長いです)
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, GL, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private 宣言 }
DC: HDC; {デバイスコンテキスト}
RC: HGLRC; {レンダリングコンテキスト}
public
{ Public 宣言 }
procedure disp;
procedure InitDraw;
end;
const
TEXSIZE = 64;
var
Form1: TForm1;
texName: string = 'test';
bits: array[0..TEXSIZE - 1, 0..TEXSIZE - 1, 0..2] of GLubyte;
pfd : Windows.TPixelFormatDescriptor = (
nSize: sizeof(TPixelFormatDescriptor); {構造体のサイズ}
nVersion: 1; {構造体のバージョン番号、現在は1}
dwFlags: PFD_DRAW_TO_WINDOW {特性フラグ}
or PFD_SUPPORT_OPENGL
or PFD_DOUBLEBUFFER;
iPixelType: PFD_TYPE_RGBA; {ピクセルのカラーデータ}
cColorBits: 24; {色のビット数}
cRedBits: 0; {RGBAカラーバッファのビット数}
cRedShift: 0; {RGBAカラーバッファのビット用シフトカウント}
cGreenBits: 0;
cGreenShift: 0;
cBlueBits: 0;
cBlueShift: 0;
cAlphaBits: 0;
cAlphaShift: 0;
cAccumBits: 0; {アキュムレーションバッファのピクセル当りのビット数}
cAccumRedBits: 0;
cAccumGreenBits: 0;
cAccumBlueBits: 0;
cAccumAlphaBits: 0;
cDepthBits: 32; {デプスバッファ のピクセル当りのビット数}
cStencilBits: 0; {ステンシルバッファのピクセル当りのビット数}
cAuxBuffers: 0; {補助バッファ Win32上は未サポート}
iLayerType: PFD_MAIN_PLANE; {Win32上ではPFD_MAIN_PLANE 以外不可}
bReserved: 0; {必ず 0}
dwLayerMask: 0; {レイヤマスク Win32上は未サポート}
dwVisibleMask: 0; {ビジブルマスク Win32上は未サポート}
dwDamageMask: 0; {ダメージマスク Win32上は未サポート}
);
implementation
{$R *.dfm}
procedure TForm1.InitDraw;
var ratio : GLdouble; {画面比率 計算結果格納用}
begin
glViewport(0, 0, ClientWidth, ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
ratio := ClientWidth / ClientHeight;
if (ClientWidth <= ClientHeight) then
glOrtho(-1.0, 1.0, -1.0 / ratio, 1.0 / ratio, -1.0, 1.0)
else
glOrtho(-1.0 * ratio, 1.0 * ratio, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
//GL.pasでは glGenTextures: procedure(n: GLsizei; textures: PGLuint); stdcall;
glGenTextures(1 , @texName[1]);
//GL.pasでは glBindTexture: procedure(target: GLenum; texture: GLuint); stdcall;
glBindTexture(GL_TEXTURE_2D , GLuint(@texName[1]));
glTexImage2D(
GL_TEXTURE_2D , 0 , 3 , TEXSIZE , TEXSIZE ,
0 , GL_RGB , GL_UNSIGNED_BYTE , @bits[0][0][0]
);
end;
procedure TForm1.disp;
begin
glClear(GL_COLOR_BUFFER_BIT);
//GL.pasでは glBindTexture: procedure(target: GLenum; texture: GLuint); stdcall;
glBindTexture(GL_TEXTURE_2D , GLuint(@texName[1]));
glBegin(GL_POLYGON);
//サンプルでは左回りになっていました
glTexCoord2f(0 , 0); glVertex2f(-0.9 , -0.9);
glTexCoord2f(0 , 1); glVertex2f(-0.9 , 0.9);
glTexCoord2f(1 , 1); glVertex2f(0.9 , 0.9);
glTexCoord2f(1 , 0); glVertex2f(0.9 , -0.9);
{これは右回り
glTexCoord2f(0 , 0); glVertex2f(-0.9 , -0.9);
glTexCoord2f(1 , 0); glVertex2f(0.9 , -0.9);
glTexCoord2f(1 , 1); glVertex2f(0.9 , 0.9);
glTexCoord2f(0 , 1); glVertex2f(-0.9 , 0.9);
}
glEnd();
glFlush();
SwapBuffers(DC);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
//Interval初期設定は100
wglMakeCurrent(DC, RC);
glRotatef(1 , 0.5 , 1 , 0.25);
InitDraw;
disp;
wglMakeCurrent(DC, 0);
Timer1.Interval := 50;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i, j: integer;
r: real;
nPixelFormat : integer;
success : boolean;
begin
//テクスチャ配列生成
for i := 0 to TEXSIZE - 1 do begin
for j := 0 to TEXSIZE - 1 do begin
r := (i * $FF) / TEXSIZE;
bits[i][j][0] := Trunc(r);
bits[i][j][1] := Trunc((( j * $FF ) / TEXSIZE));
bits[i][j][2] := Trunc($100 - r);
end;
end;
//OpenGL初期化
DC := GetDC(Handle);
nPixelFormat := ChoosePixelFormat(DC, @pfd);
success := SetPixelFormat(DC, nPixelFormat, @pfd);
RC := wglCreateContext(DC);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
wglDeleteContext(RC);
end;
end.
自己解決しました。
今後のためにサンプルのあるページを書き込みます。
http://www.sulaco.co.za/opengl.htm
クマガデルゾー さん
>今後のためにサンプルのあるページを書き込みます。
OpenGLは全然わかりませんが
ここ凄いですね。
教えていただいてありがとうございました。
ツイート | ![]() |