I want to 2 Viewports on the screen. In first one, i have some animation and i am calling gluttimerFunc for this. In other viewport i have some text displayed which updates continuously with by calling gluttimerfunc. Now when I resize the window, i need to get updated height and width of a viewport from my reshape function. I have the separate modules of the program working fine. but i am not able to integrate it to see the animation with both the viewports.
static unsigned int timer = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.5, 0.5, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.75, -0.75);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.75, -0.75);
glEnd();
}
void DrawHexagon(int o)
{
static float angle = 0;
float distance_x, distance_y;
glClear(GL_COLOR_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0f, 0.3f, 0.2f);
//glViewport(0, 0, 240, 240);
glBegin(GL_POLYGON);
glVertex2f(-0.9, -0.9);
glVertex2f(-0.9, -0.7);
glVertex2f(-0.7, -0.7);
glVertex2f(-0.7, -0.9);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-0.3, -0.3);
glVertex2f(0.3, -0.3);
glVertex2f(0.3, 0.3);
glVertex2f(-0.3, 0.3);
glEnd();
static int i = 0;
distance_x = 0.5 * cos(i*3.14 / 180);
distance_y = 0.5 * sin(i*3.14 / 180);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(distance_x, distance_y, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.2, -0.2);
glVertex2f(0.2, -0.2);
glVertex2f(0.2, 0.2);
glVertex2f(-0.2, 0.2);
glEnd();
distance_x = 0.3 * cos(i*3.14 / 180);
distance_y = 0.3 * sin(i*3.14 / 180);
i++;
if (i > 360)
i = 0;
glTranslatef(distance_x, distance_y, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.1, -0.1);
glVertex2f(0.1, -0.1);
glVertex2f(0.1, 0.1);
glVertex2f(-0.1, 0.1);
glEnd();
angle = angle + 0.01f;
glPopMatrix();
glutSwapBuffers();
glutTimerFunc(10, DrawHexagon, 0);
}
void DisplayTimer(int time)
{
glViewport(480, 480, 240, 240);
timer++;
glRasterPos2f(0.75f, 0.75f);
int q = 12345;
char p[30];
_itoa_s(timer, p, 10);
for (int i = 0; i < 5; i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, p[i]);
glutTimerFunc(100, DisplayTimer, 0);
glutSwapBuffers();
glutPostRedisplay();
}
void Resize(int w, int h)
{
GLdouble aspect_ratio = (GLdouble) w / (GLdouble) h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0 * aspect_ratio, 1.0 * aspect_ratio, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
}
void init()
{
glClearColor(1.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
/* create a window, position it, and name it */
glutInit(&argc, argv);
//Changed single to double buffer rendering
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE |GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(200, 200);
glutCreateWindow("basics");
/* create a callback routine for (re-)display */
glutDisplayFunc(display);
glutTimerFunc(1000, DrawHexagon, 0);
// This gets called whenever window gets resized
glutReshapeFunc(Resize);
/* init some states */
init();
/* entering the event loop */
glutMainLoop();
}