Mega Code Archive

 
Categories / Delphi / Graphic
 

Opengl ile 3 boyutlu molekül çizimi

// (c) Mahesh Venkitachalam 1997 http://home.att.net/~bighesh unit frmMain; interface uses Windows, Messages, Classes, Graphics, Forms, ExtCtrls, Controls, OpenGL; type TfrmGL = class(TForm) procedure FormCreate(Sender: TObject); procedure FormResize(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private DC: HDC; hrc: HGLRC; qobj : GLUquadricObj ; sphere, mol, cyl : GLInt; procedure MakeSphere; procedure MakeCylinder; procedure MakeMol; procedure SetDCPixelFormat; protected procedure WMPaint(var Msg: TWMPaint); message WM_PAINT; end; const cyl_amb_dif : Array [0..3] of GLFloat = (0.0,1.0,0.0,1.0); cyl_spec : Array [0..3] of GLFloat = (1.0,1.0,1.0,1.0); sph_amb_dif : Array [0..3] of GLFloat = (0.8,0.2,0.5,1.0); sph_spec : Array [0..3] of GLFloat = (1.0,1.0,1.0,1.0); var frmGL: TfrmGL; implementation uses mmSystem; {$R *.DFM} procedure TfrmGL.MakeSphere; begin sphere := glGenLists(1); glNewList(sphere,GL_COMPILE); glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@sph_amb_dif); glMaterialfv(GL_FRONT,GL_SPECULAR,@sph_spec); glMaterialf(GL_FRONT,GL_SHININESS,100.0); gluSphere(qobj,2.5,20,20); glEndList; end; procedure TfrmGL.MakeCylinder; begin cyl := glGenLists(1); glNewList(cyl,GL_COMPILE); glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@cyl_amb_dif); glMaterialfv(GL_FRONT,GL_SPECULAR,@cyl_spec); glMaterialf(GL_FRONT,GL_SHININESS,100.0); gluCylinder(qobj,0.5,0.5,10.0,20,20); glEndList; end; procedure TfrmGL.MakeMol; var i : Integer; begin mol := glGenLists(1); glNewList(mol,GL_COMPILE); glCallList(sphere); glCallList(cyl); glPushMatrix; glRotatef(270.0,1.0,0.0,0.0); For i := 0 to 3 do begin glPushMatrix; glRotatef(120.0*i,0.0,1.0,0.0); glCallList(cyl); glTranslatef(0.0,0.0,10.0); glCallList(sphere); glPopMatrix; end; glPopMatrix; glTranslatef(0.0,0.0,10.0); glPushMatrix; glRotatef(270.0,1.0,0.0,0.0); For i := 0 to 3 do begin glPushMatrix; glRotatef(60.0+120.0*i,0.0,1.0,0.0); glCallList(cyl); glTranslatef(0.0,0.0,10.0); glCallList(sphere); glPopMatrix; end; glPopMatrix; glEndList; end; procedure TfrmGL.WMPaint(var Msg: TWMPaint); var ps : TPaintStruct; aspect : GLFloat; begin BeginPaint(Handle, ps); aspect := Width / Height; glEnable(GL_SCISSOR_TEST); glScissor(0,0,round(Width/2),Height); glClearColor(0.55,0.7,0.7,0.0); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glDisable(GL_SCISSOR_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity; gluPerspective(60.0, aspect, 5.0, 70.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity; glViewport(0,0,round(Width/2),Height); glPushMatrix; gluLookAt(25.0,25.0,50.0,25.0,25.0,20.0,0.0,1.0,0.0); glTranslatef(25.0,25.0,10.0); glCallList(mol); glPopMatrix; // View 2 glEnable(GL_SCISSOR_TEST); glScissor(round(Width/2) + 1,round(Height/2) +1, round(Width/2), round(Height/2)); glClearColor(0.77,0.7,0.7,0.0); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glDisable(GL_SCISSOR_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity; gluPerspective(60.0, aspect, 5.0, 70.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity; glViewport(round(Width/2) + 1,round(Height/2) +1,round(Width/2), round(Height/2)); glPushMatrix; gluLookAt(25.0,50.0,50.0,25.0,25.0,20.0,0.0,1.0,0.0); glTranslatef(25.0,25.0,10.0); glCallList(mol); glPopMatrix; // View 3 glEnable(GL_SCISSOR_TEST); glScissor(round(Width/2) +1,0,round(Width/2),round(Height/2)); glClearColor(0.0,0.6,0.7,0.0); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glDisable(GL_SCISSOR_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity; gluPerspective (60.0, aspect, 5.0, 70.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity; glViewport(round(Width/2) +1,0,round(Width/2),round(Height/2)); glPushMatrix; gluLookAt(0.0,25.0,50.0,25.0,25.0,20.0,0.0,1.0,0.0); glTranslatef(25.0,25.0,10.0); glCallList(mol); glPopMatrix; SwapBuffers(DC); EndPaint(Handle, ps); end; procedure TfrmGL.FormCreate(Sender: TObject); begin DC := GetDC(Handle); SetDCPixelFormat; hrc := wglCreateContext(DC); wglMakeCurrent(DC, hrc); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); qObj := gluNewQuadric; gluQuadricDrawStyle(qobj,GLU_FILL); gluQuadricNormals(qobj,GLU_SMOOTH); MakeSphere; MakeCylinder; MakeMol; end; procedure TfrmGL.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin If Key = VK_ESCAPE then Close; end; procedure TfrmGL.SetDCPixelFormat; var nPixelFormat: Integer; pfd: TPixelFormatDescriptor; begin FillChar(pfd, SizeOf(pfd), 0); with pfd do begin nSize := sizeof(pfd); nVersion := 1; dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER; iPixelType:= PFD_TYPE_RGBA; cColorBits:= 24; cDepthBits:= 32; iLayerType:= PFD_MAIN_PLANE; end; nPixelFormat := ChoosePixelFormat(DC, @pfd); SetPixelFormat(DC, nPixelFormat, @pfd); DescribePixelFormat(DC, nPixelFormat, sizeof(TPixelFormatDescriptor), pfd); end; procedure TfrmGL.FormResize(Sender: TObject); begin InvalidateRect(Handle, nil, False); end; procedure TfrmGL.FormDestroy(Sender: TObject); begin wglMakeCurrent(0, 0); wglDeleteContext(hrc); ReleaseDC(Handle, DC); gluDeleteQuadric (qObj); glDeleteLists (sphere, 1); glDeleteLists (mol, 1); glDeleteLists (cyl, 1); end; end.