afq | Дата: Среда, 01 Августа 2018, 21:47 | Сообщение # 1 |
Разработчик
Сейчас нет на сайте
| Я уже запарился пробывать разные варианты. Может кто нибудь поможет. Я пытаюсь рисовать с помощью шейдеров, vbo vao с привязкой к буферам, в общем новейшим способом. Вот код, не рисует рисунок, выводит только черный квадрат. Что не так?
Код #include <stdio.h> #include "gl_core_4_3.h" #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> #include <SDL2/SDL_opengl.h> #include <SDL2/SDL_events.h> #include <SDL2/SDL_keycode.h> #include <stdlib.h> int *vertices; unsigned char *pixels; int *ivert; int *ipixels; float *fvert; float *fpixels; unsigned int max_draw; int screen_width; int screen_height; SDL_Window *window; int max_pixels;
void init_image ( long pos ) { FILE *fd = fopen ( "data", "r" ); fseek ( fd, pos, SEEK_SET ); int width; int height; fread ( &width, sizeof ( unsigned int ), 1, fd ); fread ( &height, sizeof ( unsigned int ), 1, fd ); fread ( &max_pixels, sizeof ( unsigned int ), 1, fd );
pixels = calloc ( max_pixels, sizeof ( unsigned char ) ); fpixels = calloc ( max_pixels, sizeof ( float ) ); ipixels = calloc ( max_pixels, sizeof ( int ) ); fread ( &pixels[0], sizeof ( unsigned char ), max_pixels, fd );
for ( int i = 0; i < max_pixels; i++ ) { float m = pixels[i]; int b = m / 0.01; float c = b * 0.01; fpixels [ i ] = c; ipixels [ i ] = pixels[i]; }
max_draw = height * width; vertices = calloc ( max_draw * 2, sizeof ( unsigned int ) ); fvert = calloc ( max_draw * 2, sizeof ( float ) ); ivert = calloc ( max_draw * 2, sizeof ( int ) );
for ( int y = height - 1, i = 0; y >= 0; y-- ) { for ( int x = 0; x < width; i += 2, x++ ) { vertices [ i + 0 ] = x; vertices [ i + 1 ] = y; fvert [ i + 0 ] = x; fvert [ i + 1 ] = y; ivert [ i + 0 ] = x; ivert [ i + 1 ] = y; } } fclose ( fd ); } #if 1 GLuint program;
GLuint load_shader ( GLenum type, const char *shader_src ) { GLuint shader; GLint compiled; shader = glCreateShader ( type ); printf ( "!\n" ); if ( shader == 0 ) return 0; glShaderSource ( shader, 1, &shader_src, NULL ); glCompileShader ( shader ); glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled ); if ( !compiled ) { GLint info_len = 0; glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &info_len ); if ( info_len > 1 ) { char *info_log = malloc ( sizeof ( char ) * info_len ); glGetShaderInfoLog ( shader, info_len, NULL, info_log ); printf ( "shader compile %s ", info_log ); free ( info_log ); switch ( type ) { case GL_VERTEX_SHADER: printf ( "vertex shader!\n" ); break; case GL_FRAGMENT_SHADER: printf ( "fragment shader!\n" ); break; } } glDeleteShader ( shader ); return 0; } return shader; } GLuint loadProgram ( const char *vshader, const char *fshader ) { GLuint vertex_shader, fragment_shader; vertex_shader = load_shader ( GL_VERTEX_SHADER, vshader ); fragment_shader = load_shader ( GL_FRAGMENT_SHADER, fshader ); GLuint program_object; program_object = glCreateProgram ( ); glAttachShader ( program_object, vertex_shader ); glAttachShader ( program_object, fragment_shader ); glLinkProgram ( program_object ); GLint linked; glGetProgramiv ( program_object, GL_LINK_STATUS, &linked ); if ( !linked ) { GLint info_len = 0; glGetProgramiv ( program_object, GL_INFO_LOG_LENGTH, &info_len ); if ( info_len > 1 ) { char *info_log = malloc ( sizeof ( char ) * info_len ); glGetProgramInfoLog ( program_object, info_len, NULL, info_log ); printf ( "%s\n", info_log ); free ( info_log ); } return -1; } return program_object; } #endif
void init_conf ( ) { const char *vShaderstr = "#version 430\n" "layout(location = 0) in vec2 position;\n" "layout(location = 1) in vec4 color;\n" "uniform mat4 transform;\n" "out vec4 v_color;\n" "void main()\n" "{\n" " v_color = vec4 ( color );\n" " gl_Position = transform * vec4 ( position, 0.0, 1.0 );\n" "}"; const char *fShaderstr = "#version 430\n" // "precision mediump float;\n" "in vec4 color;\n" "layout ( location = 0 ) out vec4 fragColor;\n" "void main()\n" "{\n" " fragColor = vec4 ( color );\n" "}"; program = loadProgram ( vShaderstr, fShaderstr ); #if 0 glOrtho ( 0.0f, screen_width, 0, screen_height, 0.0f, 1.0f ); #endif }
void draw ( ) { GLfloat transform[] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, }; float prj_mat[] = { 2.0f/screen_width, 0.0f, 0.0f, 0.0f, 0.0f, 2.0f/screen_height, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f }; glUseProgram ( program ); GLint prj_loc = glGetUniformLocation( program, "transform" ); glUniformMatrix4fv( prj_loc, 1, GL_FALSE, prj_mat ); // glUniformMatrix4fv( prj_loc, 1, GL_FALSE, transform );
GLuint vbo[2]; GLuint vao; glGenBuffers ( 2, &vbo[0] ); GLuint posbufh = vbo[0]; GLuint colbufh = vbo[1]; glBindBuffer ( GL_ARRAY_BUFFER, posbufh ); glBufferData ( GL_ARRAY_BUFFER, max_draw * 2 * sizeof ( float ), fvert, GL_STATIC_DRAW ); glBindBuffer ( GL_ARRAY_BUFFER, colbufh ); glBufferData ( GL_ARRAY_BUFFER, max_pixels * sizeof ( float ), fpixels, GL_STATIC_DRAW ); glGenVertexArrays ( 1, &vao ); glBindVertexArray ( vao ); glEnableVertexAttribArray ( 0 ); glEnableVertexAttribArray ( 1 ); glBindBuffer ( GL_ARRAY_BUFFER, posbufh ); glVertexAttribPointer ( 0, 2, GL_FLOAT, GL_FALSE, 0, NULL ); glBindBuffer ( GL_ARRAY_BUFFER, colbufh ); glVertexAttribPointer ( 1, 4, GL_FLOAT, GL_FALSE, 0, NULL ); glBindVertexArray ( vao );
glDrawArrays ( GL_POINTS, 0, max_draw ); glFlush ( ); } SDL_GLContext glc; void init_sdl ( ) { SDL_Init ( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS ); SDL_DisplayMode mode; SDL_GetCurrentDisplayMode ( 0, &mode ); screen_width = mode.w - 64; screen_height = mode.h - 128;
#if 1 if ( ogl_LoadFunctions() == ogl_LOAD_FAILED ) { exit ( EXIT_FAILURE ); } #endif window = SDL_CreateWindow ( "ipsi", 0, 0, screen_width, screen_height, SDL_WINDOW_OPENGL );
#if 0 SDL_GL_SetAttribute ( SDL_GL_CONTEXT_MAJOR_VERSION, 4 ); SDL_GL_SetAttribute ( SDL_GL_CONTEXT_MAJOR_VERSION, 3 ); #endif glc = SDL_GL_CreateContext ( window ); SDL_GL_MakeCurrent ( window, glc ); printf ( "!!\n"); const char *version = glGetString ( GL_VERSION ); printf ( "%s\n", version );
glViewport ( 0, 0, screen_width, screen_height ); printf ( "@\n" ); glEnable ( GL_BLEND ); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } int input_thread ( void *data ) { SDL_Event event; while ( SDL_WaitEvent ( &event ) ) { switch ( event.type ) { case SDL_KEYDOWN: if ( event.key.keysym.sym == SDLK_q ) { exit ( EXIT_SUCCESS ); } break; case SDL_MOUSEBUTTONDOWN: break; case SDL_MOUSEBUTTONUP: break; case SDL_MOUSEMOTION: break; } } } void print_version ( ) { const GLubyte *renderer = glGetString ( GL_RENDERER ); const GLubyte *vendor = glGetString ( GL_VENDOR ); const GLubyte *version = glGetString ( GL_VERSION ); const GLubyte *glslVersion = glGetString ( GL_SHADING_LANGUAGE_VERSION );
GLint major, minor; glGetIntegerv ( GL_MAJOR_VERSION, &major ); glGetIntegerv ( GL_MINOR_VERSION, &minor );
printf ( "gl renderer %s\n", renderer ); printf ( "gl vender %s\n", vendor ); printf ( "gl version %s %d.%d\n", version, major, minor ); printf ( "gl glsl %s\n", glslVersion ); } int main ( ) { init_sdl ( ); print_version ( ); init_conf ( ); init_image ( 0 ); SDL_CreateThread ( input_thread, "input_thread", NULL );
while ( 1 ) { glClearColor ( 1, 1, 1, 1 ); glClear ( GL_COLOR_BUFFER_BIT ); draw ( ); SDL_GL_SwapWindow ( window ); SDL_Delay ( 0.01 ); }
}
Добавлено (03 Августа 2018, 18:55) --------------------------------------------- Оказывается что имя выходного цвета должно совпадать с именем входным в фрагментном шейдере.
|
|
| |