PDA

View Full Version : problem with light



bahare
05-27-2011, 12:59 AM
hi guys, I have some codes which is written for a robot, it has some advance features include light.my codes had 2 parts: 1:dynamic and kinematic equations 2: graphical code to have vision part. after completing 2 part I linked these 2 parts(while they arnt overlapping), before linking there was my light and just after adding there was no light. i dont know why. i wanted to attach my codes, but it was to much. so if any body can help first I appreciate so much and then plz let me know, to mail my codes to him/her, since i think without codes its a bit difficult to get my point. I sincerely again appreciate if anybody knows the solution and help me.

regards

BionicBytes
05-27-2011, 02:36 AM
you only need to post bits of the code - the part where lighting is setup/initialised and the drawing function.

BionicBytes
05-27-2011, 02:37 AM
oh, and use {code} {/code} to encapsulate your code to make it readable on these forums.
Replace {} with [] brackets when you actually post.

bahare
05-27-2011, 06:18 AM
oh, this the lighting part:

{
#include<vector>
#include<iostream>
#include<sstream>
#include<fstream>
#include<glut.h>
#include"vector.cpp"

using namespace std;

class Light
{
private:
int lightNo;
SF3dVector position; //an object from SF3dVector
SF3dVector direction; //an object from SF3dVector

float diffuse[4];
float ambient[4];
float specular[4];
float cutoff;
float exponent;
float temp[4];
bool visible;
float RotatedX, RotatedY;
SF3dVector RightVector; //an object from SF3dVector
SF3dVector UpVector; //an object from SF3dVector

void check()
{
for(int i=0;i<4;i++)
[
//if(diffuse[i]<0) //colored light
// diffuse[i]=0;
//if(diffuse[i]>1)
diffuse[i]=1;

//if(ambient[i]<0) //light in arbitary direction
// ambient[i]=0;
//if(ambient[i]>1)
ambient[i]=1;

//if(specular[i]<0) //A specular light is a light that sets highlights and often the shininess of an object
// specular[i]=0;
//if(specular[i]>1)
specular[i]=1;
]
if(exponent>1)
exponent=1;
if(exponent<0) ////ina chian bara chian?////
exponent=0;
if(cutoff<0)
cutoff=0;
if(cutoff>90 &amp;&amp; cutoff!=180)
cutoff=90;
}

public:
Light(int number) //number as a intiger input for loght function
{
this->lightNo=number;
this->temp[3]=1; //3 dimension array with 1 value
this->visible=false;
this->position=SF3dVector(); //position is same as SF3dVector() constructor in SF3dVector class
this->direction=SF3dVector(0,0,-1); //direction is same as SF3dVector(floatx, float y, float z) constructor in SF3dVector class

RightVector = SF3dVector (1.0, 0.0, 0.0);
UpVector = SF3dVector (0.0, 1.0, 0.0);
RotatedX = RotatedY = 0.0;
}

void init() //intialaizing each part: ambient, diffuse, specular, attenuation, cutoff, exp
{
this->OnOff();
this->setAmbient(1.0,1.0,1.0,1);
this->setDifuse(1,1,0,1);
this->setSpecular(1,1,1,0);
this->setAttenuation(2,0,0);
this->setCutoff(30);
this->setExp(4);
}

void render()
{
check();
this->temp[0]= position.getx(); //declare the 'x' component of position
this->temp[1]= position.gety(); //declare the 'y' component of position
this->temp[2]= position.getz(); //declare the 'z' component of position
glLightfv(this->lightNo,GL_POSITION,this->temp);//glLight set light source parameters
this->temp[0]= direction.getx();//declare the 'x' component of direction
this->temp[1]= direction.gety();//declare the 'y' component of direction
this->temp[2]= direction.getz();//declare the 'z' component of direction
glLightfv(this->lightNo,GL_SPOT_DIRECTION,this->temp);//glLight set light source parameters
}

void setDifuse(float r,float g,float b, float a) //function for setting the diffusion
{
this->diffuse[0]=r; //load the first of four array to red
this->diffuse[1]=g; //load the first of four array to green
this->diffuse[2]=b; //load the first of four array to blue
this->diffuse[3]=a; //load the first of four array to
glLightfv(this->lightNo,GL_DIFFUSE,this->diffuse);//glLight set light source parameters
}

void incDifuse(const int &amp;i,const float &amp;d) //function for increasing the diffusion
{
this->diffuse[i]+=d; //increase the diffusion by 'd' step
glLightfv(this->lightNo,GL_DIFFUSE,this->diffuse);//glLight set light source parameters
}

void setAmbient(float r,float g,float b, float a) //function for setting the ambient
{
this->ambient[0]=r; //load the first of four array to red
this->ambient[1]=g; //load the first of four array to green
this->ambient[2]=b; //load the first of four array to blue
this->ambient[3]=a;
glLightfv(this->lightNo,GL_AMBIENT,this->ambient); //glLight set light source parameters
}

void incAmb(const int &amp;i,const float &amp;d) //function for increasing the diffusion
{
this->ambient[i]+=d; //increase the diffusion by 'd' step
glLightfv(this->lightNo,GL_AMBIENT,this->ambient); //glLight set light source parameters
}

void setSpecular(float r,float g,float b, float a) //function for setting the ambient
{
this->specular[0]=r; //load the first of four array to red
this->specular[1]=g; //load the first of four array to green
this->specular[2]=b; //load the first of four array to blue
this->specular[3]=a;
glLightfv(this->lightNo,GL_SPECULAR,this->specular); //glLight set light source parameters
}

void incSpec(const int &amp;i,const float &amp;d) //function for increasing the diffusion
{
this->diffuse[i]+=d; //increase the diffusion by 'd' step
glLightfv(this->lightNo,GL_SPECULAR,this->specular); //glLight set light source parameters
}

void Move ( SF3dVector Direction ) //moving the light
{
this->position = this->position + Direction;
this->temp[0]=this->position.getx(); //declare the 'x' component of position after moving
this->temp[1]=this->position.gety(); //declare the 'y' component of position after moving
this->temp[2]=this->position.getz(); //declare the 'z' component of position after moving
glLightfv(this->lightNo,GL_POSITION,this->temp);
}

void setPosition(float x,float y,float z) //intializing position afetr moving
{
this->position=SF3dVector(x,y,z);
this->temp[0]=x;
this->temp[1]=y;
this->temp[2]=z;
glLightfv(this->lightNo,GL_POSITION,this->temp);
}

void setCutoff(float a) //spotlight cutoff angle
{
this->cutoff=a;
glLightf(this->lightNo,GL_SPOT_CUTOFF,this->cutoff);
}

void incCutoff(float v)
{
this->cutoff+=v;
glLightf(this->lightNo,GL_SPOT_CUTOFF,this->cutoff);
}

void setExp(float exp) //control how concentrated the light is
{
this->exponent=exp;
glLightf(this->lightNo,GL_SPOT_EXPONENT,this->exponent);
}

void incExp(float exp)
{
this->exponent+=exp;
glLightf(this->lightNo,GL_SPOT_EXPONENT,this->exponent);
}

void setAttenuation(float constant, float linear, float quadratic)
{
glLightf(this->lightNo, GL_CONSTANT_ATTENUATION, constant);
glLightf(this->lightNo, GL_LINEAR_ATTENUATION, linear);
glLightf(this->lightNo, GL_QUADRATIC_ATTENUATION, quadratic);
}

void setDirection(float x,float y,float z)
{
this->direction=SF3dVector(x,y,z);
this->temp[0]=x;
this->temp[1]=y;
this->temp[2]=z;
glLightfv(this->lightNo,GL_SPOT_DIRECTION,this->temp);
}

void setDirection(SF3dVector dir)
{
this->direction=dir;
this->temp[0]=dir.getx();
this->temp[1]=dir.gety();
this->temp[2]=dir.getz();
glLightfv(this->lightNo,GL_SPOT_DIRECTION,this->temp);
}

void setRight(const float &amp;x,const float &amp;y,const float &amp;z)
{
this->RightVector=SF3dVector(x,y,z);
}

void setUpvector(const float &amp;x,const float &amp;y,const float &amp;z)
{
this->UpVector=SF3dVector(x,y,z);
}

void RotateX ( float Angle )
{
RotatedX += Angle;
setDirection((direction*cos(Angle*PIdiv180)+ UpVector*sin(Angle*PIdiv180)).Normalize());
UpVector = direction.CrossProduct(RightVector)*-1;
}

void setRotate(const float &amp;x,const float &amp;y)
{
this->RotatedX=x;
this->RotatedY=y;
}

void RotateY (GLfloat Angle)
{
RotatedY += Angle;
setDirection((direction*cos(Angle*PIdiv180)- RightVector*sin(Angle*PIdiv180)).Normalize());
RightVector = direction.CrossProduct(UpVector);
}

void MoveForward( GLfloat Distance )
{
this->position = this->position + (this->direction*-Distance);
}

void StrafeRight ( GLfloat Distance )
{
this->position = this->position + (RightVector*Distance);
}

void MoveUpward( GLfloat Distance )
{
this->position = this->position + (UpVector*Distance);
}

string toString()
{
stringstream str;
str<< "LightNo.Is: "<< this->lightNo-GL_LIGHT0<< endl;
str<< "Visible: "<< this->visible<< endl;
str<< "Pos: "<< this->position.getx()<< " "<< this->position.gety()<< " "<< this->position.getz()<<endl;
str<< "Dir: "<< this->direction.getx()<< " "<< this->direction.gety()<< " "<< this->direction.getz()<<endl;
str<< "Up: "<< UpVector.getx()<< " "<< UpVector.gety()<< " "<< UpVector.getz()<<endl;
str<< "Right: "<< RightVector.getx()<< " "<< RightVector.gety()<< " "<< RightVector.getz()<<endl;
str<< "Rotate: "<< RotatedX<< " "<< RotatedY<< endl;
str<< "Ambient: "<< this->ambient[0]<< " "<< this->ambient[1]<< " "<< this->ambient[2]<< " "<< this->ambient[3]<< endl;
str<< "Difuse: "<< this->diffuse[0]<< " "<< this->diffuse[1]<< " "<< this->diffuse[2]<< " "<< this->diffuse[3]<< endl;
str<< "Speclar: "<< this->specular[0]<< " "<< this->specular[1]<< " "<< this->specular[2]<< " "<< this->specular[3]<< endl;
str<< "Exponent: "<< this->exponent<< endl<< "Cutoff: "<< this->cutoff<< endl;
return str.str();
}

void OnOff()
{
visible=!visible;
if(this->visible)
glEnable(this->lightNo);
else
glDisable(this->lightNo);
}

void FinInfin()
{
temp[3]=!temp[3];
}

void DrawDirection() //draw the direction of the exsist light
{
if(!this->visible)
return;
SF3dVector t;
t=position-direction;
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(t.getx(),t.gety(),t.getz());
glutSolidSphere(.1,25,25);
glScalef(2,2,2);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3fv(SF3dVector().getV());
glVertex3fv(this->direction.getV());
glColor3f(0,1,0);
glVertex3fv(SF3dVector().getV());
glVertex3fv(this->RightVector.getV());
glColor3f(0,0,1);
glVertex3fv(SF3dVector().getV());
glVertex3fv(this->UpVector.getV());
glEnd();
t=this->direction;
glTranslatef(t.getx(),t.gety(),t.getz());
glutSolidSphere(.1,25,25);
glPopMatrix();
}
};

class Lights
{
private:
int max;
int curLight;
bool draw;
vector<Light> lights;
float colorStep;
float degreeStep;
float distanceStep;
public:
Lights()
{
this->colorStep=1.0/255.0;
this->distanceStep=.1;
this->degreeStep=1;
this->lights = vector<Light>();
this->draw=false;
}

void init()
{
int temp;
glGetIntegerv(GL_MAX_LIGHTS,&amp;temp);
this->max=temp;
for(int i=0;i<this->max;i++)
{
this->lights.push_back(Light(i+GL_LIGHT0));
this->lights[i].init();
}
Load();
}

void Render()
{
for(int i=0;i<this->max;i++)
{
this->lights[i].render();
}
}

void Draw()
{
this->draw=!this->draw;
}

string toString()
{
stringstream str;
str<< "Total_Lights: "<< this->max<< "\nCurrent_Light: "<< this->curLight<< endl;
str<< "Draw_Directions: "<< this->draw<< endl;
for(int i=0;i<this->max;i++)
{
str<< this->lights[i].toString()<< ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n";
}
return str.str();
}

void DrawDirections()
{
if(!this->draw)
return;
for(int i=0;i<this->max;i++)
{
this->lights[i].DrawDirection();
}
}

void Save()
{
ofstream out("mlights.obj");
if(out.bad())
throw exception("we have problem about light");
for(int i=0;i<this->max;i++)
out<< this->toString()<< endl;
}

void Load()
{
ifstream input("mlights.obj");
if( input.bad() )
return;
//throw exception("BAD");
string temp="";
float x,y,z,a;
int index,sz;
input>> temp>> sz>> temp>> this->curLight>> temp>> this->draw;

for(int i=0;i<this->max; i++)
{
input>> temp>> index>> temp>> x;
if(!x)
this->lights[index].OnOff();
input>> temp>> x>> y>> z;
this->lights[index].setPosition(x,y,z);

input>> temp>> x>> y>> z;
this->lights[index].setDirection(x,y,z);

input>> temp>> x>> y>> z;
this->lights[index].setUpvector(x,y,z);

input>> temp>> x>> y>> z;
this->lights[index].setRight(x,y,z);

input>> temp>> x>> y;
this->lights[index].setRotate(x,y);

input>> temp>> x>> y>> z>> a;
this->lights[index].setAmbient(x,y,z,a);

input>> temp>> x>> y>> z>> a;
this->lights[index].setDifuse(x,y,z,a);

input>> temp>> x>> y>> z>> a;
this->lights[index].setSpecular(x,y,z,a);

input>> temp>> x>> temp>> y;
this->lights[index].setExp(x);
this->lights[index].setCutoff(y);
input>> temp;
}
}

void Do(char ch)
{
if( ch>='1' &amp;&amp; ch<='8')
this->curLight=ch-'1';
else
switch (ch)
{
case 'a':
this->lights[curLight].RotateY(degreeStep);
break;
case 'd':
this->lights[curLight].RotateY(-degreeStep);
break;
case 'w':
this->lights[curLight].MoveForward( -distanceStep ) ;
break;
case 's':
this->lights[curLight].MoveForward( distanceStep ) ;
break;
case 'x':
this->lights[curLight].RotateX(degreeStep);
break;
case 'y':
this->lights[curLight].RotateX(-degreeStep);
break;
case 'c':
this->lights[curLight].StrafeRight(-distanceStep);
break;
case 'v':
this->lights[curLight].StrafeRight(distanceStep);
break;
case 'f':
this->lights[curLight].MoveUpward(-distanceStep);
break;
case 'r':
this->lights[curLight].MoveUpward(distanceStep);
break;
case 'b':
this->lights[curLight].OnOff();
break;
case '+':
this->lights[curLight].incCutoff(degreeStep);
break;
case '-':
this->lights[curLight].incCutoff(-degreeStep);
break;
case 't':
system("cls");
cout<< this->toString();
break;
case 'g':
this->lights[curLight].FinInfin();
break;
case 'q':
this->Draw();
break;
case 'e':
this->lights[curLight].incExp(.1);
break;
case 'E':
this->lights[curLight].incExp(-.1);
break;
case 'm':
this->lights[curLight].incSpec(0,this->colorStep);
break;
case 'M':
this->lights[curLight].incSpec(0,-this->colorStep);
break;
case ',':
this->lights[curLight].incSpec(1,this->colorStep);
break;
case '<':
this->lights[curLight].incSpec(1,-this->colorStep);
break;
case '.':
this->lights[curLight].incSpec(2,this->colorStep);
break;
case '>':
this->lights[curLight].incSpec(2,-this->colorStep);
break;
case '/':
this->lights[curLight].incSpec(3,this->colorStep);
break;
case '?':
this->lights[curLight].incSpec(3,-this->colorStep);
break;
case 'k':
this->lights[curLight].incDifuse(0,this->colorStep);
break;
case 'K':
this->lights[curLight].incDifuse(0,-this->colorStep);
break;
case 'l':
this->lights[curLight].incDifuse(1,this->colorStep);
break;
case 'L':
this->lights[curLight].incDifuse(1,-this->colorStep);
break;
case ';':
this->lights[curLight].incDifuse(2,this->colorStep);
break;
case ':':
this->lights[curLight].incDifuse(2,-this->colorStep);
break;
case '\'':
this->lights[curLight].incDifuse(3,this->colorStep);
break;
case '"':
this->lights[curLight].incDifuse(3,-this->colorStep);
break;
case 'o':
this->lights[curLight].incAmb(0,this->colorStep);
break;
case 'O':
this->lights[curLight].incAmb(0,-this->colorStep);
break;
case 'p':
this->lights[curLight].incAmb(1,this->colorStep);
break;
case 'P':
this->lights[curLight].incAmb(1,-this->colorStep);
break;
case '[':
this->lights[curLight].incAmb(2,this->colorStep);
break;
case '{':
this->lights[curLight].incAmb(2,-this->colorStep);
break;
case ']':
this->lights[curLight].incAmb(3,this->colorStep);
break;
case '}':
this->lights[curLight].incAmb(3,-this->colorStep);
break;
case 'S':
this->Save();
break;
}
}

int GeSubMenu(int start,void *evn)
{
int res=glutCreateMenu((void (__cdecl*)(int)) evn);
for(int i=0;i<this->max;i++)
{
glutAddMenuEntry("projector ",i+start);
}
return res;
}
};
}

