PDA

View Full Version : OpenGL bible



Fuzzy
04-16-2008, 02:14 PM
I'm doing the 3rd demo program in the openGL bible. This is the second machine I've complied it on and I'm getting the same result. The program is suppose to bounce the square around and when it hits an edge it reverses direction. However, mine is stuck in the center. I've been going over this for about a day now. i can't spot the mistake.


#include "opengl.h"

// Inital sqaure position and size
GLfloat x1 = 0.0f;
GLfloat y1 = 0.0f;
GLfloat rsize = 25;

// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;

// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;

//////////////////////////////////////////////////////////////////////////////////
// Called to draw a scene

void RenderScene(void)
{
// Cear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);

// Set current drawing color to red
// R G B
glColor3f(1.0f, 0.0f, 0.0f);

// Draw a filled rectantgle with current color
glRectf(x1, y1, x1 + rsize, y1 - rsize);

// Flush drawing commands and swap
glutSwapBuffers();
}

// Set up the rendering state

void SetupRC(void)
{
// Set clear color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}

// Called by glut library when the window has changed size

void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat aspectRatio;

// Prevent a divide by zero
if(h == 0)
h = 1;

// Set viewport to window dimensions
glViewport(0, 0, w, h);

// Reset coordinates system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Establish clipping volume (Left, Right, bottom, top, near, far)
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
glOrtho (-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
else
glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

////////////////////////////////////////////////////////////////////////////////////////////
// Called by glut library when idle (window not being resized or moved)
void TimerFunction(int value)
{
// Reverse direction when you reach left or right edge.
if(x1 > windowWidth-rsize || x1 < -windowWidth)
xstep = -xstep;

// Reverse direction when you reach top or bottom edge.
if(y1 > windowHeight || y1 < -windowHeight + rsize)
ystep = -ystep;

// Acutally move the square
x1 += xstep;
y1 += ystep;

// Check bounds. This is in case the window is made smaller while the rectangle
// is bouncing and the rectangle suddenly finds itself outside the new clipping volume
if(x1 > (windowWidth-rsize + xstep))
x1 = windowWidth-rsize-1;
else if(x1 < -(windowWidth + xstep))
x1 = - windowWidth -1;

if(y1 > (windowHeight + ystep))
y1 = windowHeight-1;
else if(y1 < -(windowHeight - rsize + ystep))
y1 = -windowHeight + rsize -1;

// redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(33, TimerFunction, 1);
}

// Main program entry point

int main(int argc, char* argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);

SetupRC();
glutMainLoop();

return 0;
}

I'm not sure why it's not working correctly.

-NiCo-
04-16-2008, 02:33 PM
Well, looks like it's using windowWidth and windowHeight a lot, but they never get initialized.

Fuzzy
04-16-2008, 02:36 PM
Wouldn't this be the place???

// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;

-NiCo-
04-16-2008, 02:38 PM
Yes. What I meant was that they never get a value assigned to them. This is a problem since they're being used in comparisons in TimerFunction(int value).

Fuzzy
04-16-2008, 02:42 PM
That's everything in the book. This sucks. I'd like to see this work.

-NiCo-
04-16-2008, 02:46 PM
#include "opengl.h"

GLfloat x1 = 0.0f;
GLfloat y1 = 0.0f;
GLfloat rsize=25;

GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;

GLfloat windowWidth=100;
GLfloat windowHeight=100;

void RenderScene() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rsize,y1-rsize);
glutSwapBuffers();
}


void TimerFunction(int value) {

if (x1 > windowWidth-rsize || x1 < -windowWidth) {
xstep = -xstep;
}

if (y1 > windowHeight || y1 < -windowHeight + rsize) {
ystep = -ystep;
}

x1 += xstep;
y1 += ystep;

// Check bounds. Tjos om case tjhe window is made smaller whie
// the rectangle is bouncing and the rectangle suddenly finds
// itself outside the new clipping volume
if (x1 > (windowWidth-rsize + xstep))
x1 = windowWidth-rsize-1;
else
if (x1 < -(windowWidth + xstep))
x1 = -windowWidth - 1;

if(y1 > (windowHeight +ystep))
y1 = windowHeight - 1;
else
if(y1 < -(windowHeight - rsize + ystep))
y1 = -windowHeight + rsize - 1;

glutPostRedisplay();
glutTimerFunc(33,TimerFunction,1);
}

