P
Hi!
Versuchs mal mit dieser Version, ich hab sie ein bischen umgemodelt. Ein Teil der Methoden waren nicht nötig. Zusätzlich hab ich eine Methoe hinzugefügt die ein Koordinatensystem zeichnet, um zu testen ob es funktioniert.
Vielleicht kann man den FAQ-Beitrag ja dahingehend anpassen? (Getestet auf BCB 5 Prof. und BCB 6 Enterprise)
Form1.h
//---------------------------------------------------------------------------
#ifndef frmOpenglH
#define frmOpenglH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall FormPaint(TObject *Sender);
void __fastcall FormResize(TObject *Sender);
private: // User declarations
HDC dc;
HGLRC GLContext;
HDC InitOpenGL(HWND Handle);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void drawScene (void);
void CoordinateSystem(GLdouble x, GLdouble y, GLdouble z);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
// ein paar Farben
const GLfloat colRed[] = {1, 0, 0},
colGreen[] = {0, 1, 0},
colBlue[] = {0, 0, 1},
colYellow[] = {1, 1, 0},
colDarkGray[] = {0.2, 0.2, 0.2},
colGray[] = {0.5, 0.5, 0.5},
colLightGray[] = {0.8, 0.8, 0.8},
colBlack[] = {0, 0, 0};
Form1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <float.h> //für _control87()
#include "Form1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
// Floatingpointeinheit initialisieren
_control87(MCW_EM, MCW_EM);
}
//---------------------------------------------------------------------------
// OpenGl Devicekontext initialisieren
HDC TForm1::InitOpenGL(HWND Handle)
{
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1; // Versionsnummer, 1 notwendig
pfd.dwFlags = PFD_DRAW_TO_WINDOW | // Zeichne in ein Fenster
PFD_SUPPORT_OPENGL; // OpenGL anstelle von GDI
pfd.dwFlags |= PFD_DOUBLEBUFFER; // flag setzen
pfd.iPixelType = PFD_TYPE_RGBA; // RGBA Pixel
pfd.cColorBits = 24; // Farbtiefe 24 Bit
pfd.cDepthBits = 32; // Tiefenpuffer 32 Bits
pfd.iLayerType = PFD_MAIN_PLANE; // Einzige Möglichkeit
HDC dc = GetDC(Handle);
int iPF = ChoosePixelFormat(dc, &pfd);
if (iPF == 0)
ShowMessage("Fehler bei ChoosePixelFormat");
SetPixelFormat(dc, iPF, &pfd);
GLContext = wglCreateContext(dc);
if (GLContext)
wglMakeCurrent(dc, GLContext);
else
ShowMessage("wglMakeCurrent nicht erfolgreich");
return dc;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
dc = InitOpenGL(Handle);
glEnable(GL_DEPTH_TEST);
//Hintergrundfarbe setzen (weis)
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_MODELVIEW);
//Auf Z-Achse um 4 nach hinten verschieben
glTranslated(0, 0, -4);
glLineWidth(1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
ReleaseDC(Handle,dc);
wglMakeCurrent(dc, NULL);
wglDeleteContext(GLContext);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
FormResize(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
glViewport(0, 0, ClientWidth, ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble aspect =(double)ClientWidth/ClientHeight;
gluPerspective(60, aspect, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
drawScene();
}
// Koordinatensystem zeichnen
void TForm1::CoordinateSystem(GLdouble x, GLdouble y, GLdouble z)
{
glBegin(GL_LINES);
glColor3fv(colRed);
glVertex3d(-x, 0, 0);
glVertex3d(x, 0, 0);
glColor3fv(colGreen);
glVertex3d(0, -y, 0);
glVertex3d(0, y, 0);
glColor3fv(colBlue);
glVertex3d(0, 0, z);
glVertex3d(0, 0, -z);
glEnd();
double h=0.05;
glBegin(GL_TRIANGLES); // Pfeil am Ende der X-Achse
glColor3fv(colRed);
glVertex3d(x-h, h, 0);
glVertex3d(x, 0, 0);
glVertex3d(x-h, -h, 0);
glEnd();
glBegin(GL_TRIANGLES); // Pfeil am Ende der Y-Achse
glColor3fv(colGreen);
glVertex3d(+h, y-h, 0);
glVertex3d(0, y, 0);
glVertex3d(-h, y-h, 0);
glEnd();
glBegin(GL_TRIANGLES); // Pfeil am Ende der Z-Achse
glColor3fv(colBlue);
glVertex3d(0, +h, z-h);
glVertex3d(0, 0, z);
glVertex3d(0, -h, z-h);
glEnd();
}
// Szene zeichnen
void TForm1::drawScene (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Koordinatensystem zeichnen
CoordinateSystem(1.0, 1.0, 1.0);
SwapBuffers(dc);
}