My app is a CPP Builder app. I pared it down to a small subset, but you would have to cut and paste the OpenGL stuff to a GLUT app. I don’t have GLUT and don’t know much about it:
#include <vcl.h>
#include <string>
#include <mmsystem.h>
#include <sstream>
#include <iomanip>
#include “GLFormUnit.h”
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#pragma hdrstop
#pragma resource “*.dfm”
using std::string;
TGLForm * GLForm;
static GLUquadricObj * quadric_;
static void checkGLErrors()
{
GLenum errCode;
const GLubyte * glErr;
if ((errCode = glGetError()) != GL_NO_ERROR) {
glErr = gluErrorString(errCode);
Application->MessageBox(glErr, "OpenGL Error", MB_OK);
}
}
__fastcall TGLForm::TGLForm(TComponent * Owner) : TForm(Owner) {}
void __fastcall TGLForm::IdleLoop(TObject *, bool & done)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// draw red ball
static float rot = 1.0f;
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glRotatef(rot, 0.0f, 1.0f, 0.0f);
gluSphere(quadric_, 2.0f, 30, 30);
rot += 0.1;
done = false;
SwapBuffers(deviceContext_);
}
void __fastcall TGLForm::FormCreate(TObject * Sender)
{
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
quadric_ = gluNewQuadric();
gluQuadricDrawStyle(quadric_, GLU_FILL);
Application->OnIdle = IdleLoop;
deviceContext_ = GetDC(GLPanel->Handle);
SetPixelFormatDescriptor();
renderContext_ = wglCreateContext(deviceContext_);
wglMakeCurrent(deviceContext_, renderContext_);
}
void _fastcall TGLForm::FormDestroy(TObject * Sender)
{
checkGLErrors();
gluDeleteQuadric(quadric);
ReleaseDC(deviceContext_, 0);
wglMakeCurrent(deviceContext_, NULL);
wglDeleteContext(renderContext_);
}
void fastcall TGLForm::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(deviceContext, &pfd);
SetPixelFormat(deviceContext_, pixelFormat_, &pfd);
}
void __fastcall TGLForm::FormResize(TObject * Sender)
{
float width = GLPanel->Width;
float height = GLPanel->ClientHeight;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float) width / (float) height, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}