Ir ao conteúdo
  • Cadastre-se

Luzes não funcionam - C++ wxWidgets OpenGL


WalbertiEvaristo

Posts recomendados

Olá, eu não sei o que eu estou fazendo e errado mais a luzes na minha "cena" não funcionam (_(. Bem a baixo segue o arquivo "View3D.cpp".

#include "View3D.h"

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

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

float trot = 0;
float trot_speed = 2.0;

GLfloat light_ambient[4] = {0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[4] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_specular[4] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_position[4] = {3.0, 3.0, 3.0, 1.0};

void set_material_2(void)
{
GLfloat material_ambient[] = {0.8, 0.8, 0.8, 1.0};
GLfloat material_diffuse[] = {0.0, 0.8, 0.8, 1.0};
GLfloat material_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat material_emission[] = {0.0, 0.0, 0.0, 1.0};
GLfloat material_shininess[] = {128.0};

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_emission);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, material_shininess);
}

View3D::View3D(wxWindow* parent)
: wxGLCanvas(parent, wxID_ANY, att_list, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
this->gl_init = false;

this->camera_position.set(0, 0, 25);
this->camera_orientation.set(25, 0, 0);

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_color[0] = 0.5;
this->grid_color[1] = 0.5;
this->grid_color[2] = 0.5;

this->grid_size = 500;

this->r_timer = new wxTimer(this, 1);

this->fps_limit = 60;
this->time_space = 1000.0 / this->fps_limit;
this->r_timer->Start(this->time_space);
}

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

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

void View3D::InitView(int tleftx, int tlefty, int brigthx, int brigthy)
{
GLclampf br = this->background_color[0];
GLclampf bg = this->background_color[1];
GLclampf bb = this->background_color[2];

glViewport(tleftx, tlefty, brigthx, brigthy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

float ratio = (float)(brigthx) / (float)(brigthy);
gluPerspective(45, ratio, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClearColor(br, bg, bb, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);

glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);

set_material_2();

glEnable(GL_LIGHTING);
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);
}

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

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

void View3D::Render(void)
{
if(!IsShown()) {
return;
}

wxGLContext cContext(this);

this->SetCurrent(cContext);
this->InitView(0, 0, this->GetSize().x, this->GetSize().y);

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());

glDisable(GL_LIGHTING);

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();

glEnable(GL_LIGHTING);

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());

glColor3f(0, 1, 0);
glBegin(GL_QUADS);
glNormal3f(0, 1, 0);
glVertex3f(-10, 0, 10);
glNormal3f(0, 1, 0);
glVertex3f(10, 0, 10);
glNormal3f(0, 1, 0);
glVertex3f(10, 0, -10);
glNormal3f(0, 1, 0);
glVertex3f(-10, 0, -10);
glEnd();

glRotatef(trot, 0, 1, 0);
glColor3f(1.0, 0.0, 0.0);
glutSolidSphere(2, 32, 32);

glFlush();
SwapBuffers();
}

void View3D::DrawGrid(void)
{
float index;

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

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

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...