PDA

View Full Version : Segfault when changing computer



ei05tbe
06-10-2010, 06:46 AM
Valgrind tells me the segfault is from this function


void setAmbient(const float ambient_[] ){
glLightfv(name, GL_AMBIENT, ambient_);
}

It used to work but krashes when I changed computer


#ifndef LIGHT_H
#define LIGHT_H

#include <GL/glut.h>
#include <vr/Matrix.h>



class Light {

private:
GLenum name;

GLfloat ambient[4];
GLfloat diffuse[4];
GLfloat specular[4];
GLfloat position[4];
bool on;

public:
Light(GLenum, vr::Vec4,vr::Vec4,vr::Vec4,vr::Vec4);
Light();
~Light();

void turnOn(){on = true;}
void turnOff(){on = false;}
bool isOn(){return on;}


void updatePosition(vr::Matrix);

void setAmbient(const float ambient_[] ){
glLightfv(name, GL_AMBIENT, ambient_);
}

void setDiffuse(const float diffuse_[]){
glLightfv(name, GL_DIFFUSE, diffuse_);
}
void setSpecular(const float specular_[]){
glLightfv(name, GL_SPECULAR, specular_);
}
void setPosition(const float position_[]){
glLightfv(name, GL_POSITION, position_);
}

vr::Vec3 getPosition(){
vr::Vec3 pos;
pos[0] = position[0];
pos[1] = position[1];
pos[2] = position[2];
return pos;
}

void setPosition(vr::Vec3 newPosition){
position[0] = newPosition[0];
position[1] = newPosition[1];
position[2] = newPosition[2];
setPosition(position);
}

void setPosition(vr::Vec4 newPosition){
position[0] = newPosition[0];
position[1] = newPosition[1];
position[2] = newPosition[2];
position[3] = newPosition[3];
setPosition(position);
}

void apply(GLuint);
};
#endif


#include <vr/Matrix.h>

#include "light.h"


using namespace std;


Light::Light(GLenum name_, vr::Vec4 amb, vr::Vec4 diff, vr::Vec4 spec, vr::Vec4 pos){
on = false;
name = name_;

ambient[0] = amb[0];
ambient[1] = amb[1];
ambient[2] = amb[2];
ambient[3] = amb[3];

diffuse[0] = diff[0];
diffuse[1] = diff[1];
diffuse[2] = diff[2];
diffuse[3] = diff[3];

specular[0] = spec[0];
specular[1] = spec[1];
specular[2] = spec[2];
specular[3] = spec[3];

position[0] = pos[0];
position[1] = pos[1];
position[2] = pos[2];
position[3] = pos[3];

setAmbient(ambient);
setDiffuse(diffuse);
setSpecular(specular);
setPosition(position);
}





// Constructor
Light::Light() {
on = false;
name = GL_LIGHT0;
ambient[0] = 0.2;
ambient[1] = 0.2;
ambient[2] = 0.2;
ambient[3] = 1;

diffuse[0] = 0.5;
diffuse[1] = 0.5;
diffuse[2] = 0.5;
diffuse[3] = 1;

specular[0] = 1;
specular[1] = 1;
specular[2] = 1;
specular[3] = 1;

position[0] = 3;
position[1] = 3;
position[2] = 3;
position[3] = 1;

setAmbient(ambient);
setDiffuse(diffuse);
setSpecular(specular);
setPosition(position);


}

// Destructor
Light::~Light() {}

void Light::apply(GLuint light){
glLightfv(light, GL_POSITION, position);
glLightfv(light, GL_AMBIENT, ambient);
glLightfv(light, GL_DIFFUSE, diffuse);
glLightfv(light, GL_SPECULAR,specular);
}


void Light::updatePosition(vr::Matrix modelView){
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadMatrixf(modelView.ptr());
setAmbient(ambient);
setDiffuse(diffuse);
setSpecular(specular);
setPosition(position);
glPopMatrix();
}