Starting learning openGL/GLUT or something O_o

Want to participate in game development? Need help in your game project? Open a new thread where you introduce yourself in here.

Moderator: Game Hunters

User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Starting learning openGL/GLUT or something O_o

Post by Maz »

Hi deeee Ho peeps!

It may be that I got too much sun, but nevertheless I suddenly decided to start learning creating something graphical with C/C++. And even though I have been doing C/C++ for a few years now, this is 100% new field for me. I have never needed to do anything graphical. Actually the environment I am programming for work, does not even have a display :D

I believe I'll start with openGL and (mesa)GLUT, due to the fact that

1. It's free (or some other library with same API).
2. It's available for many platforms. (I'll be using linux)

I try to write how I am doing in here, but I believe I'll be not advancing with too furious speed, due to the fact that I do not have much of time... My first goal is to learn enough to be able to do a simple, singleplayer pong game :) IE 2 padges on left and right, and a ball bouncing between them.

First I tried to find something to start with. Here's the first few links I am trying to familiarize myself with:
http://www.eecs.tulane.edu/www/Terry/Op ... ction.html
http://www.oreillynet.com/pub/a/network ... ender.html
http://www.oreillynet.com/pub/a/network ... build.html
http://www.oreillynet.com/pub/a/network ... build.html
http://www.opengl.org/documentation/spe ... spec3.html

If anyone has anything better to offer, I'll be glad to get advieces :)

This far I have managed to get following pieces of information:
1. libraries/headers may be named differently depending on the library you use.
2. glut/open GL (I do not really know which one I am talking about - APIs are somewhat mixed in my head) seem to be working via callback functions you create and register to the engine. Then you'll pretty much give the controll of your program to the openGL/GLUT. This is totally new way of thinking for me, so I propably do really ugly structure in my first codes :D

Finally, if anyone knows this subject and is willing to comment things I write, please do so. As I said, this is new to me, and I'll propably do bunch of errors :)
User avatar
Pager
Winner of CWF review contest!
Posts: 1249
Joined: Mon May 01, 2006 15:54
Location: Berlin, Ontario

Post by Pager »

Good to hear you're adventuring into graphics Maz!!

Just so you know, I took a course in university dealing with glut, so I could try and assist you with some things if you get stuck. :D

I'll also see if I can dig up some of the notes I took in that class...and perhaps find a few examples for you as well...
CWF - Safer than Crack, Twice as Addicting
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

EEeeeexcellent Pager! :)

Please, please do assist me :) Whenever you notice I write something incorrect in here, correct me :) Also, all notes, examples and so on are welcomed :) It's great to hear someone shares this 'task' with me :)
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Well, I had a 10 minutes time to read this today. I have done first rough plans how to go on with the pong game :)

1. I need to register callbacks for
keypress (steering bats)
drawing
resize
and whatchamacallit events.

Then I'll maintain position of ball and bats && perform some checks where they are positioned.

A few questions have arised, and I'll be seeking answers to those next time when I'll have time..

1. Is there even which can be set to occur if certain 'drawing' reaches certain position? (bats are at the top/bottom of window, ball is going out of the screen...)
Or do I need to check these things for example at the drawing callback?

2. How can I set the framerate (adjust the speed)?
3. can I safely use fork() / pthreads, or does those mess things? (Not really relevant at this part, but they tend to be basic structures in most of the apps...)

This is really getting interesting :) I believe I'll eventually manage to draw my first pictures on the screen using something else but ascii characters :D
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Maz wrote:Well, I had a 10 minutes time to read this today. I have done first rough plans how to go on with the pong game :)

1. I need to register callbacks for
keypress (steering bats)
drawing
resize
and whatchamacallit events.

Then I'll maintain position of ball and bats && perform some checks where they are positioned.

A few questions have arised, and I'll be seeking answers to those next time when I'll have time..

1. Is there even which can be set to occur if certain 'drawing' reaches certain position? (bats are at the top/bottom of window, ball is going out of the screen...)
Or do I need to check these things for example at the drawing callback?

2. How can I set the framerate (adjust the speed)?
3. can I safely use fork() / pthreads, or does those mess things? (Not really relevant at this part, but they tend to be basic structures in most of the apps...)

This is really getting interesting :) I believe I'll eventually manage to draw my first pictures on the screen using something else but ascii characters :D
watchamacallit = expose. Oh, btw. When does this expose event relly occur?
User avatar
Pager
Winner of CWF review contest!
Posts: 1249
Joined: Mon May 01, 2006 15:54
Location: Berlin, Ontario

Post by Pager »

Maz wrote:1. Is there even which can be set to occur if certain 'drawing' reaches certain position? (bats are at the top/bottom of window, ball is going out of the screen...)
Or do I need to check these things for example at the drawing callback?
As far as I know there is no callback for clipping, or for limiting the boundaries. At least I have never used it, but with pong, this is where angles and physics is going to come into play, so you might want to program that yourself anyways.