bahare
05-27-2011, 06:19 AM
and here it is the main:

{#using <system.dll>

#include<glut.h>
#include<windows.h>
#include"camera.cpp"
#include"lights.cpp"
#include"ball.cpp"
#include"axises.cpp"
#include"Robot.cpp"
#include"plane.cpp"
#include <stdio.h>
#include <math.h>

float resultArray[2000][3];
int resultCount=0;
int resultIndex=0;
/////////
void MatrixTranspose(double A[3][3], double B[3][3])
{
int i;
int j;
for(i = 0; i < 3; i++)
for(j = 0;j < 3; j++)
A[i][j] = B[j][i];
}

//-----------------------------------------------------------------------------
double Matrix3X3Det(double A[3][3])
{

double inverse_00 = A[1][1]*A[2][2] - A[1][2]*A[2][1];
double inverse_10 = A[1][2]*A[2][0] - A[1][0]*A[2][2];
double inverse_20 = A[1][0]*A[2][1] - A[1][1]*A[2][0];


double det = A[0][0] * inverse_00 +
A[0][1] * inverse_10 +
A[0][2] * inverse_20;

return det;
}
//---------------- inv: A = A^{-1} ---------------------------------------
void MatrixInverse(int z_size, double A[3][3])
{

//int z_size=3; // khodam ezafeeeeeeeeeee kardam???????????
int i;
int j;
int k;
for (k = 0; k < z_size; k++)

// the pivot element
{
A[k][k] = -1 / A[k][k];

// the pivot column
for (i = 0; i < z_size; i++)
if (i != k) A[i][k] *= A[k][k];

// elements not in a pivot row or column
for (i = 0; i < z_size; i++)
if (i != k)
for (j = 0;j < z_size; j++)
if (j != k)
A[i][j] += A[i][k] * A[k][j];

// elements in a pivot row
for (i = 0; i < z_size; i++)
if (i != k)
A[k][i] *= A[k][k];
}

// change sign
for (i = 0; i < z_size; i++) //reverse sign
for (j = 0; j < z_size; j++)
A[i][j] = -A[i][j];

}


//---------------C = A * B-----------------------------------------------------
void MatrixVectorMul(double C[3], double A[3][3], double B[3])
{
int a;
int b;
C[0]=C[1]=C[2]=0;
for(a=0;a<3;a++)
for(b=0;b<3;b++)
C[a] = C[a] + A[a][B]*B[B];
}


//---------------C = A * B-----------------------------------------------------
void MatrixMatrixMul(double C[3][3], double A[3][3], double B[3][3])
{

int a;
int b;
for(a=0;a<3;a++)
for(b=0;b<3;b++)
C[a][B] = A[a][0]*B[0][B]+A[a][1]*B[1][B]+A[a][2]*B[2][B];
}
//---------------C = A + B-----------------------------------------------------
void MatrixMatrixplus2(double C[3][3], double A[3][3], double B[3][3])
{
int a;
int b;
for(a=0;a<3;a++)
for(b=0;b<3;b++)
C[a][B] = A[a][B]+ B[a][B];//*B[0][B]+A[a][1]*B[1][B]+A[a][2]*B[2][B];
}
//------------- C = A + B (vectror) ----------------------------------------------------------
void MatrixMatrixplus(double C[3], double A[3], double B[3])
{
int a;

for(a=0;a<3;a++)
C[a] = A[a]+ B[a];//*B[0][B]+A[a][1]*B[1][B]+A[a][2]*B[2][B];
}
//------------- C=-A ----------------------------------------------------------
void NegativeMatrix(double C[3], double A[3])
{
int a;
for(a=0;a<3;a++)
C[a]=-A[a];

/*void square(int i)
{
int sq = i * i;
cout << "Square of " << i << " is " << sq;
}*/


}

/////////////////////////////////////////////////////////////

void Dynamic(double result[], double q[])//, double qdot[3] )//,double KF[3], double Mmdot[3][3])
{
double M[3][3],V[3],G[3];

M[0][0] = 0.25*pow(cos(q[1] + q[2]),2) + cos(q[1] + q[2])*cos(q[1]) + 0.25*(5*pow(cos(q[1]),2)) + 9;
M[0][1] = 0;
M[0][2] = 4;

M[1][0] = 0;
M[1][1] = cos(q[2]) + 8.500000;
M[1][2] = 0.5*cos(q[2]) + 0.250000;

M[2][0] = 4;
M[2][1] = cos(q[2])/2 + 0.250000;
M[2][2] = 4.250000;

V[0] =-(q[3]*(5*q[4]*sin(2*q[1]) + q[4]*sin(2*q[1] + 2*q[2]) + q[5]*sin(2*q[1] + 2*q[2]) + 2*q[5]*sin(q[2]) + 4*q[4]*sin(2*q[1] + q[2]) + 2*q[5]*sin(2*q[1] + q[2])))*0.25;
V[1] =(pow(q[3],2)*sin(2*q[1] + q[2]))*0.5 - (pow(q[5],2)*sin(q[2]))*0.5 + (5*pow(q[3],2)*sin(2*q[1]))*0.125 + (pow(q[3],2)*sin(2*q[1] + 2*q[2]))*0.125 - q[4]*q[5]*sin(q[2]);
V[2] = (pow(q[3],2)*sin(q[2]))*0.25 + (pow(q[4],2)*sin(q[2]))*0.5 + (pow(q[3],2)*sin(2*q[1] + q[2]))*0.25 + (pow(q[3],2)*sin(2*q[1] + 2*q[2]))*0.125;

G[0] = 0;
G[1] = 0.1*(49*cos(q[1] + q[2])) + 0.1*(147*cos(q[1]));
G[2] = 0.1*(49*cos(q[1] + q[2]));


//double a0=1.0000000;double a1=1.0000000;
// double m1=1.0000000;double m2=1.0000000;
//double I0=2.0000000;double I1=3.0000000; double I2=4.0000000;
//double g=9.800000;
//double kz=0.200000;
//// M matrices in the joint space
////notice that M is symmetric

// M[0][0] = I0 + (pow(a1,2)*m2*pow(cos(q[1] + q[2]),2))/4 + (pow(a0,2)*m1*pow(cos(q[1]),2))/4 + pow(a0,2)*m2*pow(cos(q[1]),2) + a0*a1*m2*cos(q[1] + q[2])*cos(q[1]);//sin(q[1])
//M[0][1] = 0;
//M[0][2] = 0;

//M[1][0] = 0;
//M[1][1] = I1 + (pow(a0,2)*m1)/4 + pow(a0,2)*m2 + (pow(a1,2)*m2)/4 + a0*a1*m2*cos(q[2]);//5.0000*cos(q[2])
//M[1][2] = (a1*m2*(a1 + 2*a0*cos(q[2])))/4;

//M[2][0] = 0;
//M[2][1] = (a1*m2*(a1 + 2*a0*cos(q[2])))/4;//q[1]
//M[2][2] = (m2*pow(a1,2))/4 + I2;//pai5;

////notice that C is skew-symmetric
//V[0] = -(q[3]*(pow(a0,2)*m1*q[4]*sin(2*q[1]) + 4*pow(a0,2)*m2*q[4]*sin(2*q[1]) + pow(a1,2)*m2*q[4]*sin(2*q[1] + 2*q[2]) + pow(a1,2)*m2*q[5]*sin(2*q[1] + 2*q[2]) + 4*a0*a1*m2*q[4]*sin(2*q[1] + q[2]) + 2*a0*a1*m2*q[5]*sin(2*q[1] + q[2]) + 2*a0*a1*m2*q[5]*sin(q[2])))/4;//3.0000*q[5]*sin(q[0]);
//V[1] = (pow(a0,2)*m1*pow(q[3],2)*sin(2*q[1]))/8 + (pow(a0,2)*m2*pow(q[3],2)*sin(2*q[1]))/2 + (pow(a1,2)*m2*pow(q[3],2)*sin(2*q[1] + 2*q[2]))/8 - (a0*a1*m2*pow(q[5],2)*sin(q[2]))/2 + (a0*a1*m2*pow(q[3],2)*sin(2*q[1] + q[2]))/2 - a0*a1*m2*q[4]*q[5]*sin(q[2]);//3.0000*sin(q[2 ]);
//V[2] =(a1*m2*(a1*pow(q[3],2)*sin(2*q[1] + 2*q[2]) + 2*a0*pow(q[3],2)*sin(q[2]) + 4*a0*pow(q[4],2)*sin(q[2]) + 2*a0*pow(q[3],2)*sin(2*q[1] + q[2])))/8;//2.0000;


//G[0] = 0;
//G[1] = g*m2*((a1*cos(q[1] + q[2]))/2 + a0*cos(q[1])) + (a0*g*m1*cos(q[1]))/2;//2.0000*cos(q[2]);
//G[2] = (a1*g*m2*cos(q[1] + q[2]))/2;//3.0000;
// M matrices in the joint space
//notice that M is symmetric

////////adadi
//M[0][0]= pow(cos(q[1] + q[2]),2)/4 + cos(q[1] + q[2])*cos(q[1]) + (5*pow(cos(q[1]),2))/4 + 2;
//M[0][1]= 0;
//M[0][2]= 0;
//
//M[1][0]= 0;
//M[1][1]= cos(q[2]) + 9/2;
//M[1][2]= cos(q[2])/2 + 1/4;
//
//M[2][0]= 0;
//M[2][1]= cos(q[2])/2 + 1/4;
//M[2][2]= 17/4;
//
////notic
//V[0] = -(q[3]*(5*q[4]*sin(2*q[1]) + q[4]*sin(2*q[1] + 2*q[2]) + q[5]*sin(2*q[1] + 2*q[2]) + 2*q[5]*sin(q[2]) + 4*q[4]*sin(2*q[1] + q[2]) + 2*q[5]*sin(2*q[1] + q[2])))/4;
//V[1] = (pow(q[3],2)*sin(2*q[1] + q[2]))/2 - (pow(q[5],2)*sin(q[2]))/2 + (5*pow(q[3],2)*sin(2*q[1]))/8 + (pow(q[3],2)*sin(2*q[1] + 2*q[2]))/8 - q[4]*q[5]*sin(q[2]);
//V[2] = (pow(q[3],2)*sin(q[2]))/4 + (pow(q[4],2)*sin(q[2]))/2 + (pow(q[3],2)*sin(2*q[1] + q[2]))/4 + (pow(q[3],2)*sin(2*q[1] + 2*q[2]))/8;
//
//
//G[0] = 0;
//G[1] = (49*cos(q[1] + q[2]))/10 + (147*cos(q[1]))/10;
//G[2] = (49*cos(q[1] + q[2]))/10;



////////////////////random example
// M[0][0] = sin(q[1]);//pai1+pai2*SQ(cos(Th[1]))+(pai3+pai5)*SQ(sin(Th[2]))+2*pai6*cos(Th[1])*sin(Th[2]);
//M[0][1] = 2.000000;
//M[0][2] = 5.000000;

//M[1][0] = 7.000000;
//M[1][1] = 5.000000*cos(q[2]);//pai4+pai5-2*pai6*sin(Th[1]-Th[2]);
//M[1][2] = 4.000000;//pai5-pai6*sin(Th[1]-Th[2]);

//M[2][0] = 0.000000;
//M[2][1] = q[1];//pai5-pai6*sin(Th[1]-Th[2]);
//M[2][2] = 2.000000;//pai5;

////notice that C is skew-symmetric
//V[0] = 3.000000*q[5]*sin(q[0]);//-Thdot[1]*(pai2*sin(Th[1])*cos(Th[1])+pai6*sin(Th[1])*sin(Th[2]))+Thdot[2]*((pai3+pai5)*sin(Th[2])*cos(Th[2])+pai6*cos(Th[1])*cos(Th[2]));
//V[1] = 3.000000*sin(q[2]);//-Thdot[0]*(pai2*sin(Th[1])*cos(Th[1])+pai6*sin(Th[1])*sin(Th[2]));
//V[2] =2.000000;// Thdot[0]*((pai3+pai5)*sin(Th[2])*cos(Th[2])+pai6*cos(Th[1])*cos(Th[2]));


//G[0] = 0.000000;
//G[1] = 2.000000*cos(q[2]);//pai7*cos(Th[1]);
//G[2] = 3.000000;//pai8*sin(Th[2]);
///////////////////////

double r[3] = {0,0,0};
MatrixMatrixplus(r, V, G);
double s[3] = {0,0,0};
MatrixInverse(3,M);
MatrixVectorMul(s, M, r);
NegativeMatrix(result, s);
}

//Example: y1'=y2+y3-3y1, y2'=y1+y3-3y2, y3'=y1+y2-3y3
double fp(int k, double x, double *y) {
double result[3];
Dynamic(result,y);

switch(k) {
case 0: return y[3];
case 1: return y[4];
case 2: return y[5];
case 3: return result[0];
case 4: return result[1];
case 5: return result[2];
default: return 0;

}
}

// print results
void Display(double *t1,double *t2,double *t3,double *t4,double *t5,double *t6,int m,int p,double xi,double xf) {
int i;
double h,x;
h=(xf-xi)/(m-1);
x=xi-h;
printf("\n X");
for (i=1; i<=p; i++) printf(" Y%d", i);
printf("\n");
printf("-------------------------------------------------------------------------------\n");

resultCount=m;
for (i=1; i<m+1; i++) {
x += h;
resultArray[i][0]=57.295780*t1[i];
resultArray[i][1]=57.295780*t2[i];
resultArray[i][2]=57.295780*t3[i];
printf(" %9.6f %9.6f %9.6f %9.6f %9.6f %9.6f %9.6f\n",x,57.295780*t1[i],57.295780*t2[i],57.295780*t3[i],57.295780*t4[i],57.295780*t5[i],57.295780*t6[i]);
}
// resultArray=new Point3D[m];
// resultCount=m;
// for (i=1; i<m+1; i++) {
// x += h;
//resultArray[i]=Point3D(t1[i],t2[i],t3[i]);
//printf(" %9.6f %9.6f %9.6f %9.6f %9.6f %9.6f %9.6f\n",x,t1[i],t2[i],t3[i],t4[i],t5[i],t6[i]);
// }
printf("--------------------------------------------------------------------------------\n");
}

void Eqdifp(double *t1,double *t2,double *t3,double *t4,double *t5,double *t6,double xi,double xf,double *Yi,
int m,int p,int fi)
{
double h,x;
double ta[10],tb[10],tc[10],td[10],y[10],z[10];
int i,j,k,ni;
if (fi<1) return;
h = (xf - xi) / fi / (m-1);
p--;
t1[1]=Yi[0];
t2[1]=Yi[1];
t3[1]=Yi[2];
t4[1]=Yi[3];
t5[1]=Yi[4];
t6[1]=Yi[5];


for (k=0; k<p+1; k++) {
y[k]=Yi[k]; z[k]=Yi[k];
}
for (i=1; i<m+1; i++) {
ni=(i-1)*fi-1;
for (j=1; j<fi+1; j++) {
x=xi+h*(ni+j); // baraye chieeeeeeeeeeeeeeeeeeeeeeeeeee?
for (k=0; k<p+1; k++) y[k]=z[k];
for (k=0; k<p+1; k++) ta[k]=h*fp(k,x,y);
for (k=0; k<p+1; k++) y[k]=z[k]+ta[k]/2;
x=x+h/2;
for (k=0; k<p+1; k++) tb[k]=h*fp(k,x,y);
for (k=0; k<p+1; k++) y[k]=z[k]+tb[k]/2;
for (k=0; k<p+1; k++) tc[k]=h*fp(k,x,y);
for (k=0; k<p+1; k++) y[k]=z[k]+tc[k];
x=x+h/2;
for (k=0; k<p+1; k++) td[k]=h*fp(k,x,y);
for (k=0; k<p+1; k++)
z[k]=z[k]+(ta[k]+2*tb[k]+2*tc[k]+td[k])/6;
}
t1[i+1]=z[0];
t2[i+1]=z[1];
t3[i+1]=z[2];
t4[i+1]=z[3];
t5[i+1]=z[4];
t6[i+1]=z[5];
}
Display(t1,t2,t3,t4,t5,t6,m,p+1,xi,xf);
}

////////
Camera c;
Lights ls;
Ball b(.13);
Axises ax;
Robot r(.05,.5);
Plane pl;
int bi=0,bi1=0;
void reshape(int x, int y)
{
// r=*new Robot(float b,float a,float c,float d,float e,float f)//base- arm lenght- q1-q2-q3-q hand
//if (y == 0 || x == 0) return; //Nothing is visible then, so return

//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Angle of view:40 degrees
//Near clipping plane distance: 0.5
//Far clipping plane distance: 30.0
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,30);

glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y); //Use the whole window for rendering
}
int i=0;
void Display(void)
{
//////////////////////////////////
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

c.Render();
ls.Render();

glColor3f(.8,.8,.8);
//w.Draw();
r.Draw();
ax.Draw();
pl.Draw();
b.Draw();
ls.DrawDirections();
glutSwapBuffers();
}

