Sign in to follow this  
Followers 0
KingofNoobs

OpenGL
Dealing with 2D coords as ints

8 posts in this topic

Hello,

I have written a very rudimentary D3D11 renderer and am now working with SFML (OpenGL). I am only interested in 2D games at the moment so I was just rendering quads to the screen in D3D and would like to do the same thing with GL in SFML.

However, I have one big problem. In order to work directly with the hardware I have to be in floating point coordinates, which is not only inconvenient for me, but is causing some bleed in my program. For example, I have a texture atlas with a single black pixel, surrounded by some blue pixel blocks representing buttons. I want to use that single black pixel and scale it up 1920x1080 times and use it as a black background. But when i do so I get some bleeding and there is a gradation of blue on my screen. I have a feeling this is due to floating point inaccuracy.

I need my games to be pixel accurate and I was wondering how I could do this in GL/D3D without having to resort to floating point numbers. At the same time, I don't only want to be able to run my game in 1920x1080 but at any resolution that the user wants in windowed mode. I am a bit lost as to how I would represent this with ints, as I don't want my character to move farther on the screen in windowed mode than in full screen, because if I say move 30 pixels left, 30 pixels is a lot in a 200x200 window but not much in a 1920x1080 fullscreen mode.

Does anyone know how to do this or am I stuck using the basic graphics package in SFML. I spent significant time learning D3D and basic 3D graphics principles and to be honest I don't want to sink anymore time there right now. I am not interested in 3D at all and am a bit peeved that I have to learn all this 3D stuff just to get a 2D game running very efficiently on the 3D hardware. Does anyone have an idea?

Thanks.

-Dave Ottley
0

Share this post


Link to post
Share on other sites
Your two requirement are basically the opposite of each other: You want to handle things in pixels, but also want resolution independence. You can't have it both ways.
0

Share this post


Link to post
Share on other sites
Could I deal in floats but use some math magic to make the top left of the screen 0.f,0.f and the bottom right 1920.f,1080f?
0

Share this post


Link to post
Share on other sites
Talking about the bleeding problem - I don't think this is really a floating point accuracy issue, it's a texture sampling issue.

You want scaling, so that means you won't be able to do 1 to 1 texel to pixel mapping no matter how hard you try. This means you're going to be bilinear filtering on your textures. With texture atlases you need to add a border around each of images so that the bilinear filtering doesn't accidentally pull in any adjacent images. An alternative is to modify your UVs so that the UV rectangle is squeezed inwards by half a texel on all sides, but that'll cut off a little of your image slightly so you won't always get away with it.

If you need mipmaps for your atlases then that can be extra tricky, and you'll generally need larger borders, possibly some custom mipmap generation that discourages bleeding across atlas tiles or some atlas generation rule that encourages atlas tiles to align with power of two grid lines so that there's less bleeding in your lower mips. Edited by C0lumbo
4

Share this post


Link to post
Share on other sites
[quote name='KingofNoobs' timestamp='1355405731' post='5010199']
Could I deal in floats but use some math magic to make the top left of the screen 0.f,0.f and the bottom right 1920.f,1080f?
[/quote]

That's straightforward enough, I think. Assuming you're using shaders in OpenGL, you to use a projection matrix like the one documented here: [url="http://msdn.microsoft.com/en-gb/library/windows/desktop/dd373965%28v=vs.85%29.aspx"]http://msdn.microsoft.com/en-gb/library/windows/desktop/dd373965%28v=vs.85%29.aspx[/url]. If you're using fixed function OpenGL, then you can just use the glOrtho function directly.

The problem is that you might need to think about how to deal with different aspect ratios. Sometimes working to a 14:9 virtual resolution is best, then cut off bits or black bar it to cope with 4:3 and 16:9 - depends on your game really. Edited by C0lumbo
0

Share this post


Link to post
Share on other sites
well i posted this before, but i think you need something like [url="http://www.david-amador.com/2010/03/xna-2d-independent-resolution-rendering/"]this[/url] for directx.
2

Share this post


Link to post
Share on other sites
Columbo,

A few questions:
1) [quote name='C0lumbo' timestamp='1355413236' post='5010249']
Talking about the bleeding problem - I don't think this is really a floating point accuracy issue, it's a texture sampling issue.
[/quote] How can I sample my textures pixel accurately? I mean, one pixel of black should be enough to blow up to any resolution and fill the background with black, with no bleeding at all, am I correct? Or am I missing something?

2) [quote name='C0lumbo' timestamp='1355413236' post='5010249']
This means you're going to be bilinear filtering on your textures.
[/quote] What is bilinear filtering?

3) [quote name='C0lumbo' timestamp='1355413236' post='5010249']
With texture atlases you need to add a border around each of images so that the bilinear filtering doesn't accidentally pull in any adjacent images.
[/quote] How many pixels thick should the border be, and in what color should the border be?

