Color accuracy on the edge of textures - java

I'm making a game in lwjgl (java opengl) which contains a mesh of textured cubes within it.
I have already fixed the common error of texture coordinates being inaccurate on the edges, my coordinates are 100% accurate.
While moving through the scene, the pixels on the edges of my bright red quads flash teal(which happens to be my clear color) at overlaps with green quads only for a very short moment. There are definately other quads behind the overlaps colored green.
The problem is only with the near and far sides of the top of the cube.
Before you ask, there are NO blue pixels in my texture.
My min/mag filters fixed a brown line at the overlaps of green. but I think this could be part of the problem.
How can I get these pixels to either stay red, or go green?
Or to be more specific, how can I make the blend only use the nearest color and not do any combining?
Here are my params:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GREATER, (float) 0.9);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(255, 255, 255, 1.0f);
GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
GL11.glTexParameterf( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);

Wow, solved. From looking at screenshots, it's actually my eyes tricking me with teal phantoms, as the borders flash from red to white as the screen moves and the pixel estimation changes. Stare at red long enough, close your eyes, and you'll see teal. I'll be looking up some anti-aliasing guides or changing my filter to blend the colors.

Related

OpenGL blending: Remove pixels being drawn over (immediate mode)

What I'd like to achieve is a blend where I draw two quads over eachother, both transparent, and the quad drawn last will cancel the colour of the previous quad completely, as if it was never there. However, it may not affect anything behind it besides that single other quad. Because I'm horrible at explaining, I made the following image:
I'm using very basic openGL immediate mode functions.
Currently I have something along the lines of:
glEnable(GL_BLEND)
glColor4f(0,0,1,0.3f);
glBegin(GL_QUADS);
{
glVertex2d(100, -100);
glVertex2d(-100, -100);
glVertex2d(-100, 100);
glVertex2d(100, 100);
}
glEnd();
glColor4f(0,1,0,0.3f);
glBegin(GL_QUADS);
{
glVertex2d(150, -50);
glVertex2d(-50, -50);
glVertex2d(-50, 150);
glVertex2d(150, 150);
}
glEnd();
This isn't really a blending problem per se.
One way to solve this would be to change the depth buffer comparison function.
Use GL_LESS for the green square, while drawing the blue square first.
This way the pixels of the green square overlapping the blue square, simply wouldn't be drawn at all to begin with.
glDepthFunc(GL_LEQUAL);
// Draw the blue square
glDepthFunc(GL_LESS);
// Draw the green square
If you want to have elements visible under both the blue and green square. You could draw the first and then clear the depth buffer and then it's the same.
glDepthFunc(GL_LEQUAL);
// Draw the red squares
glClear(GL_DEPTH_BUFFER_BIT);
glDepthFunc(GL_LEQUAL);
// Draw the blue square
glDepthFunc(GL_LESS);
// Draw the green square
Whether there's a simpler way to do it, depends on what the overall goal is.

Slick2d / LWJGL Adjusting alpha channel in OpenGL layer ( Java )

I apologize for some of my ignorance as I am fairly new to Slick2D and LWJGL. Essentially what I'm trying to do is make a scene look like night time by covering it with a GL_QUADS rectangle that is tinted blue and is translucent.
That part is easy enough. What I want to do from there is draw triangles into this layer that vary the alpha channel so. The reason I want to do this is so I can simulate a light source by decreasing the opacity of the blue tinted rectangle as it gets closer to the light source.
I drew an example of what the expected result should be with the green being the background, the blue being the nighttime effect created by a blue tinted rectangle, and the increasingly dim light source in the center.
I need to find a way to do this with triangles because I created a raycasting algorithm that generates the result as a series of gradient triangles.
I apologize if this is explained poorly. I will answer any questions you might have.
Here is the chunk of code used to create the blue tinted rectangle:
glColor4f (0.0f,0.0f,1.0f,0.4f);
glBegin(GL_QUADS);
glVertex2f(0,0);
glVertex2f(screenWidth,0);
glVertex2f(screenWidth,screenHeight);
glVertex2f(0,screenHeight);
glEnd();
I would like to write a modified version of the following code to adjusted the alpha channel of that rectangle.
glBegin(GL_TRIANGLES);
setAlphaOfPriorLayer(0.0f);
glVertex2f(x1,y1);
setAlphaOfPriorLayer(0.4f);
glVertex2f(x2,y2);
setAlphaOfPriorLayer(0.4f);
glVertex2f(x3,y3);
glEnd();
Again, I'm using triangles to approximate a circle and allow for proper raycasting.
To achieve this, the use of a Frame Buffer Object is super useful. A FBO allows you to essentially render to a texture which can then be displayed on the screen. In my particular case, I rendered the elements to a FBO then used a shader while drawing it to the screen to get the desired opacities.

LWJGL: Slick: Drawing fonts in 3D world

