简体   繁体   中英

opengl glsl bug in which model goes invisible if i use glsl texture function with different parameters

I want to replicate a game. The goal of the game is to create a path between any 2 squares that have the same color.

Here is the game: www.mypuzzle.org/3d-logic-2

The cube has 6 faces. Each faces has 3x3 squares.

The cube has different square types: empty squares(reflect the environment), wall squares(you cant color them), start/finish squares(which have a black square in the middle but the rest of it is the colored).

I've close to finishing my project but i'm stuck with a bug. I used c++,sfml,opengl,glm.

The problem is in the shaders.

Vertex shader:

#version 330 core

layout (location = 0) in vec3 vPosition;
layout (location = 1) in vec3 vColor;
layout (location = 2) in vec2 vTexCoord;
layout (location = 3) in float vType;
layout (location = 4) in vec3 vNormal;

out vec3 Position;
out vec3 Color;
out vec2 TexCoord;
out float Type;
out vec3 Normal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(vPosition, 1.0f);

    Position = vec3(model * vec4(vPosition, 1.0f));
    Color=vColor;
    TexCoord=vTexCoord;
    Type=vType;
    Normal = mat3(transpose(inverse(model))) * vNormal;
}

Fragment shader:

#version 330 core
in vec3 Color;
in vec3 Normal;
in vec3 Position;

in vec2 TexCoord;
in float Type;

out vec4 color;

uniform samplerCube skyboxTexture;
uniform sampler2D faceTexture;
uniform sampler2D centerTexture;

void main()
{
    color=vec4(0.0,0.0,0.0,1.0);
    if(Type==0.0)
    {
        vec3 I = normalize(Position);
        vec3 R = reflect(I, normalize(Normal));
        if(texture(faceTexture, TexCoord)==vec4(1.0,1.0,1.0,1.0))
            color=mix(texture(skyboxTexture, R),vec4(1.0,1.0,1.0,1.0),0.3);*/
    }
    else if(Type==1.0)
    {
        if(texture(centerTexture, TexCoord)==vec4(1.0,1.0,1.0,1.0))
            color=vec4(Color,1.0);
    }
    else if(Type==-1.0)
    {
        color=vec4(0.0,0.0,0.0,1.0);
    }
    else if(Type==2.0)
    {
        if(texture(faceTexture, TexCoord)==vec4(1.0,1.0,1.0,1.0))
            color=mix(vec4(Color,1.0),vec4(1.0,1.0,1.0,1.0),0.5);
    }
}

/*
Type== 0 ---> blank square(reflects light)
Type== 1 --->  start/finish square
Type==-1 ---> wall square
Type== 2 ---> colored square that was once a black square
*/

In the fragment shader i draw the pixels of a square that has a certain type, so the shader only enters in 1 of the 4 if's for each square. The program works fine if i only use the glsl function texture with the same texture. If i use this function 2 times with different textures ,in 2 differents if's ,my model goes invisible. Why is that happening?

https://postimg.org/image/lximpl0bz/

https://postimg.org/image/5dzvqz2r7/

The red square is of type 1. I've modified code in the type==0 if and then my model went invisible.

Texture sampler in OpenGL should only be accessed in (at least) dynamically uniform control flow. This basically means, that all invocations of a shader execute the same code path. If this is not the case, then no automatic gradients are available and mipmapping or anisotropic filtering will fail.

In your program this problem happens exactly when you try to use multiple textures. One solution might be not to use anything that requires gradients. There are also a number of other options, for example, patching all textures together in a texture atlas and just selecting the appropriate uv-coordinates in the shader or drawing each quad separately and providing the type through a uniform variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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