简体   繁体   English

LWJGL / OpenGL-Alpha无法使用GL_QUAD_STRIP或GL_LINE_STRIP

[英]LWJGL/OpenGL - Alpha not working using GL_QUAD_STRIP or GL_LINE_STRIP

I want to use Alpha/Blend mode for future stuff (transactions mainly and possible image blending) . 我想将Alpha / Blend模式用于将来的工作(主要是事务和可能的图像融合)

Well, I can't get it to work using LWJGL (GL1.1), I already tried other blend modes but didn't worked, nor changing the background or anything like that... 好吧,我不能使用LWJGL(GL1.1)来工作,我已经尝试了其他混合模式,但是没有用,也没有更改背景或类似的东西...

Screenshots: 屏幕截图:

  1. http://i.imgur.com/cHU4YGS.png - GL_BLEND always enabled, everything is transparent http://i.imgur.com/cHU4YGS.png-始终启用GL_BLEND,所有内容都是透明的
  2. http://i.imgur.com/sPmPqne.png - GL_BLEND enabled on QUAD and text, I can see the line that is on disabled GL_BLEND http://i.imgur.com/sPmPqne.png-在QUAD和文本上启用了GL_BLEND,我可以看到在禁用的GL_BLEND上的行
  3. i imgur com/nkda41v png - GL_BLEND disabled on everything but the text -> I need some reputation to post more than 2 links, sorry about that but I belive this image is important so i'll post it anyway. 我imgur com / nkda41v png-GL_BLEND禁用除文本外的所有内容->我需要一些声誉才能发布2个以上的链接,对此感到抱歉,但是我相信此图片很重要,因此无论如何我都将其发布。 Just fill with dots 只需填充点

The results are the same with or without alpha argument on all these tests 在所有这些测试中,无论有无alpha参数,结果都是相同的

Code: 码:

