以前对 GL.Color3 的调用使我的纹理使用了错误的颜色 [英] Previous calls to GL.Color3 are making my texture use the wrong colors

查看:71
本文介绍了以前对 GL.Color3 的调用使我的纹理使用了错误的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作 2D OpenGL 游戏.渲染帧时,我需要先绘制一些计算出的四边形几何图形,然后再绘制一些带纹理的精灵.当我的渲染方法的主体只绘制精灵时,一切正常.但是,当我尝试在精灵之前绘制几何四边形时,精灵的纹理会更改为之前使用的最后一个 GL.Color3 的颜色.我如何告诉 OpenGL(好吧,OpenTK)好吧,我们已经完成了几何图形的绘制,是时候开始使用精灵了?"

Making a 2D OpenGL game. When rendering a frame I need to first draw some computed quads geometry and then draw some textured sprites. When the body of my render method only draws the sprites, everything works fine. However, when I try to draw my geometric quads prior to the sprites the texture of the sprite changes to be the color of the last GL.Color3 used previously. How do I tell OpenGL (well, OpenTK) "Ok, we are done drawing geometry and its time to move on to sprites?"

渲染代码如下所示:

        // Let's do some geometry 
        GL.Begin(BeginMode.Quads);
        GL.Color3(_dashboardBGColor);  // commenting this out makes my sprites look right

        int shakeBuffer = 100;
        GL.Vertex2(0 - shakeBuffer, _view.DashboardHeightPixels);
        GL.Vertex2(_view.WidthPixelsCount + shakeBuffer, _view.DashboardHeightPixels);
        GL.Vertex2(_view.WidthPixelsCount + shakeBuffer, 0 - shakeBuffer);
        GL.Vertex2(0 - shakeBuffer, 0 - shakeBuffer);

        GL.End();

        // lets do some sprites
        GL.Begin(BeginMode.Quads);
        GL.BindTexture(TextureTarget.Texture2D, _rockTextureId);

        float baseX = 200;
        float baseY = 200;

        GL.TexCoord2(0, 0); GL.Vertex2(baseX, baseY);
        GL.TexCoord2(1, 0); GL.Vertex2(baseX + _rockTextureWidth, baseY);
        GL.TexCoord2(1, 1); GL.Vertex2(baseX + _rockTextureWidth, baseY - _rockTextureHeight);
        GL.TexCoord2(0, 1); GL.Vertex2(baseX, baseY - _rockTextureHeight);  

        GL.End();

        GL.Flush();
        SwapBuffers();

推荐答案

默认的纹理环境模式是 GL_MODULATE,这样做,它将纹理颜色与顶点颜色相乘.

The default texture environment mode is GL_MODULATE, which does that, it multiplies the texture color with the vertex color.

一个简单的解决方案是在绘制纹理图元之前将顶点颜色设置为 1,1,1,1:

A easy solution is to set the vertex color before drawing a textured primitive to 1,1,1,1 with:

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

另一种解决方法是将纹理环境模式改为GL_REPLACE,这样纹理颜色替换顶点颜色就没有问题了:

Another solution is to change the texture environment mode to GL_REPLACE, which makes the texture color replace the vertex color and doesn't have the issue:

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

这篇关于以前对 GL.Color3 的调用使我的纹理使用了错误的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