aboutsummaryrefslogtreecommitdiff
path: root/src/point.c~
diff options
context:
space:
mode:
Diffstat (limited to 'src/point.c~')
-rw-r--r--src/point.c~558
1 files changed, 172 insertions, 386 deletions
diff --git a/src/point.c~ b/src/point.c~
index 8ae9b55..d587fb6 100644
--- a/src/point.c~
+++ b/src/point.c~
@@ -7,12 +7,49 @@
double NUU = 0.0;
double FL_DIS = 1e-10;
double NaN = 0.0f/0.0f;
+
typedef struct {
double x;
double y;
double z;
int vertex;
} cord;
+cord poi_d(double x1,double y1,double x2, double y2,int len,GLfloat* pixels){
+ double m1 = (y2-y1)/(x2-x1);
+ double b1 = y1 - m1 * x1;
+ cord aa;
+ aa.x = 0;
+ aa.y = 0;
+ aa.z = -1;
+
+ int coll = 0;
+ for(int yyu = 0; yyu!=len-1; yyu++){
+
+ double x3 = pixels[yyu*2];
+ double x4 = pixels[(yyu+1)*2];
+ double y3 = pixels[yyu*2+1];
+ double y4 = pixels[(yyu+1)*2+1];
+ double m2 = (y4-y3)/(x4-x3);
+
+ double b2 = y3 - m2 * x3;
+
+ double nsx = (b2-b1)/(m1-m2);
+ double nsy = m1*nsx+b1;
+
+ if(!(nsx >= greater(lesser(x1, x2), lesser(x3, x4)) && nsx <= lesser(greater(x1, x2), greater(x3, x4)))
+
+ ||(diff(nsx,x2)<FL_DIS&&diff(nsy,y2)<FL_DIS)
+ ||(diff(nsx,x1)<FL_DIS&&diff(nsy,y1)<FL_DIS))
+ continue;
+ aa.x = nsx;
+ aa.y = nsy;
+ aa.z = 1;
+ coll=1;
+ break;
+ }
+ return aa;
+}
+
typedef struct {
float r;
float g;
@@ -83,12 +120,7 @@ GLuint build_shader(GLuint vertid, GLuint fragid){
glLinkProgram(progid);
return progid;
}
-point_arr* basier2d(double*xx,double*yy,int n,float rr, float gg, float bb){
-
- //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;
+point_arr* basier2d(double*xx,double*yy,int n,float rr, float gg, float bb){
n-=1;
@@ -111,13 +143,8 @@ point_arr* basier2d(double*xx,double*yy,int n,float rr, float gg, float bb){
for(int i = 0; i <=n;i++){
double pp = binomial(n,i) * pow((1 - t),(n - i)) * pow(t,i);
bcx += pp * xx[i];
- bcy += pp * yy[i];
- //if(ii==3){
- // printf("pp: %f bi : %f p1 : %f p2 : %f| %f, %f, %f\n",pp,binomial(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);
+ bcy += pp * yy[i];
+ }
pa->c[iy].at.x = bcx;
pa->c[iy].at.y = bcy;
pa->c[iy].at.vertex = 0;
@@ -130,7 +157,6 @@ point_arr* basier2d(double*xx,double*yy,int n,float rr, float gg, float bb){
pa->c[iy].color.r = rr;
pa->c[iy].color.g = gg;
pa->c[iy].color.b = bb;
- //printf("(%f,%f,%f)\n",bcx,bcy,bcz);
}
for(int i = 0; i<=n; i++){
pa->vert[i].at.x = xx[i];
@@ -139,30 +165,44 @@ point_arr* basier2d(double*xx,double*yy,int n,float rr, float gg, float bb){
pa->vlen = n;
return pa;
}
-void perspective_proj(GLFWwindow* b,point_arr* c,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;
- //printf("%i\n",glGetError());
- //glColor3f(1.0f,0.0f,0.0f);
- //GLfloat* pixels = malloc(sizeof(*pixels)*c->len*5);
- //printf("s\n");
- //GLfloat pixels[(int)ceil(c->len*3)];
- //GLfloat colors[(int)ceil(c->len*3)];
+typedef struct {
+ GLfloat* pix;
+ GLfloat* col;
+ int len;
+} glfl_ar;
+void render_p(glfl_ar* bba){
+ GLuint verta;
+ glGenVertexArrays(1,&verta);
+ glBindVertexArray(verta);
+
+ GLuint vetb;
+ glGenBuffers(1,&vetb);
+ glBindBuffer(GL_ARRAY_BUFFER,vetb);
+ glBufferData(GL_ARRAY_BUFFER,sizeof(*bba->pix)*(bba->len*3),bba->pix,GL_STATIC_DRAW);
+
+ glEnableVertexAttribArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER,vetb);
+ glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,(void*)0);
+
+ GLuint colb;
+ glGenBuffers(1,&colb);
+ glBindBuffer(GL_ARRAY_BUFFER,colb);
+ glBufferData(GL_ARRAY_BUFFER,sizeof(*bba->col)*(bba->len*4),bba->col,GL_STATIC_DRAW);
+
+ glEnableVertexAttribArray(1);
+ glBindBuffer(GL_ARRAY_BUFFER,colb);
+ glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void*)0);
+
+ glDrawArrays(GL_POINTS,0,bba->len);
+ glDeleteBuffers(1,&vetb);
+ glDeleteBuffers(1,&colb);
+}
+glfl_ar* perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ctz,double cx, double cy, double cz){
GLfloat* pixels = malloc(sizeof(*pixels)*((1+c->len)*4));
- //GLfloat* abpixels = malloc(sizeof(*abpixels)*(c->len*3));
GLfloat* colors = malloc(sizeof(*colors)*((1+c->len)*4));
- //GLfloat* colors = malloc(sizeof(*colors)*c->len*4);
if(pixels==NULL||colors==NULL)
err("failed to allocate perspective array:(",pexit);
-
-
- //glfwSwapBuffers(b);
- //return;
+
double coy = cos(cty);
double siz = sin(ctz);
double coz = cos(ctz);
@@ -171,22 +211,14 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
double cox = cos(ctx);
double fov = 0.002;
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);
- //printf("e\n");
- //printf("---\n");
+ double ey = cy;
+ refresh_size(b);
GLuint fb = 0;
int c_len = 0;
//double ez=1/tan(fov/2); //i dont get this at all
double ez=get_w()*2;
- //glGenFramebuffers(1,&fb);
glEnableClientState(GL_VERTEX_ARRAY);
- //glEnableClientState(GL_VERTEX_ARRAY);
- //glEnableClientState(GL_COLOR_ARRAY);
+
for(int i = 0; i!=c->len; i++){
double ax = c->c[i].at.x;
double ay = c->c[i].at.y;
@@ -197,42 +229,20 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
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);
+ double by = ez/dz*dy+dy;
if(dz>-1){
ab_to_vp(xa,ya,get_w(),get_h(),bx,by);
- //printf("%f\n",dz);
- //return;
- //printf("%i:%f %f | %f %f\n",i*2,xa,ya,bx,by);
- //return;
- //if(c->c[i].at.vertex&&i==4800)
- // printf("%i : %f:%f %f:%f\n",i,bx,xa,by,ya);
pixels[c_len*2] = xa+1;
pixels[c_len*2+1] = ya;
- //abpixels[i*2] = bx;
- //abpixels[i*2+1] = by;
colors[c_len*3] = c->c[i].color.r;
colors[c_len*3+1] = c->c[i].color.g;
colors[c_len*3+2] = c->c[i].color.b;
c_len++;
- }/* else {
- pixels[i*2] = -20;
- pixels[i*2+1]= -20;
- //abpixels[i*2] = -20;
- //abpixels[i*2+1]= -20;
- colors[i*3] = 0.0f;
- colors[i*3+1] = 1.0f;
- colors[i*3+2] = 0.0f;
- }*/
- //glfw_circle_partial(b,nearbyint(bx),nearbyint(by),/*(0>=aaa?1:*/1/*)*/);
- //glfw_pixel_partial(b,round(bx),round(by));
+ }
}
- //int c_len = c->len;
- //connect all vertcies
+
double fc_len = c_len;
for(int i = 0; i<=fc_len-1; i++){
double x22[2] = {pixels[i*2],pixels[(i+1)*2]};
@@ -252,48 +262,35 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
free(bas->vert);
free(bas);
}
- //c->len = c_len;
double dclen = c_len;
- //highlight vertic
int vvi = 0;
- //printf("l:%i\n",c_len);
- //printf("%i\n",c_len);
glfl_m* trline = malloc(sizeof(*trline)*get_w()*30);
trline->len = 0;
- //printf("ss\n");
trline->at = malloc(sizeof(*trline->at)*get_w()*40);
for(int i = 0; i<=c->len; i++){
if(c->c[i].at.vertex==1){
if(pixels==NULL||colors==NULL)
abort();
vvi++;
- //printf("%i\n",i);
- //printf("%i\n",i);
- //printf("%i : %f %f\n",i,pixels[i*2],pixels[i*2+1]);
- //trline.at = realloc(trline.at,sizeof(*trline.at)*(vvi+1));
- //printf("1\n");
- //trline->at[trline->len].at = malloc(sizeof(*trline->at[trline->len].at)*(get_w()+(trline->len+1))*20);
+
trline->at[trline->len].at = malloc(sizeof(*trline->at[trline->len].at)*(c_len+get_w()*2)*30);
trline->at[trline->len].len = 0;
- //printf("2\n");
double ttt = -1.0;
int p_b = 0;
int p_b2 = 0;
int p_b3 = 0;
int p_b4 = 0;
double ttt2 = 1.0;
- //printf("%f\n",NUU);
int found1 = 1;
int found2 = 1;
- for(int jj = 0; jj<=dclen-1; jj++){
+
+ for(int jj = 0; jj<=dclen-1; jj++){
float sad = 3.0f;
float sad2 = 30.0f;
if(pixels[jj*2]<pixels[i*2]&&diff(pixels[jj*2],pixels[i*2])>(float)sad2/get_w()
&&diff(pixels[jj*2+1],pixels[i*2+1])<(float)sad/get_w()&&i!=jj){
- //printf("%f %f\n",ttt,pixels[jj*2]);
- //if(ttt<pixels[jj*2])
p_b=1;
if(pixels[jj*2]>ttt)
ttt=pixels[jj*2];
@@ -301,17 +298,12 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
}
if(diff(pixels[jj*2],pixels[i*2])>(float)sad2/get_w()
&&diff(pixels[jj*2+1],pixels[i*2+1])<(float)sad/get_w()&&i!=jj&&
- pixels[jj*2]>pixels[i*2]){
- //printf("%f %f\n",ttt,pixels[jj*2]);
- //if(ttt<pixels[jj*2])
+ pixels[jj*2]>pixels[i*2]){
p_b2=1;
- //printf("sss\n");
- //printf("%i/%f, %f %f\n",jj,c->len,pixels[jj*2],ttt2);
- //printf("aaa\n");
+
if(pixels[jj*2]<ttt2)
ttt2=pixels[jj*2];
- //printf("lll\n");
-
+
}
if(diff(pixels[jj*2+1],pixels[i*2+1])>(float)sad/get_w()
&&diff(pixels[jj*2],pixels[i*2])<(float)sad2/get_w()&&i!=jj&&pixels[jj*2+1]>pixels[i*2+1]){
@@ -323,38 +315,25 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
}
}
- //printf("%i\n",trline->len);
- //printf("aaaa\n");
+
if(!p_b3||!p_b4){
free(trline->at[trline->len].at);
- //printf("fffff\n");
+
continue;
}
- //printf("aaa\n");
- //printf("%f %f\n",ttt,ttt2);
- //printf("%i : %f %f\n",i,ttt,ttt2);
- //printf("3\n");
- // printf("%f\n",NUU);
+
double cb = 1.0;
double sb = 1.0/get_w();
double ib = cb/get_w();
if(p_b)
- for(double zz = pixels[i*2]; diff(zz,ttt)>ib&&zz>-1;zz-=(float)sb){
- //printf("4 %lu\n",sizeof *pixels *((c_len+1)*3));
- //trline->at[trline->len].at = realloc(trline->at[trline->len].at,sizeof(*trline->at[trline->len].at)*(trline->at[trline->len].len+1)*30);
- pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
- //printf("4.5\n");
+ for(double zz = pixels[i*2]; diff(zz,ttt)>ib&&zz>-1;zz-=(float)sb){
+ pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
- int brea = 0;
- //printf("5\n");
trline->at[trline->len].at[trline->at[trline->len].len*2] = zz;
- //printf("6\n");
trline->at[trline->len].at[trline->at[trline->len].len*2+1] = pixels[i*2+1];
- //printf("7\n");
pixels[c_len*2] = zz;
pixels[c_len*2+1] = pixels[i*2+1];
- //printf("8\n");
colors[c_len*3] = 0.1f;
colors[c_len*3+1] = vvi==3?0.1f:vvi==4?0.5f:1.0f;
colors[c_len*3+2] = 1.0f;
@@ -362,17 +341,15 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
trline->at[trline->len].len++;
- }
- //printf("4\n");
+ }
if(p_b2)
for(double zz = pixels[i*2]; diff(zz,ttt2)>ib&&zz<1;zz+=(float)sb){
- //trline->at[trline->len].at = realloc(trline->at[trline->len].at,sizeof(*trline->at[trline->len].at)*(trline->at[trline->len].len+1)*30);
+
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
int brea = 0;
trline->at[trline->len].at[trline->at[trline->len].len*2] = zz;
trline->at[trline->len].at[trline->at[trline->len].len*2+1] = pixels[i*2+1];
- //printf("%f %f\n",zz,ttt2);
pixels[c_len*2] = zz;
pixels[c_len*2+1] = pixels[i*2+1];
colors[c_len*3] = 0.1f;
@@ -387,30 +364,20 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
trline->len++;
}
}
- //printf("aa\n");
- //printf(">>>\n");
+
if(trline->len>1){
- for(int ii = 0; ii!=trline->len-1; ii++){
- //printf("[%i] %f %i < %f %i\n",ii,trline->at[ii].at[3],trline->at[ii].len,trline->at[ii+1].at[3],trline->at[ii+1].len);
+ for(int ii = 0; ii!=trline->len-1; ii++){
if(trline->at[ii].at[3]<trline->at[ii+1].at[3]){
- //printf("swapped\n");
glfl_a temp = trline->at[ii];
trline->at[ii] = trline->at[ii+1];
trline->at[ii+1] = temp;
ii=-1;
}
- }}/*
- printf("---\n");
- for(int ii = 0; ii!=trline->len;ii++){
- printf("%f %i\n",trline->at[ii].at[3],trline->at[ii].len);
- }*/
+ }}
double ffclen = c_len;
- //printf("---\n");
- //printf("%i\n",trline->len);
- double ite = ((float)get_w()/4);
- double lmax_t = -2.0;
+ double lmax_t = -2.0;
double lmin_t = 2.0;
double lmax2_t = -2.0;
double lmin2_t = 2.0;
@@ -420,7 +387,6 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
double max2_t = -2.0;
double min2_t = 2.0;
for(int zzi2 = 0; zzi2!=trline->at[zzi].len;zzi2++){
- //printf("(%f %f):\n",trline.at[zzi].at[zzi2*2],trline.at[zzi].at[zzi2*2+1]);
if(max_t<trline->at[zzi].at[zzi2*2]){
max_t = trline->at[zzi].at[zzi2*2];
max2_t = trline->at[zzi].at[zzi2*2+1];
@@ -429,15 +395,14 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
min_t = trline->at[zzi].at[zzi2*2];
min2_t = trline->at[zzi].at[zzi2*2+1];
}
- //min_t = (min_t>trline.at[zzi].at[zzi2]?trline.at[zzi].at[zzi2]:min_t);
+
}
- // printf("%i\n",zzi);
+
free(trline->at[zzi].at);
- //printf("lls\n");
if (trline->at[zzi].len == 0)
continue;
if(lmax2_t!=max2_t&&lmin_t!=2.0&&lmax_t!=-2.0){
- //printf("ll\n");
+
float color = (float)zzi/trline->len;
float color2 = 1.0f-((float)zzi/trline->len);
double di = pow(lmin_t-max_t,2)+pow(lmin2_t-max2_t,2);//sqrt should be used here
@@ -456,12 +421,13 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
ux2 = lmax_t;
uy2 = lmax2_t;
}
- //printf("%f %f >> %f %f (f,%f) | %f %f\n",di,di2,ux1,uy1,min_t,ux2,uy2);
+
color2 = 1.0f;
double x1 = ma_to_mi?max_t:min_t;
double x2 = ma_to_mi?lmin_t:lmax_t;
-
+ double ite = ((float)get_w()/4);
+
double ite1 = fabs(max_t/ite);
double ite2 = fabs(lmax_t/ite);
@@ -470,58 +436,20 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
for(;(ma_to_mi?x1>=x2:x1<=x2);(ma_to_mi?(x1-=ite1):(x1+=ite2))){
- double m1 = (y2-y1)/(x2-x1);
- double b1 = y1 - m1 * x1;
-
-
- int coll = 0;
- for(int yyu = 0; yyu!=fc_len-1; yyu++){
-
- double x3 = pixels[yyu*2];
- double x4 = pixels[(yyu+1)*2];
- double y3 = pixels[yyu*2+1];
- double y4 = pixels[(yyu+1)*2+1];
- double m2 = (y4-y3)/(x4-x3);
-
- double b2 = y3 - m2 * x3;
-
- double nsx = (b2-b1)/(m1-m2);
- double nsy = m1*nsx+b1;
-
- if(!(nsx >= greater(lesser(x1, x2), lesser(x3, x4)) && nsx <= lesser(greater(x1, x2), greater(x3, x4)))
- //||(nsx<x1||nsx>x2||nsy<y1||nsy>y2)
- ||(diff(nsx,x2)<FL_DIS&&diff(nsy,y2)<FL_DIS)
- ||(diff(nsx,x1)<FL_DIS&&diff(nsy,y1)<FL_DIS))
- continue;
- /*
- pixels = realloc(pixels,sizeof *pixels *((c_len+1)*4));
- colors = realloc(colors,sizeof *colors *((c_len+1)*5));
- pixels[c_len*2] = nsx;
- pixels[c_len*2+1] = nsy;
- colors[c_len*3] = 1.0f;//yyu/(fc_len-1);
- colors[c_len*3+1] = 0.5f;//vvi==3?0.1f:vvi==4?0.5f:1.0f;
- colors[c_len*3+2] = 1.0f;//-yyu/(fc_len-1);
- c_len++;*/
- coll=1;
- break;
- //printf("aaa\n");
- //printf("%f,%f,%f,%f,%f,%f %f,%f,%f,%f,%f,%f, %f %f\n",x1,y1,x2,y2,m1,b1, x3,y3,x4,y4,m2,b2,nsx,nsy);
- //printf("(%f,%f) -> (%f,%f) :: x:%f,y:%f\n",ux1,uy1,ux2,uy2,nsx,nsy);
- }
+ cord aaaa = poi_d(x1,y1,x2,y2,fc_len,pixels);
- if(coll){
+ if(aaaa.z==1){
continue;
}
double bb[] = {x1,x2};
double bb2[] = {y1, y2};
point_arr* asd = basier2d(bb,bb2,2,0.1,0.1,0.1);
- for(int lli = 0; !coll&&lli!=asd->len; lli++){
+ for(int lli = 0; lli!=asd->len; lli++){
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*4));
colors = realloc(colors,sizeof *colors *((c_len+1)*5));
double dd = 10;
- //if(diff(asd->c[lli].at.x,ex)<(float)dd/get_w()&&diff(asd->c[lli].at.y,ey)<(float)dd/get_w())
- // break;
+
pixels[c_len*2] = asd->c[lli].at.x;
pixels[c_len*2+1] = asd->c[lli].at.y;
colors[c_len*3] = color2;
@@ -534,113 +462,23 @@ void perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ct
free(asd);
break;
}
- /*
- pixels = realloc(pixels,sizeof *pixels *((c_len+1)*4));
- colors = realloc(colors,sizeof *colors *((c_len+1)*5));
- //ab_to_vp(nn1,nn2,get_w(),get_h(),abba->c[hhi].at.x,abba->c[hhi].at.y)
- //printf("%f->%f %f->%f\n",abba->c[hhi].at.x,nn1,abba->c[hhi].at.y,nn2);
- pixels[c_len*2] = ux1;
- pixels[c_len*2+1] = uy1;
- colors[c_len*3] = color2;
- colors[c_len*3+1] = color;//vvi==3?0.1f:vvi==4?0.5f:1.0f;
- colors[c_len*3+2] = 0.0f;
- c_len++;
-
- pixels = realloc(pixels,sizeof *pixels *((c_len+1)*4));
- colors = realloc(colors,sizeof *colors *((c_len+1)*5));
- //ab_to_vp(nn1,nn2,get_w(),get_h(),abba->c[hhi].at.x,abba->c[hhi].at.y)
- //printf("%f->%f %f->%f\n",abba->c[hhi].at.x,nn1,abba->c[hhi].at.y,nn2);
- pixels[c_len*2] = ux2;
- pixels[c_len*2+1] = uy2;
- colors[c_len*3] = color2;
- colors[c_len*3+1] = color;//vvi==3?0.1f:vvi==4?0.5f:1.0f;
- colors[c_len*3+2] = 0.0f;
- c_len++;*/
+
}
lmax_t = max_t;
lmin_t = min_t;
lmax2_t = max2_t;
lmin2_t = min2_t;
}
- //printf("done\n");
free(trline->at);
free(trline);
- //return;
glPointSize(4.0f);
- //glUseProgram(prog);
- //glVertexAttribPointer(0,3,GL_FLOAT,0, 7*4,0);
- //glVertexAttribPointer(3,4,GL_FLOAT,0, 7*4,3*4);
- //GLint posAttrib = glGetAttribLocation(0,"position");
- //GLuint vertbuff;
- //glGenBuffers(1,&vertexBufferObject);
- //for(int i = 0; i!=c->len*3; i++){
- // colors[i] = 1.0f;
- //}
- /*GLint uni = glGetUniformLocation(prog,"r");
- glUniform1f(uni,1.0);
- uni = glGetUniformLocation(prog,"g");
- glUniform1f(uni,0.2);
- uni = glGetUniformLocation(prog,"b");
- glUniform1f(uni,0.3);*/
- /*glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE, c->len/2*sizeof(float),(void*)0);
- glEnableVertexAttribArray(0);
- int aaa3 = c->len/2*sizeof(float);
- glVertexAttribPointer(2,5,GL_FLOAT,GL_FALSE, c->len/2*sizeof(float),(void*)(&aaa3));
- glEnableVertexAttribArray(1);*/
- /* GLuint colorb;
- glGenBuffers(1,&colorb);
- glBindBuffer(GL_ARRAY_BUFFER,colorb);
- glBufferData(GL_ARRAY_BUFFER,sizeof(colors),colors,GL_STATIC_DRAW);
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER,colorb);
- glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void*)0);
-*/// return;
- GLuint verta;
- glGenVertexArrays(1,&verta);
- glBindVertexArray(verta);
-
- GLuint vetb;
- glGenBuffers(1,&vetb);
- glBindBuffer(GL_ARRAY_BUFFER,vetb);
- glBufferData(GL_ARRAY_BUFFER,sizeof(*pixels)*(c_len*3),pixels,GL_STATIC_DRAW);
-
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER,vetb);
- glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,(void*)0);
-
- GLuint colb;
- glGenBuffers(1,&colb);
- glBindBuffer(GL_ARRAY_BUFFER,colb);
- glBufferData(GL_ARRAY_BUFFER,sizeof(*colors)*(c_len*4),colors,GL_STATIC_DRAW);
-
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER,colb);
- glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void*)0);
+ glfl_ar* rea = malloc(sizeof(*rea));
+ rea->col = colors;
+ rea->pix = pixels;
+ rea->len = c_len;
- glDrawArrays(GL_POINTS,0,c_len);
- glDeleteBuffers(1,&vetb);
- glDeleteBuffers(1,&colb);
- free(pixels);
- free(colors);
- //free(abpixels);
- //printf("aa\n");
- //glDisableVertexAttribArray(0);
- //glDisableVertexAttribArray(1);
- //glVertexPointer(2,GL_FLOAT,0,pixels);
- //glVertexPointer(3,GL_FLOAT,0,colors);
- //glDrawArrays(GL_POINTS,0,c->len);
- //glDisableClientState(GL_VERTEX_ARRAY);
- //glDisableClientState(GL_COLOR_ARRAY);
- //free(pixels);
- //free(colors);
- /*for(int i = 0; i!=get_w();i++){
- glfw_pixel(b,i,get_h()/2);
- }
- for(int i = 0; i!=get_h();i++){
- glfw_pixel(b,get_w()/2,i);
- }*/
- //glEnd();
+ return rea;
}
point_arr* basier3d(double*xx,double*yy,double*zz,int n,float rr, float gg, float bb){
point_arr* pa;
@@ -649,13 +487,7 @@ point_arr* basier3d(double*xx,double*yy,double*zz,int n,float rr, float gg, floa
pa->vert = malloc(sizeof(*pa->vert)*(n*60));
if(pa->c==NULL||pa->vert==NULL)
- err("failed to allocate basier array",pexit);
-
- //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;
-
+ err("failed to allocate basier array",pexit);
n-=1;
@@ -671,13 +503,8 @@ point_arr* basier3d(double*xx,double*yy,double*zz,int n,float rr, float gg, floa
double pp = binomial(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,binomial(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);
+ bcz += pp * zz[i];
+ }
pa->c[iy].at.x = bcx;
pa->c[iy].at.y = bcy;
pa->c[iy].at.z = bcz;
@@ -691,27 +518,16 @@ point_arr* basier3d(double*xx,double*yy,double*zz,int n,float rr, float gg, floa
pa->c[iy].color.r = rr;
pa->c[iy].color.g = gg;
pa->c[iy].color.b = bb;
- //printf("(%f,%f,%f)\n",bcx,bcy,bcz);
}
for(int i = 0; i<=n; i++){
pa->vert[i].at.x = xx[i];
pa->vert[i].at.y = yy[i];
pa->vert[i].at.z = zz[i];
}
- pa->vlen = n;
- //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);
+ pa->vlen = n;
return pa;
}
void join_cords(point_arr* a, point_arr* b){
- //printf("%lu\n",sizeof(*a)*(a_len+b_len+2));
int a_len = a->len;
int a_vlen = a->vlen;
a->c = realloc(a->c,sizeof(*a->c)*(a->len+b->len)*60);
@@ -721,37 +537,66 @@ void join_cords(point_arr* a, point_arr* b){
if(a->c==NULL)
err("failed to reallocate cords",pexit);
for(int i = 0; i<=b->len; i++){
- //printf("%i/%f\n",i,b->len);
a->c[a_len+i].at = b->c[i].at;
a->c[a_len+i].color = b->c[i].color;
}
for(int i = 0; i<=b->vlen; i++){
- //printf("%i/%f\n",i,b->len);
- a->vert[a_vlen+i].at = b->vert[i].at;
- //a->vert[a_len+i].color = b->vert[i].color;
+ a->vert[a_vlen+i].at = b->vert[i].at;
+ }
+}
+void join_glfl_a(glfl_ar* a, glfl_ar* b){
+ int a_len = a->len;
+ a->pix = realloc(a->pix,sizeof(*a->pix)*(a->len+b->len)*60);
+ a->col = realloc(a->col,sizeof(*a->col)*(a->len+b->len)*60);
+
+ a->len+=b->len;
+
+ if(a->pix==NULL||a->col==NULL)
+ err("failed to reallocate float array",pexit);
+ for(int i = 0; i<=b->len*2; i++){
+ a->pix[a_len*2+i] = b->pix[i];
+ }
+ for(int i = 0; i<=b->len*3; i++){
+ a->col[a_len*3+i] = b->col[i];
}
- //return a.len+b.len;
+}
+point_arr* polygon3d(double* vx, double*vy, double* vz, int n){
+ double xx[2] = {vx[0],vx[1]};
+ double yy[2] = {vy[0],vy[1]};
+ double zz[2] = {vz[0],vz[1]};
+ point_arr* y = basier3d(xx,yy,zz,2,1.0f,0.0f,0.5f);
+ for(int i = 1; i<=n-2; i++){
+ double xx1[2] = {vx[i],vx[i+1]};
+ double yy1[2] = {vy[i],vy[i+1]};
+ double zz1[2] = {vz[i],vz[i+1]};
+ point_arr* aa = basier3d(xx1,yy1,zz1,2,1.0f,0.0f,0.5f);
+ join_cords(y,aa);
+ free(aa->c);
+ free(aa->vert);
+ free(aa);
+ }
+ return y;
}
point_arr* square_gen(double* tl, double* tr, double* bl, double*br,float rr, float gg, float bb){
double xx[3] = {tl[0],tr[0]};
double yy[3] = {tl[1],tr[1]};
double zz[3] = {tl[2],tr[2]};
- point_arr* a = basier3d(xx,yy,zz,2,rr,gg,bb);
+ point_arr* a = polygon3d(xx,yy,zz,2);
double xx1[3] = {tl[0],bl[0]};
double yy1[3] = {tl[1],bl[1]};
double zz1[3] = {tl[2],bl[2]};
- point_arr* b = basier3d(xx1,yy1,zz1,2,rr,gg,bb);
+ point_arr* b = polygon3d(xx1,yy1,zz1,2);
double xx2[3] = {tr[0],br[0]};
double yy2[3] = {tr[1],br[1]};
double zz2[3] = {tr[2],br[2]};
- point_arr* c = basier3d(xx2,yy2,zz2,2,rr,gg,bb);
+ point_arr* c = polygon3d(xx2,yy2,zz2,2);
double xx3[3] = {bl[0],br[0]};
double yy3[3] = {bl[1],br[1]};
double zz3[3] = {bl[2],br[2]};
- point_arr* d = basier3d(xx3,yy3,zz3,2,rr,gg,bb);
+ point_arr* d = polygon3d(xx3,yy3,zz3,2);
join_cords(a,b);
join_cords(a,c);
@@ -768,7 +613,6 @@ point_arr* square_gen(double* tl, double* tr, double* bl, double*br,float rr, fl
return a;
}
void fill3d(point_arr* a){
- //printf("AAA");
warn("please dont use this lol");
double m_x = 0.0;
double m_y = 0.0;
@@ -789,18 +633,17 @@ void fill3d(point_arr* a){
mi_y = a->c[i].at.y;
if(a->c[i].at.z<mi_z||mi_z==0)
mi_z = a->c[i].at.z;
- }
- //printf("%f %f %f | %f %f %f\n",m_x,m_y,m_z,mi_x,mi_y,mi_z);
+ }
int a_l = a->len;
a->c = realloc(a->c,sizeof(*a->c)*(m_x*m_y+a->len)*60);
for(double y = mi_y; y<=m_y; y+=.1){
- //printf("%f/%f\n",y,m_y);
+
double* zz = malloc(sizeof(*zz));
int zzl = 0;
double* xx = malloc(sizeof(*xx));
double* yy = malloc(sizeof(*yy));
for(int i = 0; i<=a_l; i++){
- //printf("%i/%f\n",i,a->len);
+
if(diff(a->c[i].at.y,y)<.1){
zz = realloc(zz,sizeof(*zz)*(zzl+1));
zz[zzl] = a->c[i].at.z;
@@ -819,38 +662,12 @@ void fill3d(point_arr* a){
free(yy);
free(zz);
}
- /*for(int i = 0; i!=a->len+m_x*m_y; i++){
- a->c[i].at.x = ;
- a->c[i].at.y = fmod(i,m_y);
- a->c[i].at.z = 20;
- a->c[i].color.r = 0.0f;
- a->c[i].color.g = 1.0f;
- a->c[i].color.b = 0.1f;
- }*/
- /*
- for(int y = mi_y; y<=m_y; y++){
- for(int x = mi_x; x<=m_x; x++){
- a->len++;
- int i = a->len;
- // printf("%i\n",i);
- a->c[a_l+i].at.x = x;
- a->c[a_l+i].at.y = y;
- a->c[a_l+i].at.z = mi_z;
- a->c[a_l+i].color.r = 0.0f;
- a->c[a_l+i].color.g = 1.0f;
- a->c[a_l+i].color.b = 0.1f;
- }
- }
- printf("done\n");
- */
- //join_cords(a,bb);
}
point_arr* cube_gen(double* tl, double* tr, double* bl, double*br,
double* tl2, double* tr2, double* bl2, double*br2,
float rr, float gg, float bb){
point_arr* a = square_gen(tl,tr,bl,br,rr,gg,bb);
- //fill3d(a);
point_arr* b = square_gen(tl2,tr2,bl2,br2,rr,gg,bb);
double s;
join_cords(a,b);
@@ -880,23 +697,6 @@ point_arr* cube_gen(double* tl, double* tr, double* bl, double*br,
free(f);
return a;
}
-point_arr* polygon3d(double* vx, double*vy, double* vz, int n){
- double xx[2] = {vx[0],vx[1]};
- double yy[2] = {vy[0],vy[1]};
- double zz[2] = {vz[0],vz[1]};
- point_arr* y = basier3d(xx,yy,zz,2,1.0f,0.0f,0.5f);
- for(int i = 1; i<=n-2; i++){
- double xx1[2] = {vx[i],vx[i+1]};
- double yy1[2] = {vy[i],vy[i+1]};
- double zz1[2] = {vz[i],vz[i+1]};
- point_arr* aa = basier3d(xx1,yy1,zz1,2,1.0f,0.0f,0.5f);
- join_cords(y,aa);
- free(aa->c);
- free(aa->vert);
- free(aa);
- }
- return y;
-}
int main(int argc,char*argv[]){
flag_handle(argc,argv);
atexit(sig_handle);
@@ -930,15 +730,13 @@ int main(int argc,char*argv[]){
double zz[8] = {50.0,50.0,50.0,50.0,50.0,50.0,50.0,50.0};
point_arr* a = polygon3d(xx,yy,zz,8);
+ double xx2[8] = {0.0, 15.0, 50.0, 60.0,20.0,10.0, 0.0,0.0};
+ double yy2[8] = {5.0, 15.0, 30.0, 20.0,64.0, 45.0,55.0,5.0};
+ double zz2[8] = {50.0,50.0,50.0,50.0,50.0,50.0,50.0,50.0};
+ point_arr* a2 = polygon3d(xx2,yy2,zz2,8);
- /*for(int i = 0;i!=a->vlen;i++){
- printf("%f %f %f\n",a->vert[i].at.x,a->vert[i].at.y,a->vert[i].at.z);
- }*/
- //exit(0);
int max_r = 630;
double half_max_r = (double)max_r/2/2;
- //printf("%s\n",glGetString(GL_VERSION));
- //glfwSetKeyCallback(w,key_press);
double pl_x = 0;
double pl_y = 0;
double pl_z = 0;
@@ -956,13 +754,14 @@ int main(int argc,char*argv[]){
double p5 = -pl_y+pl_z;
double p6 = pl_x;
- perspective_proj(w,a,p1,p2,p3,p4,p5,p6);
-
- glfw_load(w);
- //break;
+ glfl_ar* bba = perspective_proj(w,a,p1,p2,p3,p4,p5,p6);
+ render_p(bba);
+ free(bba->col);
+ free(bba->pix);
+ free(bba);
+ glfw_load(w);
int mod_move=2;
double run_mul=2;
- //usleep(10000);
glfwPollEvents();
if(glfwGetKey(w,GLFW_KEY_R)){
pl_x = 0;
@@ -979,7 +778,6 @@ int main(int argc,char*argv[]){
else
plr_x--;
plr_x = fmod(plr_x,max_r);
- //plr_y = fmod(plr_y,max_r);
}
if(glfwGetKey(w,GLFW_KEY_K)){
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
@@ -987,14 +785,12 @@ int main(int argc,char*argv[]){
else
plr_x++;
plr_x = fmod(plr_x,max_r);
- //plr_y = fmod(plr_y,max_r);
}
if(glfwGetKey(w,GLFW_KEY_J)){
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
plr_y-=mod_move;
else
plr_y--;
- //plr_x = fmod(plr_x,max_r);
plr_y = fmod(plr_y,max_r);
}
if(glfwGetKey(w,GLFW_KEY_L)){
@@ -1002,7 +798,6 @@ int main(int argc,char*argv[]){
plr_y+=mod_move;
else
plr_y++;
- //plr_x = fmod(plr_x,max_r);
plr_y = fmod(plr_y,max_r);
}
@@ -1013,7 +808,6 @@ int main(int argc,char*argv[]){
pl_x+=cosf(plr_y*0.01)*mul;
pl_y+=sinf(plr_y*0.01)*mul;
pl_z-=sinf(plr_x*0.01)*mul;
- //printf("%f\n",pl_z);
}
if(glfwGetKey(w,GLFW_KEY_S)){
double mul = 1;
@@ -1037,24 +831,17 @@ int main(int argc,char*argv[]){
pl_x+=cosf((half_max_r+plr_y)*0.01)*mul;
pl_y+=sinf((half_max_r+plr_y)*0.01)*mul;
}
- /*if(glfwGetKey(w,GLFW_KEY_M))
- NUU+=0.001;
- if(glfwGetKey(w,GLFW_KEY_N))
- NUU-=0.001;
- printf("%f\n",NUU);*/
- //printf("%f %f %f\n",plr_y,cosf(plr_y*0.01),sinf(plr_y*0.01));
+
usleep(1000*1000/60);
glfw_clear(w);
- if(glfwWindowShouldClose(w)||(glfwGetKey(w,GLFW_KEY_Q)))break;
- //t = clock() - t;
+ if(glfwWindowShouldClose(w)||(glfwGetKey(w,GLFW_KEY_Q)))break;
frames+=1;
if(((double)clock() - t)/CLOCKS_PER_SEC > 1){
printf("%f fps\n",frames);
frames=0;
t = clock();
}
- //printf("took %f\n",((double)clock() - t)/CLOCKS_PER_SEC);
}
free(a->c);
free(a->vert);
@@ -1065,6 +852,5 @@ int main(int argc,char*argv[]){
glDeleteShader(fid);
glDeleteShader(prog);
info("killed window:p");
- return 0;
-
+ return 0;
}