wxGLCanvas Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

wxGLCanvas

Post by sethjackson »

I'm trying to use OpenGL with wxWidgets... I compiled my program but all I get is a window with a black screen unfortunately my drawing code isn't working.

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;
}

opengl.h

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

Ok that was a long code posting, but I thought I would post all of it so you can see what I'm doing (wrong). If someone would tell me how to get it to draw the code in the Render() function I would appreciate it. NOTE: the code in the Render() function worked when I was using the Win32 API, but
when I ported it to wxWidgets I get nothing except a window with a black background.
Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Post by Ravilj »

It looks ok interms of wxWidgets, you dont initialise your Projection or your Model view matrices in your init function. Maybe use something like this in your initGL function:

Code: Select all

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective( 60.0, (GLfloat)canvasWidth / (GLfloat)canvasHeight, 0.1, 8.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(0.0, 0.0, -3.0);
Oh also you do know that you are calling initGL function twice, once in the contructor and once when you call Render when it is called for the first time. To solve this add

Code: Select all

m_bInit = true; 
to you initGL function rather.

Last thing I promise, you dont initialise m_rquad. Well not that I can see.
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

m_rquad i not initialized to anything it is supposed to start a zero.

I did

Code: Select all

glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective( 60.0, (GLfloat)canvasWidth / (GLfloat)canvasHeight, 0.1, 8.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.0, 0.0, -3.0);
 
and i still get a blank black screen.

Also, this code worked just fine when I was using the Win32 API, but now it only half-works. Any other suggestions?
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

Alright I got it to diasplay but now it doesn't repaint itself... If I minimize then maximize it updates the window (repaints it), I need to figure out how to get it to continually draw. Can someone help me fix this?

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;
    m_rquad = 0.0f;       
}

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)
{   
    wxGLCanvas::OnSize(event);    
    
    int width, height;
    
    GetClientSize(&width, &height);
    
    if(height <= 0)
    {
        height = 1;    
    }
    
#ifndef __WXMOTIF__
    if (GetContext())
#endif
    {
        SetCurrent();

        glViewport(0, 0, width, height);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        gluPerspective(45.0f, width/height, 1.0f, 100.0f);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();    
    }
}

void MainGLCanvas::OnEraseBackground(wxEraseEvent& event)
{
    
}

void MainGLCanvas::InitGL()
{    
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    
    glClearDepth(1.0f);    

    glEnable(GL_DEPTH_TEST);    
    glShadeModel(GL_SMOOTH);
    glDepthFunc(GL_LEQUAL);   
}

void MainGLCanvas::Render()
{	
    if(!m_bInit)
    {
        InitGL();
        m_bInit = true;
    }      
    
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    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;
}

EDIT:

Ok I fixed it so the colors appear correctly (don't enable lighting DUH).
Anyways if someone could tell me how to repaint continually I would appreciate it. :)
Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Post by Ravilj »

You will either need to use a timer to generate an event or you will need to use the idle loop event un wxApp as far as I know. What did you change to get it working?
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

I turned off the lighting which fixed it so the colors would appear correctly. I fixed OnSize so that it would position the object onto the middle of the screen. :) Here is the complete changed code.

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_init = false;
    m_rquad = 0.0f;       
}

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)
{    
    wxGLCanvas::OnSize(event);    
    
    int width, height;
    
    GetClientSize(&width, &height);
    
    if(height <= 0)
    {
        height = 1;    
    }
    
#ifndef __WXMOTIF__
    if (GetContext())
#endif
    {
        SetCurrent();
        
        glViewport(0, 0, width, height);
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        
        gluPerspective(45.0f, width / height, 1.0f, 100.0f);
        
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();    
    }
}

void MainGLCanvas::OnEraseBackground(wxEraseEvent& event)
{
    
}

void MainGLCanvas::InitGL()
{   
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    
    glClearDepth(1.0f);   
    
    glEnable(GL_DEPTH_TEST);    
    glShadeModel(GL_SMOOTH);
    glDepthFunc(GL_LEQUAL);   
}

void MainGLCanvas::Render()
{
    if(!m_init)
    {
        InitGL();
        m_init = true;
    }      
    
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    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;    
}

opengl.h

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, const wxPoint& pos,
                     const wxSize& size, long style, const wxString& name);
        ~MainGLCanvas();        

    private:
        
        void OnPaint(wxPaintEvent& event);
        void OnSize(wxSizeEvent& event);
        void OnEraseBackground(wxEraseEvent& event);
        void OnEnterWindow(wxMouseEvent& event);
        
        void InitGL();
        void Render();

        bool m_init;

        float m_rquad;

        DECLARE_EVENT_TABLE()
};

#endif // OPENGL_H

If you would show me how to use the idle loop I would appreciate it. Thanks. :)
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

YEAH I got it to refresh the window now.

I added an Idle event that refreshed the window.

This thread helped.

http://forums.wxwidgets.org/viewtopic.php?t=839

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_IDLE(MainGLCanvas::OnIdle)
    
    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_init = false;
    m_rquad = 0.0f;       
}

MainGLCanvas::~MainGLCanvas()
{

}

void MainGLCanvas::OnIdle(wxIdleEvent& event)
{
	this->Refresh();
}

