[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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.html

Unit1.hpp:
//---------------------------------------------------------------------------
  1. ifndef Unit1H
  2. define Unit1H
//---------------------------------------------------------------------------
  1. include <Classes.hpp>
  2. include <Controls.hpp>
  3. include <StdCtrls.hpp>
  4. include <Forms.hpp>
  5. include <gl/gl.h>
  6. include <gl/glu.h>
  7. include <math>
//--------------------------------------------------------------------------- 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; //---------------------------------------------------------------------------
  1. endif


//Unit1.cpp
//---------------------------------------------------------------------------
  1. include <vcl.h>
  2. pragma hdrstop
  1. include "Unit1.h"
//---------------------------------------------------------------------------
  1. pragma package(smart_init)
  2. pragma resource "*.dfm"
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(); } //---------------------------------------------------------------------------


Code list



last edited (May 12, 2006) by bilderbikkel, Number of views: 2604, Current Rev: 3 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.