`    private void init() {
        try {
            Display.setDisplayMode(new DisplayMode(DEFAULT_WIDTH, DEFAULT_HEIGHT));
            Display.setResizable(true);
            Display.setVSyncEnabled(true);
            Display.setTitle(DEFAULT_TITLE + " v" + VERSION);
            Display.create();
            updateMatrix();
        } catch(LWJGLException e) {
            e.printStackTrace();
        }
        Keyboard.enableRepeatEvents(true);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Font consolas = new Font("consolas", Font.PLAIN, 13); font = new TrueTypeFont(consolas, antiAliasedFont); } private void updateMatrix() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, 1, -1); //glScaled((double) DEFAULT_WIDTH / (double) Display.getWidth(), (double) DEFAULT_HEIGHT / (double) Display.getHeight(), 0); glViewport(0, 0, Display.getWidth(), Display.getHeight()); glMatrixMode(GL_MODELVIEW); } @Override public void run() { init(); Main main = Main.getMain(); while(!Display.isCloseRequested()) { currentGraphicsTick++; { glClear(GL_COLOR_BUFFER_BIT); glClearColor(0f, 0f, 0f, 1f); if(Display.wasResized()) updateMatrix(); if(vsyncMode == 1) Display.setVSyncEnabled(true); else if(vsyncMode == 2) Display.setVSyncEnabled(false); if(Display.isActive()) { glPushMatrix(); try { // Draw float alpha = (float) Math.cos(Math.toRadians(currentGraphicsTick % 90)); System.out.println("Alpha: " + alpha); glBegin(GL_LINE_STRIP); { float sin = (float) Math.abs(Math.sin(Math.toRadians(currentGraphicsTick % 360))); new Color(0.7f, 0.7f, 0.7f, alpha).bind(); glVertex2f(DEFAULT_WIDTH * 0.03f, DEFAULT_HEIGHT * 0.05f); glVertex2f(DEFAULT_WIDTH * 0.93f * sin, DEFAULT_HEIGHT * 0.95f * sin); } glEnd(); glBegin(GL_QUAD_STRIP); { new Color(0.5f, 0.5f, 0.5f, alpha).bind(); glVertex2i(0, 0); glVertex2i(0, DEFAULT_HEIGHT); glVertex2i(DEFAULT_WIDTH, 0); glVertex2i(DEFAULT_WIDTH, DEFAULT_HEIGHT); } glEnd(); String[] split = main.getGameLoopThread().getDebugString().split("\n"); for(int i = 0; i < split.length; i++) { font.drawString(1, 1 + (i * font.getLineHeight()), split[i], Color.white); } } catch(Throwable throwable) { throwable.printStackTrace(); } glPopMatrix(); } Display.update(); Display.sync(TARGET_FPS); } } Display.destroy(); closeRequested = true; }

I already tried: 我已经尝试过:

  1. Removing the 'alpha' argument from the Slick's Color constructor 从Slick的Color构造函数中删除'alpha'参数
  2. Using OpenGL's glColor with and without alpha argument 使用带有和不带有alpha参数的OpenGL的glColor
  3. Disabling/Enabling GL_BLEND in part of the code (I know some things wouldn't work, but you never know, right?) 在部分代码中禁用/启用GL_BLEND (我知道有些事情行不通,但您永远不知道,对吧?)
  4. Used constants to the alpha variable (such as 0.3f, 0.5f, 0.7f, 1f) instead of making it variable through Math.sin / cos using the tick as the degree 常量用于alpha变量(例如0.3f,0.5f,0.7f,1f),而不是通过使用tick作为度数通过Math.sin / cos使其变量
  5. Using glRect(...) 使用glRect(...)
  6. Changing the background 改变背景
  7. Removing the glClearColor 删除glClearColor
  8. Removing glClear (nice effect, never did this lol) 删除glClear (效果不错,从来没有这样做过)

What I expect to see was a fading moving LINE_STRIP: 我期望看到的是LINE_STRIP的淡入淡出:

  • On one side it moves from the (0, 0) to (width - 7%, height - 5%) 在一侧,它从(0,0)移至(宽度-7%,高度-5%)

  • On the other it stand still on (width + 3%, height + 5%) 另一方面,它静止不动(宽度+ 3%,高度+ 5%)

  • the rectangle would make it fade (the original idea would use the same color as the background, but it didn't on my tests because I want to see the rectangle) 矩形将使其褪色(最初的想法将使用与背景相同的颜色,但由于我想查看矩形而未在我的测试中使用)

I've had a similar(in terms of what I had to tackle) when doing a 2D game using my own engine. 使用自己的引擎进行2D游戏时,我遇到了类似的问题(就我必须解决的问题而言)。 LWJGL's blend functions always have a few issues and there aren't really that many concrete answers for them as it really boils down to your code and how you placed things. LWJGL的blend函数总是有一些问题,实际上并没有很多具体的答案,因为它实际上可以归结为您的代码以及如何放置事物。

I presume you're using SlickUtil, and to that I would say write your own (or of course search around) your own methods for this. 我假设您正在使用SlickUtil,为此,我想说说您自己的方法(或者当然是在周围搜索) Util's methods were always somewhat wonky with blending. Util的方法在混合时总是有些古怪。

  1. Removing alpha shouldn't have changed much in terms of getting what you want 在获取所需内容方面,删除alpha不会有太大变化
  2. The only time I explicitly enable GL_BLEND is when rendering lights(always presuming you're doing 3D) and when I would render textures 我唯一明确启用GL_BLEND的时间是渲染灯光(始终假设您正在执行3D)和渲染纹理时
  3. I suggest not removing glClearColor, that doesn't directly affect your situation as long as you put it in the correct place 我建议不要删除glClearColor,只要您将其放置在正确的位置,它不会直接影响您的情况

Like I said this bug/problem could be due to quite a few things that I can't quite pin-point from what you've posted. 就像我说的那样,此错误/问题可能是由于很多原因,我无法从您已发布的内容中准确指出。

My suggestions are: 我的建议是:

  1. Organise the whole project well to be able to point bugs out quickly 很好地组织整个项目,以便能够迅速指出错误
  2. Make alot of stuff generic ASAP(Doesn't matter if you just want to quickly write something) 尽快使很多东西通用(不要紧,只是想快速写点东西)
  3. Avoid SlickUtil, it's great and I LOVE it but it does get it's issues 避免使用SlickUtil,它很棒,我喜欢它,但确实可以解决问题

Sorry I can't help too much but tracking down your issue from the jumbled up code you posted is a bit difficult. 抱歉,我帮不上什么忙,但是要从发布的混乱代码中查找问题变得有些困难。 I'll keep my eye on this question in case I may be of help in the future. 我会一直关注这个问题,以防将来对您有所帮助。

Besides the tips from @Juxhin, what fixed my problem with alpha blend was TextureImpl.bindNone(); 除了@Juxhin的技巧外,解决我的Alpha混合问题的是TextureImpl.bindNone() ;。 from SlickUtils (check openGL's similar below) 来自SlickUtils(请检查以下类似的openGL)

This method is similar to glDisable(GL_TEXTURE_2D) before rendering the thing that needs to be blent (I know that searching and checking Slick's source) 该方法类似于glDisable(GL_TEXTURE_2D),然后渲染需要弯曲的东西(我知道搜索并检查Slick的源代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM