From 84bcb0274fc80d360ff0fbe5ede89643169f6165 Mon Sep 17 00:00:00 2001 From: kitty piapiac Date: Tue, 18 Apr 2023 01:59:53 -0700 Subject: window projection --- Makefile | 2 +- graph.c | 18 +++++++++++++----- include/graph.h | 7 ++++++- mino.c | 28 ++++++++++++---------------- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 358b282..e8dfb62 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .POSIX: CC := clang -CFLAGS := -std=c11 -Wall -Wextra -Wshadow -Iinclude $(shell sdl2-config --cflags) $(shell pkg-config --cflags cglm) +CFLAGS := -std=c11 -g -O0 -Wall -Wextra -Wshadow -Iinclude $(shell sdl2-config --cflags) $(shell pkg-config --cflags cglm) LDFLAGS := LDLIBS := -ldl -lm $(shell sdl2-config --libs) $(shell pkg-config --libs cglm) all: mino diff --git a/graph.c b/graph.c index 4e025c9..78965fd 100644 --- a/graph.c +++ b/graph.c @@ -2,6 +2,8 @@ #include #define SDL_MAIN_HANDLED #include +#include +#include #include #include"mino.h" #include"graph.h" @@ -23,7 +25,7 @@ V graph_init(V){SDL_version sdl_vh,sdl_vl; N(win=SDL_CreateWindow("mino",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,graph_win_w,graph_win_h,SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE),on_err("create window")) N(ctx=SDL_GL_CreateContext(win),on_err("create OpenGL context")) N(gladLoadGLLoader(SDL_GL_GetProcAddress),on_err("load GL")) - SDL_GL_SetSwapInterval(1); + SDL_GL_SetSwapInterval(1), q_init(); SDL_VERSION(&sdl_vh)SDL_GetVersion(&sdl_vl); fprintf(stderr, @@ -41,7 +43,8 @@ V graph_events(V){SDL_Event ev;U32 t;WH(SDL_PollEvent(&ev),t=ev.type; Q(SDL_QUIT==t,exit(0))OR Q(SDL_WINDOWEVENT==t,SDL_WindowEvent ew=ev.window; Q(SDL_WINDOWEVENT_RESIZED==ew.event - ||SDL_WINDOWEVENT_SIZE_CHANGED==ew.event,graph_win_w=ew.data1,graph_win_h=ew.data2,glViewport(0,0,ew.data1,ew.data2))))} + ||SDL_WINDOWEVENT_SIZE_CHANGED==ew.event,graph_win_w=ew.data1,graph_win_h=ew.data2, + glViewport(0,0,ew.data1,ew.data2))))} V graph_deinit(V){SDL_GL_DeleteContext(ctx),SDL_DestroyWindow(win),SDL_Quit();} V graph_before(V){glClearColor(1.0,0.75,0.8,1.0),glClear(GL_COLOR_BUFFER_BIT);} @@ -71,7 +74,12 @@ _ 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(){ +V graph_quad_xywh(F32 x,F32 y,F32 w,F32 h){graph_quad((vec2){x,y},(vec2){w,h});} +V graph_quad(vec2 p,vec2 z){mat4 trans; + glm_mat4_identity(trans), + glm_translate(trans,(vec3){p[0],p[1],0}), + glm_scale(trans,(vec3){z[0],z[1],1}), + graph_shader_setM4("model",trans), glBindVertexArray(q_vao), glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0), glBindVertexArray(0);} @@ -87,8 +95,8 @@ U32 graph_shader_create(IM C*vert,IM C*frag){I ok;C info[512];U32 v,f,p; 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);} -V graph_shader_setM4(IM C*s,mat4 m4){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniformMatrix4fv(glGetUniformLocation(p,s),1,GL_FALSE,m4[0]);} +V graph_shader_setI(IM C*s,I i) {I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniform1i(glGetUniformLocation(p,s),i);} +V graph_shader_setM4(IM C*s,mat4 m4){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniformMatrix4fv(glGetUniformLocation(p,s),1,GL_FALSE,&m4[0][0]);} U32 graph_tex_create(IM C*f){Tex tex;U32 t; glGenTextures(1,&t); diff --git a/include/graph.h b/include/graph.h index 8e09bac..f04bfb5 100644 --- a/include/graph.h +++ b/include/graph.h @@ -1,13 +1,18 @@ #ifndef MINO_GRAPH_H #define MINO_GRAPH_H #define GRAPH_GLSL(...)"#version 330 core\n"#__VA_ARGS__ +X U32 graph_win_w,graph_win_h; + 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(V); X U32 graph_ticks(V); + +X V graph_quad(vec2,vec2); +X V graph_quad_xywh(F32,F32,F32,F32); + X U32 graph_shader_create(IM C*,IM C*); X U32 graph_shader_tex; X V graph_shader_use(U32); diff --git a/mino.c b/mino.c index f59a8f0..be07e7f 100644 --- a/mino.c +++ b/mino.c @@ -16,39 +16,35 @@ _ IM C shader_tex_vert[]=GRAPH_GLSL( layout (location=2) in vec2 tex; out vec3 Col; out vec2 Tex; - uniform mat4 trans; - void main(){gl_Position=trans*vec4(pos,1.0);Col=col;Tex=tex;}); + uniform mat4 proj; + uniform mat4 model; + void main(){gl_Position=proj*model*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);}); + void main(){Frag=texture(buf0,Tex);}); + _ U32 shader_tex; +_ mat4 win_proj; _ V shader_tex_init(V){ shader_tex=graph_shader_create(shader_tex_vert,shader_tex_frag), graph_shader_use(shader_tex); - mat4 trans; - glm_mat4_identity(trans), - glm_translate(trans,(vec3){0.5,-0.5,0.5}), - glm_rotate(trans,0.2,(vec3){0,0,1}), - graph_shader_setM4("trans",trans), - graph_shader_use(shader_tex), - graph_shader_setI("buf0",0), - graph_shader_setI("buf1",1);} + graph_shader_setM4("proj",win_proj), + graph_shader_setI("buf0",0);} -I main(I ac,C**av){(V)ac,(V)av;U32 cat,fart; +I main(I ac,C**av){(V)ac,(V)av;U32 cat; graph_init(); atexit(graph_deinit); shader_tex_init(); cat=graph_tex_create("res/tex/cat.jpg"); - fart=graph_tex_create("res/tex/fart.jpg"); WH(1,graph_before(),graph_events(); + glm_ortho(0,graph_win_w,0,graph_win_h,0,4,win_proj), + graph_shader_setM4("proj",win_proj), graph_tex_use(cat,0), - graph_tex_use(fart,1), - graph_quad(), + graph_quad_xywh(200,200,200,200), graph_after()); R 0;} -- cgit v1.2.3