As for the Expose events, I can't recall. But, I think it may have something to do with setting the camera or portal over the canvas, then moving it around. showing more of the scene, then taking it away...but I'll have to check...
CWF - Safer than Crack, Twice as Addicting
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Dangnabbit.

It seems to me that mesa has dropped the tk library :| (At least header gltk.h is not anymore in my mesaGL/mesaGLU/mesaGLUT packages :|) All TK_ family defines and functions the tutorial I followed used, are now undefined :| So basically I need to find another, more up-to-date tuto. It's sad since the tutorial I used was pretty simple, easy to follow, and had lot's of examples :| Well, one cannot always win :|

Anyways, THANKS PAGER! :)
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Okay.. I spent half an hour studying this a bit more. I tried to comprehend idea of using vertexes in order to produce objects, and idea of rotate and translate functions. I also wrote the first draft of the first callbacks :) (Did not have time to try to compile the though :p ) Basically reshaping callback (generic) and displaying calbacks for bats and the ball are done... Except the working motion part. I added translate funcs in there though, but I do not really know how to use them... I hate global variables, but... dunno. It is quite propable that I need to do another thread (or process) for maintaining the position & checking we are on valid position and for timing anyways. I just am not really enthusiastic about the idea of creating socket connection between timing/positioning process and drawing callback though :|

However it's way too late for me to be awake, but this thing just calls me...
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

This is more challenging than I thought. How to share the data betweeb objects which will be drawn? IE, if think of pong game, how to maintain the position of the ball and plates? I could of course use global variables, but those tend to lead into a mess in larger apps... Well, I think one answer can be static variables / functions. (Naturally with C++ it would be static classes).

Somehow I like plain C, although it makes things a bit more complicated. I have been playing with a thought of using a few static functions which take care updating && holding the common data. Can you see any point in it? If we talk about the pong game, I mean something like:

Code: Select all

CB_drawWindow()  //Callback for display event
{
          int game_status=0;
/*things*/
         update_common_data(DMODE_GET_GAME_STATUS,(void *)&game_status);
         switch(game_status)
         {
                   case DGAME_STATUS_PL1_LOST:
                   /*do things...*/
                   break;
                   .
                   .
                   case DGAME_STATUS_NORMAL:
                   drawLeftPlate();
                   drawRightPlate();
                   drawBall();
         }
/*swap buffers etc..*/
}

CB_keyboard()
{
        switch(key)
        {
                case DKEY_PL1_DOWN:
                *common_data_ptr=DKEY_PL1_DOWN;
                 update_common_data(DMODE_PL_1_KEY_DETECTED,(void *)common_data_ptr);
                break;
                .
                .
                .
        }
}

drawLeftPlate()
{
            SpositionStruct lplate_pos;
           update_common_data(DMODE_LEFT_PLATE_PLACE_GET,(void *)&lplate_pos);
.
.
.
}
static update_common_data(int function_mode,void *data)
{
           switch(function_mode)
           {
                     case DMODE_LEFT_PLATE_PLACE_GET:
                            //cast dataptr to right type and update values&&do necessary checks...
                      .
                      .
                      .
            }
/* Here we do appropriate operations && store data. */
/* Another possibility is to make this just an ordinary function, which calls appropriate static functions/functions with static data members.. */
          }
}
Okay, This is just a draft, but I assume you get the overall idea. Does this sound reasonable at all?
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

First graphics produced ^_^;
You do not have the required permissions to view the files attached to this post.
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Post by Chroelle »

WOW! :lol:

I cant help but see the irony in such a long technically difficul post leading up to a screenshot of PONG. I think no one realy knows how much effort that is put into something like this.
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Pager
Winner of CWF review contest!
Posts: 1249
Joined: Mon May 01, 2006 15:54
Location: Berlin, Ontario

Post by Pager »

Do you mean you want to use the same Positioning resolution function for both paddles?? (ie, down_direct == y-- && up_direct == y++, sort of thing)

hmmm...i can't really think of why that wouldn't work...especially for the paddles...

I think that would keep your code a little bit more clean...you'll need to do keep track of the positioning anyways, but do you want to write it out twice and keep it localized to its respected paddle...i'd think it's a pretty safe bet to do it as a common...

Although that definitely won't work for the ball. I have a feeling that'll be a whole new monster...
CWF - Safer than Crack, Twice as Addicting
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

I am sorry.. I messed static functions and variables :/

Naturally I meant I'll have static variables in the 'update_common_data' function. something like

Code: Select all

#define D_DIRECTION_UP 1
#define D_DIRECTION_DOWN -1
#define D_DIRECTION_STANDBY 0
#define D_DIRECTION_LEFT 1
#define D_DIRECTION_UP -1;

