Lights do not work

Hi,

I have a small problem. I do not know why but I can not do the lights in my scene work. See the code below… Sorry for my English (Powred by GoogleTranslator XD). I’m using wxDevC++ wxGLCanvas.

Source code here!

#include "View3D.h"

#include <GL/glu.h>
#include <GL/glut.h>

const int att_list[5] = {
    WX_GL_RGBA,
    WX_GL_DOUBLEBUFFER,
    WX_GL_DEPTH_SIZE,
    24,
    WX_GL_SAMPLE_BUFFERS
};

float trot = 0;
float trot_speed = 0.2;

GLfloat light_ambient[4] = {0.1, 0.1, 0.1, 1.0};
GLfloat light_diffuse[4] = {1.0, 0.0, 0.0, 1.0};
GLfloat light_specular[4] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_position[4] = {0, 5, 0, 0.5};//{5.0, 5.0, 5.0, 1.0};

View3D::View3D(wxWindow* parent)
    : wxGLCanvas(parent, wxID_ANY, att_list, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
    this->background_color[0] = 0.15;
    this->background_color[1] = 0.15;
    this->background_color[2] = 0.15;
    
    this->show_grid = true;
    this->show_grid_x = true;
    this->show_grid_y = true;
    this->show_grid_z = true;
    this->grid_size = 50;
    
    this->grid_color[0] = 0.5;
    this->grid_color[1] = 0.5;
    this->grid_color[2] = 0.5;
    
    this->camera_position.set(0, 0, 25);
    this->camera_orientation.set(25, 0, 0);
    
    this->m_context = new wxGLContext(this);
    
    this->r_timer = new wxTimer(this, 1);
    this->r_timer->Start(1000.0/60);
}

BEGIN_EVENT_TABLE(View3D, wxGLCanvas)
EVT_SIZE(View3D::OnResize)
EVT_PAINT(View3D::OnPaint)
EVT_TIMER(1, View3D::OnTimer)
END_EVENT_TABLE()

void View3D::SetBackgroundColor(float r, float g, float b)
{
    this->background_color[0] = r;
    this->background_color[1] = g;
    this->background_color[2] = b;
}

void View3D::StopRender()
{
    this->r_timer->Stop();
}

void View3D::OnResize(wxSizeEvent& evt)
{
    this->Render();
}

void View3D::OnPaint(wxPaintEvent& evt)
{
    this->Render();
}

void View3D::OnTimer(wxTimerEvent& evt)
{
    this->Render();
}

void View3D::PrepareView()
{
    GLclampf br = this->background_color[0];
    GLclampf bg = this->background_color[1];
    GLclampf bb = this->background_color[2];
    
    float wx = this->GetSize().x;
    float wy = this->GetSize().y;
    
    glClearColor(br, bg, bb, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glViewport(0, 0, wx, wy);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    float ratio = (float)(wx) / (float)(wy);
    gluPerspective(45, ratio, 0.1, 500);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
	glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
	
	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	glEnable(GL_COLOR_MATERIAL);
    
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_LIGHTING);
}

void View3D::Render()
{
    if(!IsShown()) {
        return;
    }
    
    this->SetCurrent(*this->m_context);
    this->PrepareView();
    
    glPushMatrix();
    
    this->mat_view.identity();
    this->mat_view.rotate(this->camera_orientation.x, 1, 0, 0);
    this->mat_view.rotate(this->camera_orientation.y, 0, 1, 0);
    this->mat_view.rotate(this->camera_orientation.z, 0, 0, 1);
    this->mat_view.translate(-this->camera_position.x, -this->camera_position.y, -this->camera_position.z);
    
    glLoadMatrixf(this->mat_view.getTranspose());
    
    if (this->show_grid) {
        this->DrawGrid();
    }
    
    glPointSize(8);
    glBegin(GL_POINTS);
    glColor3f(1.0, 1.0, 0.0);
    glVertex3f(light_position[0], light_position[1], light_position[2]);
    glEnd();
    
    this->mat_model.identity();
    this->mat_model.rotate(0, 1, 0, 0);
    this->mat_model.rotate(0, 0, 1, 0);
    this->mat_model.rotate(0, 0, 0, 1);
    this->mat_model.translate(0, 0, 0);
    
    this->mat_modelview = this->mat_view * this->mat_model;
    
    glLoadMatrixf(this->mat_modelview.getTranspose());
    
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glEnable(GL_LIGHTING);
    
    glRotatef(trot, 1, 1, 1);
    glColor3f(1.0, 0.0, 0.0);
    glutSolidSphere(2, 8, 8);
    
    trot += trot_speed;
    
    //glFlush();
    SwapBuffers();
}

void View3D::DrawGrid()
{
    float index;
    
    glDisable(GL_LIGHTING);
    glPolygonMode(GL_FRONT, GL_LINE);
    
    glBegin(GL_LINES);
    glColor3fv(this->grid_color);
    
    for(index =- grid_size; index < grid_size+1; index++){
        glVertex3f(-this->grid_size, 0, index);
        glVertex3f(this->grid_size, 0, index);
        
        glVertex3f(index, 0, -this->grid_size);
        glVertex3f(index, 0, this->grid_size);
    }
    
    if(this->show_grid_x){
        glColor3f(1.0, 0.0, 0.0);
        glVertex3f(-this->grid_size, 0.0, 0.0);
        glVertex3f(this->grid_size, 0.0, 0.0);
    }
    
    if(this->show_grid_y){
        glColor3f(0.0, 1.0, 0.0);
        glVertex3f(0.0, -this->grid_size, 0.0);
        glVertex3f(0.0, this->grid_size, 0.0);
    }
    
    if(this->show_grid_z){
        glColor3f(0.0, 0.0, 1.0);
        glVertex3f(0.0, 0.0, -this->grid_size);
        glVertex3f(0.0, 0.0, this->grid_size);
    }
    
    glEnd();
    glPolygonMode(GL_FRONT, GL_FILL);
}

[QUOTE=Walberti Evaristo;1252496]Hi,

I have a small problem. I do not know why but I can not do the lights in my scene work. See the code below… Sorry for my English (Powred by GoogleTranslator XD). I’m using wxDevC++ wxGLCanvas.
[/QUOTE]


    ...
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glEnable(GL_LIGHTING);                                               // <- try glEnable(GL_LIGHT0); here
    
    glRotatef(trot, 1, 1, 1);
    ...

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.