Jump to content
  • Advertisement
Sign in to follow this  
assainator

wxwidgets glcanvas problem

This topic is 2857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to setup a glcanvas in wxwidgets but I'm having a problem. If I create the glCanvas and connect the render and resize function, I only get a light square(see below), no black background as I suspect. Can anyone help me with this?

the image



my code:

RenderCanvas.hpp
#pragma once
#include <wx/wx.h>
#include <wx/glcanvas.h>

class RenderCanvas : public wxGLCanvas
{
public:
RenderCanvas(wxWindow * parent);
~RenderCanvas(void);

void OnPaint(wxPaintEvent& WXUNUSED(event));
void Render();
void OnResize(wxSizeEvent& event);

void GLSetup();

private:
bool GLReady;
};





RenderCanvas.cpp
#include "RenderCanvas.hpp"

//arguments for setting up the canvas
int SetupArguments[] = {WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0};


//constructor
RenderCanvas::RenderCanvas(wxWindow *parent) : wxGLCanvas(parent, wxID_ANY, SetupArguments, wxDefaultPosition,
wxDefaultSize, 0, L"GLCanvas", wxNullPalette)
{
GLReady = false;
Connect(GetId(), wxEVT_PAINT, wxPaintEventHandler(RenderCanvas::OnPaint));
Connect(GetId(), wxEVT_SIZE, wxSizeEventHandler(RenderCanvas::OnResize));
}

//destructor
RenderCanvas::~RenderCanvas(void)
{
}

void RenderCanvas::OnPaint(wxPaintEvent &event)
{
//set this wxGLCanvas to current
this->SetCurrent();

//set to paint dc
wxPaintDC(this);

//if GL has not been initialised yet, do it
if(!GLReady)
{
GLSetup();
GLReady = true;
}

//now call the render function
Render();

//now swap buffers
this->SwapBuffers();
}


void RenderCanvas::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_TRIANGLES);
glVertex2f( 0, 0.5);
glVertex2f(-0.5,-0.5);
glVertex2f( 0.5,-0.5);
glEnd();

glFlush();
}

void RenderCanvas::GLSetup()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

void RenderCanvas::OnResize(wxSizeEvent& event)
{
this->SetCurrent();
glViewport(0,0, GetSize().x, GetSize().y);

Refresh();
}



Viewer.hpp

#pragma once

#include "FormBuilderGenerated/ModelerBase.h"
#include "RenderCanvas.hpp"

class Viewer : public ViewerBase
{
public:
Viewer(wxWindow* Parent);
~Viewer(void);

private:
RenderCanvas *can1;
};





Viewer.cpp

#include "Viewer.hpp"

Viewer::Viewer(wxWindow* parent) : ViewerBase(parent)
{
//show
this->Show();

//Create the display
can1 = new RenderCanvas(this);
can1->SetSize(wxSize(300, 300));
can1->SetPosition(wxPoint(50, 50));

Viewer::~Viewer(void)
{
}


Share this post


Link to post
Share on other sites
Advertisement
I'm not as entirely well versed in OpenGL as some others are, and I don't know all that much about wxWidgets, but I think I might be seeing your problem. It doesn't look like you're initializing OpenGL quite thoroughly enough. Calling glClearColor() probably isn't enough. Particularly, it doesn't appear you've set up the projection mode. Also, you haven't specified if the GLCanvas is to render in 2D using gluOrtho2D() or to render in 3D with gluPerspective().

Judging from the wxWidgets GLCanvas wiki page your code seems to be missing a lot. Look at the function definition for prepare2DViewport() in the provided main.cpp and you'll see what I've been talking about.


/** Inits the OpenGL viewport for drawing in 2D. */
void BasicGLPane::prepare2DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
glEnable(GL_TEXTURE_2D); // textures
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(topleft_x, bottomrigth_x, bottomrigth_y, topleft_y);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}





The rest of your code looks as if it should be fine if you setup OpenGL in a similar manner as the wiki shows. If you don't immediately understand what all of this OpenGL code means, there are plenty of resources available that clearly explain each function and how they all work together in an OpenGL application.

Share this post


Link to post
Share on other sites
yes, I know but I used a lot less, but glClearColor should create a black background for me. It could be perfectly right that I don't get the triangle I'm drawing but I did expect a black background. I did update my setup code because I could be wrong, but I still get the same image.

The SetupGL and OnReisze functions now looks like this:


void RenderCanvas::GLSetup()
{
//set clear color to red
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

//set the viewport
glViewport(0, 0, GetSize().x, GetSize().y);

//setup projection matrix
glMatrixMode(GL_PROJECTION);

//load identity
glLoadIdentity();

//setup orthographic view
gluOrtho2D(-1, 1, -1, 1);

//setup modelview matrix
glMatrixMode(GL_MODELVIEW);

//load identity
glLoadIdentity();
}

void RenderCanvas::OnResize(wxSizeEvent& event)
{
//set this glCanvas to current
this->SetCurrent();

//tell gl what the viewport is
glViewport(0,0, GetSize().x, GetSize().y);

//Setup a 2d orthograpic matrix
gluOrtho2D(-1, 1, -1, 1);

//Refresh the window so the viewport will get rendered
Refresh();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!