From f92b613698b5c4066068bb937b5993b1c5a3bd24 Mon Sep 17 00:00:00 2001 From: kitty piapiac Date: Mon, 17 Apr 2023 22:15:31 -0700 Subject: separate shader stuff from graph.c --- graph.c | 37 ++++++++++--------------------------- include/graph.h | 6 +++++- mino.c | 35 +++++++++++++++++++++++++++++++++-- res/tex/fart.jpg | Bin 0 -> 52858 bytes tex.c | 3 +-- 5 files changed, 49 insertions(+), 32 deletions(-) create mode 100644 res/tex/fart.jpg diff --git a/graph.c b/graph.c index fdc28be..92316c3 100644 --- a/graph.c +++ b/graph.c @@ -6,23 +6,6 @@ #include"graph.h" #include"tex.h" -#define GLSL(...)"#version 330 core\n"#__VA_ARGS__ -_ IM C shader_tex_vert[]=GLSL( - layout (location=0) in vec3 pos; - layout (location=1) in vec3 col; - layout (location=2) in vec2 tex; - out vec3 Col; - out vec2 Tex; - void main(){gl_Position=vec4(pos,1.0);Col=col;Tex=tex;}); - -_ IM C shader_tex_frag[]=GLSL( - out vec4 Frag; - in vec3 Col; - in vec2 Tex; - uniform sampler2D tex_buf; - void main(){Frag=texture(tex_buf,Tex);}); - -U32 graph_shader_tex; U32 graph_win_w=1280,graph_win_h=720; _ SDL_Window*win; @@ -40,8 +23,7 @@ V graph_init(V){SDL_version sdl_vh,sdl_vl; N(ctx=SDL_GL_CreateContext(win),on_err("create OpenGL context")) N(gladLoadGLLoader(SDL_GL_GetProcAddress),on_err("load GL")) SDL_GL_SetSwapInterval(1); - q_init(), - graph_shader_tex=graph_shader_create(shader_tex_vert,shader_tex_frag); + q_init(); SDL_VERSION(&sdl_vh)SDL_GetVersion(&sdl_vl); fprintf(stderr, "Graphics initialization successful!\n" @@ -86,13 +68,7 @@ _ V q_init(V){ glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*SZ(F32),(V*)(6*SZ(F32))), glEnableVertexAttribArray(2), glBindVertexArray(0);} -V graph_quad(U32 tex){//F32 t,g;I l; - /*t=SDL_GetTicks(), - g=sinf(sinf(t/255)/2)+0.5, - l=glGetUniformLocation(solid_shader,"col"), - glUniform3f(l,0,0,g),*/ - glBindTexture(GL_TEXTURE_2D,tex), - glUseProgram(graph_shader_tex), +V graph_quad(){ glBindVertexArray(q_vao), glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0), glBindVertexArray(0);} @@ -107,13 +83,20 @@ U32 graph_shader_create(IM C*vert,IM C*frag){I ok;C info[512];U32 v,f,p; glDeleteShader(v),glDeleteShader(f); R p;} +V graph_shader_use(U32 s){glUseProgram(s);} +V graph_shader_seti(IM C*s,I i){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p), + glUniform1i(glGetUniformLocation(p,s),i);} + U32 graph_tex_create(IM C*f){Tex tex;U32 t; glGenTextures(1,&t); glBindTexture(GL_TEXTURE_2D,t), glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_NEAREST), glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); tex=tex_load(f); - glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,tex.w,tex.h,0,GL_RGB,GL_UNSIGNED_BYTE,tex.d), + glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,tex.w,tex.h,0,GL_RGBA,GL_UNSIGNED_BYTE,tex.d), glGenerateMipmap(GL_TEXTURE_2D), tex_free(tex); R t;} +V graph_tex_use(U32 tex,U8 i){ + glActiveTexture(GL_TEXTURE0+i); + glBindTexture(GL_TEXTURE_2D,tex);} diff --git a/include/graph.h b/include/graph.h index d3bbbcf..61951f5 100644 --- a/include/graph.h +++ b/include/graph.h @@ -1,13 +1,17 @@ #ifndef MINO_GRAPH_H #define MINO_GRAPH_H +#define GRAPH_GLSL(...)"#version 330 core\n"#__VA_ARGS__ X V graph_init(V); X V graph_deinit(V); X V graph_events(V); X V graph_before(V); X V graph_after(V); -X V graph_quad(U32); +X V graph_quad(V); X U32 graph_shader_create(IM C*,IM C*); X U32 graph_shader_tex; +X V graph_shader_use(U32); +X V graph_shader_seti(IM C*,I); X U32 graph_tex_create(IM C*); +X V graph_tex_use(U32,U8); #endif diff --git a/mino.c b/mino.c index 2da1700..3362ab3 100644 --- a/mino.c +++ b/mino.c @@ -8,9 +8,40 @@ V*make(Uz z){V*p;N(p=malloc(z), die("oom"))R p;} V*remk(V*p,Uz z){N(p=realloc(p,z),die("oom"))R p;} V del(V*p){free(p);} -I main(I ac,C**av){(V)ac,(V)av;U32 cat; +_ IM C shader_tex_vert[]=GRAPH_GLSL( + layout (location=0) in vec3 pos; + layout (location=1) in vec3 col; + layout (location=2) in vec2 tex; + out vec3 Col; + out vec2 Tex; + void main(){gl_Position=vec4(pos,1.0);Col=col;Tex=tex;}); + +_ IM C shader_tex_frag[]=GRAPH_GLSL( + out vec4 Frag; + in vec3 Col; + in vec2 Tex; + uniform sampler2D buf0; + uniform sampler2D buf1; + void main(){Frag=texture(buf0,Tex)*texture(buf1,Tex);}); +_ U32 shader_tex; + +_ V shader_tex_init(V){ + shader_tex=graph_shader_create(shader_tex_vert,shader_tex_frag), + graph_shader_use(shader_tex), + graph_shader_seti("buf0",0), + graph_shader_seti("buf1",1);} + +I main(I ac,C**av){(V)ac,(V)av;U32 cat,fart; graph_init(); atexit(graph_deinit); + + shader_tex_init(); cat=graph_tex_create("res/tex/cat.jpg"); - WH(1,graph_before(),graph_events(),graph_quad(cat),graph_after()); + fart=graph_tex_create("res/tex/fart.jpg"); + WH(1,graph_before(),graph_events(), + graph_shader_use(shader_tex), + graph_tex_use(cat,0), + graph_tex_use(fart,1), + graph_quad(), + graph_after()); R 0;} diff --git a/res/tex/fart.jpg b/res/tex/fart.jpg new file mode 100644 index 0000000..44c31bc Binary files /dev/null and b/res/tex/fart.jpg differ diff --git a/tex.c b/tex.c index 9523066..34b00df 100644 --- a/tex.c +++ b/tex.c @@ -4,7 +4,6 @@ #include"tex.h" Tex tex_load(IM C*n){Tex r;U32 c; stbi_set_flip_vertically_on_load(1); - N(r.d=stbi_load(n,(S32*)&r.w,(S32*)&r.h,(S32*)&c,3),die("failed to load texture: %s\n",stbi_failure_reason())); + N(r.d=stbi_load(n,(S32*)&r.w,(S32*)&r.h,(S32*)&c,4),die("failed to load texture: %s\n",stbi_failure_reason())); R r;} - V tex_free(Tex t){stbi_image_free(t.d);} -- cgit v1.2.3