Jump to content
  • Advertisement
Sign in to follow this  
barantamer

OpenGL simple rotation problem

This topic is 3947 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello , i am new to opengl and i am trying to design a 2d arm. its basicly consist of 3 rectangles. Everything was fine till i try to rotate the joints of the arm.I couldnt make the pic move continuosly... the whole arm,wrist and elbow rotations works perfectly individually , but i couldn`t make them me rotate together . If u please have a look at my code you are gonna understand me better . My code is below note:: Press '1' ,'2','3' to rotate arm,elbow and wrist #include<stdio.h> #include<stdlib.h> #include<gl/glut.h> #include<gl/gl.h> #include<time.h> void display(void); void init(void); void drawstack(void); void DrawRectangles(void); void BaranKeyboardFunc(unsigned char ,int , int ); void readfile(void); void RandomColor(void); void ChangeSize(GLsizei w, GLsizei h); void DrawArmOneJointOne(void); void DrawArmOneJointTwo(void); void DrawArmOneJointThree(void); int flag=0,stackwidth,RecNum,unit,i,DrawCounter; int g_window,random,random2,random3; float posx=0.0f; float posy=0.0f; float posz=-20.0f; int bayrak; FILE *BaranPtr; float a=0; float arm1joint1=0,arm1joint2=0,arm1joint3=0; struct rectangle { float x; float y; float wit; float hayt; float x2; float y2; int color1; int color2; int color3; int cakisma; }rec[100]; int main(int argc,char **argv) { // srand(time(NULL)); RandomColor(); glutInit(&argc,argv); init(); glutMainLoop(); return 0; } void init(void){ glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); glutInitWindowSize(800,600); g_window=glutCreateWindow("Baran Tamer"); glutDisplayFunc(display); glutKeyboardFunc(BaranKeyboardFunc); //glutSpecialFunc(); //glutReshapeFunc(ChangeSize); //ekrana fit olan sekilde kullanmak icin bu satiri kapatin glClearColor(0.0f,0.0f,0.0f,1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,800.0f/600.0f,0.1f,100.0f); glDisable(GL_DEPTH_TEST); } void display(void){ glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //arm1 joint1 glTranslatef(0,0,-20); //DrawArmOneJointOne(); if(flag==1) { glRotatef(arm1joint1,0.0, 0.0, -20); arm1joint1++; } DrawArmOneJointOne(); //end of arm joint one //arm1 joint2 glTranslatef(-4,0,0); if(flag==2) { glRotatef( arm1joint2,0.0, 0.0,-20); arm1joint2++; } DrawArmOneJointTwo(); //end of arm joint two //arm3 joint3 glTranslatef(-4,0,0); if(flag==3) { glRotatef( arm1joint3,0.0, 0.0, -20); arm1joint3++; } DrawArmOneJointThree(); //end of arm joint three glFlush(); glutSwapBuffers(); } void DrawArmOneJointOne(void){ glColor3b(100.0,100,100); glRecti(-4,-1,0,0); } void DrawArmOneJointTwo(void){ glColor3b(400.0 ,100.0,100); glRecti(-4,-1,0,0); } void DrawArmOneJointThree(void){ glColor3b(400.0 ,200.0,100); glRecti(-2,-1,0,0); } void BaranKeyboardFunc(unsigned char key,int x , int y) { switch(key){ case '1': flag=1; printf("joint1"); break; case '2': flag=2; printf("joint2"); break; case '3': flag=3; printf("joint3"); break; default: printf("yanlis tus"); break; return; } glutPostRedisplay(); } void RandomColor(void) { int sayac; for(sayac=0;sayac<RecNum;sayac++){ random=rand()%100+1; random2=rand()%100+1; random3=rand()%100+1; rec[sayac].color1=random; rec[sayac].color2=random2; rec[sayac].color3=random3; } } void ChangeSize(GLsizei w, GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(float)w/h,0.1f,100.0f); }

Share this post


Link to post
Share on other sites
Advertisement

#include<stdio.h>
#include<stdlib.h>
#include<gl/glut.h>
#include<gl/gl.h>
#include<time.h>

void display(void);
void init(void);
void drawstack(void);
void DrawRectangles(void);
void BaranKeyboardFunc(unsigned char ,int , int );
void BaranKeyboardUpFunc(unsigned char ,int , int );
void readfile(void);
void RandomColor(void);
void ChangeSize(GLsizei w, GLsizei h);
void DrawArmOneJointOne(void);
void DrawArmOneJointTwo(void);
void DrawArmOneJointThree(void);

int stackwidth,RecNum,unit,i,DrawCounter;
int g_window,random,random2,random3;
float posx=0.0f;
float posy=0.0f;
float posz=-20.0f;
int bayrak;
FILE *BaranPtr;
float a=0;
float arm1joint1=0,arm1joint2=0,arm1joint3=0;
int flags[3] = {0,0,0};

struct rectangle
{
float x;
float y;
float wit;
float hayt;
float x2;
float y2;
int color1;
int color2;
int color3;
int cakisma;

}rec[100];

int main(int argc,char **argv)
{

// srand(time(NULL));

RandomColor();
glutInit(&argc,argv);
init();

glutMainLoop();
return 0;
}

void idle()
{
if(flags[0])
{
arm1joint1+=0.1f;
}
if(flags[1])
{
arm1joint2+=0.1f;
}
if(flags[2])
{
arm1joint1+=0.1f;
}
glutPostRedisplay();
}

void init(void)
{
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowSize(800,600);
g_window=glutCreateWindow("Baran Tamer");

glutDisplayFunc(display);
glutKeyboardFunc(BaranKeyboardFunc);
glutKeyboardUpFunc(BaranKeyboardUpFunc);
glutIdleFunc(idle);

glClearColor(0.0f,0.0f,0.0f,1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,800.0f/600.0f,0.1f,100.0f);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//arm1 joint1
glTranslatef(0,0,-20);
//DrawArmOneJointOne();
glRotatef(arm1joint1,0.0, 0.0, -20);
DrawArmOneJointOne();
//end of arm joint one


//arm1 joint2
glTranslatef(-4,0,0);
glRotatef(arm1joint2,0.0, 0.0, -20);
DrawArmOneJointTwo();
//end of arm joint two


//arm3 joint3
glTranslatef(-4,0,0);
glRotatef(arm1joint3,0.0, 0.0, -20);
DrawArmOneJointThree();
//end of arm joint three


glutSwapBuffers();
}

void DrawArmOneJointOne()
{
glColor3b(100.0,100,100);
glRecti(-4,-1,0,0);
}

void DrawArmOneJointTwo()
{
glColor3b(400.0 ,100.0,100);
glRecti(-4,-1,0,0);

}


void DrawArmOneJointThree()
{
glColor3b(400.0 ,200.0,100);
glRecti(-2,-1,0,0);

}

void BaranKeyboardFunc(unsigned char key,int x , int y)
{
switch(key){
case '1':
flags[0]=1;
break;
case '2':
flags[1]=1;
break;
case '3':
flags[2]=1;
break;

default:
break;

return;
}
}

void BaranKeyboardUpFunc(unsigned char key,int x , int y)
{
switch(key){
case '1':
flags[0]=0;
break;
case '2':
flags[1]=0;
break;
case '3':
flags[2]=0;
break;

default:
break;

return;
}
}

void RandomColor()
{
int sayac;

for(sayac=0;sayac<RecNum;sayac++)
{
random=rand()%100+1;
random2=rand()%100+1;
random3=rand()%100+1;

rec[sayac].color1=random;
rec[sayac].color2=random2;
rec[sayac].color3=random3;
}
}

void ChangeSize(GLsizei w, GLsizei h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(float)w/h,0.1f,100.0f);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!