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

[SDL2 OpenGles GLSL] - rysowanie przezroczystych prostokatow

Ostatnio zmodyfikowano 2024-02-08 23:11
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
[SDL2 OpenGles GLSL] - rysowanie przezroczystych prostokatow
» 2023-02-25 13:13:48
Czesc.
Potrzebuje napisac program rysujacy dwa polprzezroczyste przecinajace sie prostokaty. Jak na moje oko, kod jst poprawny, nie jestem w stanie zlokalizowac bledu.

 OBECNY REZULTAT PROGRAMU

 SHADERS
C/C++
const char * vColorSource =
"attribute vec4 position;\n"
"attribute vec4 color;\n"
"varying vec4 vcolor;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.xyz, 1.0);\n"
"vcolor = color;\n"
"}\n"
;

const char * fColorSource =
"precision mediump float;\n"
"varying vec4 vcolor;\n"
"void main()\n"
"{\n"
"gl_FragColor = vcolor;\n"
"}\n"
;

GLuint vColorShader;
GLuint fColorShader;
GLuint colorProgram;

void initShader()
{
   
vColorShader = glCreateShader( GL_VERTEX_SHADER );
   
fColorShader = glCreateShader( GL_FRAGMENT_SHADER );
   
   
glShaderSource( vColorShader, 1, & vColorSource, NULL );
   
glShaderSource( fColorShader, 1, & fColorSource, NULL );
   
   
glCompileShader( vColorShader );
   
glCompileShader( fColorShader );
   
   
colorProgram = glCreateProgram();
   
glAttachShader( colorProgram, vColorShader );
   
glAttachShader( colorProgram, fColorShader );
   
   
glLinkProgram( colorProgram );
   
}

void drawRectColor( float screenWidth, float screenHeight, float posX, float posY, float width, float height, float red, float green, float blue, float alpha )
{
   
// rgba must have a [0 - 256]
    // vertexes of Rect
    // v1 v2
    // v4 v3
   
float v1x, v1y, v2x, v2y, v3x, v3y, v4x, v4y;
   
v1x = posX - width / 2.f; v1y = posY + height / 2.f;
   
v2x = posX + width / 2.f; v2y = posY + height / 2.f;
   
v3x = posX + width / 2.f; v3y = posY - height / 2.f;
   
v4x = posX - width / 2.f; v4y = posY - height / 2.f;
   
   
static GLfloat colors[ 12 ]; // [r,g,b,a]
   
static GLfloat vertexes[ 6 ]; // x=[-1,1], y=[-1,1]
   
GLint position, color;
   
// GLSL uses rgba = [0.0 - 1.0]
   
for( int i = 0; i < 4; i++ )
   
{
       
colors[ i * 4 + 0 ] = red / 256.f;
       
colors[ i * 4 + 1 ] = green / 256.f;
       
colors[ i * 4 + 2 ] = blue / 256.f;
       
colors[ i * 4 + 3 ] = alpha / 256.f;
   
};
   
   
// draw first triangle
   
vertexes[ 0 ] = v1x * 2 / screenWidth; vertexes[ 1 ] = v1y * 2 / screenHeight;
   
vertexes[ 2 ] = v2x * 2 / screenWidth; vertexes[ 3 ] = v2y * 2 / screenHeight;
   
vertexes[ 4 ] = v3x * 2 / screenWidth; vertexes[ 5 ] = v3y * 2 / screenHeight;
   
   
glUseProgram( colorProgram );
   
position = glGetAttribLocation( colorProgram, "position" );
   
glEnableVertexAttribArray( position );
   
glVertexAttribPointer( position, 2, GL_FLOAT, GL_FALSE, 0, vertexes );
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
   
color = glGetAttribLocation( colorProgram, "color" );
   
glEnableVertexAttribArray( color );
   
glVertexAttribPointer( color, 4, GL_FLOAT, GL_FALSE, 0, colors );
   
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
   
// draw second triangle
   
vertexes[ 0 ] = v1x * 2 / screenWidth; vertexes[ 1 ] = v1y * 2 / screenHeight;
   
vertexes[ 2 ] = v3x * 2 / screenWidth; vertexes[ 3 ] = v3y * 2 / screenHeight;
   
vertexes[ 4 ] = v4x * 2 / screenWidth; vertexes[ 5 ] = v4y * 2 / screenHeight;
   
   
position = glGetAttribLocation( colorProgram, "position" );
   
glEnableVertexAttribArray( position );
   
glVertexAttribPointer( position, 2, GL_FLOAT, GL_FALSE, 0, vertexes );
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
   
color = glGetAttribLocation( colorProgram, "color" );
   
glEnableVertexAttribArray( color );
   
glVertexAttribPointer( color, 4, GL_FLOAT, GL_FALSE, 0, colors );
   
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
}

 MAIN
