• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Need help with C++ OpenGL program

ConfusedNewbie

New Member
Hi ppls, I have nil programming experience with C++ and OpenGL (spent 2-3 weeks learning). Which will explain why the following program might be pretty primitive or inefficient. Hope some kind soul out there can help me sort this out as I've spent days and lots of coffee hitting a brick wall. Dont worry, this is not homework:) just some simple demo i MUST do before going on to deeper stuff.

My robot looks something like where angle 2 is positive anticlockwise relative to link1. Angle1 is positive anticlockwise from the horizontal (x) axis.

angle1 angle2
O---------0------------
link1 link2
0,0 x1,y1 x2,y2


_________________________________________________________________
// What this program is supposed to do is:
// i) Use OpenGL to draw a two link planar robot where the dragged mouse coordinates indicate the end
// effector (x2, y2). (x0, y0) refers to the first joint or 'shoulder' at the base whereas (x1, y1) is the 2nd
// joint or 'elbow'. These links are just simple lines.
// ii) The angles and joint coordinates of the robot are determined based on a set of inverse kinematics equations.
// iii) If the mouse moves outside the robot workspace (a circle or radius link1+link2), the mouse
// will be deactivated and the last position and orientation is kept until the mouse moves in the
// workspace again.


#include <iostream.h>
#include <string>
#include <fstream.h>
#include <cmath>
#include <iomanip.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

//Declare some stuff
const float link1=8;
const float link2=7;
const double T_SAMPLE=0.1;
float angle2;
float pi;
int i=0;

//Declare a structure to store mouse input coordinates.
struct Effdata
{
float x2, y2, time;
};

Effdata t[10000000];

//Angle 21 (named that way because of ambiguity - two solutions for angle2, one + one -)
float angle21 (float x2, float y2)
{
float a;

a=2*atan(sqrt((pow((link1+link2),2)-(pow(x2,2)+pow(y2,2)))/
((pow(x2,2)+pow(y2,2))-pow((link1-link2),2))));

return (a);
}

//Angle 1
float angle1 (float x2, float y2, float angle2)
{
float b, c;
b=angle2;
c=atan(y2/x2)-atan((link2*sin(b))/(link1+link2*cos(b)));
return (c);
}

//Coordinate x1
float x1 (float x2, float y2)
{
float d, e;

d=angle1(x2, y2, angle2);
e=link1*cos(d);

return (e);
}

//Coordinate y1
float y1 (float x2, float y2)
{
float f, g;

f=angle1(x2, y2, angle2);
g=link1*sin(f);

return (g);
}

//Init
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

//Draw the robot
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f(0.0,0.0);
glVertex2f(x1, y1);
glVertex2f(x1, y1);
glVertex2f(t.x2, t.y2);
glEnd();
}

//Reshape
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}

//Mouse- trying to deal with mouse activation only in robot workspace
void Mouse (int x, int y)
{
if ((pow(t.x2,2)+pow(t.y2,2)-pow((link1+link2),2))<=0)
{
t.x2=x;
t.y2=y;
i++;
}
return ();
}


// Main program
int main(int argc, char** argv)
{
float x2dot, y2dot;
float angle1dot, angle2dot;
double pi=3.142;


glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutMotionFunc(Mouse);

//This if-else is supposed to deal with the ambiguity, it simply says if angle21 is 180
// or 0 deg, the next angle should be the opposite sign of the last angle before angle2
// reached 180 or 0 deg.
{
if (((angle21(t.x2,t.y2)==pi)||(angle21(t.x2,t.y2)==0))
&&(angle21(t[i-1].x2,t[i-1].y2>0)))
angle2=angle21(t[i+1].x2,t[i+1].y2)*(-1);
else
angle2=angle21(t[i+1].x2,t[i+1].y2);
}
//Calculate x1, y1, angle1 based on angle2
x1 (t.x2,t.y2);
y1 (t.x2,t.y2);
angle1(t.x2,t.y2,angle2);

//These velocities are just optional stuff. I don't know how to use the mouse velocity to replace x2dot and y2dot.

x2dot=(t.x2-t[i-1].x2)/T_SAMPLE;
y2dot=(t.y2-t[i-1].y2)/T_SAMPLE;
angle1dot=(((x2dot*cos(angle1(t.x2,t.y2, angle2)+angle2))+
(y2dot*sin(angle1(t.x2,t.y2, angle2)+angle2)))/(link1*sin(angle2)));
angle2dot=(angle1dot-((x2dot*cos(angle1(t.x2,t.y2, angle2))+
y2dot*sin(angle1(t.x2,t.y2, angle2)))/(link2*sin(angle2))));

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();

return (0);
}

