PDA

View Full Version : I have an object and a background. the background static,object move. how ?



jackbauer
04-23-2014, 11:56 PM
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.

Aleksandar
04-24-2014, 04:06 AM
There are many errors in your code. On the first glance it seems that adding two lines could solve the problem:
1. Add glMatrixMode(GL_PROJECTION) before gluOrtho2D() in the myInit() function
2. Add glLoadIdentity() after glClear() in the Display() function.

glPushMatrix()/glPopMatrix() should work afterwards.

jackbauer
04-24-2014, 10:27 AM
There are many errors in your code. On the first glance it seems that adding two lines could solve the problem:
1. Add glMatrixMode(GL_PROJECTION) before gluOrtho2D() in the myInit() function
2. Add glLoadIdentity() after glClear() in the Display() function.

glPushMatrix()/glPopMatrix() should work afterwards.


I tried what you said but its still not working :( . I am new to opengl. Can you please help me make it work ?

jackbauer
04-24-2014, 11:36 AM
Actually i kinda Figured it out. Its working now. Thanks for your help aleksandar. I have a bigger project i'm working on. its working kinda fine except for the flickering x.x X( .