OpenGL and Light

Peter Poulsen peter at pgpnet.dk
Sun May 4 00:09:24 PDT 2008


I have this little program that is based on the one found here: http://wiki.wxwidgets.org/WxGLCanvas

I'm trying to get light to work, but when I enable lighting it doesn't accept color values between 0.0f and 1.0f; if the value is >0.0f it is set to 1.0f, so the color is either full on or full off. Hopefully someone can explain to me how to get this working? I have it working in a "pure" glut implementation so I think it some wxWidget stuff I'm not setting up properly.

My compile command is this: g++ box.cc -o box -lGL -lglut `wx-config --cxxflags --libs --gl_libs` (I'm using wxWidget/GTK-2.8.7 under Linux)

And this is the program:
----

#include "wx/wx.h"
#include "wx/glcanvas.h"
 
class wxGLCanvasSubClass: public wxGLCanvas {
        void Render();
public:
    wxGLCanvasSubClass(wxFrame* parent);
    void Paintit(wxPaintEvent& event);
protected:
    DECLARE_EVENT_TABLE()
};
 
BEGIN_EVENT_TABLE(wxGLCanvasSubClass, wxGLCanvas)
    EVT_PAINT    (wxGLCanvasSubClass::Paintit)
END_EVENT_TABLE()
 
wxGLCanvasSubClass::wxGLCanvasSubClass(wxFrame *parent)
:wxGLCanvas(parent, wxID_ANY,  wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas")){
    int argc = 0;
    char** argv = NULL;
}
  
void wxGLCanvasSubClass::Paintit(wxPaintEvent& WXUNUSED(event)){
    Render();
}
 
void wxGLCanvasSubClass::Render()
{
    SetCurrent();

    wxPaintDC(this);
	
	glEnable(GL_LIGHTING);
 	glEnable(GL_COLOR_MATERIAL);
 	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	
	glEnable(GL_LIGHT0);	
	float white[] = { 1.0f, 1.0f, 1.0f, 1.0f };

	float l_pos[] = { 0.0f, 0.0f, 0.6f };
	glLightfv(GL_LIGHT0, GL_POSITION, l_pos);

	float dark[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
	glLightfv(GL_LIGHT0, GL_AMBIENT, dark);
	glLightfv(GL_LIGHT0, GL_SPECULAR, dark);

	
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, (GLint)GetSize().x, (GLint)GetSize().y);

	/// This is where the color is set, it should be grey
    glColor3f(0.5f, 0.5f, 0.5f);
	
    glBegin(GL_POLYGON);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
        glVertex2f(0.0, -0.8);
    glEnd();

    glFlush();
    SwapBuffers();
}
 
class MyApp: public wxApp
{
    virtual bool OnInit();
    wxGLCanvas * MyGLCanvas;
};
 
 
IMPLEMENT_APP(MyApp)
 
  
bool MyApp::OnInit()
{
    wxFrame *frame = new wxFrame((wxFrame *)NULL, -1,  wxT("Hello GL World"), wxPoint(50,50), wxSize(200,200));
    new wxGLCanvasSubClass(frame);
 
    frame->Show(TRUE);
    return TRUE;
}


More information about the wx-users mailing list