## Some OpenGL 2D questions

Member
Posts: 22
Joined: 2008.10
Post: #1
Hello!

I'm trying to create some very simple 2d games using cocoa + OpenGL but i have some trouble getting there.

I'm currently playing around with shapes and mostly drawing squares in my custom OpenGLView. Something I have trouble understanding is however the coordinate system. When I type glVertex3f( -1, -1, 0); the corner is positioned at the top left. 1, 1 would be the bottom right. Is there a way to enter these values as pixels? I figured it has something to do with OpenGL beeing mainly used for 3D.

I also tried finding some OpenGL 2D tutorials but without any decent results. If you have any good suggestions where to get started please let me know! All tips are welcome! Books, links, tutorials!
Sage
Posts: 1,234
Joined: 2002.10
Post: #2
Read the manpages for glOrtho and glViewport.
Member
Posts: 22
Joined: 2008.10
Post: #3
Ah thanks!
One more problem though. When I draw a rectangle at x:0, y:0 it appears in the bottom left. I'd like it to appear in the top left corner. Increasing y seems to move the point upwards on the screen but my logic tells me it should be the other way around. Am I mixing this up in my head?
Member
Posts: 283
Joined: 2006.05
Post: #4
That's the standard in mathematics, though it often seems odd on a computer screen. If you want to flip it, just swap the bottom and top values in glOrtho.
Member
Posts: 22
Joined: 2008.10
Post: #5
Ah that explains alot I guess i just have to get used to it. Thanks a bunch!
Sage
Posts: 1,234
Joined: 2002.10
Post: #6
Note that while you can flip the projection, other OpenGL commands (like glReadPixels) always use window coordinates with the origin in the lower left.
Member
Posts: 22
Joined: 2008.10
Post: #7
Good to know! Thanks!

One more beginner question.

Now I can draw my beautiful rectangles on the screen but I now want to use some simple images instead of colored cubes.

What is the easiest and best way to apply textures to my objects?
Member
Posts: 67
Joined: 2006.07
Post: #8
That would be something called texture mapping. You can learn the basics pretty much anywhere (the Red Book, NeHe, etc). Google can find you tons of tutorials.

As for loading the actual image into the texture...well, that's kind of complicated. There are several ways of doing it on the Mac. Apple's OpenGL Programming Guide for Mac OS X covers a few techniques for loading image data into textures.

Personally, I use an external loading library called glpng to load texture data from PNG files. It's simple enough, and has worked quite well for me.

Also, since you're making a 2D game, you might want to look into using something called rectangular textures, which basically removes the normal power-of-two restriction from your texture sizes (this will all make sense after you've learned basic texture mapping). The (relatively minor) downside is that only graphics cards newer than the ATI Rage 128 can handle it. Anyways, search the forums if you're interested -- it's been brought up many times before.

Since when was "Fred" a placeholder variable?
Member
Posts: 22
Joined: 2008.10
Post: #9
Ah glpng sounds very interesting! Sorry for beeing a complete beginner though. Can someone give some directions on how to build the library from the source code for glpng?

I've found some nice tutorials but im stuck with the part where i need to load the png file as a CGImageRef. It seems to be very specific for the iPhone (which I'm currently trying this out on) and all the good info i found has been removed och cencored because of the NDA...

Found this particular example very interesting but I can't find how to replace the line
Code:
`textureImage = getCGImageForImageNamed(name);`
with something that works for iPhone. I know there is a forum for iPhone stuff but i felt it would be a bit of overkill to start a thread just for this line

Really great feedback btw, this forum rawks
Member
Posts: 22
Joined: 2008.10
Post: #10
Ah i finally found it!
Code:
```    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"texture" ofType:@"png"];     CGImageRef textureImage = [UIImage imageNamed:imagePath].CGImage;```

On to the next problem

When i try to build my project I get some linking errors.
Code:
```"_CGContextRelease", referenced from:       -[EAGLView drawView] in EAGLView.o   "_CGContextDrawImage", referenced from:       -[EAGLView drawView] in EAGLView.o   "_CGBitmapContextCreate", referenced from:       -[EAGLView drawView] in EAGLView.o   "_CGImageGetColorSpace", referenced from:       -[EAGLView drawView] in EAGLView.o ld: symbol(s) not found collect2: ld returned 1 exit status```

All my code so far
Code:
```    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"texture" ofType:@"png"];          NSFileManager *fm = [NSFileManager defaultManager];     BOOL fileExists = [fm fileExistsAtPath:imagePath];          if(fileExists) // if i remove the lines who generate errors this == true         NSLog(@"i think, therefore i am image"); // and yes you have to be funny when writing log messages :(     else         NSLog(@"no luck");              CGImageRef textureImage = [UIImage imageNamed:imagePath].CGImage;          GLuint texture[1];     CGContextRef textureContext;     GLubyte *textureData;     size_t textureWidth, textureHeight;          textureWidth = 50;     textureHeight = 50;          if (textureImage) {         textureData = (GLubyte*) malloc(textureWidth*textureHeight*4);         textureContext = CGBitmapContextCreate(textureData, textureWidth, textureHeight, 8, textureWidth*4, CGImageGetColorSpace(textureImage), kCGImageAlphaPremultipliedLast);                  CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (CGFloat)textureWidth, (CGFloat)textureHeight), textureImage);         CGContextRelease(textureContext);                  glGenTextures(1, texture);         glBindTexture(GL_TEXTURE_2D, texture[0]);         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);                  free(textureData);         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);     }```
Any thoughts?
Member
Posts: 67
Joined: 2006.07
Post: #11

Personally, I'd be careful about using Quartz/CoreGraphics for texture loading, mainly because you can run into some frustrating pitfalls if you use translucent images (stuff with premultiplied alpha and such). That's not to say that you shouldn't use it -- for the most part it should be fine. But if you run into any weird problems with alpha channels and such later on, come back and let us know.

And as for glpng, well, I didn't bother building it from the source -- I just stuck the source code in my project. Maybe not the most elegant way, but certainly the simplest! I also did the same thing with libpng and zlib, which glpng requires as well. I don't think it's that hard to compile these libraries from source, but I didn't feel like bothering.

Since when was "Fred" a placeholder variable?
Member
Posts: 22
Joined: 2008.10
Post: #12
Ah yes i've heard of the Quarts and texture woes. Does glpng work around that?

Unforetunatley the <ApplicationServices/ApplicationServices.h> did not get rid of the binding errors :/ Let me know if you need more code/info!
Luminary
Posts: 5,143
Joined: 2002.04
Post: #13
It's a linker error, not a compile error, Wowbagger. He'll need to add a framework.
Member
Posts: 67
Joined: 2006.07
Post: #14
Ah, that is indeed a linker error, sorry about that. Yeah, you'll have to add ApplicationServices.framework (located in /System/Library/Frameworks/) to your project in order to build successfully.

Anyways, glpng does get around the premultiplied issues that can come up with Quartz, since it loads through libpng instead (that's one of the reasons why I use it). Still, if I recall correctly, a lot of people here do use Quartz for texture loading.

Since when was "Fred" a placeholder variable?
Member
Posts: 22
Joined: 2008.10
Post: #15
Strange.... When i add the ApplicationServices i get
Code:
`framework not found ApplicationServices`
I just right clicked the frameworks folder and chose "add existing framework" and selected ApllicationServices.framework

Same wether or not i include the #import command for the framework :O