int h=0;
int y;
int th=0;
void KeyDown(unsigned char key, int x, int y)
{

if(key=='1')
{
r.SetTetaArm1(i++);
}
if(key=='2')
{
r.SetTetaArm1(i--);
}
if(key=='3')
{
r.SetTetaArm2(bi++);
}
if(key=='4')
{
r.SetTetaArm2(bi--);
}
/*if(key=='3')
{
r.SetTetaHand(th++);
}*/
if(key=='b')
{
r.SetTetaBase(bi1++);
}
if(key==27)
PostQuitMessage(0);
switch(h)//-y)
{
case 0:
c.Do(key);
break;
case 1:
ls.Do(key);
break;
case 2:
b.Do(key);
break;
default:
//w.Do(h,key);
break;
}
glutPostRedisplay();
}

void myinit()
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);

glShadeModel(GL_SHADE_MODEL);
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
ls.init();

glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE) ;

r.SetBall(&amp;b);
}

void menu(int ch)
{
h=ch;
}

void createMenu()
{
int i=0;
glutCreateMenu(menu);
glutAddMenuEntry("Camera",i++);
glutAddMenuEntry("Lights",i++);
glutAddMenuEntry("Ball",i++);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}


void OnTimedEvent( System::Object^ source, System::Timers::ElapsedEventArgs^ e )
{
resultIndex++;
if(resultIndex>=resultCount)
{
((System::Timers::Timer^)source)->Enabled = false;
return;
}
r.SetTetaBase(resultArray[resultIndex][0]);
r.SetTetaArm1(resultArray[resultIndex][1]);
r.SetTetaArm2(resultArray[resultIndex][2]);

//cout<<"\n x of base is"<<r.targetBase.getX()<<endl;
//cout<<"\n x of Arm1 is"<<r.targetArm1.getX()<<endl;
//cout<<"\n x of base is"<<r.targetArm2.getX()<<endl;

//r.SetTetaBase(resultArray[resultIndex].GetX());
//r.SetTetaArm1(resultArray[resultIndex].GetY());
//r.SetTetaArm2(resultArray[resultIndex].GetZ());

glutPostRedisplay();
}
//cout<<r.targetHand.getX();
/////////////////////////////////////////////////////
#define SIZE 2000 //baraye chieeeeeeeeeeeeee?

