I am new to openGL. I have an object and a background. i want the background to be static,while the object moves continuosly using glTranslatef() and the idlefunction. How do i do it ?
I came across many places where they’ve asked to use glPushMatrix and glPopMatrix. but when i use them it doesnt work at all. For example, Look at this code
#include<iostream>
#include<conio.h>
#include<GL/glut.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<stdlib.h>
#include <windows.h>
using namespace std;
float boundary=800.0;
void DrawPoly(int a, int b,int c,int d,int e,int f,int g,int h)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(a,b);
glVertex2f(c,d);
glVertex2f(e,f);
glVertex2f(g,h);
glEnd();
glFlush();
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
DrawPoly(400,200,400,700,500,200,500,700); //This should be static
//glPushMatrix();
glTranslatef(1,0.0,0.0);
DrawPoly(100,100,400,100,400,400,100,400); //Only this should move continuously
Sleep(50);
//glPopMatrix();
glEnd();
glutSwapBuffers();
}
void myInit()
{
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(0.0,boundary,0.0,boundary);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(boundary,boundary);
glutInitWindowPosition(0, 0);
glutCreateWindow(“Moving Poly”);
glutDisplayFunc(Display);
glutIdleFunc(Display);
myInit();
glutMainLoop();
return 1;
}
When i use the above code, It moves both the objects and when i remove the commenting for Push() and Pop(), nothing happens.
Please help guys.