C/C++
#include <iostream>
using namespace std;
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengles2.h>

#include "shaders.cpp"
SDL_Window * window;
bool programRun;
float screenWidth, screenHeight;

void initSDL()
{
   
//program graphics init
   
SDL_Init( SDL_INIT_EVERYTHING );
   
// We use OpenGL ES 2.0
   
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
   
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 0 );
   
   
// We want at least 8 bits per color, alpha and depth
   
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 8 );
   
   
// get current display settings
   
SDL_DisplayMode DM;
   
SDL_GetCurrentDisplayMode( 0, & DM );
   
   
window = SDL_CreateWindow(
   
"Terrain War",
   
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
   
int( DM.w ), int( DM.h ),
   
SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN );
   
   
// and now can use OpenGles 2.0 !! :D
   
SDL_GL_CreateContext( window );
   
}

void initProgram()
{
   
SDL_DisplayMode DM;
   
SDL_GetCurrentDisplayMode( 0, & DM );
   
   
screenWidth = int( DM.w );
   
screenHeight = int( DM.h );
   
   
programRun = true;
}

void drawBackground()
{
   
glClearColor( 0.125f, 0.125f, 0.125f, 1.0f );
   
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}

int main()
{
   
initSDL();
   
initProgram();
   
initShader();
   
   
while( programRun )
   
{
       
drawBackground();
       
// draw first rectangle
       
drawRectColor(
       
screenWidth, screenHeight,
       
0, 0, 256, 256, // x, y, width, height
       
128, 48, 48, 128 // rgba
       
);
       
       
drawRectColor(
       
screenWidth, screenHeight,
       
128, 128, 256, 256,
       
48, 128, 48, 128
       
);
       
SDL_GL_SwapWindow( window );
   
};
   
   
return 0;
}
P-179994
DejaVu
» 2023-02-25 14:00:27
1. Sprawdź czy masz włączony Z-index
2. Ustaw alphę na 0.5
3. Skonfiguruj "BLEND"
C/C++
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_BLEND );

https://learnopengl.com/Advanced-OpenGL/Blending
https://stackoverflow.com/questions/1617370/how-to-use-alpha-transparency-in-opengl
P-179995
tBane
Temat założony przez niniejszego użytkownika
» 2023-02-25 14:22:27
ok, dziala. Dodalem to w funkcji initSDL(). teraz jej kod dziala, przezroczystosc rowniez jest uwzgledniana.

 kod :
C/C++
void initSDL()
{
   
//program graphics init
   
SDL_Init( SDL_INIT_EVERYTHING );
   
// We use OpenGL ES 2.0
   
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
   
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 0 );
   
   
// We want at least 8 bits per color, alpha and depth
   
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 8 );
   
   
// get current display settings
   
SDL_DisplayMode DM;
   
SDL_GetCurrentDisplayMode( 0, & DM );
   
   
window = SDL_CreateWindow(
   
"Terrain War",
   
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
   
int( DM.w ), int( DM.h ),
   
SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN );
   
   
// and now can use OpenGles 2.0 !! :D
   
SDL_GL_CreateContext( window );
   
// for the transparency
   
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
   
glEnable( GL_BLEND );
}

 rezultat programu :
P-179996
« 1 »
  Strona 1 z 1