I have a drawn a filled circle using ShapeRenderer and now I want to draw this circle as a transparent one. I am using the following code to do that: But the circle is not coming as transparent. Also, I checked th libgdx API and from the wiki, it says that, need to Create CameraStrategy. Has somebody faced similar issue ever before? If so, please give me some clues. Thanks in advance.
Gdx.gl.glEnable(GL10.GL_BLEND);
Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
drawFilledCircle();
Gdx.gl.glDisable(GL10.GL_BLEND);
private void drawFilledCircle(){
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.FilledCircle);
shapeRenderer.setColor(new Color(0, 1, 0, 1));
shapeRenderer.filledCircle(470, 45, 10);
shapeRenderer.end();
}
The following code is working for me in this case, maybe it will help someone else:
Gdx.gl.glEnable(GL10.GL_BLEND);
Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.FilledCircle);
shapeRenderer.setColor(new Color(0, 1, 0, 0.5f));
shapeRenderer.filledCircle(470, 45, 10);
shapeRenderer.end();
Gdx.gl.glDisable(GL10.GL_BLEND);
First we need to enable blending:
Gdx.gl.glEnable(GL10.GL_BLEND);
And make sure that you don't call SpriteBatch.begin() and SpriteBatch.end() between that line of code and your Shaperender.drawSomething() line of code. I don't know why but that's what works in my case
Only this worked for me :
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
//Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); // <<< this line here makes the magic we're after
game.shapeRenderer.setProjectionMatrix(camera.combined);
game.shapeRenderer.begin(ShapeType.Filled);
go.drawShapes();
game.shapeRenderer.end();
//Gdx.gl.glDisable(GL20.GL_BLEND);
Well, there is not really a point in drawing something fully transparent. If you did want to make a half transparent circle, you would have to clear the color buffer by glClearColor before each frame and set Color alpha component to 0.5f.
If you wouldn't clear the buffer, after few render draws, the circle would blend into one with almost solid color.
private void drawFilledCircle(Camera camera){
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.FilledCircle);
shapeRenderer.setColor(new Color(0, 1, 0, 0.5f)); // last argument is alpha channel
shapeRenderer.filledCircle(470, 45, 10);
shapeRenderer.end();
}
Related
Our current assignment requires us to use older fixed-pipeline methods in openGL. We're using LWJGL 2.9.3. The following code displays a triangle. The problem is, it flickers like crazy. The Display.swapBuffers() method doesn't throw an exception, and it doesn't make any difference if I include it or not. I created this example based off this StackOverflow question:
gluPerspective, glViewport, gluLookAt and the GL_PROJECTION and GL_MODELVIEW Matricies
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 10, 0, 10, -1, 100);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
gluLookAt(0, 0, 0, 0, 0, 1, 0, 1, 0);
glBegin(GL_POLYGON);
glVertex3d(1, 1, 1);
glVertex3d(1, 5, 1);
glVertex3d(5, 5, 1);
glEnd();
glFlush();
try {
Display.swapBuffers();
} catch (Exception e) {
e.printStackTrace();
}
EDIT
One other thing. If I call glLoadIdentity() after glMatrixMode(GL_MODELVIEW), it's as if the gluLookAt() method is ignored. I just see a blank black screen if I do that. But if I don't, it keeps multiplying with itself (if I change the eye position from 0,0,0).
The reason for the flickering was that the gluLookAt() function was swapping back and forth between looking different directions.
I'm making a game in which there will be red and blue Shapes moving around on screen. I've looked high and low for how to make anywhere they overlap a different color (purple). I am only using Java2D, which to my understanding does not support Shaders. I looked into drawing the red shapes to one BufferedImage and the blue shapes to another, then trying to use AlphaComposite to combine the colors and draw it to the screen, but it never produced correct results. I'm using 127,0,0 and 0,0,127 for red and blue instead of 255 because 255,0,255 looks, in my opinion, terrible for purple. I would effectively like this.
Thanks to copeg's suggestion, I was able to figure it out. Here is the code segment (Context: the shapes I'm drawing are attacks):
//Attacks
BufferedImage attackImg = new BufferedImage(S_WIDTH, S_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D ag = (Graphics2D) attackImg.getGraphics();
//Make all of attackImg a transparent image
ag.setComposite(AlphaComposite.Clear);
ag.fillRect(0, 0, S_WIDTH, S_HEIGHT);
ag.setComposite(AlphaComposite.SrcOver);
//Render red attacks to attackImg
ag.setColor(new Color(127, 0, 0, 255));
for(Shape s : redAttacks)
ag.fill(s);
//Render overlap areas using composites to attackImg
ag.setColor(new Color(127, 0, 127, 255));
ag.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
g.setColor(new Color(0, 0, 127, 255));
for(Shape s : blueAttacks)
{
ag.fill(s);
g.fill(s); //Render blue attacks
}
//Render red and purple attacks
g.drawImage(attackImg, 0, 0, null);
Basicly I want to open a Door and I thought of using glRotatef.
My Problem is that it is affecting every object which is drawn after it.
Does anyone know how to stop that ?
Door.class
public static void draw(Texture door) {
door.bind();
if(Door_Test.state == "out" && d != 90){
glRotatef(i, 0, 1, 0);
i+=5;
}
glBegin(GL_QUADS);
glColor3f(1f, 1f, 1f);glTexCoord2f(0,0);glVertex3f(-2,3, -15);
glColor3f(1f, 1f, 1f);glTexCoord2f(0,1);glVertex3f(-2,-3, -15);
glColor3f(1f, 1f, 1f);glTexCoord2f(1,1);glVertex3f(2,-3, -15);
glColor3f(1f, 1f, 1f);glTexCoord2f(1,0);glVertex3f(2,3, -15);
glEnd();
}
Common way to do it:
glPushMatrix();
glRotatef(/*...*/);
// Drawing commands here
glPopMatrix();
When you rotate, it rotates the entire scene. So to rotate a single object, you rotate the entire scene, draw your object, then rotate your screen back.
glRotatef(i, 0, 1, 0);
// Draw object.
glRotatef(-i, 0, 1, 0);
As Reto Koradi pointed out, if you continually do this you might have floating point rounding errors that accumulate over time. HolyBlackCat's answer offers a better solution.
I can't figure out why this:
glPushMatrix();
GL11.glBindTexture(this.target, this.textureID);
glColor3f(1, 1, 1);
glTranslated(posX, posY, 0);
glBegin(GL_QUADS);
{
glTexCoord2d(posXLeft, posYTop);
glVertex2d(0, 0);
glTexCoord2d(posXLeft, posYBottom);
glVertex2d(0, verH);
glTexCoord2d(posXRight, posYBottom);
glVertex2d(verW, verH);
glTexCoord2d(posXRight, posYTop);
glVertex2d(verW, 0);
}
glEnd();
glPopMatrix();
is working perfectly, where posX and posY are obviously the position in pixels, posXLeft etc is the ratio of the texture to show.
But this:
glPushMatrix();
GL11.glBindTexture(this.target, this.textureID);
glColor3f(1, 1, 1);
glTranslated(posX, posY, 0);
glBegin(GL_LINES);
{
glVertex2d(10, 10);
glVertex2d(800, 600);
}
glEnd();
glPopMatrix();
isn't. And it should be even easier to draw lines instead of a piece of a texture.
What I want to reach is to add some zig-zag lines on a texture to simulate cracks as it is damaged or broken, but I can't even draw a single line, so I am stuck here.
Any advice?
You still got texturing enabled in your line drawing code. But you don't specify texture coordinates, so you'll draw your line with a solid color as defined by texture at the currently set texture coordinate.
My suggestion: Disable texturing for drawing that line.
As said by datenwolf you have to disable the texturing, thes thing is that than you have to re-enable it, although you will have problems the next cycle of drawing if that property is not set correctly.
the solution is:
glPushMatrix();
GL11.glBindTexture(this.target, this.textureID);
glColor3f(1, 1, 1);
glDisable(GL_TEXTURE_2D);
glTranslated(posX, posY, 0);
glBegin(GL_LINES);
{
glVertex2d(10, 10);
glVertex2d(800, 600);
}
glEnd();
glEnable(GL_TEXTURE_2D);
glPopMatrix();
and that should solve your problem.
I'm drawing a couple of shapes on a JPanel using the paintComponent() method. The final touch is to add a transparent white gradient towards the top.
I have this:
and I want to get something like this:
I've tried to use the GradientPaint method, but it doesn't seem to work properly for me at all. When I call g.setPaint(new GradientPaint(...)), it can't seem to draw over the existing pixels at all.
If anyone would like to see what I'm doing, an SSCCE of the code is available at this Pastebin.
It seems to produce an effect if these are added as the last lines of paintComponent(Graphics).
// now we have set a paint, DO SOMETHING WITH IT!
g.fillRect(0, 0, getWidth(), getHeight());
Result
Try applying a AlphaComposite before painting the gradient
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
The other thing you could try is to use a color with an alpha value within the gradient...
LinearGradientPaint lgp = new LinearGradientPaint(
startPoint, endPoint, new float[]{...},
new Color[] {
new Color(255, 255, 255, 0),
new Color(255, 255, 255, 128),
new Color(255, 255, 255, 0),
});