void MainGLCanvas::OnSize(wxSizeEvent& event)
{    
    wxGLCanvas::OnSize(event);    
    
    int width, height;
    
    GetClientSize(&width, &height);
    
    if(height <= 0)
    {
        height = 1;    
    }
    
#ifndef __WXMOTIF__
    if (GetContext())
#endif
    {
        SetCurrent();
        
        glViewport(0, 0, width, height);
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        
        gluPerspective(45.0f, width / height, 1.0f, 100.0f);
        
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();    
    }
}

void MainGLCanvas::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);

#ifndef __WXMOTIF__
    if (!GetContext()) 
    {
    	return;
    }
#endif
	
    SetCurrent();
	
    Render();
    
    SwapBuffers();	
}

void MainGLCanvas::OnEraseBackground(wxEraseEvent& event)
{
    
}


void MainGLCanvas::OnEnterWindow(wxMouseEvent& event)
{
    SetFocus();
}

void MainGLCanvas::InitGL()
{   
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    
    glClearDepth(1.0f);   
    
    glEnable(GL_DEPTH_TEST);    
    glShadeModel(GL_SMOOTH);
    glDepthFunc(GL_LEQUAL);   
}

void MainGLCanvas::Render()
{
    if(!m_init)
    {
        InitGL();
        m_init = true;
    }      
    
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    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;    
}

opengl.h

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, const wxPoint& pos,
                     const wxSize& size, long style, const wxString& name);
        ~MainGLCanvas();        

    private:        
        
        void OnIdle(wxIdleEvent& event);
        void OnPaint(wxPaintEvent& event);
        void OnSize(wxSizeEvent& event);
        void OnEraseBackground(wxEraseEvent& event);
        void OnEnterWindow(wxMouseEvent& event);
        
        void InitGL();
        void Render();

        bool m_init;

        float m_rquad;

        DECLARE_EVENT_TABLE()
};

#endif // OPENGL_H

Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Post by Ravilj »

Good to hear, what are you planning on making?

*cough* I suggested idle event *cough* :wink:
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

Hehe I forgot to mark it as solved. :oops: Anyways I'm pobably just going to do the NeHe tutorials. I started on them but I was using the Win32 API. I then discovered wxWidgets, and I had been trying to convert my OpenGL code to it. Thanks for helping me. :D
Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Post by Ravilj »

The NeHe tutorials are a great place to start also download the "Red Book" for basic tutorials on openGL commands and "Blue Book" for references.
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

I own the Red Book..... I haven't checked out the Blue Book yet I really haven't needed to yet.... :)
GotenXiao
In need of some credit
In need of some credit
Posts: 8
Joined: Tue Oct 11, 2005 8:40 pm

Post by GotenXiao »

Is the code posted missing the wxApp code, or is there some obscure section of the manual evading me?
Image
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

It is missing I didn't post it.

app.cpp

Code: Select all

#include <wx/glcanvas.h>
#include "opengl.h"
#include "main.h"
#include "app.h"

IMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
	InitFrame();

	return true;
}

void MyApp::InitFrame()
{
	MainFrame* frame = new MainFrame(0L, "OpenGL Example",
	                                 50, 50, 760, 530);
    
    
    frame->g_pGLCanvas = new MainGLCanvas(frame, wxID_ANY, wxDefaultPosition, 
                                          wxDefaultSize, 0, "MainGLCanvas");
    
    
	frame->Show(true);
}

app.h

Code: Select all

class MyApp : public wxApp
{
    public:
    
        virtual bool OnInit();
        
    private:        
        
        void InitFrame();                    
};

main.cpp

Code: Select all

#include <wx/glcanvas.h>
#include "opengl.h"
#include "main.h"

BEGIN_EVENT_TABLE(MainFrame, wxFrame)

	EVT_MENU(wxID_EXIT, MainFrame::OnExit)	

END_EVENT_TABLE()

MainFrame::MainFrame(wxFrame *frame, const wxString& title,
                     int xpos, int ypos, int width, int height)
	: wxFrame(frame, wxID_ANY, title, wxPoint(xpos, ypos), wxSize(width, height))
{
	g_pGLCanvas = 0;		
}

MainFrame::~MainFrame()
{
    
}

void MainFrame::OnExit(wxCommandEvent& event)
{
    Close(true);
}

main.h

Code: Select all

#ifndef MAIN_H
#define MAIN_H

#include <wx/wxprec.h>

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

class MainFrame : public wxFrame
{
	public:

		MainFrame(wxFrame *frame, const wxString& title,
                  int xpos, int ypos, int width, int height);
		~MainFrame();
		
		MainGLCanvas* g_pGLCanvas;

	private:

        void OnExit(wxCommandEvent& event);        

        DECLARE_EVENT_TABLE()
};

#endif // MAIN_H

GotenXiao
In need of some credit
In need of some credit
Posts: 8
Joined: Tue Oct 11, 2005 8:40 pm

Post by GotenXiao »

Thanks, I was trying to get something of my own working with wxGLCanvas (porting an old QuickBASIC game to C++) and I just couldn't figure out quite what to do from the examples.
Image
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

Yeah the OGL examples confused me too.
Post Reply