Note that there are some explanatory texts on larger screens.

plurals
  1. POBlending problems using opengl (via lwjgl) when using a png texture
    primarykey
    data
    text
    <p>I have a (hopefully) small problem when using blending in OpenGL. Currently I use LWJGL and Slick-Util to load the Texture. The texture itself is a 2048x2048 png graphic, in which I store tiles of a size of 128x128 so that I have 16 sprites per row/column. Since glTexCoord2f() uses normalized Coordinates, I wrote a little function to scale the whole image to only show the sprite I want to. It looks like this:</p> <pre><code>private Rectangle4d getTexCoord(int x, int y) { double row = 0, col = 0; if(x &gt; 0) row = x/16d; if(y &gt; 0) col = y/16d; return new Rectangle4d(row, col, 1d/16d, 1d/16d); } </code></pre> <p>(Rectangle4d is just a type to store x, y, width and height as double coords)</p> <p>Now the problem is, once I use these coords, the sprite displays correctly, the transparency works correctly too, just everything else becomes significantly darker (well more correctly it becomes transparent I guess, but since the ClearColor is black). The sprite itself however is drawn correctly. I already tried changing all the glColor3d(..) to glColor4d(..) and setting alpha to 1d, but that didn't change anything. The sprite is currently the only image, everything else are just colored quads.</p> <p>Here is how I initialised OpenGL:</p> <pre><code> glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); glMatrixMode(GL_MODELVIEW); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); </code></pre> <p>And here is how I load the sprite (using slick-util):</p> <pre><code>texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/sprites.png")); </code></pre> <p>And finally I render it like this (using the helper function getTexCoord() mentioned at the top):</p> <pre><code> texture.bind(); glColor4d(1, 1, 1, 1); glBegin(GL_QUADS); { Rectangle4d texCoord = getTexCoord(0, 0); glTexCoord2f((float)texCoord.getX(), (float)texCoord.getY()); glVertex2i((Game.WIDTH-PLAYERSIZE)/2, (Game.HEIGHT-PLAYERSIZE)/2); glTexCoord2f((float)texCoord.getX()+(float)texCoord.getWidth(), (float)texCoord.getY()); glVertex2i((Game.WIDTH+PLAYERSIZE)/2, (Game.HEIGHT-PLAYERSIZE)/2); glTexCoord2f((float)texCoord.getX()+(float)texCoord.getWidth(), (float)texCoord.getY()+(float)texCoord.getHeight()); glVertex2i((Game.WIDTH+PLAYERSIZE)/2, (Game.HEIGHT+PLAYERSIZE)/2); glTexCoord2f((float)texCoord.getX(), (float)texCoord.getY()+(float)texCoord.getHeight()); glVertex2i((Game.WIDTH-PLAYERSIZE)/2, (Game.HEIGHT+PLAYERSIZE)/2); } glEnd(); </code></pre> <p>The result is this (sprite is drawn correctly, just everything else is darker/transparent): <img src="https://i.stack.imgur.com/eEDMw.png" alt="How it looks after assigning the Texture to the quad"> Without the texture (just a gray quad), it looks like this (now everything is correctly drawn except I don't have a sprite): <img src="https://i.stack.imgur.com/avD2B.png" alt="How it looks without assigning a texture to the quad"></p> <p>Thanks for everyone who bothers to read this at all!</p> <hr> <p><strong>Edit</strong>:</p> <p>Some additional info, from my attempts to find the problem: This is how it looks when I set the ClearColor to white (using glClearColor(1, 1, 1, 1) ): <img src="https://i.stack.imgur.com/K8FjC.png" alt="Setting ClearColor to white"></p> <p>Another thing I tried is enabling blending just before I draw the player and disable it again right after I finished drawing: <img src="https://i.stack.imgur.com/tEyXX.png" alt="this happens while only using blending when drawing the player (blackclear)"></p> <p>Its a bit better now, but its still noticeably darker. In this case it really seems to be "darker" not "more transparent" because it is the same when I use white as a clear color (while still only enabling blending when needed and disabling it right after) as seen here: <img src="https://i.stack.imgur.com/eHbj0.png" alt="this happens while only using blending when drawing the player (whiteclear)"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload