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

[SDL2 OpenGles2] - wczytywanie tekstur do shadera oraz ich rysowanie

Ostatnio zmodyfikowano 2024-02-08 23:08
Autor Wiadomość
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 :
step.png
step.png
lowland.png
lowland.png

obecny wynik programu :
currentResult.png
currentResult.png

C/C++
// program rysujacy dwie rozne tekstury
#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 ]; // vertexes of triangle (x,y) x3
static GLfloat coords[ 2 ]; // coords of texture to func texture2d(tex, texcoord)

// SHADERS //////////////////////////////////////////////////////
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()
{
   
// ja juz nie wiem co programuje
    //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
   
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 );
   
// add array [vertexes] to edit position
   
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 );
   
   
// get the sampler2D from shaders
   
GLint textureUniform = glGetUniformLocation( textureProgram, "texture" );
   
glUniform1i( textureUniform, 0 ); // texture from shader = GL_TEXTURE0
   
   
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 );
   
   
// now set parameters of texture // texture must be repeat when end of tex
   
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 );
   
   
// now set the pixels to texture
   
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 )
   
{
       
// clear screen
       
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 ] );
       
       
// draw submit
       
SDL_GL_SwapWindow( window );
   
}
   
return 0;
}
P-179922
pekfos
» 2023-02-02 18:14:00
Współrzędne tekstury też powinny być zdefiniowane dla każdego wierzchołka osobno i pewnie nawet tak są czytane, tylko tablica jest za mała.
P-179924
tBane
Temat założony przez niniejszego użytkownika
» 2023-02-02 18:34:23
o tym nie pomyślałem, ale naprowadziłeś mnie na pewien trop.

C/C++
GLint texcoord = glGetAttribLocation( textureProgram, "texcoord" );
glEnableVertexAttribArray( texcoord );
glVertexAttribPointer( texcoord, 2, GL_FLOAT, GL_FALSE, 0, vertexes );


rezultat:
 
P-179925
« 1 »
  Strona 1 z 1