Help on drawing in openGL from C++

Hi,Iam Qasim and I am undertaking C++ course. I am working on my project which allows the user to calculate shear force and bending moments on a beam. What basically is the output are the graphs drawn on the beam which show the forces acting after calculation. I am facing two problems in it, Firstly, how can I draw a rectangle with the length equal to the length given by the user. Secondly, how can I use the answers obtained in drawing a graph.

This is my code.

#include <stdlib.h>
#include "glut.h"
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <time.h>

#define max 10

int check=1;
int shear(float p[][2], int num,float reaction1,float reaction2,float length,float e[][3],int num1);
float add(float r1,int point,float q[][2],int random);
float momentpoint(float u[][2],float w[][3],float shearvalue,int momentnum, float previous,float end1,float end2);
void body();


int shear(float p[][2], int num,float reaction1,float reaction2,float length,float e[][3],int num1)
{
float force=0;
float area=0;
float torque=0;
float ans=0;
float pre=0;
float ls=0;
srand(time(NULL));
for(int x=0;x<num;x++)
{
int a;
int b;
a=p[x][1];
b=0;
if(x>0)
b=p[x-1][1];
else
b=0;
float end=0;
if(x>0)
end=p[x-1][1];
else
end=0;
float req=p[x][1]-end;
int sub=a-b;
int d=sub+1;
int result=b+rand()%d;
force=add(reaction1,num,p,result);
int count=0;
for(int y=0;y<num1;y++)
{
if(e[y][1]<=end && e[y][1]>=p[x][1])
count++;
}
printf("Shear force in %d section is %.2fkN from %fm-to-%fm\n",x+1,force,end,p[x][1]);

if(count==0)
{

area=force*req;

printf("The BM for this section lies btw %.2fkNm-to-%.2fkNm\n",torque,area);
torque=area;
}
else if(count>0)
{ans=momentpoint(p,e,force,num1,pre,end,p[x][1]);
pre=ans;
}
if(x==(num-1))
ls=p[x][1];




}
float r;
r=reaction2;
printf("The shear force in last section is %.2fkN\n", -r);
if(num1==0)
{
printf("The BM in the last section lies btw %.2fkNm-to-0.00",area);
}
else
momentpoint(p,e,-r,num1,pre,ls,length);



return 0;
}

float add(float r1,int point,float q[][2],int random)
{
int counter=0;

for(int x=0;x<point;x++)
{
if(q[x][1]<=random)
counter++;
else
break;
}
float sum=0;
for(int y=0;y<counter;y++)
{
sum = sum + q[y][0];
}
float result;
result=r1-sum;
return result;

}

float momentpoint(float u[][2],float w[][3],float shearvalue,int momentnum, float previous,float end1,float end2)
{

int counter=0;
float momentplace[max];
float momentvalue[max];
float momentdirection[max];
for(int x=0;x<momentnum;x++)
{
if(w[x][1]>=end1&&w[x][1]<=end2)
{
counter++;
momentplace[x]=w[x][1];
momentvalue[x]=w[x][0];
momentdirection[x]=w[x][2];
}

}
float end=0;
if(counter!=0)
{
printf("We will furthur divide this section into %d sections\n",counter+1);
float store=0;
float d=0;
float value=0;

for(int s=0;s<counter+1;s++)
{
if(s==0)
d=momentplace[s]-end1;
else if(s>0 && s<counter)
d=momentplace[s]-momentplace[s-1];
else
d=end2-momentplace[s-1];

if(s==0)
{
value=d*shearvalue;
printf("The value of moment lies btw %.2f-to-%.2f\n",previous,value);
store=value;
}
else if(s>0 && s<counter)
{
if(momentdirection[s]==0)
{
value=(d*shearvalue)+momentvalue[s-1];
printf("The vaue lies btw %.2f-to-%.2f\n",(store+momentvalue[s-1]),(value +store));
store=value+store;
}
else
{
value=momentvalue[s-1]-(d*shearvalue);
printf("The vaue lies btw %.2f-to-%.2f\n",(store-momentvalue[s-1]),(store-value));
store=store+value;

}
}
else
{
value=d*shearvalue;
printf("The value lies btw %.2f-to-%.2f\n",store+momentvalue[s-1],store+value+momentvalue[s-1]);
end=store+value;

}
}
}
return end;

}
int g_Width=400;
int g_Height=400;

void init();
void myMouseFunction( int button, int state, int mouseX, int mouseY );
void myKeyboardFunction( unsigned char key, int mouseX, int mouseY );
void mySpecialKeysFunction(int key, int x, int y);
void myReshapeFunction( int width, int height );
void myTimerFunction( int val );
void myDisplayFunction();


