mirror of
				https://github.com/zenorogue/hyperrogue.git
				synced 2025-10-31 14:02:59 +00:00 
			
		
		
		
	new renderer for Solv
This commit is contained in:
		| @@ -275,16 +275,15 @@ void display_data::set_projection(int ed) { | |||||||
|       glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |       glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||||||
|       glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |       glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||||||
|       glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); |       glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); | ||||||
|       // unsigned int loc = glGetUniformLocation(_program, "inv_exp_table"); |  | ||||||
|       // glUniform4fv(loc, PRECX*PRECX*PRECZ, &(xbuffer[0])); |  | ||||||
|        |        | ||||||
|       auto xbuffer = new glvertex[PRECZ][PRECX][PRECX]; |       auto xbuffer = new glvertex[PRECZ*PRECY*PRECX]; | ||||||
|        |        | ||||||
|       // for(int y=0; y<PRECX*PRECX*PRECZ; y++) xbuffer[y] = glvertex({inv_exp_table[0][0][y][0]/64, inv_exp_table[0][0][y][1]/64, inv_exp_table[0][0][y][2]/64, 1}); |       ld maxd = 0; | ||||||
|       for(int z=0; z<PRECZ; z++) |       for(int z=0; z<PRECZ*PRECY*PRECX; z++) { | ||||||
|       for(int y=0; y<PRECX; y++) |         auto& t = inverse_exp_table[z]; | ||||||
|       for(int x=0; x<PRECX; x++) |         xbuffer[z] = glhr::makevertex(t[0], t[1], t[2]); | ||||||
|         xbuffer[z][y][x] = glhr::makevertex(inverse_exp_table[z][y][x][0], inverse_exp_table[z][y][x][1], inverse_exp_table[z][y][x][2]); |         } | ||||||
|  |       println(hlog, "maxd = ", maxd); | ||||||
|        |        | ||||||
|       glTexImage3D(GL_TEXTURE_3D, 0, 34836 /*GL_RGBA32F*/, PRECX, PRECX, PRECZ, 0, GL_RGBA, GL_FLOAT, xbuffer); |       glTexImage3D(GL_TEXTURE_3D, 0, 34836 /*GL_RGBA32F*/, PRECX, PRECX, PRECZ, 0, GL_RGBA, GL_FLOAT, xbuffer); | ||||||
|       delete[] xbuffer; |       delete[] xbuffer; | ||||||
| @@ -297,6 +296,10 @@ void display_data::set_projection(int ed) { | |||||||
|     glBindTexture(GL_TEXTURE_3D, invexpid); |     glBindTexture(GL_TEXTURE_3D, invexpid); | ||||||
|  |  | ||||||
|     glActiveTexture(GL_TEXTURE0 + 0); |     glActiveTexture(GL_TEXTURE0 + 0); | ||||||
|  |      | ||||||
|  |     glUniform1f(glhr::current->uPRECX, solv::PRECX); | ||||||
|  |     glUniform1f(glhr::current->uPRECY, solv::PRECY); | ||||||
|  |     glUniform1f(glhr::current->uPRECZ, solv::PRECZ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   auto cd = current_display; |   auto cd = current_display; | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								shaders.cpp
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								shaders.cpp
									
									
									
									
									
								
							| @@ -218,7 +218,7 @@ struct GLprogram { | |||||||
|   GLuint _program; |   GLuint _program; | ||||||
|   GLuint vertShader, fragShader; |   GLuint vertShader, fragShader; | ||||||
|    |    | ||||||
|   GLint uMVP, uFog, uFogColor, uColor, tTexture, tInvExpTable, uMV, uProjection, uAlpha, uFogBase, uILP; |   GLint uMVP, uFog, uFogColor, uColor, tTexture, tInvExpTable, uMV, uProjection, uAlpha, uFogBase, uILP, uPRECX, uPRECY, uPRECZ; | ||||||
|    |    | ||||||
|   GLprogram(string vsh, string fsh) { |   GLprogram(string vsh, string fsh) { | ||||||
|     _program = glCreateProgram(); |     _program = glCreateProgram(); | ||||||
| @@ -277,6 +277,10 @@ struct GLprogram { | |||||||
|     tTexture = glGetUniformLocation(_program, "tTexture"); |     tTexture = glGetUniformLocation(_program, "tTexture"); | ||||||
|     tInvExpTable = glGetUniformLocation(_program, "tInvExpTable"); |     tInvExpTable = glGetUniformLocation(_program, "tInvExpTable"); | ||||||
|  |  | ||||||
|  |     uPRECX = glGetUniformLocation(_program, "PRECX"); | ||||||
|  |     uPRECY = glGetUniformLocation(_program, "PRECY"); | ||||||
|  |     uPRECZ = glGetUniformLocation(_program, "PRECZ"); | ||||||
|  |      | ||||||
|     #if DEBUG_GL |     #if DEBUG_GL | ||||||
|     printf("uniforms: %d %d %d %d\n", uMVP, uFog, uColor, tTexture); |     printf("uniforms: %d %d %d %d\n", uMVP, uFog, uColor, tTexture); | ||||||
|     #endif |     #endif | ||||||
| @@ -651,9 +655,13 @@ void init() { | |||||||
|        |        | ||||||
|       s3,        "vec4 t = uMV * aPosition;", |       s3,        "vec4 t = uMV * aPosition;", | ||||||
|       ssol,      "t = inverse_exp(uILP * t);", |       ssol,      "t = inverse_exp(uILP * t);", | ||||||
|  |       ssol,      "float d = sqrt(t[0] * t[0] + t[1] * t[1] + t[2] * t[2]);", | ||||||
|  |       ssol,      "float ad = (d == 0.) ? 0. : (d < 1.) ? min(atanh(d), 10.) : 10.; ", | ||||||
|  |       ssol,      "float m = ad / d / 11.; t[0] *= m; t[1] *= m; t[2] *= m; ", | ||||||
|  |         | ||||||
|       sh3,       "float fogs = (uFogBase - acosh(t[3]) / uFog);", |       sh3,       "float fogs = (uFogBase - acosh(t[3]) / uFog);", | ||||||
|       sr3,       "float fogs = (uFogBase - sqrt(t[0]*t[0] + t[1]*t[1] + t[2]*t[2]) / uFog);", |       sr3,       "float fogs = (uFogBase - sqrt(t[0]*t[0] + t[1]*t[1] + t[2]*t[2]) / uFog);", | ||||||
|       ssol,      "float fogs = (uFogBase - sqrt(t[0]*t[0] + t[1]*t[1] + t[2]*t[2]) / uFog);", |       ssol,      "float fogs = (uFogBase - ad / uFog);", | ||||||
|        |        | ||||||
|       ss30,      "float fogs = (uFogBase - (6.284 - acos(t[3])) / uFog); t = -t; ", |       ss30,      "float fogs = (uFogBase - (6.284 - acos(t[3])) / uFog); t = -t; ", | ||||||
|       ss31,      "float fogs = (uFogBase - (6.284 - acos(t[3])) / uFog); t.xyz = -t.xyz; ", |       ss31,      "float fogs = (uFogBase - (6.284 - acos(t[3])) / uFog); t.xyz = -t.xyz; ", | ||||||
|   | |||||||
							
								
								
									
										108
									
								
								sol.cpp
									
									
									
									
									
								
							
							
						
						
									
										108
									
								
								sol.cpp
									
									
									
									
									
								
							| @@ -7,24 +7,29 @@ namespace hr { | |||||||
|  |  | ||||||
| namespace solv { | namespace solv { | ||||||
|    |    | ||||||
|   const int PRECX = 64; |   int PRECX, PRECY, PRECZ; | ||||||
|   const int PRECZ = 32; |  | ||||||
|   const ld SXY = 32.; |  | ||||||
|   const ld SZ = 8.; |  | ||||||
|    |    | ||||||
|   typedef hyperpoint pt; |   typedef hyperpoint pt; | ||||||
|   typedef array<float, 3> ptlow; |   typedef array<float, 3> ptlow; | ||||||
|  |  | ||||||
|   ptlow inverse_exp_table[PRECZ][PRECX][PRECX]; |   vector<ptlow> inverse_exp_table; | ||||||
|    |    | ||||||
|   bool table_loaded; |   bool table_loaded; | ||||||
|    |    | ||||||
|  |   string solfname = "soltable-final.dat"; | ||||||
|  |    | ||||||
|  |   pt inverse_exp(pt h); | ||||||
|  |  | ||||||
|   void load_table() { |   void load_table() { | ||||||
|     if(table_loaded) return; |     if(table_loaded) return; | ||||||
|     FILE *f = fopen("soltable.dat", "rb"); |     FILE *f = fopen(solfname.c_str(), "rb"); | ||||||
|     if(!f) f = fopen("/usr/lib/soltable.dat", "rb"); |     // if(!f) f = fopen("/usr/lib/soltable.dat", "rb"); | ||||||
|     if(!f) { addMessage(XLAT("geodesic table missing")); pmodel = mdPerspective; return; } |     if(!f) { addMessage(XLAT("geodesic table missing")); pmodel = mdPerspective; return; } | ||||||
|     fread(inverse_exp_table, sizeof(inverse_exp_table), 1, f); |     fread(&PRECX, 4, 1, f); | ||||||
|  |     fread(&PRECY, 4, 1, f); | ||||||
|  |     fread(&PRECZ, 4, 1, f); | ||||||
|  |     inverse_exp_table.resize(PRECX * PRECY * PRECZ); | ||||||
|  |     fread(&inverse_exp_table[0], sizeof(ptlow) * PRECX * PRECY * PRECZ, 1, f); | ||||||
|     fclose(f); |     fclose(f); | ||||||
|     table_loaded = true;     |     table_loaded = true;     | ||||||
|     } |     } | ||||||
| @@ -57,7 +62,11 @@ namespace solv { | |||||||
|     } |     } | ||||||
|    |    | ||||||
|   pt direct_exp(pt v, int steps) { |   pt direct_exp(pt v, int steps) { | ||||||
|     pt at = hpxy(0, 0); |     pt at; | ||||||
|  |     at[0] = 0; | ||||||
|  |     at[1] = 0; | ||||||
|  |     at[2] = 0; | ||||||
|  |     at[3] = 1; | ||||||
|     v[0] /= steps; |     v[0] /= steps; | ||||||
|     v[1] /= steps; |     v[1] /= steps; | ||||||
|     v[2] /= steps; |     v[2] /= steps; | ||||||
| @@ -89,19 +98,34 @@ namespace solv { | |||||||
|     return hpxy3(x, y, z); |     return hpxy3(x, y, z); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |   ld x_to_ix(ld u) { | ||||||
|  |     if(u == 0.) return 0.; | ||||||
|  |     ld diag = u*u/2.; | ||||||
|  |   | ||||||
|  |     ld x = diag; | ||||||
|  |     ld y = u; | ||||||
|  |     ld z = diag+1.; | ||||||
|  |    | ||||||
|  |     x /= (1.+z); | ||||||
|  |     y /= (1.+z); | ||||||
|  |    | ||||||
|  |     return 0.5 - atan((0.5-x) / y) / M_PI; | ||||||
|  |     } | ||||||
|  |    | ||||||
|   pt inverse_exp(pt h) { |   pt inverse_exp(pt h) { | ||||||
|     load_table(); |     load_table(); | ||||||
|      |      | ||||||
|     ld sx = 1, sy = 1; |     ld ix = h[0] >= 0. ? x_to_ix(h[0]) : -x_to_ix(-h[0]); | ||||||
|     bool nz = false; |     ld iy = h[1] >= 0. ? x_to_ix(h[1]) : -x_to_ix(-h[1]); | ||||||
|  |     ld iz = tanh(h[2]); | ||||||
|      |      | ||||||
|     ld ix = asinh(h[0]) * SXY; |     if(ix < 0.) ix = -ix; | ||||||
|     ld iy = asinh(h[1]) * SXY; |     if(iy < 0.) iy = -iy; | ||||||
|     ld iz = h[2] * SZ / log(2); |     if(iz < 0.) { iz = -iz; ld s = ix; ix = iy; iy = s; } | ||||||
|      |      | ||||||
|     if(ix < 0) ix = -ix, sx = -sx; |     ix *= PRECX-1; | ||||||
|     if(iy < 0) iy = -iy, sy = -sy; |     iy *= PRECY-1; | ||||||
|     if(iz < 0) iz = -iz, nz = true, swap(ix, iy); |     iz *= PRECZ-1; | ||||||
|  |  | ||||||
|     if(ix >= PRECX-1) ix = PRECX-2; |     if(ix >= PRECX-1) ix = PRECX-2; | ||||||
|     if(iy >= PRECX-1) iy = PRECX-2; |     if(iy >= PRECX-1) iy = PRECX-2; | ||||||
| @@ -113,25 +137,22 @@ namespace solv { | |||||||
|      |      | ||||||
|     pt res; |     pt res; | ||||||
|  |  | ||||||
|     // println(hlog, "inv_table", make_tuple(iz,iy,ix), " = ", make_tuple(inverse_exp_table[z][y][x][0], inverse_exp_table[z][y][x][1], inverse_exp_table[z][y][x][2])); |     #define S0(x,y,z) inverse_exp_table[(z*PRECY+y)*PRECX+x][t] | ||||||
|      |  | ||||||
|     #define S0(x,y,z) inverse_exp_table[z][y][x][t] |  | ||||||
|     #define S1(x,y) (S0(x,y,az) * (bz-iz) + S0(x,y,bz) * (iz-az)) |     #define S1(x,y) (S0(x,y,az) * (bz-iz) + S0(x,y,bz) * (iz-az)) | ||||||
|     #define S2(x) (S1(x,ay) * (by-iy) + S1(x,by) * (iy-ay)) |     #define S2(x) (S1(x,ay) * (by-iy) + S1(x,by) * (iy-ay)) | ||||||
|  |  | ||||||
|     for(int t=0; t<3; t++) |     for(int t=0; t<3; t++) | ||||||
|       res[t] = S2(ax) * (bx-ix) + S2(bx) * (ix-ax); |       res[t] = S2(ax) * (bx-ix) + S2(bx) * (ix-ax); | ||||||
|     if(nz) swap(res[0], res[1]), res[2] = -res[2]; |  | ||||||
|     res[0] *= sx; res[1] *= sy; |  | ||||||
|     res[3] = 1; |  | ||||||
|    |    | ||||||
|     #undef S0 |     if(h[2] < 0.) { swap(res[0], res[1]); res[2] = -res[2]; } | ||||||
|     #undef S1 |     if(h[0] < 0.) res[0] = -res[0]; | ||||||
|     #undef S2 |     if(h[1] < 0.) res[1] = -res[1]; | ||||||
|      |  | ||||||
|     // println(hlog, kz(h), " => ", kz(res), " => ", kz(direct_exp(res, 1000)), " [", ix, ",", iy, ",", iz, " | ", sx, "/", sy, "/", nz, "]"); |  | ||||||
|      |      | ||||||
|     return res; |     return res; | ||||||
|  |  | ||||||
|  |     /* ld r = sqrt(res[0] * res[0] + res[1] * res[1] + res[2] * res[2]); | ||||||
|  |     if(r == 0.) return res; | ||||||
|  |     return res * atanh(r) / r; */ | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   transmatrix local_perspective, ilocal_perspective; |   transmatrix local_perspective, ilocal_perspective; | ||||||
| @@ -344,20 +365,35 @@ namespace solv { | |||||||
|   string solshader =  |   string solshader =  | ||||||
|     "uniform mediump sampler3D tInvExpTable;"     |     "uniform mediump sampler3D tInvExpTable;"     | ||||||
|     "uniform mediump mat4 uILP;" |     "uniform mediump mat4 uILP;" | ||||||
|  |     "uniform mediump float PRECX, PRECY, PRECZ;" | ||||||
|  |  | ||||||
|  |     "float x_to_ix(float u) {" | ||||||
|  |     "  if(u < 1e-6) return 0.;" | ||||||
|  |     "  float diag = u*u/2.;" | ||||||
|  |     | ||||||
|  |     "  float x = diag;" | ||||||
|  |     "  float y = u;" | ||||||
|  |     "  float z = diag+1.;" | ||||||
|  |      | ||||||
|  |     "  x /= (1.+z);" | ||||||
|  |     "  y /= (1.+z);" | ||||||
|  |      | ||||||
|  |     "  return 0.5 - atan((0.5-x) / y) / 3.1415926535897932384626433832795;" | ||||||
|  |     "  }" | ||||||
|  |  | ||||||
|  |  | ||||||
|     "vec4 inverse_exp(vec4 h) {" |     "vec4 inverse_exp(vec4 h) {" | ||||||
|      |      | ||||||
|     "float ix = asinh(h[0]) * 32.;" |     "float ix = h[0] >= 0. ? x_to_ix(h[0]) : x_to_ix(-h[0]);" | ||||||
|     "float iy = asinh(h[1]) * 32.;" |     "float iy = h[1] >= 0. ? x_to_ix(h[1]) : x_to_ix(-h[1]);" | ||||||
|     "float iz = h[2] * 8. / log(2.);" |     "float iz = tanh(h[2]);" | ||||||
|      |      | ||||||
|     "if(ix < 0.) ix = -ix;" |     "if(h[2] < 1e-6) { iz = -iz; float s = ix; ix = iy; iy = s; }" | ||||||
|     "if(iy < 0.) iy = -iy;" |     "if(iz < 0.) iz = 0.;" | ||||||
|     "if(iz < 0.) { iz = -iz; float s = ix; ix = iy; iy = s; }" |  | ||||||
|    |    | ||||||
|     "vec4 res = texture3D(tInvExpTable, vec3((ix + 0.5) / 64., (iy + 0.5) / 64., (iz + 0.5) / 32.));" |     "vec4 res = texture3D(tInvExpTable, vec3(ix*(1.-1./PRECX) + 0.5/PRECX, iy*(1.-1./PRECY) + .5/PRECY, iz*(1.-1./PRECZ) + .5/PRECZ));" | ||||||
|    |    | ||||||
|     "if(h[2] < 0.) { res.xy = res.yx; res[2] = -res[2]; }" |     "if(h[2] < 1e-6) { res.xy = res.yx; res[2] = -res[2]; }" | ||||||
|     "if(h[0] < 0.) res[0] = -res[0];" |     "if(h[0] < 0.) res[0] = -res[0];" | ||||||
|     "if(h[1] < 0.) res[1] = -res[1];" |     "if(h[1] < 0.) res[1] = -res[1];" | ||||||
|      |      | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Zeno Rogue
					Zeno Rogue