void ChangeSize(GLsizei w,GLsizei h) {
GLfloat aspectRatio;

if (h==0)
h=1;

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio=(GLfloat)w/(GLfloat)h;

if (w <= h)
glOrtho(-100.0,100.0,-100.0/aspectRatio,100.0/aspectRatio,1.0,-1.0);
else
glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100.0,100.0,1.0,-1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void SetupRC() {
glClearColor(0.0f,0.0f,1.0f,1.0f);
}

int main(int argc,char *argv[]) {
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutCreateWindow("Bounce");
glutInitWindowSize(800,600);
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33,TimerFunction,1);
SetupRC();
glutMainLoop();

return 0;
}

Fuzzy
04-16-2008, 02:51 PM
Ok..It's working now. However, there is 1 minor issue. The "y1" is not extending all the way to the edge of the wall. It's stopping like maybe a thumbs lenght from the wall. Any suggestions?

-NiCo-
04-16-2008, 04:17 PM
I don't want to be rude...but my suggestion is that you try to understand what the code does, this is almost as basic as it gets.

Fuzzy
04-16-2008, 05:55 PM
I know. I'm a new to C++ and I don't understand a lot of the stuff they call to that OpenGL calls to. I just started reading the bible 3 days ago. I have 4 weeks left of prgraming C++ 1 left. I get the fact that most of the stuff were dealing with are just functions.

I did however figure it out. But what if I want it to go ahead and write so it automatically go to the borders? I'm assuming 100 x 100 is a perfect square which is why it short changed the Width paramature. So I changed the code accordly.


GLfloat windowWidth = 133;
GLfloat windowHeight = 100;

I guess what I'm asking is there a way to make it reference


glutInitWindowSize(800,600); instead of spending all that time maunally resizing it??? I mean you could easly make a screen saver with this code and if you allowed it to automatically ajust to the screen size you definatly can't make changes to live code on the fly.

Fuzzy
04-16-2008, 05:56 PM
I don't want to be rude...but my suggestion is that you try to understand what the code does, this is almost as basic as it gets.

Your Right. I shouldn't depend on folks. I'm sorry.

-NiCo-
04-16-2008, 06:21 PM
When the window is created and each time the window is resized the function you pass to glutReshapeFunc is called, so you can update your variables there.

Try this code for changeSize:



