diff options
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | graph.c | 66 | ||||
-rw-r--r-- | include/graph.h | 5 | ||||
m--------- | include/stb | 0 | ||||
-rw-r--r-- | include/tex.h | 6 | ||||
-rw-r--r-- | mino.c | 5 | ||||
-rw-r--r-- | res/tex/cat.jpg | bin | 0 -> 73692 bytes | |||
-rw-r--r-- | tex.c | 10 |
9 files changed, 81 insertions, 21 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1bbb38a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "include/stb"] + path = include/stb + url = https://github.com/nothings/stb @@ -2,11 +2,12 @@ CC := clang CFLAGS := -std=c11 -Wall -Wextra -Wshadow -Iinclude $(shell sdl2-config --cflags) LDFLAGS := -LDLIBS := -ldl $(shell sdl2-config --libs) +LDLIBS := -ldl -lm $(shell sdl2-config --libs) all: mino -mino: glad.o graph.o mino.o +mino: glad.o tex.o graph.o mino.o mino.o: mino.c include/mino.h graph.o: graph.c include/graph.h +tex.o: tex.c include/tex.h glad.o: glad.c include/glad/glad.h clean: - rm -rf mino mino.o graph.o glad.o + rm -rf mino mino.o graph.o tex.o glad.o @@ -4,14 +4,30 @@ #include<SDL.h> #include"mino.h" #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; _ SDL_GLContext ctx; -_ U32 win_w=1280,win_h=720; -_ IM C solid_vert[]="#version 330 core\nlayout(location=0)in vec3 pos;void main(){gl_Position=vec4(pos.x,pos.y,pos.z,1.0);}"; -_ IM C solid_frag[]="#version 330 core\nout vec4 Colour;void main(){Colour=vec4(1.0, 0.5, 0.2, 1.0);}"; -_ U32 solid_shader; -_ V on_err(IM C*d){die("Could not %s: %s\n",d,SDL_GetError());} +_ V on_err(IM C*d){die("Could not %s: %s",d,SDL_GetError());} _ V q_init(V); V graph_init(V){SDL_version sdl_vh,sdl_vl; @@ -20,12 +36,12 @@ V graph_init(V){SDL_version sdl_vh,sdl_vl; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,3), SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_CORE), SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); - N(win=SDL_CreateWindow("mino",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,win_w,win_h,SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE),on_err("create window")) + 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); q_init(), - solid_shader=graph_shader_create(solid_vert,solid_frag); + graph_shader_tex=graph_shader_create(shader_tex_vert,shader_tex_frag); SDL_VERSION(&sdl_vh)SDL_GetVersion(&sdl_vl); fprintf(stderr, "Graphics initialization successful!\n" @@ -42,7 +58,7 @@ 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,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);} @@ -51,10 +67,11 @@ V graph_after(V){SDL_GL_SwapWindow(win);} _ U32 q_vao,q_vbo,q_ebo; _ V q_init(V){ _ IM F32 verts[]={ - 0.5, 0.5,0.0, - 0.5,-0.5,0.0, - -0.5,-0.5,0.0, - -0.5, 0.5,0.0}; + /*positions*/ /*colours*/ /*texture*/ + 0.5, 0.5,0.0, 1.0,0.0,0.0, 1.0,1.0, + 0.5,-0.5,0.0, 0.0,1.0,0.0, 1.0,0.0, + -0.5,-0.5,0.0, 1.0,0.0,1.0, 0.0,0.0, + -0.5, 0.5,0.0, 0.0,0.0,0.0, 0.0,1.0}; _ IM U32 indxs[]={0,1,3,1,2,3}; glGenVertexArrays(1,&q_vao),glGenBuffers(1,&q_vbo),glGenBuffers(1,&q_ebo); glBindVertexArray(q_vao), @@ -62,11 +79,20 @@ _ V q_init(V){ glBufferData(GL_ARRAY_BUFFER,SZ verts,verts,GL_STATIC_DRAW), glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,q_ebo), glBufferData(GL_ELEMENT_ARRAY_BUFFER,SZ indxs,indxs,GL_STATIC_DRAW); - glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*SZ verts[0],0), + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8*SZ(F32),(V*)(0*SZ(F32))), glEnableVertexAttribArray(0), + glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8*SZ(F32),(V*)(3*SZ(F32))), + glEnableVertexAttribArray(1), + glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*SZ(F32),(V*)(6*SZ(F32))), + glEnableVertexAttribArray(2), glBindVertexArray(0);} -V graph_quad(V){ - glUseProgram(solid_shader), +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), glBindVertexArray(q_vao), glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0), glBindVertexArray(0);} @@ -81,3 +107,13 @@ 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;} +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), + glGenerateMipmap(GL_TEXTURE_2D), + tex_free(tex); + R t;} diff --git a/include/graph.h b/include/graph.h index 9780714..d3bbbcf 100644 --- a/include/graph.h +++ b/include/graph.h @@ -5,6 +5,9 @@ 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 V graph_quad(U32); X U32 graph_shader_create(IM C*,IM C*); +X U32 graph_shader_tex; + +X U32 graph_tex_create(IM C*); #endif diff --git a/include/stb b/include/stb new file mode 160000 +Subproject 5736b15f7ea0ffb08dd38af21067c314d6a3aae diff --git a/include/tex.h b/include/tex.h new file mode 100644 index 0000000..8b5b3ff --- /dev/null +++ b/include/tex.h @@ -0,0 +1,6 @@ +#ifndef MINO_TEX_H +#define MINO_TEX_H +typedef struct{U32 w,h;U8*d;}Tex; +X Tex tex_load(IM C*); +X V tex_free(Tex); +#endif @@ -8,8 +8,9 @@ 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; +I main(I ac,C**av){(V)ac,(V)av;U32 cat; graph_init(); atexit(graph_deinit); - WH(1,graph_before(),graph_events(),graph_quad(),graph_after()); + cat=graph_tex_create("res/tex/cat.jpg"); + WH(1,graph_before(),graph_events(),graph_quad(cat),graph_after()); R 0;} diff --git a/res/tex/cat.jpg b/res/tex/cat.jpg Binary files differnew file mode 100644 index 0000000..f709f3d --- /dev/null +++ b/res/tex/cat.jpg @@ -0,0 +1,10 @@ +#define STB_IMAGE_IMPLEMENTATION +#include<stb/stb_image.h> +#include"mino.h" +#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())); + R r;} + +V tex_free(Tex t){stbi_image_free(t.d);} |