K
K
krembrule20162019-09-19 17:12:43
Java
krembrule2016, 2019-09-19 17:12:43

Why doesn't Shaperenderer want to change the transparency?

Hey!
Broke my whole brain. The essence of the problem. There is a Shaperenderer and it draws a regular rectline().
I wrote a function that uses delta-time to change its transparency. It all looks like this, the code is not complete, but the essence is conveyed here.

public void disapearingTentacle(float delta){
        Iterator<Tentacle> iter = controlElement.getDisapearingTentacle().iterator();
        boolean find = true;
        while (iter.hasNext()){
           //тут магия, шобы не загрязнять вам мозги лишней инфой
            }else find = false;
            if(find == false){
                System.out.println(str.trans); // тут я в консоле воочию наблюдая, как переменная trans действительно как того требует логика моего воспаленного мозга изменяет свое значение, и она float
                if(str.trans > 0) rend.setColor(0,0,0,str.trans);
                str.trans -= 0.5f*delta; // тут мы ее уменьшаем со временем
                if(str.trans < 0) // если ушли за ноль, то эта str самое слабое звено - прощайте..!!
                iter.remove();
                else str.renderLine(rend); // иначе мы должны отрисовать прямоугольник с прозрачными линиями
            }
        }
    }

renderLine contains a trite, this is to make sure that I'm not a moose
public void renderLine(ShapeRenderer rend){
        this.rend = rend;
        rend.rectLine(startCoordinateX, startCoordinateY, endCoordinateX, endCoordinateY, width);
    }

One dude advised me to cast these spells
Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

I said this too, but SOTONA never appeared, even black magic did not make my straight line translucent.
By the way, yes, I draw a straight line using a rectangle, because libgdx, java or god knows what other higher powers did not provide for drawing a straight line of the required thickness in a normal way. (>_<)
What should I do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
exenza, 2019-09-20
@krembrule2016

What is actually being rendered? Rectangle drawn?
It's hard to say right away - after all, it's not clear where this `disappearingTentacle` is called from and a lot of the rest is not shown. Try to simplify and debug.
As a starting point, here is a simple working example:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class RectrenderTest extends ApplicationAdapter {
    private ShapeRenderer shapeRenderer;
    private float alpha = 1f;
    private float dAlpha = -0.5f;
    private boolean fadingOut = true;

    @Override
    public void create() {
        shapeRenderer = new ShapeRenderer();
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Включается блендинг:
        Gdx.gl.glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        // Задается форма:
        shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
        update(Gdx.graphics.getDeltaTime());
        // Задается цвет и прозрачность alpha:
        shapeRenderer.setColor(new Color(1, 1, 1, alpha));
        // Рисуется линия:
        shapeRenderer.line(0, 0, 470, 350);
        shapeRenderer.end();
        // Выключается блендинг:
        Gdx.gl.glDisable(GL20.GL_BLEND);
    }

    private void update(float dt) {
        // System.out.println("dt=" + dt + ", alpha=" + alpha + ", dAlpha=" + dAlpha);
        // Если прозрачность за пределами допустимых значений, меняем направление
        if (fadingOut && alpha <= 0 || !fadingOut && alpha >= 1f) {
            dAlpha = -dAlpha;
            fadingOut = !fadingOut;
        }
        alpha += dAlpha * dt;
    }

    @Override
    public void dispose() {
        shapeRenderer.dispose();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question