4) [quote name='C0lumbo' timestamp='1355413236' post='5010249']
An alternative is to modify your UVs so that the UV rectangle is squeezed inwards by half a texel on all sides
[/quote] What does "Modify the UV mean?"

As a side note, I have switched over to using SFML so that rendering can be simple while I master other programming and game design concepts, but I noticed that if I place something at, for example, location (0, 1000) then it goes outside my screen if I have, say, a (800, 600) resolution. Is there any way in SFML to have the coordinates scale with the window, i.e. a y-coord of 1000 (or whatever, I really don't care) would always be just near the bottom of the screen just as it would in 1920x1080?

Thanks for all your help.
0

Share this post


Link to post
Share on other sites
[quote name='KingofNoobs' timestamp='1355577116' post='5010930']
What is bilinear filtering?
[/quote]
That alone makes me not want to help you. You do own a computer [url="http://lmgtfy.com/?q=What+is+bilinear+filtering%3F"]connected to the Internet[/url], don't you?
1

Share this post


Link to post
Share on other sites
Alvaro,

Appologies. I spend all day on the internet looking this and that thing up. It is now on my list to look up, but sometimes the best explanations come from kind individuals on this site. :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By recp
      Hi,
      I'm working on new asset importer (https://github.com/recp/assetkit) based on COLLADA specs, the question is not about COLLADA directly
      also I'm working on a new renderer to render (https://github.com/recp/libgk) imported document.
      In the future I'll spend more time on this renderer of course, currently rendering imported (implemented parts) is enough for me
      assetkit imports COLLADA document (it will support glTF too),
      importing scene, geometries, effects/materials, 2d textures and rendering them seems working
      My actual confusion is about shaders. COLLADA has COMMON profile and GLSL... profiles,
      GLSL profile provides shaders for effects so I don't need to wory about them just compile, link, group them before render

      The problem occours in COMMON profile because I need to write shaders,
      Actually I wrote them for basic matrials and another version for 2d texture
      I would like to create multiple program but I am not sure how to split this this shader into smaller ones,

      Basic material version (only colors):
      https://github.com/recp/libgk/blob/master/src/default/shader/gk_default.frag
      Texture version:
      https://gist.github.com/recp/b0368c74c35d9d6912f524624bfbf5a3
      I used subroutines to bind materials, actually I liked it,
      In scene graph every node can have different program, and it switches between them if parentNode->program != node->program
      (I'll do scene graph optimizations e.g.  view frustum culling, grouping shaders... later)

      I'm going to implement transparency but I'm considering to create separate shaders,
      because default shader is going to be branching hell
      I can't generate shader for every node because I don't know how many node can be exist, there is no limit.
      I don't know how to write a good uber-shader for different cases:

      Here material struct:
      struct Material { ColorOrTexture emission; ColorOrTexture ambient; ColorOrTexture specular; ColorOrTexture reflective; ColorOrTexture transparent; ColorOrTexture diffuse; float shininess; float reflectivEyety; float transparency; float indexOfRefraction; }; ColorOrTexture could be color or 2d texture, if there would be single colorOrTex then I could split into two programs,
      Also I'm going to implement transparency, I am not sure how many program that I needed

      I'm considering to maintain a few default shaders for COMMON profile,
      1-no-texture, 2-one of colorOrTexture contains texture, 3-........

      Any advices in general or about how to optimize/split (if I need) these shaders which I provied as link?
      What do you think the shaders I wrote, I would like to write them without branching if posible,
      I hope I don't need to write 50+ or 100+ shaders, and 100+ default programs

      PS: These default shaders should render any document, they are not specific, they are general purpose...
             I'm compiling and linking default shaders when app launched

      Thanks
    • By CircleOfLight97
      Hi guys,
      I would like to contribute to a game project as a developer (open source possibly). I have some experiences in C/C++ in game development (perso projects). I don't know either unreal or unity but I have some knowledges in opengl, glsl and shading theory as I had some courses at university regarding to that. I have some knowledges in maths and basic in physics. I know a little how to use blender to do modelling, texturing and simple game assets (no characters, no animation no skinning/rigging). I have no game preferences but I like aventure game, dungeon crawler, platformers, randomly generated things. I know these kind of projects involve a lot of time and I'd be really to work on it but if there are no cleary defined specific design goals/stories/gameplay mechanics I would like to not be part of it x) and I would rather prefer a smaller but well defined project to work on that a huge and not 'finishable' one.
      CircleOfLight97
    • By gamesthatcouldbeworse
      Hi, I finally released KILL COMMANDO on gamejolt for free. It is a retro-funsplatter-shooter with C64 style. Give it a try.
    • By phil67rpg

      void TimerFunction(int value) {  glutPostRedisplay();  glutTimerFunc(1000, TimerFunction, 1); } void drawScene() {  glClear(GL_COLOR_BUFFER_BIT);      drawScene_bug();  TimerFunction(1);  eraseScene_bug(); // drawScene_bug_two(); // eraseScene_bug_two(); drawScene_ship(); drawScene_bullet();  glutSwapBuffers(); }