int fi,ndata,p=6.00;
double xi,xf;
double yi[10];
double pi=3.1416;
double v1[SIZE],v2[SIZE],v3[SIZE],v4[SIZE],v5[SIZE],v6[SIZE]; //baraye chieeeeeeeeeeeeeee?
/////////////////////////////////////////////////////

int main(int argc, char **argv)
{
/////////////////////////////////////////////////////
printf("\n DIFFERENTIAL EQUATIONS WITH 6 VARIABLES OF ORDER 1\n");
printf(" of type yi' = f(y1,y2,...,yn), i=1..n\n\n");

xi=0;
xf=5;
printf("\n end value x : %f ",xf);
i=0; yi[i]=0;
i++;yi[i]=-1.570796;
i++;yi[i]=-1.570796;
i++;yi[i]=0;
i++;yi[i]=0;
i++;yi[i]=0;
ndata=501; fi=1;

// printf("\n begin value x : ");
// scanf("%lf",&amp;xi);
// printf(" end value x : ");
// scanf("%lf",&amp;xf);
// for (int i=0; i<p; i++) {
// printf(" y%d value at x0 : ",i+1);
//scanf("%lf",&amp;yi[i]);
// }
// printf(" number of points : ");
// scanf("%d",&amp;ndata);
// printf(" finesse : ");
// scanf("%d",&amp;fi);

Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);


/////////////////////////////////////////////////////
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(900,500);
int u=glutCreateWindow("3DOF robot");
myinit();
createMenu();
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(KeyDown);
System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 10 );
//tt=aTimer;
// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler( OnTimedEvent );

// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Enabled = true;
glutMainLoop();
return 0;
}
}

BionicBytes
05-27-2011, 06:51 AM
So you didn't bother to read my last post about using tags. Nice.

BionicBytes
05-27-2011, 06:56 AM
It would help if you post the parts which are relevant and trim the rest. We don't need to see pages of comments and key press code.

You have glEnable(GL_ColorMaterial)
Try disabling that instead.