Multimidia - exemplo de uso de opengl |
Top Previous Next |
|
// exemplo completo de uso de OpenGL
unit untGLForm;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, OpenGL;
type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormResize(Sender: TObject); procedure FormPaint(Sender: TObject); private { Private declarations } FDC: HDC; // Device Context FRC: HGLRC; // Rendering Context public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
// // Inicialização! // procedure TForm1.FormCreate(Sender: TObject); var PFD: PIXELFORMATDESCRIPTOR; PixelFormat: GLuint; begin
// Cria o device context FDC := GetDC(Handle);
// formato do nosso device with pfd do begin nSize := SizeOf(PIXELFORMATDESCRIPTOR); // Tamanho do descritor nVersion := 1; // versão - sempre 1 dwFlags := PFD_DRAW_TO_WINDOW // Pode desenhar na janela or PFD_SUPPORT_OPENGL // vamos usar OpenGL! or PFD_DOUBLEBUFFER; // e double-buffer. iPixelType := PFD_TYPE_RGBA; // Formato de cor RGBA cColorBits := 32; // usando 32 bits de cor cDepthBits := 16; // Depth Buffer de 16 bits end;
PixelFormat := ChoosePixelFormat(FDC, @pfd); // Busca o formato mais próximo SetPixelFormat(FDC, PixelFormat, @pfd); // aplica ao device context
FRC := wglCreateContext(FDC); // Cria o rendering context If (FRC <> 0) Then // Se deu certo... wglMakeCurrent(FDC, FRC); // Diz qual o RC a ser usado
//*** Inicialização
glClearColor(0.0, 0.0, 0.0, 0.0); // Fundo preto glShadeModel(GL_SMOOTH); // Usa smooth shading glEnable(GL_DEPTH_TEST); // Habilita o depth buffer glEnable(GL_CULL_FACE); // Não renderiza a parte de dentro dos polígonos glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Melhora a perspectiva
end;
procedure TForm1.FormDestroy(Sender: TObject); begin wglMakeCurrent(FDC, 0); // Nosso RC não é mais o principal wglDeleteContext(FRC); // Então nos livramos dele.
If (FDC <> 0) Then ReleaseDC(Handle, FDC); // Liberamos então o DC
end;
procedure TForm1.FormResize(Sender: TObject); begin if (Height <= 0) then // Não existe divisão por zero Height := 1;
glViewport(0, 0, Width, Height); // Ajusta o campo de visão glMatrixMode(GL_PROJECTION); // Muda para matriz de Projeção glLoadIdentity(); // Inicializa // Ajusta nosso frustum gluPerspective(45.0, Width/Height, 1.0, 400.0);
glMatrixMode(GL_MODELVIEW); // Retorna para matriz principal glLoadIdentity(); // Inicializa end;
procedure TForm1.FormPaint(Sender: TObject); begin // limpa os buffers glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); // carrega a matriz dos objetos glLoadIdentity;
glTranslatef(0.0, 0.0, -5.0); // posiciona a câmera
glBegin(GL_TRIANGLES); // Vamos desenhar triângulos glVertex3f(-1.0,-1.0, 0.0); // primeiro vértice glVertex3f( 1.0,-1.0, 0.0); // segundo... glVertex3f( 0.0, 1.0, 0.0); // e terceiro glEnd;
SwapBuffers(FDC); // manda a tela da memória para a janela end;
end. |