_________________________________________________________________
I'm using VC++ 6.0 and the error generated is:

Compiling...
Robot plus OpenGL2.cpp
C:\robot2\Robot plus OpenGL2.cpp(92) : error C2664: 'glVertex2f' : cannot convert parameter 1 from 'float (float,float)' to 'float'
Context does not allow for disambiguation of overloaded function
C:\robot2\Robot plus OpenGL2.cpp(93) : error C2664: 'glVertex2f' : cannot convert parameter 1 from 'float (float,float)' to 'float'
Context does not allow for disambiguation of overloaded function
C:\robot2\Robot plus OpenGL2.cpp(119) : error C2059: syntax error : ')'
Error executing cl.exe.

Robot plus OpenGL2.obj - 3 error(s), 0 warning(s)

--------------------------------------------------------------------------
Since I could never resolve this, i have not viewed the results...

I need to initialize angle21 first (give the robot a starting configuration) but not sure where i should do this since am a bit unclear on how the OpenGL loops.

I also have one way of dealing with the ambiguity of angle2 which is setting a constraint on the mouse. When the mouse moves out of the workspace, the robot will be fully extended (like the diagram). Therefore, it MUST re-enter the side of link2 opposite of where it last left the workspace, before the mouse event will be activated again. If he moves back to the same side, the robot will not move (no x2, y2 input).

However, I have no idea how to do this using GLUT :p

Thanks for your time, need help pretty desperately :confused4

cheers!!!
JC
 
ConfusedNewbie said:
glVertex2f(x1, y1);
glVertex2f(x1, y1);



I'm using VC++ 6.0 and the error generated is:

Compiling...
Robot plus OpenGL2.cpp
C:\robot2\Robot plus OpenGL2.cpp(92) : error C2664: 'glVertex2f' : cannot convert parameter 1 from 'float (float,float)' to 'float'
Context does not allow for disambiguation of overloaded function
C:\robot2\Robot plus OpenGL2.cpp(93) : error C2664: 'glVertex2f' : cannot convert parameter 1 from 'float (float,float)' to 'float'
Context does not allow for disambiguation of overloaded function
C:\robot2\Robot plus OpenGL2.cpp(119) : error C2059: syntax error : ')'
Error executing cl.exe.

Robot plus OpenGL2.obj - 3 error(s), 0 warning(s)

remove the first error, you are passing x1 to a function

glVertex2f(x1, y1);

but the definition of x1 function is
---------------------------
float x1 (float x2, float y2)
{
float d, e;

d=angle1(x2, y2, angle2);
e=link1*cos(d);

return (e);
}

---------
means x1 is a function that returns a float and takes two floating point integers as parameter , so the correction the above line would be like this

glVertex2f(x1(0.0,0.0), y1(0.0,0.0));

or
glVertex2f(x1(param1,param2), y1(param1,param2));

replace param1,param2 with your desired values :):)

similarly correct the very next line of it

the last error is here

if ((pow(t.x2,2)+pow(t.y2,2)-pow((link1+link2),2))<=0)


correct it also , you are missing one ) or similar


hope it will work
 
Back
Top