tBane Temat założony przez niniejszego użytkownika |
[SDL2 OpenGles2] - wczytywanie tekstur do shadera oraz ich rysowanie » 2023-02-02 17:20:00 Czesc! Pisze program w jezyku c++ z uzyciem bibioteki SDL oraz opengles2. Problem jest trywialny a mianowicie probuje utworzyc dzialajaca funkcje drawTriangle(vec2f screenSize, vec2f v1, vec2f v2, vec2f v3, GLuint texture)Funkcja czesciowo dziala, tzn nie ma bledow kompilacji, rysuje teksture ale jakos rozciagnieta w osi Y. Wiem, ze utknalem z zmienna texCoord w funkcji texture2D() w shaderze. Jak poprawnie narysoawc teksture ? ( ps. wiecie jak ominac skalowanie testury w shaderze? ) tekstury :obecny wynik programu :#include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include <SDL2/SDL_opengles2.h> #include<iostream> using namespace std;
#include "math.cpp"
SDL_Window * window;
GLuint vTextureShader; GLuint fTextureShader; GLuint textureProgram; GLuint textures[ 2 ]; static GLfloat vertexes[ 6 ]; static GLfloat coords[ 2 ]; 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 * fTextureSource = "precision mediump float;\n" "uniform sampler2D texture;\n" "varying vec2 Texcoord;\n" "void main()\n" "{\n" "gl_FragColor = texture2D(texture, Texcoord);\n" "}\n";
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( "Load PNG x2 and draw textures", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, int( DM.w ), int( DM.h ), SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN ); cout << "created window size is a [" << DM.w << "x" << DM.h << "]\n"; SDL_GL_CreateContext( window ); }
void initShaders() { vTextureShader = glCreateShader( GL_VERTEX_SHADER ); fTextureShader = glCreateShader( GL_FRAGMENT_SHADER ); glShaderSource( vTextureShader, 1, & vTextureSource, NULL ); glShaderSource( fTextureShader, 1, & fTextureSource, NULL ); glCompileShader( vTextureShader ); glCompileShader( fTextureShader ); textureProgram = glCreateProgram(); glAttachShader( textureProgram, vTextureShader ); glAttachShader( textureProgram, fTextureShader ); glLinkProgram( textureProgram ); glUseProgram( textureProgram ); }
void setVertexes( vec2f screenSize, vec2f v1, vec2f v2, vec2f v3 ) { vertexes[ 0 ] = v1.x /( screenSize.x / 2.f ); vertexes[ 1 ] = v1.y /( screenSize.y / 2.f ); vertexes[ 2 ] = v2.x /( screenSize.x / 2.f ); vertexes[ 3 ] = v2.y /( screenSize.y / 2.f ); vertexes[ 4 ] = v3.x /( screenSize.x / 2.f ); vertexes[ 5 ] = v3.y /( screenSize.y / 2.f ); }
void drawTriangle( vec2f screenSize, vec2f v1, vec2f v2, vec2f v3, GLuint texture ) { glUseProgram( textureProgram ); setVertexes( screenSize, v1, v2, v3 ); coords[ 0 ] = min( v1.x, v2.x, v3.x ); coords[ 1 ] = min( v1.y, v2.y, v3.y ); GLint position = glGetAttribLocation( textureProgram, "position" ); glEnableVertexAttribArray( position ); glVertexAttribPointer( position, 2, GL_FLOAT, GL_FALSE, 0, vertexes ); GLint texcoord = glGetAttribLocation( textureProgram, "texcoord" ); glEnableVertexAttribArray( texcoord ); glVertexAttribPointer( texcoord, 2, GL_FLOAT, GL_FALSE, 0, coords ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, texture ); GLint textureUniform = glGetUniformLocation( textureProgram, "texture" ); glUniform1i( textureUniform, 0 ); glDrawArrays( GL_TRIANGLES, 0, 4 ); }
void loadTexture( GLuint & texture, const char * path ) { SDL_Surface * img = IMG_Load( path ); if( img == NULL ) { cout << "error load texture: " << path << "\n"; return; } glGenTextures( 1, & texture ); glBindTexture( GL_TEXTURE_2D, texture ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, img->w, img->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->pixels ); cout << "load texture: " << path << "\n"; }
void loadTextures() { loadTexture( textures[ 0 ], "step.png" ); loadTexture( textures[ 1 ], "lowland.png" ); }
int main() { initSDL(); initShaders(); loadTextures(); vec2f screen, v1, v2, v3, v4; float side = 1024; screen = vec2f( 2640, 1200 ); v1 = vec2f( - side / 2, side / 2 ); v2 = vec2f( side / 2, side / 2 ); v3 = vec2f( side / 2, - side / 2 ); v4 = vec2f( - side / 2, - side / 2 ); while( true ) { glClearColor( 0.125f, 0.125f, 0.125f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); drawTriangle( screen, v1, v2, v3, textures[ 0 ] ); drawTriangle( screen, v1, v3, v4, textures[ 1 ] ); SDL_GL_SwapWindow( window ); } return 0; }
|