Hi guys,
I’m actually trying out OpenGL for the first time. What I’m trying to do is two squares. the squares move diagonally from one end of the window to another end of the window.
The problem I’m facing is that whenever I resize my window, the squares seems to go off the screen and bounces back in a weird manner. If its resized horizontally (equally vertically and horizontally) the squares move fine. However, when i resize my window only vertically or horizontally, my squares are out of position.
What i have in mind is even if i resize my window, my square should go frm one corner of the window to another corner.
Another problem I seem to find is my squares seems to flicker when it moves diagonally. Is there any explaination on this?
Here’s my coding.
#include<windows.h>
#include<gl\gl.h>
#include<gl\glaux.h>
// initial square position and its size
GLfloat x = 250.0f;
GLfloat y=0.0f;
GLsizei rsize=20;
GLfloat x2=0.0f;
GLfloat y2=0.0f;
//step size in x and y directions
//number of pixels to move each time
GLfloat xstep=0.9f;
GLfloat ystep=0.9f;
GLfloat x2step=0.9f;
GLfloat y2step=0.9f;
//keep track of window's changing width and height
GLfloat windowWidth;
GLfloat windowHeight;
//called by AUX library when the windows has changed size
void CALLBACK ChangeSize(GLsizei w,GLsizei h)
{
//prevent a divide by zero, when is too short
// you cannot make a windowzero width
if(h==0)
h=1;
//set the viewport to be the entire window
glViewport(0,0,w,h);
//Reset the corordinate system before modifying
glLoadIdentity();
//keep the square square,this time, save calculated width and height for later use
if(w<=h)
{
windowHeight=250.0f*h/w;
windowWidth=250.0f;
}
else
{
windowWidth=250.0f*w/h;
windowHeight=250.0f;
}
glOrtho(1.0f,windowWidth,0.01,windowHeight,1.0f,-1.0f);
}
//called by AUX library to update window
void CALLBACK RenderScene(void)
{
glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//set drawing color to red and draw a rectangle at current position
glColor3f(0.0f,0.1f,0.0f);
glRectf(x2,y2,x2+rsize,y2+rsize);
glColor3f(1.0f,1.0f,0.0f);
glRectf(x,y,x+rsize,y+rsize);
glFlush();
}
//called by AUX library when idle(window not being resized pr moved)
void CALLBACK IdleFunction(void)
{
//reverse direction when you reach left or right edge
if(x>windowWidth-rsize||x<0)
xstep = -xstep;
//reverse direction when you reach the top or bottom edge
if(y>windowHeight-rsize||y<0)
ystep = -xstep;
//check bounds. This is in the case the window is made smaller and rectangle is outside new clipping volume
if(x>windowWidth-rsize)
x=windowWidth-rsize-1;
if(y>windowHeight-rsize)
y=windowHeight-rsize-1;
//Actually move the square
x+=xstep;
y+=ystep;
//reverse direction when you reach left or right edge
if(x2>windowWidth-rsize||x2<0)
x2step = -x2step;
//reverse direction when you reach top or bottom edge
if(y2>windowHeight-rsize||y2<0)
y2step = -y2step;
//check bounds. This is in case the window is made smaller and the rectangle is outside the new clipping volume
if(x2>windowWidth-rsize)
x2=windowWidth-rsize-1;
if(y2>windowHeight-rsize)
y2=windowHeight-rsize-1;
//Actually move the square
x2+=x2step;
y2+=y2step;
//redraw the scene with new cordinates
RenderScene();
}
//main body of the program
void main(void)
{
//aux window setup and initialization
auxInitDisplayMode(AUX_DOUBLE || AUX_RGBA);
auxInitPosition(100,100,250,250);
auxInitWindow("Assignment 1");
//set function to call when window is resized
auxReshapeFunc(ChangeSize);
auxIdleFunc(IdleFunction);
//start main loop
auxMainLoop(RenderScene);
}