OpenGL Program / Loop Help

Jones
Unregistered
 
Post: #1
Howdy all,

I'm having some trouble making a loop in an OpenGL program. I used "OneSadCookie's" GLUT tutorial to build the "moving box" program. Then I decided I'd try to re-program how the box moved. Originally it used an opengl command that moved everything on the screen one way, using coordinates. (At least, I think that's how it was supposed to work.)

Anyway, I changed it so that it would update all the boxes vertex coordinates relative to the bottom left corner of the box. I added a simple function that would take the variable "boxSpeed", the amount of pixels to move the box diagonally. Then, based on that number and the current position of the box, I'd redraw the box.

However, things didn't work out as planned. The box would move once, at the start of the program, but not again. I realized that I'd forgotten to install some sort of looping mechanism into the main loop. I tried inserting a basic "while 2 > 1" loop, just to test it. Unfortunatly the program would then get stuck during startup, while the app was still bouncing in the dock.

I think the problem here is, I'm just not thinking logically. I'm not realizing exactly which parts (ie: which functions) need to be looped or not.

Here is the code:

Quote:#include <stdlib.h>

#include <GLUT/glut.h>

// The X and Y coordinates of the bottom-left corner of the square
float box_bottomLeft_X = 0.0;
float box_bottomLeft_Y = 0.0;

void increaseXY(float boxSpeed)
{

// Increase the x and y values by 1 pixel(s)
box_bottomLeft_X = box_bottomLeft_X + boxSpeed;
box_bottomLeft_Y = box_bottomLeft_Y + boxSpeed;

}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glVertex2f(box_bottomLeft_X, box_bottomLeft_Y);
glVertex2f(box_bottomLeft_X + 128.0f, box_bottomLeft_Y);
glVertex2f(box_bottomLeft_X + 128.0f, box_bottomLeft_Y + 128.0f);
glVertex2f(box_bottomLeft_X, box_bottomLeft_Y + 128.0f);
glEnd();

glutSwapBuffers();
}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);

glutCreateWindow("Moving Square");

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);

increaseXY(5);

glutMainLoop();
return EXIT_SUCCESS;
}

Any help would be nice.

Oh yes, and a special thanks to OneSadCookie, for his wonderful GLUT introduction.

'Jones
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
The problem you are having is increaseXY(5); is only being called once before you enter your main loop, try moving that command to inside your draw function.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Jones
Unregistered
 
Post: #3
Thanks! It works great now!
Quote this message in a reply
Jones
Unregistered
 
Post: #4
Ok, new problem. Annoyed

I just added a new screen-edge collision detection system and I'm having trouble getting it to work properly. Maybe I'm just really bad at this (Wacko), but I'm not sure why it's not working. For some reason, the box just shoots off the left edge of the screen. Blink

Here's the new code.

Quote:#include <stdlib.h>

#include <GLUT/glut.h>

// The X and Y coordinates of the bottom-left corner of the square
float box_bottomLeft_X = 0.0;
float box_bottomLeft_Y = 0.0;

// These integers are true(1) or false(0) values for "Has the box hit the top/bottom of the screen?"
int box_hit_Top = 0;
int box_hit_Bottom = 0;

// The desired speed (pixel movement) of the box
float boxSpeed;
// The speed after processing
float boxSpeedOut;

// This function updates the box's coordinates
void increaseXY(float boxSpeed)
{

// If the check_boxPosition functions tells us the box has hit the top of the window, reverse the pixel movement value
if (box_hit_Top = 1 && box_hit_Bottom != 1)
{
boxSpeedOut = boxSpeed - (boxSpeed * 2);
}

else if (box_hit_Bottom = 1 && box_hit_Top != 1)
{
boxSpeedOut = boxSpeed;
}
else
{
boxSpeedOut = boxSpeed;
}

// Increase the x and y values by 1 pixel(s)
box_bottomLeft_X = box_bottomLeft_X + boxSpeedOut;
box_bottomLeft_Y = box_bottomLeft_Y + boxSpeedOut;

}

// Checks if the box has hit the top/bottom of the screen
void check_boxPosition()
{

if (box_bottomLeft_Y = 480 - 128.0) {

box_hit_Top = 1;

}

if (box_bottomLeft_Y = 0.0) {

box_hit_Bottom = 1;

}

}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glVertex2f(box_bottomLeft_X, box_bottomLeft_Y);
glVertex2f(box_bottomLeft_X + 128.0f, box_bottomLeft_Y);
glVertex2f(box_bottomLeft_X + 128.0f, box_bottomLeft_Y + 128.0f);
glVertex2f(box_bottomLeft_X, box_bottomLeft_Y + 128.0f);
glEnd();

check_boxPosition();
increaseXY(1);

glutSwapBuffers();
}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);

glutCreateWindow("Moving Square Application");

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);

glutMainLoop();
return EXIT_SUCCESS;
}

Thanks.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #5
try

void check_boxPosition()
{

if (box_bottomLeft_Y > 480 - 128.0) {
box_hit_Top = 1;
box_hit_Bottom = 0;
}

if (box_bottomLeft_Y < 0.0) {
box_hit_Bottom = 1;
box_hit_Top = 0;
}


Personally I always account for overflows, when dealing with collision detection its good to get into the habit because very very rarely will you have a whole value to do a precise ==. The reason you were having a problem is you were using "=" instead of "==", "=" sets the value to the right side, the "==" compares the two values.

Let me know if that works, if not I will look over the code a bit more closely

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Jones
Unregistered
 
Post: #6
Doh! Why didn't I remember that "==" stuff.

*smashes head against wall*

It works wonderful now, by the way.

Thanks for your help, kodex!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  display link to drive opengl loop? alloca 21 8,649 Jan 21, 2009 10:56 PM
Last Post: arekkusu
  3 line OpenGL program strangeness... WhatMeWorry 1 2,485 Nov 19, 2004 07:37 PM
Last Post: arekkusu