tBane Temat założony przez niniejszego użytkownika |
C++ 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 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 ) { 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 ]; static GLfloat vertexes[ 6 ]; GLint position, color; 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; }; 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 ); 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 #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() { SDL_Init( SDL_INIT_EVERYTHING ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 0 ); 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 ); 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 ); 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(); drawRectColor( screenWidth, screenHeight, 0, 0, 256, 256, 128, 48, 48, 128 ); drawRectColor( screenWidth, screenHeight, 128, 128, 256, 256, 48, 128, 48, 128 ); SDL_GL_SwapWindow( window ); }; return 0; }
|
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 : void initSDL() { SDL_Init( SDL_INIT_EVERYTHING ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 0 ); 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 ); 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 ); SDL_GL_CreateContext( window ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glEnable( GL_BLEND ); }
rezultat programu : |