PDA

View Full Version : How to rotate a method without disturbing another mathod



waqar ahmad
06-16-2013, 06:41 AM
Hello all i am building a analog clock i am calling secondsLine function in DrawCircle function. it is rotating secongLine but not displaying DrawCircle Function code. and how can i rotate different functions with different time frames without disturbing eachother Help please:


#include<windows.h>
#include<gl/GLU.h>
#include<gl/GL.h>
#include<glut.h>
#include<math.h>
#define PI 3.141592653589793

GLfloat secAngle=0.0;
GLfloat minAngle=0.0;
GLfloat hourAngle=0.0;
GLfloat hour=0.0;



void smallLines(GLfloat,GLfloat,GLfloat,GLfloat,GLfloat ,int);
void secondsLine(void);
void minutesLines(void);
void hourLine(void);
void centerpoint(void);

void myInit(void)
{
glColor3f(1.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);

}


void DrawCircle()
{

char buff[100];
GLfloat x1=0.0;
GLfloat y1=0.0;
GLfloat z1=0.0;
GLfloat radius = 10.0;
GLfloat smallradius = 3.0;
int angle;

secAngle += -0.1;

glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glOrtho(0.0, 20.0, 0.0, 20.0, -10.0, 10.0);

glPushMatrix();
glTranslatef(10.0, 10.0, 0.0);


// ------------------------------- Circle ---------------------------------------

glBegin(GL_TRIANGLE_FAN);
glColor3f(0.9,0.9,0.9);
glVertex3f(x1, y1, z1);
for(angle=0; angle <= 360; angle +=1)
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y1 + sin(angle * PI/180.0f) * radius, z1);
glEnd();
glPopMatrix();
glFlush();

smallLines(x1,y1,z1,radius,smallradius,angle);
secondsLine();
minutesLines();
hourLine();
centerpoint();
}

// ------------------------------- Small Lines ---------------------------------------

void smallLines(GLfloat x1,GLfloat y1,GLfloat z1,GLfloat radius,GLfloat smallradiu,int angle)
{
glPushMatrix();
glTranslatef(10.0, 10.0, 0.0);
glColor3f(0.2,0.2,0.2);
glLineWidth(2);
for(angle=0; angle <= 360; angle +=30)
{
glBegin(GL_LINES);
if(angle == 0.0 || angle==90.0 || angle == 180.0 || angle == 270.0 || angle == 360.0 )
{
glColor3f(1.0,0.2,0.2);
glVertex3f(x1 + cos(angle * PI/180.0f)* (radius-1.2), y1 + sin(angle * PI/180.0f) * (radius-1.2), z1+0.1);
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y1 + sin(angle * PI/180.0f) * radius, z1+0.1);
}
else
{
glColor3f(0.2,0.2,0.2);
glVertex3f(x1 + cos(angle * PI/180.0f)* (radius-0.5), y1 + sin(angle * PI/180.0f) * (radius-0.5), z1);
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y1 + sin(angle * PI/180.0f) * radius, z1);
}
glEnd();
}
glPopMatrix();
glFlush();
}

// ------------------------------- Seconds Line ---------------------------------------

void secondsLine()
{
//GLfloat sec = 5;

glColor3f(1.0,1.0,0.0);
glLineWidth(2.0);

glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-4.0);
glRotatef(-secAngle,0.0,0.0,10.0);

glBegin(GL_LINES);
glVertex2f(0.0, 0.0);
glVertex2f(0.5, 0.5);
glEnd();
glPopMatrix();
glFlush();
glutSwapBuffers();

}
void Reshape(int x, int y)
{
if(y==0 || x==0)
return;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y);
}

void Idle(void)
{
//xRotated += 0.1;
//yRotated += 0.1;
secAngle += -0.1;
secondsLine();
}

// ------------------------------- Minutes Line ---------------------------------------
/**/
void minutesLines()
{
glColor3f(1.0,0.0,0.0);
glLineWidth(2.0);
glPushMatrix();

glTranslatef(10.0, 10.0, 0.0);
glRotatef(-minAngle, 0.0, 0.0, 1.0);

glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 9.0, 1.0);
glEnd();
glPopMatrix();
glFlush();
}

// ------------------------------- Hour Line ---------------------------------------

void hourLine()
{
glColor3f(0.0,1.0,0.0);
glLineWidth(3.0);
glPushMatrix();

glTranslatef(10.0, 10.0, 0.0);
glRotatef(-hourAngle, 0.0, 0.0, 1.0);

glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 6.5, 0.0);
glEnd();
}

// ------------------------------- Center Point ---------------------------------------

void centerpoint()
{
glColor3f(0.0,0.0,0.0);
glPointSize(8.0);
glPushMatrix();

glBegin(GL_POINTS);
glVertex3f(0.0,0.0,0.0);
glEnd();
glPopMatrix();
glFlush();

}

void main(int argc , char **argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(200,300);
glutCreateWindow("Analog Clock");
glutDisplayFunc(DrawCircle);
glutReshapeFunc(Reshape);
glutIdleFunc(DrawCircle);
myInit();
glutMainLoop();
}

tonyo_au
06-16-2013, 06:46 AM
DrawCircle() and hourLine() have a push without a pop of the view matrix - this might be your problem

GClements
06-16-2013, 06:55 AM
it is rotating secongLine but not displaying DrawCircle Function code.
Your problem is caused by calling glutSwapBuffers() at the end of secondsLine(), when it should be called at the very end of your display function. Anything drawn after the glutSwapBuffers() call never gets displayed.
Also: drawing calls should normally only be performed in the display function. The idle function should simply call glutPostRedisplay().

waqar ahmad
06-16-2013, 06:57 AM
i have added rotation in secondsLine function and i am calling this function in Drawcircle. it is displaying rotating line but when it refreshes for rotation does not display Code in DrawCircle function. if you please tell me the better implementation to rotate a secondsLine method

waqar ahmad
06-16-2013, 07:06 AM
[QUOTE=GClements;1251789]Your problem is caused by calling glutSwapBuffers() at the end of secondsLine(), when it should be called at the very end of your display function. Anything drawn after the glutSwapBuffers() call never gets displayed.

Still not resolved

waqar ahmad
06-16-2013, 08:04 AM
how to rotate a line in Opengl ? Simplest way