Tema: OpenGL
Ver Mensaje Individual
  #7  
Antiguo 22-03-2013
Avatar de cesarsoftware
cesarsoftware cesarsoftware is offline
Miembro
 
Registrado: nov 2006
Posts: 241
Reputación: 18
cesarsoftware Va por buen camino
Cita:
Empezado por mirkogonzales Ver Mensaje
Si ya las he leido y si alguien me podria orientar se los agradeceria. He leido varios tutoriales y libritos (ademas de foros en otros idiomas) pero no le encuentro solución. Gracias y saludos.
En fin, mirko, tendre que echante una mano

En un nuevo formulario añade el uses "OpenGL" y a partir del codigo que muestro tendras que trabajar un poco, pero la base ya te la doy echa. Basicamente lo que hace es iniciar el contexto opengl, cerrando y diseñar el "cubo" de trabajo, espero que te sirva.

Código Delphi [-]
uses Form, OpenGl, etc.

procedure TfrmOpenGl.glInicia;
var
  PixelFormat : Integer;
  pfd : PIXELFORMATDESCRIPTOR;
begin
  // Leer el dispositivo de contexto en la ventana
  h_DC := GetDC(frmOpenGl.Handle);
  if h_DC = NULL then begin
    Application.MessageBox('No se ha asignado contexto de formulario',
                           'frmOpenGl', MB_OK or MB_ICONERROR);
    Exit;
  end;
  // Poner todos los campos de pixelformatdescriptor a cero
  ZeroMemory(@pfd, SizeOf(pfd));
  // Inicializar solo los campos necesarios
  pfd.nSize       := SizeOf(PIXELFORMATDESCRIPTOR);
  pfd.nVersion    := 1;
  pfd.dwFlags     := PFD_DRAW_TO_WINDOW
                     or PFD_SUPPORT_OPENGL
                     or PFD_DOUBLEBUFFER;
  pfd.iPixelType  := PFD_TYPE_RGBA;
  pfd.cColorBits  := 16;
  pfd.cDepthBits  := 16;
  // Comprobar que se soporta el descriptor de pixeles
  PixelFormat := ChoosePixelFormat(h_DC, @pfd);
  if (PixelFormat = 0) then begin
    glAcaba();
    MessageBox(0, 'No puedo cambiar el descriptor de pixeles', 'Error',
               MB_OK or MB_ICONERROR);
    Exit;
  end;
  // Especificar el descriptor de pixeles
  if (not SetPixelFormat(h_DC, PixelFormat, @pfd)) then begin
    glAcaba();
    MessageBox(0, 'No puedo definir un formato de pixeles', 'Error',
               MB_OK or MB_ICONERROR);
    Exit;
  end;
  // Crear el contexto de generación OpenGl sobre el formulario/ventana
  h_RC := wglCreateContext(h_DC);
  if (h_RC = 0) then begin
    glAcaba();
    MessageBox(0, 'No puedo crear el contexto OpenGL sobre el formulario',
               'Error', MB_OK or MB_ICONERROR);
    Exit;
  end;
  // Activar el contexto de generación OpenGl sobre la ventana
  if (not wglMakeCurrent(h_DC, h_RC)) then begin
    glAcaba();
    MessageBox(0, 'No puedo activar el contexto de generación OpenGl', 'Error',
               MB_OK or MB_ICONERROR);
    Exit;
  end;
  // Seleccionar la ventana de visualización
  SetForegroundWindow(frmOpenGl.Handle);
  // Activar correccion de perpectiva para dibujado rapido
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  glIniciaLuz();
end;

procedure TfrmOpenGl.glAcaba();
begin
  // Quitar la definición de contexto del formulario
  if (not wglMakeCurrent(h_DC, 0)) then begin
    MessageBox(0, 'Devolver recursos OpenGl', 'Error',
               MB_OK or MB_ICONERROR);
    h_DC := null;
  end;
  // Devolver el contexto de generación OpenGL
  if (not wglDeleteContext(h_RC)) then begin
    MessageBox(0, 'Borrar contexto OpenGl', 'Error',
               MB_OK or MB_ICONERROR);
    h_RC := 0;
  end;
end;

procedure TfrmOpenGl.glAjustaVentana();
var
  rojo, verde, azul: glDouble;
  w, h, i, c: Integer;
  newExtX, newExtY, newExtZ: glDouble;
begin
  Val(EditAreaX.Text, i, c);
  if c = 0 then extX := i;  // ancho del area de trabajo
  Val(EditAreaY.Text, i, c);
  if c = 0 then extY := i;  // alto del area de trabajo
  Val(EditAreaZ.Text, i, c);
  if c = 0 then extZ := i;  // fondo del area de trabajo
  w := frmOpenGl.Width; // ancho del formulario
  h := frmOpenGl.Height - 30; // alto del formulario - titulo
  glViewport(0, 0, w, h); // ajusta la vista a la ventana
  // factor alto, ampliación baja
  newExtX := extX; newExtY := extY; newExtZ := extZ;
  if SpinZoom.Position > 99 then SpinZoom.Position := 99;
  newExtX := newExtX - ((SpinZoom.Position * newExtX) / 100);
  newExtY := newExtY - ((SpinZoom.Position * newExtY) / 100);
  newExtZ := newExtZ - ((SpinZoom.Position * newExtZ) / 100);
  glMatrixMode(GL_PROJECTION); // la matriz activa es la de proyección
  glLoadIdentity();            // reinicia el sistema de coordenadas
  // establece el volumen de trabajo izqda, dcha, abajo, arriba, cerca, lejos
  if w <= h then begin
    glOrtho(-newExtX, newExtX,
            -newExtY * h / w, newExtY * h / w,
            -newExtZ * 100, newExtZ * 100);
  end else begin
    glOrtho(-newExtX * w / h, newExtX * w / h,
            -newExtY, newExtY,
            -newExtZ * 100, newExtZ * 100);
  end;
  glMatrixMode(GL_MODELVIEW); // la matriz activa es la del modelador
  glLoadIdentity();           // reinicia el sistema de coordenadas
  // color de fondo y borrado
  rojo := 0; verde := 0; azul := 0;
  Val(SimCfg.SpinRojo.Text, i, c);
  if c = 0 then rojo := i / 100;
  Val(SimCfg.SpinVerde.Text, i, c);
  if c = 0 then verde := i / 100;
  Val(SimCfg.SpinAzul.Text, i, c);
  if c = 0 then azul := i / 100;
  glClearColor(rojo, verde, azul, 1.0);
  glClear(GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT);
end;
__________________
Disfruta de la vida ahora, vas a estar muerto mucho tiempo.
Responder Con Cita