typedef struct SPlateInfo
{
      int x; ///< x-coordinate
      int y;
      int z;
      int direction;
}SPlateInfo;
typedef struct SBallInfo
{
      int x; ///< x-coordinate
      int y;
      int z;
      int speed_vertical;
      int speed_horizontal;
      int direction_vertical;
      int direction_horizontal;
}SBallInfo;
/*I just noticed data needs to be double pointer if we wish to be able to reallocate it in the function. This leads into mess. Perhaps I should use yet another struct, which holds tyhe data pointer. Then I could pass pointer to that struct in the func, and not to worry about realloc changing the address of data pointer, since correct address would be updated in the struct anyways...
*/
update....(int mode, void **data)
{
       static SPlateInfo plates[PLAYER_MAX]; /*player max will definitely be 2 at start, but perhaps if I'll have enthusiasm left, I'll develope something like multiplayer pong for 4 players :D (with networking capabilities :p ) */
       static SBallInfo[BALL_MAX];
       int player_no;
       void *tmp;

       switch(mode)
       {
               case DMODE_LEFT_PLATE_PLACE_GET:
                      //cast dataptr to right type and update values&&do necessary checks... 
                      player_no=*((int*)data[0]);
                      if(NULL==(tmp=realloc(*data,sizeof(SPlateInfo))))
                      {
                             fprintf(stderr,"malloc failed, out of mem?");
                             exit(1);
                      }
                      else
                      data[0]=(void *)plates[player_no];
                      return;
                      break;
                 case DMODE_PL_1_KEY_DETECTED:
                      plates[0].direction=( D_DOWNKEY==*((int*)data[0]))?D_DIRECTION_DOWN : D_DIRECTION_UP;
                      //reallocate datapointer
                      //cast and store player[0] in datapointer
                      .
                      .
                      .
I hope this clears up what I thought?
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Okay..

First development version is slowly getting it's structure.
This does not include moving of plates, scores, changing ball angle or... Well basically nothing much :D

But this is a start, and those who wish to comment the structure can now do so.

(Oh, and I am not sure if it compiles :D) Here it is anyways :)

.... PaaaaGeeeerrrrrr... I'm waiting for some comments :D ....
Mmm.. Perhaps we should create CWF pong out of this :D
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Okay..

First development version is slowly getting it's structure.
This does not include moving of plates, scores, changing ball angle or... Well basically nothing much :D

But this is a start, and those who wish to comment the structure can now do so.

(Oh, and I am not sure if it compiles :D) Here it is anyways :)

.... PaaaaGeeeerrrrrr... I'm waiting for some comments :D ....
Mmm.. Perhaps we should create CWF pong out of this :D
You do not have the required permissions to view the files attached to this post.
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Okay. I decided to do this a bit harder way. Let's see if I'll manage ;)

Unfortunately this means my pong won't be portable, IE I will not be able to do windows version. Reason is that I decided to solve the speed/timing issue by using linux signals. I'll attempt to write a callback timer function, which allows one to set a timer executing callback function with certain time intervall. Purpose is to a) learn linux signals a bit better, meanwhile creating a timer service which I can perhaps use later on other projects.

Basically my idea is to use timer_create() system call, to create timer. Naturally this timer thingee will be forked in it's own process. Another thing this forces me to ponder is semaphores/mutexes, in order to prevent multiple processes from accessing the same resource(s) simultaneously. This will probs be hard for me, but if I manage to learn... Well, then it's worth =) Unfortunately I do not have a good book about this topic, and man pages etc. are really a mess... Not to mention browsing linux librarie's headerfiles for some information about typedefines :|

Basically signals should fulfill POSIX standard though, but I doubt I can do this portable.. Pager/Zyx/Spanek/MrGreen - do you have any idea if there's good online resource explaining these things?

Oh, naturally I plan implementing the 'update positions' callback function, which I could then register into the timer ;)
User avatar
spanek
Tycoon
Posts: 266
Joined: Thu Dec 21, 2006 16:12
Location: Athens

Post by spanek »

C/C++ is not my strong point... I can't help you in this :(
[url=http://s1.gladiatus.gr/game/c.php?uid=17513]Press me if you dare!![/url]
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Thank's anyways Span =}
I hope you've been doing well. It has been a while since we were talking properly :| Well, I hope I'll have some time to be in here in the future :)
User avatar
spanek
Tycoon
Posts: 266
Joined: Thu Dec 21, 2006 16:12
Location: Athens

Post by spanek »

Me too! I am running all day with my job and with my new house... :(
[url=http://s1.gladiatus.gr/game/c.php?uid=17513]Press me if you dare!![/url]
User avatar
Pager
Winner of CWF review contest!
Posts: 1249
Joined: Mon May 01, 2006 15:54
Location: Berlin, Ontario

Post by Pager »

Sorry Maz, been going through hell at work lately. I'll take a look more in depth tomorrow evening. :)
CWF - Safer than Crack, Twice as Addicting
Post Reply