diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/glfww.c | 121 | ||||
-rw-r--r-- | src/glfww.h | 24 | ||||
-rw-r--r-- | src/point.c | 212 | ||||
-rw-r--r-- | src/sdlw.c.old | 62 | ||||
-rw-r--r-- | src/sdlw.h.old | 13 | ||||
-rw-r--r-- | src/strings.c | 75 | ||||
-rw-r--r-- | src/strings.h | 29 | ||||
-rw-r--r-- | src/util.c | 112 | ||||
-rw-r--r-- | src/util.h | 22 |
9 files changed, 670 insertions, 0 deletions
diff --git a/src/glfww.c b/src/glfww.c new file mode 100644 index 0000000..bfcc8d0 --- /dev/null +++ b/src/glfww.c @@ -0,0 +1,121 @@ +#include <stdio.h> +#include <GLFW/glfw3.h> +#include "util.h" +#include <unistd.h> +#include <stdlib.h> +#include <math.h> +#include "glfww.h" +int w,h; +#define ab_to_vp(x,y,w,h,x1,y1) float x = 2 * ((float)x1/w) -1;\ + float y = 2 * ((float)y1/h) -1; +GLFWwindow* glfw_init(){ + GLFWwindow* window; + + if(!glfwInit()) + err("failed to init glfw",pexit); + + window = glfwCreateWindow(800,500,"nya",NULL,NULL); + if(!window){ + glfwTerminate(); + err("failed to create window",pexit); + } + + glfwMakeContextCurrent(window); + int w,h; + glfwGetFramebufferSize(window,&w,&h); + glViewport(0,0,w,h); + return window; + /* + while(!glfwWindowShouldClose(window)){ + glClear(GL_COLOR_BUFFER_BIT); + glColor3f(1.0f,0.0f,0.0f); + glBegin(GL_POINTS); + for(float x = 0.0; x!=200.0;x+=1.0){ + ab_to_vp(ax,ay,w,h,x,x); + glVertex2f(ax,ay); + } + glEnd(); + glfwSwapBuffers(window); + glfwPollEvents(); + } + glfwTerminate(); + return 0; + */ +} +void refresh_size(GLFWwindow*wi){ + glfwGetFramebufferSize(wi,&w,&h); +} +#define glfw_load(w) glfwSwapBuffers(w); +void glfw_loop(GLFWwindow*window){ + while(!glfwWindowShouldClose(window)){ + + //glfw_load(window); + glfwPollEvents(); + } + glfwTerminate(); +} +void glfw_pixel_partial(GLFWwindow*wi,int x, int y){ + ab_to_vp(ax,ay,w,h,x,y); + glVertex2f(ax,ay); + +} + +void glfw_clear(GLFWwindow*w){ + glClear(GL_COLOR_BUFFER_BIT); +} + +void glfw_circle(GLFWwindow* wi,int x, int y, int r){ + //SDL_SetRenderDrawColor(w.r,255,255,0,0); + glfwGetFramebufferSize(wi,&w,&h); + glBegin(GL_POINTS); + glColor3f(1.0f,0.0f,0.0f); + for(int i = 1; i!=360; i++){ + float cf = cosf(i)*r; + float sf = sinf(i)*r; + //for(int z = 1; z<=r; z++){ + //int x2 = x + z * cf; + //int y2 = y + z * sf; + + glfw_pixel_partial(wi,x+cf,y+sf); + //} + } + glEnd(); +} +void glfw_circle_partial(GLFWwindow* wi,int x, int y, int r){ + //SDL_SetRenderDrawColor(w.r,255,255,0,0); + if(r==0){ + glfw_pixel_partial(wi,x,y); + return; + } + for(int i = 1; i!=360; i++){ + float cf = cosf(i)*r; + float sf = sinf(i)*r; + //for(int z = 1; z<=r; z++){ + //int x2 = x + z * cf; + //int y2 = y + z * sf; + + glfw_pixel_partial(wi,x+cf,y+sf); + //} + } +} +void glfw_square(GLFWwindow* wi,int x, int y, int r){ + //SDL_SetRenderDrawColor(w.r,255,255,0,0); + glfwGetFramebufferSize(wi,&w,&h); + glBegin(GL_POINTS); + glColor3f(1.0f,0.0f,0.0f); + for(int i = 0; i<=r; i++){ + for(int j = 0; j<=r; j++){ + glfw_pixel_partial(wi,i-r/2+x,j-r/2+y); + } + } + glEnd(); +} +int __glw_main(){ + GLFWwindow* w = glfw_init(); + glfw_circle(w,50,50,20); + glfw_load(w); + sleep(5); + glfwTerminate(); + //glfw_loop(w); + return 0; +} diff --git a/src/glfww.h b/src/glfww.h new file mode 100644 index 0000000..fd784c2 --- /dev/null +++ b/src/glfww.h @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <GLFW/glfw3.h> +#include "util.h" +#include <unistd.h> +#include <stdlib.h> +#include <math.h> +#ifndef __glfww_ +#define __glfww_ +#define win_clean() glfwTerminate(); +#define ab_to_vp(x,y,w,h,x1,y1) float x = 2 * ((float)x1/w) -1;\ + float y = 2 * ((float)y1/h) -1; +GLFWwindow* glfw_init(); +#define glfw_load(w) glfwSwapBuffers(w); +void glfw_loop(GLFWwindow*window); +void glfw_pixel_partial(GLFWwindow*wi,int x, int y); +void glfw_clear(GLFWwindow*w); +void refresh_size(GLFWwindow*); +#define glfw_pixel(wi,x,y)\ + glBegin(GL_POINTS);\ + glfw_pixel_partial(wi,x,y);\ + glEnd(); +void glfw_circle(GLFWwindow* w,int x, int y, int r); +void glfw_circle_partial(GLFWwindow* w,int x, int y, int r); +#endif diff --git a/src/point.c b/src/point.c new file mode 100644 index 0000000..d8e4c56 --- /dev/null +++ b/src/point.c @@ -0,0 +1,212 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include "glfww.h" +#include <unistd.h> +typedef struct { + double x; + double y; + double z; +} cord; +typedef struct { + int roll; //x axis + int pitch; //y axis + int yaw; //z axis +} rot; +typedef struct { + float opacity; +} opts; +typedef struct { + cord loc; + rot rot; + int uid; + int id; + opts opts; +} point; + +double binominal(int n, int k){ + if(n==k) + return 1.0; + double v = 1.0; + for(int i = 1; i<=k; i++){ + v=v*((float)(n+1-i)/i); + } + return v; +} +int ma = 4; +void mul_matr(double a[][ma],double b[][ma], double res[][ma]){ + for(int i = 0; i<ma; i++){ + for(int j = 0; j<ma;j++){ + res[i][j] = 0; + for(int k = 0; k<ma;k++){ + res[i][j]+=a[i][k]*b[k][j]; + + } + } + } +} +void perspective_proj(GLFWwindow* b,cord* c, int len,double ctx,double cty,double ctz,double cx, double cy, double cz){ + //double cx = -100; + //double cy = 0; + //double cz = 0; + //double ctx = 0; + //double cty = 0; + //double ctz = 0; + //cz=100-cz; + double coy = cos(cty); + double siz = sin(ctz); + double coz = cos(ctz); + double six = sin(ctx); + double siy = sin(cty); + double cox = cos(ctx); + double fov = .1; + double ex = cx; + double ey = cy; + //glfwGetFramebufferSize(b,&w,&h); + refresh_size(b); + glColor3f(1.0f,0.0f,0.0f); + double ez = 1/tan(fov/2); + glBegin(GL_POINTS); + for(int i = 0; i!=len; i++){ + double ax = c[i].x; + double ay = c[i].y; + double az = c[i].z; + + double eyz = (coz*(ay-cy)-siz*ax-cx); + double yzm = (coy*(az-cz) + siy*(siz*(ay-cy) + coz*(ax-cx))); + double dx = coy * (siz*(ay-cy) + coz*(ax-cx)) - (siy*(az-cz)); + double dy = six * yzm + cox*eyz; + double dz = cox * yzm - six*eyz; + + //printf("%f %f %f, %f %f\n",dx,dy,ez,ez/dz*dx+dx,dy*ez); + double bx = ez/dz*dx+dx; + double by = ez/dz*dy+dy; + //printf("%f %f | %f %f %f\n",bx,by,ctx,cty,ctz); + //int aaa = round((150-dz)/2); + if(dz>=0) + glfw_circle_partial(b,nearbyint(bx),nearbyint(by),/*(0>=aaa?1:*/1/*)*/); + //glfw_pixel_partial(b,round(bx),round(by)); + } + glEnd(); +} +cord* basier3d(double*xx,double*yy,double*zz,int n){ + cord* pa = malloc(sizeof(*pa)*(3000)); + //double xx[5] = {5.0,5.0,50.0,5.0,5.0}; + //double yy[5] = {5.0,5.0,5.0,5.0,10.0}; + //double zz[5] = {10.0,5.0,5.0,5.0,5.0}; + //int n = 5-1; + n-=1; + for(int iy = 0; iy<=100;iy+=1){ + double t = 0.01*iy; + double bcx = 0; + double bcy = 0; + double bcz = 0; + for(int i = 0; i <=n;i++){ + double pp = binominal(n,i) * pow((1 - t),(n - i)) * pow(t,i); + bcx += pp * xx[i]; + bcy += pp * yy[i]; + bcz += pp * zz[i]; + //if(ii==3){ + // printf("pp: %f bi : %f p1 : %f p2 : %f| %f, %f, %f\n",pp,binominal(n,i),bcx,bcy,bcz,pow((1-t),(n-i)),pow(t,i)); + //} + } + //int ii = floor(100*(double)(t/1.0)); + //printf("%i\n",iy); + pa[iy].x = bcx; + pa[iy].y = bcy; + pa[iy].z = bcz; + //printf("(%f,%f,%f)\n",bcx,bcy,bcz); + } + //bw b = sdl_init(); + /*for(int i = 0; i!=100; i ++){ + SDL_SetRenderDrawColor(b.r,0,255-255*(pa[i].z/100),0,255); + SDL_RenderDrawPoint(b.r,pa[i].x,pa[i].y); + sdl_circle(b,round(pa[i].x),round(pa[i].y),round(100-pa[i].z)); + //printf("%i : (%f,%f,%f)\n",i,pa[i].x,pa[i].y,(100-pa[i].z)); + }*/ + //perspective_proj(b,pa,100); + //sdl_loop(b); + return pa; +} +void join_cords(cord* a, cord* b, int a_len, int b_len){ + a = realloc(a,sizeof(*a)*(a_len+b_len+2)); + for(int i = 0; i<=a_len+b_len; i++){ + a[a_len+i] = b[i]; + } +} +int main(){ + double xx[5] = {5.0,5.0,5.0}; + double yy[5] = {5.0,100.0,200.0}; + double zz[5] = {95.0,95.0,95.0}; + cord* a = basier3d(xx,yy,zz,3); + + double xx2[5] = {200.0,200.0,200.0}; + double yy2[5] = {5.0,100.0,200.0}; + double zz2[5] = {95.0,95.0,95.0}; + cord* b = basier3d(xx2,yy2,zz2,3); + + double xx3[5] = {5.0,100.0,200.0}; + double yy3[5] = {5.0,5.0,5.0}; + double zz3[5] = {95.0,95.0,95.0}; + cord* c = basier3d(xx3,yy3,zz3,3); + + double xx4[5] = {5.0,100.0,200.0}; + double yy4[5] = {200.0,200.0,200.0}; + double zz4[5] = {95.0,95.0,95.0}; + cord* d = basier3d(xx4,yy4,zz4,3); + + double xx5[5] = {5.0,100.0,200.0}; + double yy5[5] = {200.0,200.0,200.0}; + double zz5[5] = {150.0,150.0,150.0}; + cord* e = basier3d(xx5,yy5,zz5,3); + + double xx6[5] = {5.0,100.0,200.0}; + double yy6[5] = {5.0,5.0,5.0}; + double zz6[5] = {150.0,150.0,150.0}; + cord* f = basier3d(xx6,yy6,zz6,3); + + double xx7[5] = {200.0,200.0,200.0}; + double yy7[5] = {5.0,100.0,200.0}; + double zz7[5] = {150.0,150.0,150.0}; + cord* g = basier3d(xx7,yy7,zz7,3); + + double xx8[5] = {5.0,5.0,5.0}; + double yy8[5] = {5.0,100.0,200.0}; + double zz8[5] = {150.0,150.0,150.0}; + cord* h = basier3d(xx8,yy8,zz8,3); + join_cords(a,b,100,100); + join_cords(a,c,200,100); + join_cords(a,d,300,100); + join_cords(a,e,400,100); + join_cords(a,f,500,100); + join_cords(a,g,600,100); + join_cords(a,h,700,100); + GLFWwindow* w = glfw_init(); + for(double rr = 0.01;rr<=20;rr+=0.01){ + double p1 = 0; + double p2 = rr; + double p3 = 0; + double p4 = -100; + double p5 = 0; + double p6 = rr*100*0; + perspective_proj(w,a,800,p1,p2,p3,p4,p5,p6); + //perspective_proj(w,b,100,p1,p2,p3,p4,p5,p6); + //perspective_proj(w,c,100,p1,p2,p3,p4,p5,p6); + //perspective_proj(w,d,100,p1,p2,p3,p4,p5,p6); + //perspective_proj(w,e,100,p1,p2,p3,p4,p5,p6); + //perspective_proj(w,f,100,p1,p2,p3,p4,p5,p6); + //perspective_proj(w,g,100,p1,p2,p3,p4,p5,p6); + //perspective_proj(w,h,100,p1,p2,p3,p4,p5,p6); + glfw_load(w); + usleep(500); + glfw_clear(w); + if(glfwWindowShouldClose(w))break; + } + free(a); + free(b); + free(c); + free(d); + glfwDestroyWindow(w); + win_clean(); + return 0; +} diff --git a/src/sdlw.c.old b/src/sdlw.c.old new file mode 100644 index 0000000..cfc2963 --- /dev/null +++ b/src/sdlw.c.old @@ -0,0 +1,62 @@ +#include <SDL2/SDL.h> +#include <stdio.h> +#include <stdlib.h> +#include "util.h" +#include "sdlw.h" +#define sdl_clear(r) SDL_SetRenderDrawColor(r,0,0,0,0);\ + SDL_RenderClear(r); +bw sdl_init(){ + if(SDL_Init(SDL_INIT_VIDEO)<0){ + err("failed to initialize sdl2 lib:(",pexit); + } + SDL_Renderer *r; + SDL_Window *w; + SDL_CreateWindowAndRenderer(400,400,0,&w,&r); + + if(!w) + err("failed to create window",pexit); + sdl_clear(r); + SDL_RenderPresent(r); + //SDL_Delay(5000); + bw both; + both.w =w; + both.r =r; + return both; +} +void sdl_loop(bw w){ + SDL_Event e; + //SDL_SetRenderDrawColor(w.r,255,0,0,255); + //for(int i = 0; i!=50;i++) + // SDL_RenderDrawPoint(w.r,i,i); + SDL_RenderPresent(w.r); + for(;;){ + while(SDL_PollEvent(&e) > 0){ + switch(e.type){ + case SDL_QUIT: + return; + } + + //SDL_UpdateWindowSurface(w.w); + } + } +} +void sdl_circle(bw w,int x, int y, int r){ + SDL_SetRenderDrawColor(w.r,255,255,0,0); + for(int i = 1; i!=360; i++){ + float cf = cosf(i); + float sf = sinf(i); + for(int z = 1; z<=r; z++){ + int x2 = x + z * cf; + int y2 = y + z * sf; + + SDL_RenderDrawPoint(w.r,x2,y2); + } + } +} +int main__2(){ + bw b = sdl_init(); + SDL_SetRenderDrawColor(b.r,255,255,0,0); + SDL_RenderDrawPoint(b.r,5,5); + sdl_loop(b); + return 0; +} diff --git a/src/sdlw.h.old b/src/sdlw.h.old new file mode 100644 index 0000000..7648d93 --- /dev/null +++ b/src/sdlw.h.old @@ -0,0 +1,13 @@ +#include <SDL2/SDL.h> +#include <stdio.h> +#include <stdlib.h> +#include "util.h" +typedef struct { + SDL_Window* w; + SDL_Renderer* r; +} bw; +#define sdl_clear(r) SDL_SetRenderDrawColor(r,0,0,0,0);\ + SDL_RenderClear(r); +bw sdl_init(); +void sdl_loop(bw); +void sdl_circle(bw w,int x, int y, int r); diff --git a/src/strings.c b/src/strings.c new file mode 100644 index 0000000..83b7fb8 --- /dev/null +++ b/src/strings.c @@ -0,0 +1,75 @@ +#include "strings.h" +#include "util.h" +int str_size(str*str){ + for(int i = 0;;i++) + if(str->str[i]=='\0') + return i; + return 0; +} +int ca_size(char*str){ + for(int i = 0;;i++) + if(str[i]=='\0') + return i; + return 0; +} +void str_pushc(str*str,char ch){ + str->str = realloc(str->str,sizeof(str->str)*(str->len+1)); + if(str->str==NULL) + err("failed to realloc string",pexit); + str->str[str->len]=ch; + str->str[str->len+1]='\0'; + str->len++; +} +void str_pushca(str*str,char*ins){ + int size = ca_size(ins); + str->str = realloc(str->str,sizeof(str->str)*(str->len+1+size)); + if(str->str==NULL) + err("failed to realloc string",pexit); + for(int i = 0; i!=size; i++) + str_pushc(str,ins[i]); +} +str* str_init(){ + str* str = malloc(sizeof(*str)); + str->str = malloc(sizeof(str->str)); + if(str->str==NULL||str==NULL) + err("failed to alloc string",pexit); + str->str[0] = '\0'; + str->len = 0; + return str; +} +int str_cmp_str(str*str1,str*str2){ + if(str1->len!=str2->len) + return 0; + for(int i = 0; i!=str1->len;i++) + if(str1->str[i]!=str2->str[i]) + return 0; + return 1; +} +int str_cmp_ca(str*str,char*ca){ + int len = ca_size(ca); + if(len!=str->len) + return 0; + for(int i = 0; i!=str->len;i++) + if(str->str[i]!=ca[i]) + return 0; + return 1; +} +void str_free(str*str){ + free(str->str); + free(str); +} +str* str_init_only_str(str*str){ + //str* str = malloc(sizeof(*str)); + str->str = malloc(sizeof(str->str)); + if(str->str==NULL||str==NULL) + err("failed to alloc string",pexit); + str->str[0] = '\0'; + str->len = 0; + return str; +} +void str_clear(str*str){ + for(int i = 0; i!=str->len; i++){ + str->str[i]='\0'; + } + str->len=0; +} diff --git a/src/strings.h b/src/strings.h new file mode 100644 index 0000000..7a6fb08 --- /dev/null +++ b/src/strings.h @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <stdlib.h> +#ifndef __str__ +#define __str__ +//#define init_str(buffer) (buffer); \ + {buffer = (char*)malloc(sizeof(char));\ + buffer[0]='\0';} +//#define str_size(str,sizeb) (sizeb); \ + for(sizeb=0;;sizeb++)if(str[sizeb]=='\0')break; +//#define str_push(str,ch)\ + int size_y = str_size(str);\ + str = (char*)realloc(str,sizeof(char*)*(size_y+1));\ + str[size_y]=ch;\ + str[size_y+1]='\0'; +typedef struct { + char*str; + int len; +}str; +str* str_init(); +int str_size(str*str); +int ca_size(char*str); +void str_pushc(str*str,char ch); +void str_pushca(str*str,char*ins); +int str_cmp_str(str*,str*); +int str_cmp_ca(str*,char*); +void str_free(str*str); +str* str_init_only_str(str*str); +void str_clear(str*str); +#endif diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..e256dc1 --- /dev/null +++ b/src/util.c @@ -0,0 +1,112 @@ +#include <stdio.h> +#include <stdlib.h> +#include "util.h" +#include "strings.h" +int log_level = 0; +int __signal = 0; +void pexit(int s){ + __signal = s; + exit(s); +} +void sig_handle(void){ + if(__signal==0){ + printf("\x1b[90mexited with \x1b[32m\x1b[1msignal [ %i ] \x1b[0m\x1b[90mgraceful exit\x1b[0m\n",__signal); + } else if(__signal>0){ + printf("\x1b[90mexited with \x1b[31m\x1b[1msignal [ %i ] \x1b[0m\x1b[90mgraceful exit\x1b[0m\n",__signal); + //extra cleanup if needed + } else { + printf("\x1b[90mexited with \x1b[31m\x1b[1msignal [ %i ] \x1b[0m\x1b[90mnon-graceful exit\x1b[0m\n",__signal); + } +} +unsigned int_len(const unsigned n) { + if (n < 10) return 1; + return 1 + int_len(n / 10); +}//https://stackoverflow.com/a/3068415 +char* force_ca_length(char*inp,int len){ + char* nya = malloc(sizeof(*nya)*(len+1)); + int skip = 0; + for(int i = 0;; i++){ + + if((inp[i]=='\0'||skip)&&i>=len) + break; + if(inp[i]=='\0') + skip=1; + if(i>=len){ + for(int y = 1; y<=len; y++) + nya[y-1] = nya[y]; + nya[len-1] = inp[i]; + continue; + } + if(skip) + nya[i] = ' '; + else + nya[i] = inp[i]; + } + if(!skip){ + nya[2] = '.'; + nya[1] = '.'; + nya[0] = '.'; + } + nya[len+1]='\0'; + return nya; +} +void err_m(char*ca,void (*cb)(int),char*f,int l){ + if(log_level!=-1){ + int len = ca_size(f) + int_len(l); + char nn[len]; + sprintf(nn,"%s:%i",f,l); + char* aa = force_ca_length(nn,15); + printf("\x1b[90m%s \x1b[31m[ !err ]\x1b[0m %s\n",aa,ca); + free(aa); + } + cb(1); +} +void warn_m(char*ca,char*f,int l,...){ + if(log_level==-1) + return; + int len = ca_size(f) + int_len(l); + char nn[len]; + sprintf(nn,"%s:%i",f,l); + char* aa = force_ca_length(nn,15); + printf("\x1b[90m%s \x1b[33m[ warn ]\x1b[0m %s\n",aa,ca); + free(aa); +} +void info_m(char*ca,char*f,int l,...){ + if(log_level<1) + return; + int len = ca_size(f) + int_len(l); + char nn[len]; + sprintf(nn,"%s:%i",f,l); + char* aa = force_ca_length(nn,15); + printf("\x1b[90m%s [ info ] %s\x1b[0m\n",aa,ca); + free(aa); +} +void log_m(char*ca,char*f,int l,...){ + if(log_level<0) + return; + int len = ca_size(f) + int_len(l); + char nn[len]; + sprintf(nn,"%s:%i",f,l); + char* aa = force_ca_length(nn,15); + printf("\x1b[35m%s [ log ] \x1b[0m%s\n",aa,ca); + free(aa); +} +void flag_handle(int argc,char* argv[]){ + for(int i = 0; i<argc;i++){ + if(argv[i][0]=='-'){ + //partial + for(int y = 1;;y++){ + if(argv[i][y]=='\0') + break; + switch(argv[i][y]){ + case 'q': + log_level=-1; + break; + case 'd':case 'v': + log_level=2; + break; + } + } + } + } +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..a1bd599 --- /dev/null +++ b/src/util.h @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#ifndef __util__ +#define __util__ +#include "string.h" +void err_m(char*,void (*)(int),char*,int); +void warn_m(char*,char*,int ,...); +void info_m(char*,char*,int ,...); +void log_m(char*ca,char*f,int l,...); +void flag_handle(int argc,char* argv[]); +void sig_handle(void); +unsigned int_len(const unsigned n); +char* force_ca_length(char*inp,int len); +void pexit(int s); + +#define err(s,f,...) err_m(s,f,__FILE__,__LINE__,##__VA_ARGS__); +#define warn(s) warn_m(s,__FILE__,__LINE__); +#define info(s) info_m(s,__FILE__,__LINE__); +#define logm(s) log_m(s,__FILE__,__LINE__); + +#endif |