void ChangeSize(GLsizei w,GLsizei h) {

if (h==0)
h=1;

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

windowHeight = 0.5*h;
windowWidth = 0.5*w;

glOrtho(-windowWidth,windowWidth,-windowHeight,windowHeight,1.0,-1.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

Fuzzy
04-16-2008, 09:14 PM
Ok...I think I've figured out the problem. I think they are assuming you have a programing knowledge with these examples and I'm getting lost real fast cause they missing code fragments because they are just giving you just the new content<updates only not the full code>.

I've entered chapter 3 of the book and they give code snippetts and assume you know how to fill in the rest. They assume you use the previous code and add on to it but they are leaving the freedom up to you to modify that code accordingly. Let me explain to you what's going on here and maybe you point me in the right direction.

I am being stupid and I'm learning almost every aspect to game making on my own. I have no history with this type of stuff but it's my passion and I do it in my spare time. I've given myself 5 years to learn what I want. Here's what I'm presently doing.

I'm currently using <ug hate to say this> Microsoft Visual C++ 2008. I have openGL, Glee, Glew & glut installed. I'm currently enrolled for C++ at a local college. I have a fair grasp of C++ but lack the expirence with it. So I'm making a lot of mistakes and missing simple things I should know.

My Goal:
by the 5th year manage to make a Game engine I can use on windows, mac <hopefully Linux> using currently technology to call to the graphics portion and shaders. I believe OpenGL can help realize the graphics end of my goals. I will learn about the rest as I go.

What I want:
I need something that will teach the in's and out of OpenGL with tutorials so I can practice the code read up on the functions and get a better understanding of how OpenGL can help me in the future.

respectfully,

Sean

babis
04-16-2008, 10:02 PM
My opinion on this :

5 years is more than enough, if you're hardcore about the thing.
Since you chose C++, be sure to get experience with it, because it's more fundamental than OpenGL itself, if you want to write sth. Also be sure to understand that OpenGL is just a graphics API (*just*, *coughs*), and that in order to make a game engine, many ^ n aspects are needed. Linear algebra, vector math, graphics theory are just for the graphics alone, without talking about sound, networking & other stuff. And don't plan to build the Grand Engine before you've tried some 100s of stuff.

You can find millions of tutorials for OpenGL on the net, but be sure to understand also the concepts behind the tutorials, and how they work. The concepts & the theory why everything works like it does are more important, so be sure to select carefully your sources.

Anyway, one of my fav sites when I started was Lighthouse3D.

Good luck!

babis

Spindiggy
04-16-2008, 10:59 PM
Hello,

Have you visited the publishers web site to see if they have updated code samples?

Just a thought as I've noticed many sites offer newer code that didn't work in the books sometimes.

Heck, find more opengl books, visit the publisher's site and you can download all the code included in all game programming books and on the cd's for free.

EDIT: I don't see an OpenGL Bible book, I do see OpenGL Super Bible, Code here, http://www.starstonesoftware.com/OpenGL/

Chris

Fuzzy
04-16-2008, 11:31 PM
My opinion on this :

5 years is more than enough, if you're hardcore about the thing.
Since you chose C++, be sure to get experience with it, because it's more fundamental than OpenGL itself, if you want to write sth. Also be sure to understand that OpenGL is just a graphics API (*just*, *coughs*), and that in order to make a game engine, many ^ n aspects are needed. Linear algebra, vector math, graphics theory are just for the graphics alone, without talking about sound, networking & other stuff. And don't plan to build the Grand Engine before you've tried some 100s of stuff.

You can find millions of tutorials for OpenGL on the net, but be sure to understand also the concepts behind the tutorials, and how they work. The concepts & the theory why everything works like it does are more important, so be sure to select carefully your sources.

Anyway, one of my fav sites when I started was Lighthouse3D.

Good luck!

babis

thanks...it's a good site.

Fuzzy
04-24-2008, 01:16 PM
Hey Nico since you were nice enough to assist me I figured I'd follow up with you and let you know I did some probing. After a little work I discovered they have the source code to their work here on the openGL site. After getting the source code all my answers where answered. there are 4 lines of code they omitted from the book which allows the program to work flawlessly. Below is the code.

[CODE]if (w <= h)
{
windowWidth = 100;
windowHeight = 100 / aspectRatio;
glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
}
else
{
windowWidth = 100 * aspectRatio;
windowHeight = 100;
glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
}[\code]

Actually after carefully inspecting it they completly redid the Changesize function.

-NiCo-
04-24-2008, 02:15 PM
Glad to hear your answers were answered ;)
Kind of stupid that it's not in the book though...

Fuzzy
04-28-2008, 12:01 PM
Glad to hear your answers were answered ;)
Kind of stupid that it's not in the book though...

Well what I find more interesting is that all the examples in the book use global functions. All the changes that were made were done inside the fuction. Plus they complete changed the code to the ChangeSize Function. So I don't feel so stupid now I couldn't figure it out. But it kinda sorta does make sense to intialize them inside the function. Like you I'm use to intializing outside the function but I'm straight out of class.

Fuzzy
04-28-2008, 12:04 PM
Hello,

Have you visited the publishers web site to see if they have updated code samples?

Just a thought as I've noticed many sites offer newer code that didn't work in the books sometimes.

Heck, find more opengl books, visit the publisher's site and you can download all the code included in all game programming books and on the cd's for free.

EDIT: I don't see an OpenGL Bible book, I do see OpenGL Super Bible, Code here, http://www.starstonesoftware.com/OpenGL/

Chris

You can also find the source code here as well

http://www.opengl.org/superbible

They also give you mac, linux and stuff. So there are more complete files here. It's 255 megs...FYI.