opengl.cpp
Code: Select all
#include <wx/glcanvas.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "opengl.h"
BEGIN_EVENT_TABLE(MainGLCanvas, wxGLCanvas)
EVT_SIZE(MainGLCanvas::OnSize)
EVT_PAINT(MainGLCanvas::OnPaint)
EVT_ERASE_BACKGROUND(MainGLCanvas::OnEraseBackground)
EVT_ENTER_WINDOW(MainGLCanvas::OnEnterWindow)
END_EVENT_TABLE()
MainGLCanvas::MainGLCanvas(wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxSize& size,
long style, const wxString& name)
: wxGLCanvas(parent, id, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name)
{
m_bInit = false;
InitGL();
}
MainGLCanvas::~MainGLCanvas()
{
}
void MainGLCanvas::OnEnterWindow(wxMouseEvent& event)
{
SetFocus();
}
void MainGLCanvas::OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
#ifndef __WXMOTIF__
if (!GetContext()) return;
#endif
SetCurrent();
Render();
SwapBuffers();
}
void MainGLCanvas::OnSize(wxSizeEvent& event)
{
// this is also necessary to update the context on some platforms
wxGLCanvas::OnSize(event);
// set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
int w, h;
GetClientSize(&w, &h);
#ifndef __WXMOTIF__
if (GetContext())
#endif
{
SetCurrent();
glViewport(0, 0, (GLint) w, (GLint) h);
}
}
void MainGLCanvas::OnEraseBackground(wxEraseEvent& event)
{
// Do nothing, to avoid flashing.
}
void MainGLCanvas::InitGL()
{
SetCurrent();
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void MainGLCanvas::Render()
{
if (!m_bInit)
{
InitGL();
m_bInit = true;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -7.0f);
glRotatef(m_rquad, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
// Top face of 3D Cube.
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
// Bottom face of 3D Cube.
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
// Front face of 3D Cube.
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
// Back face of 3D Cube.
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
// Left face of 3D Cube.
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face of 3D Cube.
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
glPopMatrix();
m_rquad += 1.0f;
}
Code: Select all
#ifndef OPENGL_H
#define OPENGL_H
#include <wx/wxprec.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#if !wxUSE_GLCANVAS
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
#endif
class MainGLCanvas : public wxGLCanvas
{
public:
MainGLCanvas(wxWindow *parent, wxWindowID id = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0, const wxString& name = "MainGLCanvas");
~MainGLCanvas();
private:
void OnPaint(wxPaintEvent& event);
void OnSize(wxSizeEvent& event);
void OnEraseBackground(wxEraseEvent& event);
void OnEnterWindow(wxMouseEvent& event);
void InitGL();
void Render();
bool m_bInit;
float m_rquad;
DECLARE_EVENT_TABLE()
};
#endif // OPENGL_H
when I ported it to wxWidgets I get nothing except a window with a black background.