PDA

View Full Version : Moving objects ( few questions about OGL )



Neomex01
12-07-2010, 10:32 AM
Hello, I'm making a 2d game, and I think it's good moment now to say that I've got completly no experience coding in OpenGL, however I've read lot of stuff about how all this works...
My first question is:
What is the easiest way to move object, and what is known in OpenGL as 'object' ?
Thats my code:


#include <glut.h>

void displayCB(void) /* function called whenever redisplay needed */
{
glClear(GL_COLOR_BUFFER_BIT); /* clear the display */
glColor3f(1.0, 1.0, 1.0); /* set current color to white */
glBegin(GL_POLYGON); /* draw filled triangle */
glVertex2i(0,0); /* specify each vertex of triangle */
glVertex2i(800,0);
glVertex2i(300,300);
glEnd(); /* OpenGL draws the filled triangle */
glFlush(); /* Complete any pending operations */
}

void keyCB(unsigned char key, int x, int y) /* called on key press */
{
if( key == 'q' ) exit(0);

}

GLvoid Timer( int value )
{
if( value ) glutPostRedisplay();
glutTimerFunc(40,Timer,value);
}



int main(int argc, char *argv[])
{
int win;

glutInit(&amp;argc, argv); /* initialize GLUT system */

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(800,600); /* width=400pixels height=500pixels */
win = glutCreateWindow("Triangle"); /* create window */

/* from this point on the current window is win */

glClearColor(0.0,0.0,0.0,0.0); /* set background to black */
gluOrtho2D(0,800,0,600); /* how object is mapped to window */
glutDisplayFunc(displayCB); /* set window's display callback */

glutTimerFunc(40,Timer,0);

glutKeyboardFunc(keyCB); /* set window's key callback */

glutMainLoop(); /* start processing events... */

/* execution never reaches this point */

return 0;
}

and I want to move this triangle, how to do this?

What glut(smthing)Func (like glutKeyboardFunc()) which may be usefull I'm missing?

How to do double buffering in 2D project like mine?

And what is the easiest way to texture this triangle? I have to write whole file loading function?

I will be really thankfull for any help.

_arts_
12-07-2010, 12:37 PM
I think you should first read more about GL and try more simple programs so that you'll get more used to do things with it.

Nehe tutorials are very good from the beginning to the experienced level: http://nehe.gamedev.net/

Neomex01
12-07-2010, 12:49 PM
But it uses glaux&amp;winapi instead of glut.

MaxH
12-07-2010, 03:15 PM
But it uses glaux&amp;winapi instead of glut. Look at the bottom of each tutorial. Versions are listed for download in almost every computing environment including glut. The 4th lesson shows you how to move a triangle. It's in 3-space, but 2D is actually simpler.

_arts_
12-08-2010, 04:32 AM
But it uses glaux&amp;winapi instead of glut.

Yes I don't know why first lessons have not been updated yet (only the mac os version use glut)...

But as MaxH said, you have examples for glut for every tutorial :)

Neomex01
12-08-2010, 09:37 AM
Awesomness! thanks! :)
---
However my question is:
why isn't it moving when i change value? It can be only moved at beginning?


void displayCB(void) /* function called whenever redisplay needed */
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
//glLoadIdentity(); // Reset The Current Modelview Matrix
glPushMatrix();
glColor3f(1.0, 1.0, 1.0); /* set current color to white */

glTranslatef(x, y, 0.0);
glBegin(GL_POLYGON); /* draw filled triangle */
glVertex2f(0,0); /* specify each vertex of triangle */
glVertex2f(300,0);
glVertex2f(300,300);
glEnd(); /* OpenGL draws the filled triangle */
glFlush(); /* Complete any pending operations */
glPopMatrix();

glutSwapBuffers();
x++;
}

ugluk
12-08-2010, 11:32 PM
I agree with arts and I'd like to add, go develop on linux, not windblows. Direct X is the prime Windows 3D API, but on linux M$ has no influence and developing there is cosier.

lordmule
12-09-2010, 01:25 AM
Well GLUT decides when to call the display function, usually when the window is resized etc.

Since your starting out in OpenGL, I recommend adding this line in your main function:


glutIdleFunc(displayCB);

It will just cause display to be called all the time. You can later decide when something should be redrawn, thats when you can create your own idle function.

you could also do:

void keyCB(unsigned char key, int x, int y) /* called on key press */
{
if( key == 'q' ) exit(0);
glutPostRedisplay();
}

so display updated when any key is pressed.

Neomex01
12-11-2010, 04:45 PM
@lordmule
Thanks a lot! Works now!

@ugluk
For now I'm making games for myself, but if I'll choose game development on studies, I'll switch to DX ( learning it now it's completly pointless, because they will probably make few other version until I go to work and completly rewrite it, what is in their style :-) )

Why I want to stay with Windows, is that I'm using it from beginning, know what can do with win libraries, etc. ( windows.h and similar can be really helpfull, excluding fact, that are messy :-) )

---

Why when I don't use glPush/glPopMatrix objects move with growing velocity (x++) and when I use it - with same one.
What I mean, is that when I use it, and have something like this: x+=5, it moves whole time per 5 units, and when I don't use, it is rowing each frame ( 5u , 10u ... )?