How to draw Slick's fonts (UnicodeFont), which can be drawn only via drawString(), which provides only x and y, in a 3D world?
I have tried this:
public void drawHudString(float _posX, float _posY, String _text, UnicodeFont _font, Color _color) {
glPushMatrix();
glRotatef(cam.rotX, -1.0f, 0.0f, 0.0f);
glRotatef(cam.rotY, 0.0f, -1.0f, 0.0f);
glRotatef(cam.rotZ, 0.0f, 0.0f, -1.0f);
glTranslatef(-cam.posX, -cam.posY, -cam.posZ + 20);
_font.drawString(_posX, _posY, _text, _color);
glPopMatrix();
}
but the text was not displayex. If used without glRotatefs and glTranslatef, then the text will be rendered by 3D-world coords
The rendering of fonts with Slick is based on OpenGL's immediate mode. This means that the behavior of drawString is determined by the current state, or in other words, the current GL_PROJECTION and GL_MODELVIEW matrices.
Your question does not really make it clear whether you want to draw text as a 2D overlay (probably in screen coordinates) or truly perspective embedded in 3d space. Nevertheless, you can achieve both with drawString. drawString renders the font texture at the specified x/y coordinates in the z=0 plane. Iirc the convention in drawString is to assume a left-handed coordinate system and the texture is visible from the negative z-side. The reason for this convention is probably that OpenGL uses a left-handed coordinate system for window space, while using a right-handed for world/object space (see this answer for a good explanation). As a result, rendering in 2D is straightforward with Slick: Set up a typical orthogonal projection matrix (as suggested by ryuyah2000) and you are good to go.
If you want to render in 3D space, you instead keep your regular perspective projection matrix (i.e., you are using the same projection you use for rendering your world). In order to control the position of the text in 3D space you have to set up the modelview matrix accordingly (i.e., aligning the z=0 plane in the plane where you want to render your text). Due to the left-hand and z-visibility conventions you may have to rotate your z-axis by 180° and have to invert the handedness (just scale your modelview by -1). In case you get one of these steps wrong, your text is either not visible (= looking to wrong z-side) or is written right-to-left (= wrong handedness). Slick's drawString method uses a scaling of 1 unit = 1 pixel of the font texture, so you have to apply an appropriate scaling to match that to your world units.
use this
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 800, 600, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// render font
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

Rendering image to texture and preserving blending

I'm using OpenGL in Java and I've been trying to draw something on the back buffer, copy it onto a texture, and then draw the texture on to the screen.
This is the code I use to do the copying.
int textureNumber = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureNumber);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_REPLACE);
GL11.glCopyTexImage2D
(
GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA,
0, 0,
NewMath.roundUpTwo(width), NewMath.roundUpTwo(height),
0
);
(The roundUpTwo method just rounds the width and height up to the nearest power of 2.)
Now the problem here is that when I re-draw the texture to the screen, it comes out differently than the way it rendered to the backbuffer.
I don't have enough rep to post images directly <_< so here's a screenshot of the behavior.
Note that the top-left is what happens when I render directly to the screen, the lower-right is what happens when I copy it to a texture and then render the texture.
What I think is happening is that the object I am drawing is made up of a lot of triangles drawn with alpha transparency and with a blending function, and that the CopyTexImage2D function isn't preserving that. I'm not totally sure though because really I have no idea what is happening here.
Is that's what the problem is, how do I fix it? And if it's not, what is and how do I fix it?
am drawing is made up of a lot of triangles drawn with alpha transparency… and … CopyTexImage2D function isn't preserving that
That's not how it works. Once a picture is drawn, no matter to which framebuffer, all that remains are pixels' values. The geometry used doesn't matter from there on.
The most likely problem is, that you're rendering that texture with the state of the previous drawing steps still enabled and those are "bleeding" through. Hard to tell without seeing the source code you use for drawing of all the different stages, though. I suggest you edit your question, adding that code.

can't render shape in openGL

I was trying to follow example codes to simply display a rectangle on a black background, but it didn't seem to be displaying. What I did was
private static void initGL(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Display.getWidth(),0,Display.getHeight(),-1,1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST); //2D mode
glColor3f(0.5f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(-0.75, 0.75);
glVertex2f(-0.75, -0.75);
glVertex2f(0.75, -0.75);
glVertex2f(0.75, 0.75);
glEnd();
}
It doesn't display anything on the screen except for a black background. Does anyone know what I might have done wrong? I'm using lwjgl in eclipse.
First things first: You only have to run the whole
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Display.getWidth(),0,Display.getHeight(),-1,1);
glMatrixMode(GL_MODELVIEW);
thing once during your program, probably shortly after you run Display.create().
Also, you're tessellating using the wrong vertices. You wrote
glVertex2f(-0.75, 0.75);
glVertex2f(-0.75, -0.75);
glVertex2f(0.75, -0.75);
glVertex2f(0.75, 0.75);
which means draw a rectangle from (-0.75, -0.75) pixels to (0.75, 0.75) pixels. This is too small to be noticed. My guess is you assumed glVertex2f deals with fractions of the display width. It does not. glVertex2f deals with actual coordinates, it just allows fractional pixels, unlike glVertex2i (this is useful believe it or not, it helps with smoother animations). Something like
glVertex2f(100F, 100F);
places a vertex at (100, 100), and is effectively equivalent to
glVertex2i(100, 100);
Also, remember that negative pixels will be rendered off the screen, because OpenGL's origin of the coordinate system, (0, 0), is in the lower left and behaves like the first quadrant from the coordinate system in math class, not like the traditional computer coordinate system with (0, 0) in the upper left.
As for the the black background, LWJGL's Display has a black background by default, so it's recommended to draw a quad with your background color that covers the entire display width and height. One quad won't really affect your performance.
glVertex2f uses same size units as your glOrtho so unless your display width and height are in units of ones, like 10 or less, you may not see anything!

Categories

Resources