#include "wxgl.h"
BEGIN_EVENT_TABLE(wxGL,wxGLCanvas)
EVT_PAINT(wxGL::OnPaint)
EVT_SIZE(wxGL::OnSize)
EVT_ERASE_BACKGROUND(wxGL::OnEraseBackground)
END_EVENT_TABLE()
wxGL::wxGL(wxWindow *parent, wxWindowID id,
const wxPoint& pos,
const wxSize& size,
long style, const wxString& name,int* attribList )
: wxGLCanvas(parent, (wxGLCanvas*) NULL, id, pos, size,
style|wxFULL_REPAINT_ON_RESIZE , name, attribList )
{
if ( InitGL() )
{
}
}
wxGL::~wxGL()
{
}
void wxGL::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
{
// Do nothing, to avoid flashing.
}
void wxGL::OnSize(wxSizeEvent& event)
{
event.Skip();
ResizeGL();
}
void wxGL::OnPaint( wxPaintEvent& WXUNUSED(event) )
{
wxPaintDC dc(this);
RenderScene();
}
void wxGL::OnFullScreen(wxCommandEvent& event)
{
// fullscreen = event.IsChecked();
}
bool wxGL::InitGL()
{
if (GetContext())
{
SetCurrent();
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
// Really Nice Perspective Calculations
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
return true;
}
void wxGL::ResizeGL()
{
if (GetContext())
{
SetCurrent();
int w, h;
GetClientSize(&w, &h);
if (h==0) // Prevent A Divide By Zero By
{
h=1; // Making Height Equal One
}
glViewport(0,0,w,h); // Reset The Current Viewport
// If I use the following code, nothing will shown on screen!!!!!
// glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
// glLoadIdentity(); // Reset The Projection Matrix
//
// // Calculate The Aspect Ratio Of The Window
// gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);
//
// glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
// glLoadIdentity();
glOrtho(-1.0f, 1.0f, -1.0f, 1.0, 1.0f, -1.0f);
}
}
void wxGL::RenderScene()
{
if (!GetContext()) return;
SetCurrent();
// Clear Screen And Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // Reset The Current Modelview Matrix
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
glFlush();
SwapBuffers();
}
/////////////////////////////////////////
// inside myframe's constructor, I use the following code to create
// wxGLCanvas based object
wxInt32 attrib[] = {
WX_GL_RGBA,
WX_GL_DEPTH_SIZE,
32,
WX_GL_DOUBLEBUFFER,
0,
};
gl =new wxGL(this,-1,wxPoint(0,0),wxSize(w,h),0,"",&attrib[0]);
Nehe tutorial and wxGLCanvas
I'm studying opengl through Nehe tutorial. What I'm going to do is to port Nehe tutorial code into wxWidgets. Lesson 1 is no problem for me. But I got some problem in lesson 2. The shapes are not shown at all if I follow the tutorial to use perspective projection. But if I use glOrtho, everything is fine. Can anybody advise?
Here is my code
I think its probably because you are drawing at 0.0 on the Z axis, you need to move into the screen a bit try replacing RenderScene with:
Notice the Z value of each vertex. You're near depth in gluPerspective is 0.1 so anything less than that on the Z axis wont be displayed.
Luke.
void wxGL::RenderScene(){ if (!GetContext()) return; SetCurrent(); // Clear Screen And Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // Reset The Current Modelview Matrix glBegin(GL_TRIANGLES); // Drawing Using Triangles glVertex3f( 0.0f, 1.0f, -4.0f); // Top glVertex3f(-1.0f,-1.0f, -4.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, -4.0f); // Bottom Right glEnd(); // Finished Drawing The Triangle glFlush(); SwapBuffers();}
Notice the Z value of each vertex. You're near depth in gluPerspective is 0.1 so anything less than that on the Z axis wont be displayed.
Luke.
When you start to add textures to your projects,
pay attention: you have to execute the code of glBindTexture, glGenTexture, glTex2DImage, gluBuild2DMipmaps,... whithin your wXGLCanvas-Instance!!
That means it has to be located for instance in InitGL()
I once tried to port my whole engine to wxWidgets, and when you have several classes to manage everything, you have to build something like a TexturePool, where the names and texIDs are saved and then loaded from wxGLCanvas-instance.
pay attention: you have to execute the code of glBindTexture, glGenTexture, glTex2DImage, gluBuild2DMipmaps,... whithin your wXGLCanvas-Instance!!
That means it has to be located for instance in InitGL()
I once tried to port my whole engine to wxWidgets, and when you have several classes to manage everything, you have to build something like a TexturePool, where the names and texIDs are saved and then loaded from wxGLCanvas-instance.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement