PDA

View Full Version : HELP!



maddict
06-20-2006, 12:34 AM
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&amp;#0124;&amp;#0124;x<0)
xstep = -xstep;

//reverse direction when you reach the top or bottom edge
if(y>windowHeight-rsize&amp;#0124;&amp;#0124;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&amp;#0124;&amp;#0124;x2<0)
x2step = -x2step;

//reverse direction when you reach top or bottom edge
if(y2>windowHeight-rsize&amp;#0124;&amp;#0124;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 &amp;#0124;&amp;#0124; 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);
}

Komat
06-20-2006, 03:32 PM
Another problem I seem to find is my squares seems to flicker when it moves diagonally. Is there any explaination on this?
You are passing incorrect parameter to the auxInitDisplayMode function. That function expects bitwise OR of bitmasks however what you are doing is a logical OR. The correct parameter should be AUX_DOUBLE | AUX_RGBA. Because of this error the double buffered mode is not enabled and you see the flicker. After you fix that parameter, you will need to add call to the auxSwapBuffers() function to the end of your RenderScene() function to see anything.

Komat
06-20-2006, 03:53 PM
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.
If you are talking about the yellow square that sometimes moves bellow the bottom portion of the window, your problem is the following code:


if(y>windowHeight-rsize&amp;#0124;&amp;#0124;y<0)
ystep = -xstep;

maddict
06-20-2006, 09:09 PM
Hi Komat!

Thanks for the help. I've solved the flicker thing and the boxes frm running below the window. However, I still can't figure out how to make those boxes run frm one edge to another after resizing

It seems like, after I resize the boxes seems to bounce awkwardly off the sides and all. I'm guessing my ChangeSize's calculation is wrong or something. Can you help me recode to make them bounce off the edges everytime i resize the screen? Thanks. I'll understand once i see the codes.

supercat1
06-20-2006, 09:13 PM
i would suggest using something like the glut framework to learn opengl and then switch to a more expansive/flexible framework once you have the hang of it (something like glfw or sdl). those have some nice ways of accessing window sizes for that sort of stuff.

that or check out nehe.gamedev.net. it has great info for beginner opengl users.