Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[SDL2 opengles2] instrukcje warunkowe w shaderach - edycja konkretnego koloru

Ostatnio zmodyfikowano 2023-08-19 18:23
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
[SDL2 opengles2] instrukcje warunkowe w shaderach - edycja konkretnego koloru
» 2023-08-18 20:43:48
 Witam!
Piszę grę na telefon. Wiele rzeczy takich jak miecze, nogawice i pancerze będą podobne wizualnie, ale i będą różnic się kolorami. Piszę więc shader, który po otrzymaniu wartości RGB będzie podmienial dany przeze mnie kolor. Mój shader wygląda następująco. Nie wiem czy to błąd kompilacji, ale nie rysuje nic. Nie mam oprogramowania do pisania shaderów(GLSL), więc "czaruje" z głowy :D

C/C++
const char * vTextureSource =
"attribute vec4 position;\n"
"attribute vec2 texcoord;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
"gl_Position = position;\n"
"Texcoord = texcoord;\n"
"}\n"
;

const char * fTextureWithMaskSource =
"precision mediump float;\n"
"uniform sampler2D texture;\n"
"uniform vec4 uColor;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
"vec4 fragColor = texture2D(texture, Texcoord);\n"
"if( fragColor.x == 39.0/256.0 && fragColor.y == 39.0/256.0 )\n"
"gl_FragColor = vec4(uColor, 1.0);\n"
"else\n"
"gl_FragColor = fragColor;\n"
"}\n"
;

Shadery są utworzone chyba poprawnie (opengles2) oraz poprawnie linkowane. Tak samo tworzenie programu.

A w ten sposób uzyskuje dostęp do zmiennej uColor oraz wyrysowuje teksture:

C/C++
void drawRectImageWithMask( int x, int y, int w, int h, GLuint texture, float r, float g, float b )
{
   
// TO-DO
   
v1x = x - w / 2.f; v1y = y + h / 2.f;
   
v2x = x + w / 2.f; v2y = y + h / 2.f;
   
v3x = x + w / 2.f; v3y = y - h / 2.f;
   
v4x = x - w / 2.f; v4y = y - h / 2.f;
   
   
// here no using color program!!!
   
glUseProgram( textureWithMaskProgram );
   
   
// prepare out texture
   
glActiveTexture( GL_TEXTURE0 );
   
glBindTexture( GL_TEXTURE_2D, texture );
   
   
textureUniform = glGetUniformLocation( textureWithMaskProgram, "texture" );
   
glUniform1i( textureUniform, 0 );
   
   
colorUniform = glGetUniformLocation( textureWithMaskProgram, "uColor" );
   
glUniform4f( colorUniform, r / 256.f, g / 256.f, b / 256.f, 1.0f );
   
   
// draw first triangle
   
vertexes[ 0 ] = v1x * 2.f / scrw; vertexes[ 1 ] = v1y * 2.f / scrh;
   
vertexes[ 2 ] = v2x * 2.f / scrw; vertexes[ 3 ] = v2y * 2.f / scrh;
   
vertexes[ 4 ] = v3x * 2.f / scrw; vertexes[ 5 ] = v3y * 2.f / scrh;
   
   
texCoords[ 0 ] = 0; texCoords[ 1 ] = 0;
   
texCoords[ 2 ] = 1; texCoords[ 3 ] = 0;
   
texCoords[ 4 ] = 1; texCoords[ 5 ] = 1;
   
   
position = glGetAttribLocation( textureWithMaskProgram, "position" );
   
glEnableVertexAttribArray( position );
   
glVertexAttribPointer( position, 2, GL_FLOAT, GL_FALSE, 0, vertexes );
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
   
texcoord = glGetAttribLocation( textureWithMaskProgram, "texcoord" );
   
glEnableVertexAttribArray( texcoord );
   
glVertexAttribPointer( texcoord, 2, GL_FLOAT, GL_FALSE, 0, texCoords );
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
   
// draw second triangle
   
vertexes[ 0 ] = v1x * 2.f / scrw; vertexes[ 1 ] = v1y * 2.f / scrh;
   
vertexes[ 2 ] = v3x * 2.f / scrw; vertexes[ 3 ] = v3y * 2.f / scrh;
   
vertexes[ 4 ] = v4x * 2.f / scrw; vertexes[ 5 ] = v4y * 2.f / scrh;
   
   
texCoords[ 0 ] = 0; texCoords[ 1 ] = 0;
   
texCoords[ 2 ] = 1; texCoords[ 3 ] = 1;
   
texCoords[ 4 ] = 0; texCoords[ 5 ] = 1;
   
   
position = glGetAttribLocation( textureWithMaskProgram, "position" );
   
glEnableVertexAttribArray( position );
   
glVertexAttribPointer( position, 2, GL_FLOAT, GL_FALSE, 0, vertexes );
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
   
texcoord = glGetAttribLocation( textureWithMaskProgram, "texcoord" );
   
glEnableVertexAttribArray( texcoord );
   
glVertexAttribPointer( texcoord, 2, GL_FLOAT, GL_FALSE, 0, texCoords );
   
   
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
}
P-180304
tBane
Temat założony przez niniejszego użytkownika
» 2023-08-19 12:05:44
Tutaj wstępny działający kod, ale..  Tu jest mnozony kolor a nie zastępowanie. Trzeba też wziąć pod uwagę możliwe odchylenia dla glFloat ( w końcu jest zmiennoprzecinkowa i różne może mieć zakresy w c++, opengles2 oraz GLSL)