int main(int arg, char** argv)
{
glutInit( &arg, argv );

glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( g_Width, g_Height );
glutInitWindowPosition( 50, 50 );
glutCreateWindow( "Qaisar's first GUI application" );

init();


glutKeyboardFunc( myKeyboardFunction );

glutSpecialFunc( mySpecialKeysFunction );

glutReshapeFunc( myReshapeFunction );

glutDisplayFunc( myDisplayFunction );

glutTimerFunc( 33, myTimerFunction, 0 );

glutMouseFunc( myMouseFunction );
//glutFullScreen();
//glutSetCursor( GLUT_CURSOR_NONE );

glutMainLoop();



}

void init(void)
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glOrtho(0.0, g_Width, 0.0, g_Height, -1.0, 1.0);
}

void myMouseFunction( int button, int state, int mouseX, int mouseY )
{
if (check==1)
check=0;
else
check=1;
if (check==1)
body();
printf("%d %d %d %d\n",button,state,mouseX,mouseY);
glEnd();
//return(0);
}

void myKeyboardFunction( unsigned char key, int mouseX, int mouseY )
{

if( key == 27 ) // i.e. Esc key
exit(0);

}

void mySpecialKeysFunction(int key, int x, int y)
{
switch( key )
{
case GLUT_KEY_UP:
break;
case GLUT_KEY_DOWN:
break;
case GLUT_KEY_LEFT:
break;
case GLUT_KEY_RIGHT:
break;
case GLUT_KEY_HOME:
break;
case GLUT_KEY_END:
break;
case GLUT_KEY_PAGE_UP:
break;
case GLUT_KEY_PAGE_DOWN:
break;
}
}

void myReshapeFunction( int width, int height )
{
glClear(GL_COLOR_BUFFER_BIT);

g_Width = width;
g_Height = height;

glViewport (0, 0, g_Width, g_Height);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0.0, g_Width, 0.0, g_Height, -1.0, 1.0);
}

void myTimerFunction( int val )
{
glutTimerFunc( 33, myTimerFunction, 0 );
myDisplayFunction();
}

void myDisplayFunction()
{



glClear( GL_COLOR_BUFFER_BIT );

glBegin(GL_LINE_LOOP);

static int x;
if (x<=303)
x++;
else x--;

glColor3ub(0, 0, 255);

glVertex2i( 50, 50+x );

glVertex2i(50 , 100+x );

glVertex2i( 100, 100 );

glVertex2i( 100, 50 );


glEnd();



glutSwapBuffers();

//printf(" %d", x);

}


void body()
{printf("Enter the value of reaction 1\n");
float react1=0;
scanf("%f", &react1);
printf("Enter the value of reaction 2\n");
float react2=0;
scanf("%f", &react2);
printf("Enter the length of the beam\n");
float l=0;
scanf("%f", &l);
printf("Enter the number of point loads\n");
float number;
scanf("%f", &number);
float u[max][2];
for(int x=0;x<number;x++)
{
for(int y=0;y<2;y++)
{
if(y==0)
printf("Enter the magnitude of %d load",x+1);
else
printf("Enter its point of application");
scanf("%f", &u[x][y]);
}
}
printf("Enter the number of moments acting on our beam");
int number1=0;
scanf("%d", &number1);
float w[max][3];
for(int g=0;g<number1;g++)
{
for(int h=0;h<3;h++)
{
if(h==0)
printf("Enter the magnitude of %d moment",g+1);
else if(h==1)
printf("Enter its point of application");
else
printf("Enter '0' if clockwise and '1' if anticlockwise");


scanf("%f", &w[g][h]);
}
}

shear(u,number,react1,react2,l,w,number1);

}


Please help me.
Firstly, http://www.cplusplus.com/forum/articles/42672/

Also, avoid posting irrelevant code..

Also, don't double post.

To draw a rectangle,
1
2
3
4
5
6
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x+w, y);
glVertex2i(x+w, y+h);
glVertex2i(x, y+h);
glEnd();
where x, y are the top right corner coordinates, w is width and h is height.

To draw a graph depends on the kind of graph you want.
Anyway, it could be something like
1
2
3
glBegin(GL_LINE_STRIP);
for(int i = 0; i < amount_of_data; i++) glVertex2i( i*5, data_value[i] );
glEnd();
What basically i want is to draw the shape with the length entered here;


printf("Enter the length of the beam\n");
float l=0;
scanf("%f", &l);


How will I link this length with the drawing shape function?
What's the problem? Just replace w with l and others with constants..
Topic archived. No new replies allowed.