Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: OpenGL 2D tutorial?  (Read 2725 times)

Hound

  • Bay Watcher
    • View Profile
OpenGL 2D tutorial?
« on: February 07, 2010, 01:40:34 pm »

Basically I wanna get started learning how to do 2d graphics with OpenGL.  I'm pretty out of practice with programming in general and i've never actually used OpenGL, or any other graphics library.  I think I could do well if I could just find a tutorial to get me started, but all the ones I can find only seem to be interested in 3D graphics, which don't really interest me right now.

So, has anyone seen a good 2d opengl tutorial they could point me to?  Specifically I'm looking for graphics similar to DF (big surprise I know) but I'll take whatever you got.  And yes, I already checked out the Roguelike Megathread, though I guess if I missed something there that you think is exactly what I'm looking for, feel free to make fun of me (as long as you point it out first :P )

EDIT: Oh, I suppose I should mention I intend to be programming in C++ just cause it's waht I know best.  I don't have the money for a class right now and I don't feel like trying to learn a language on my own at this time.  Thanks.
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: OpenGL 2D tutorial?
« Reply #1 on: February 08, 2010, 08:16:56 pm »

I don't know any tutorial without looking one up, but I can tell you that 2d in OpenGL is not very far from 3d.

First, by enabling the depth buffer, you can use layering regardless of render order(though semitransparency requires proper render order, and second, you can have fully 3d objects in a 2d appearance, if the view is not perspective(the further objects are the same size as the closer ones. PErspective is the more standard 3d, objects get smaller as they get farther away))

To do 2d graphics, use the vertex2* functions, or the vertex3* ones with the last coordinate set to 0.

OpenGL is more of a lower level graphics system, so you would probably end up writing a function to draw a textured quad from a spritesheet or something.

Something like this

Setup:
Code: [Select]
    glEnable(GL_BLEND);//Unnessecary, unless you want to use semitransparency
    glEnable(GL_TEXTURE_2D);//The only important line. Enables textures.
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//the/one of the better looking transparency modes, I think
(fancy transparency :))
(Texture loading excluded)

Drawing:
Code: [Select]
    glBegin(GL_QUADS);
      glTexCoord2f((0+t*32)/256.0,(0)/32.0);
      glVertex3f((x-16)/200.0,(y)/200.0,z/1000.0);
      glTexCoord2f((32+t*32)/256.0,(0)/32.0);
      glVertex3f((x+16)/200.0,(y)/200.0,z/1000.0);
      glTexCoord2f((32+t*32)/256.0,(32)/32.0);
      glVertex3f((x+16)/200.0,(y+32)/200.0,(z)/1000.0);
      glTexCoord2f((0+t*32)/256.0,(32)/32.0);
      glVertex3f((x-16)/200.0,(y+32)/200.0,(z)/1000.0);
    glEnd();
(That includes way too much unnessecary math, but is a real example from an isometric level engine I started today.)

Simplified:
Code: [Select]
    glBegin(GL_QUADS);
      glTexCoord2f(0,0);
      glVertex2f(-1,-1);
      glTexCoord2f(1,0);
      glVertex2f(1,-1);
      glTexCoord2f(1,1);
      glVertex2f(1,1);
      glTexCoord2f(0,1);
      glVertex2f(-1,1);
    glEnd();
(Draws a single texture taking up the whole screen. Add some simple math in, and you can draw a fragment of the texture, with slightly more effort, a full 2d game)

One possibility is to render individual layers to single textures, so you can draw the background layer and the foreground layer each with a single quad. This might be faster, though generating/changing the texture likely wouldn't.
Logged
Eh?
Eh!

Hound

  • Bay Watcher
    • View Profile
Re: OpenGL 2D tutorial?
« Reply #2 on: February 10, 2010, 06:27:56 pm »

hm, interesting many thanks :)
Logged

Empty

  • Bay Watcher
    • View Profile
Re: OpenGL 2D tutorial?
« Reply #3 on: February 10, 2010, 06:32:31 pm »

Why not take a look at the nehe opengl tutorials.

Those got me to learn a lot about opengl.
Logged

sproingie

  • Bay Watcher
    • View Profile
Re: OpenGL 2D tutorial?
« Reply #4 on: February 10, 2010, 06:43:50 pm »

Using glOrtho with the view volume width and height the same as your window size, and the depth going from -10 to 10 or so is typical.  On that you just draw textured quads or point sprites.

I would just start with some decent OpenGL tutorials, since they typically start with a little 2D anyway, and by the time you get to textures, it will become obvious how to do the 2d.  Unfortunately I don't know of any good free OpenGL tutorials -- everyone points at the NeHe tutorials, and they're frankly not very good.  I got a copy of the OpenGL SuperBible which is about a million times better.

Logged
Toady is the man who Peter Molyneux wishes he was

Quote from: ToadyOne
dragon pus was like creamy gold. Infect and collect!

Hound

  • Bay Watcher
    • View Profile
Re: OpenGL 2D tutorial?
« Reply #5 on: February 10, 2010, 08:14:07 pm »

Empty: already have a little bit, and they would probably work.  Thanks

Sproingie: would love to try that book, thing is money is super tight for me right now and I can't really afford to drop 50$ on a book, so I was sorta hoping for something free.
Logged

sproingie

  • Bay Watcher
    • View Profile
Re: OpenGL 2D tutorial?
« Reply #6 on: February 10, 2010, 08:23:02 pm »

I hear you about the money situation.  Anyway the NeHe tutorials aren't too terrible, they're just not very up to date, and very cluttered.  But there's not much to learn as far as 2D goes with opengl, at least not until you start wanting to mess with shaders, and nvidia's Cg tutorial is free and excellent as far as shader programming goes.

And while learning OpenGL is always good, you might also consider using SFML, which is an SDL alternative that renders 2D to an OpenGL context (something that SDL 1.3 would do too if its final release weren't vaporware).
Logged
Toady is the man who Peter Molyneux wishes he was

Quote from: ToadyOne
dragon pus was like creamy gold. Infect and collect!