C/C++
const char * fTextureWithMaskSource =
"precision mediump float;\n"
"uniform sampler2D texture;\n"
"uniform vec4 uColor;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
"vec4 fragColor = texture2D(texture, Texcoord);\n"
"if( fragColor.w > 0.0 )\n"
"gl_FragColor = fragColor * uColor;\n"
"}\n"
;

Kolory do podmiany:
(39, 39, 55)
(67, 67, 79)
P-180306
tBane
Temat założony przez niniejszego użytkownika
» 2023-08-19 12:57:45
Kod działający, acz nie podmienial koloru. Pewno to wina precyzji dla glFloat.

C/C++
const char * fTextureWithMaskSource =
"precision mediump float;\n"
"uniform sampler2D texture;\n"
"uniform vec4 uColor;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
"vec4 fragColor = texture2D(texture, Texcoord);\n"
"if( fragColor.x==39.0/256.0 && fragColor.y==39.0/256.0 && fragColor.z==55.0/256.0 )"
"gl_FragColor = uColor;\n"
"else\n"
"gl_FragColor = fragColor;\n"
"}\n"
;
P-180307
tBane
Temat założony przez niniejszego użytkownika
» 2023-08-19 13:35:23
No i rozwiązanie problemu
C/C++
const char * fTextureWithMaskSource =
"precision mediump float;\n"
"uniform sampler2D texture;\n"
"uniform vec4 uColor;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
"vec4 fragColor = texture2D(texture, Texcoord);\n"
"if( fragColor.x>38.0/256.0 && fragColor.x<40.0/256.0 && \n"
// r == 39
"fragColor.y>38.0/256.0 && fragColor.y<40.0/256.0 && \n" // g == 39
"fragColor.z>54.0/256.0 && fragColor.z<56.0/256.0 )" // b == 55
"gl_FragColor = uColor;\n"
""
"else if( fragColor.x>66.0/256.0 && fragColor.x<68.0/256.0 && \n"
// r == 67
"fragColor.y>66.0/256.0 && fragColor.y<68.0/256.0 && \n" // g == 67
"fragColor.z>78.0/256.0 && fragColor.z<80.0/256.0 )" // b == 79
"gl_FragColor = uColor;\n"
""
"else\n"
"gl_FragColor = fragColor;\n"
"}\n"
;
P-180308
pekfos
» 2023-08-19 15:42:21
https://en.cppreference.com/w/cpp/language/string_literal, forma nr 6.
C/C++
const char * fTextureWithMaskSource = R"(
precision mediump float;
uniform sampler2D texture;
uniform vec4 uColor;
varying vec2 Texcoord;
void main()
{
vec4 fragColor = texture2D(texture, Texcoord);
if( fragColor.x>38.0/256.0 && fragColor.x<40.0/256.0 &&  // r == 39
fragColor.y>38.0/256.0 && fragColor.y<40.0/256.0 &&  // g == 39
fragColor.z>54.0/256.0 && fragColor.z<56.0/256.0 ) // b == 55
gl_FragColor = uColor;

else if( fragColor.x>66.0/256.0 && fragColor.x<68.0/256.0 &&  // r == 67
fragColor.y>66.0/256.0 && fragColor.y<68.0/256.0 &&  // g == 67
fragColor.z>78.0/256.0 && fragColor.z<80.0/256.0 ) // b == 79
gl_FragColor = uColor;

else
gl_FragColor = fragColor;
};
)";
P-180309
tBane
Temat założony przez niniejszego użytkownika
» 2023-08-19 18:23:43
Dzięki za radę! Przyda się przy testowaniu skryptów! :-)
P-180310
« 1 »
  Strona 1 z 1