[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppBuilderOpenGlWithoutGlut
Unit1.hpp:
//Unit1.cpp
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppBuilderOpenGlWithoutGlut
C++ Builder How do I use OpenGL without using GLUT?
Adepted from this article by John Ray Thomas: http://community.borland.com/article/0,1410,10528,00.htmlUnit1.hpp:
//---------------------------------------------------------------------------//---------------------------------------------------------------------------
- ifndef Unit1H
- define Unit1H
//--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components void __fastcall FormCreate(TObject *Sender); void __fastcall FormDestroy(TObject *Sender); void __fastcall FormResize(TObject *Sender); private: // User declarations HDC hdc; HGLRC hrc; int PixelFormat; double angle; public: // User declarations __fastcall TForm1(TComponent* Owner); void __fastcall IdleLoop(TObject*, bool&); void __fastcall RenderGLScene(); void __fastcall SetPixelFormatDescriptor(); void __fastcall SetupRC(); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //---------------------------------------------------------------------------
- include <Classes.hpp>
- include <Controls.hpp>
- include <StdCtrls.hpp>
- include <Forms.hpp>
- include <gl/gl.h>
- include <gl/glu.h>
- include <math>
- endif
//Unit1.cpp
//---------------------------------------------------------------------------
- include <vcl.h>
- pragma hdrstop
//---------------------------------------------------------------------------
- include "Unit1.h"
TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Application->OnIdle = IdleLoop; angle = 0; //_control87(MCW_EM, MCW_EM); //OLD, removed by Richel } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { hdc = GetDC(Handle); SetPixelFormatDescriptor(); hrc = wglCreateContext(hdc); wglMakeCurrent(hdc, hrc); SetupRC(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { //ReleaseDC(hdc); //changed by Richel ReleaseDC(0, hdc); wglMakeCurrent(hdc, NULL); wglDeleteContext(hrc); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormResize(TObject *Sender) { GLfloat nRange = 200.0f; glViewport(0, 0, ClientWidth, ClientHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (ClientWidth <= ClientHeight) glOrtho(-nRange, nRange, -nRange*ClientHeight/ClientWidth, nRange*ClientHeight/ClientWidth, -nRange, nRange); else glOrtho(-nRange*ClientWidth/ClientHeight, nRange*ClientWidth/ClientHeight, -nRange, nRange, -nRange, nRange); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } //--------------------------------------------------------------------------- void __fastcall TForm1::IdleLoop(TObject*, bool& done) { RenderGLScene(); SwapBuffers(hdc); done = false; } //--------------------------------------------------------------------------- void __fastcall TForm1::RenderGLScene() { //Place your OpenGL drawing code here //Code below by Richel glClear(GL_COLOR_BUFFER_BIT); angle+=(M_PI*0.001); //glColor3f(1.0,1.0,1.0); glColor3f(random(100)/100.0,random(100)/100.0,random(100)/100.0); glBegin(GL_POLYGON); glVertex3f( 0.0 + (sin(angle+(0.0*M_PI))*125) , 0.5 + (cos(angle+(0.0*M_PI))*125) ,0.0); glVertex3f( 0.0 + (sin(angle+(0.5*M_PI))*125) , 0.5 + (cos(angle+(0.5*M_PI))*125) ,0.0); glVertex3f( 0.0 + (sin(angle+(1.0*M_PI))*125) , 0.5 + (cos(angle+(1.0*M_PI))*125) ,0.0); glVertex3f( 0.0 + (sin(angle+(1.5*M_PI))*125) , 0.5 + (cos(angle+(1.5*M_PI))*125) ,0.0); glEnd(); glFlush(); } //--------------------------------------------------------------------------- void __fastcall TForm1::SetPixelFormatDescriptor() { PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0,0,0,0,0,0, 0,0, 0,0,0,0,0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; PixelFormat = ChoosePixelFormat(hdc, &pfd); SetPixelFormat(hdc, PixelFormat, &pfd); } //--------------------------------------------------------------------------- void __fastcall TForm1::SetupRC() { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } //---------------------------------------------------------------------------
- pragma package(smart_init)
- pragma resource "*.dfm"
Code list
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
