*/}}
浏览代码

Better 3d floor drawing.

YimingWu 2 年之前
父节点
当前提交
3aaab507e9
共有 4 个文件被更改,包括 54 次插入41 次删除
  1. 2 2
      la_tns.h
  2. 39 28
      la_tns_kernel.c
  3. 9 7
      resources/la_tns_shaders.cpp
  4. 4 4
      resources/la_widgets_viewers.c

+ 2 - 2
la_tns.h

@@ -106,7 +106,7 @@ struct _tnsShader{
     int iInputColorSpace, iOutputColorSpace, iShowStripes;
     int iInputColorSpace, iOutputColorSpace, iShowStripes;
     int iDoOffset;
     int iDoOffset;
 
 
-    int uViewDir,uViewPos,uFOV;
+    int uViewDir,uViewPos,uFOV,uNear,uFar;
 };
 };
 typedef struct _tnsTexture tnsTexture;
 typedef struct _tnsTexture tnsTexture;
 typedef struct _tnsCommand tnsCommand;
 typedef struct _tnsCommand tnsCommand;
@@ -1376,7 +1376,7 @@ void tnsMakeLinerGradient3dv(real *arr, int num_points, real *rgb0, real *rgb1);
 void tnsMakeLinerGradient4dv(real *arr, int num_points, real *rgb0, real *rgb1);
 void tnsMakeLinerGradient4dv(real *arr, int num_points, real *rgb0, real *rgb1);
 
 
 void tnsMakeFoucsSquare(int L, int R, int U, int B, int W);
 void tnsMakeFoucsSquare(int L, int R, int U, int B, int W);
-void tnsDrawFloor(int Size, int Span, int *ShowAxis);
+void tnsDrawFloor(real* CamPosition, real Far, int *ShowAxis);
 void tnsDraw2DGrid10(real L, real R, real U, real B, real xmin, real xmax, real ymin, real ymax, real MostDenseW, real MostDenseH,
 void tnsDraw2DGrid10(real L, real R, real U, real B, real xmin, real xmax, real ymin, real ymax, real MostDenseW, real MostDenseH,
                      real* color4, real AlphaFactor, int ShowGrid, int TextAlign);
                      real* color4, real AlphaFactor, int ShowGrid, int TextAlign);
 
 

+ 39 - 28
la_tns_kernel.c

@@ -272,6 +272,8 @@ void tnsShaderMakeIndex(tnsShader *tns){
 
 
     tns->uViewDir = glGetUniformLocation(program, "uViewDir");
     tns->uViewDir = glGetUniformLocation(program, "uViewDir");
     tns->uViewPos = glGetUniformLocation(program, "uViewPos");
     tns->uViewPos = glGetUniformLocation(program, "uViewPos");
+    tns->uNear = glGetUniformLocation(program, "uNear");
+    tns->uFar = glGetUniformLocation(program, "uFar");
     tns->uFOV = glGetUniformLocation(program, "uFOV");
     tns->uFOV = glGetUniformLocation(program, "uFOV");
 }
 }
 void tnsShaderApplyProjection(tnsShader *tns, tnsMatrix44d m){
 void tnsShaderApplyProjection(tnsShader *tns, tnsMatrix44d m){
@@ -4365,27 +4367,35 @@ void tnsMakeFoucsSquare(int L, int R, int U, int B, int W){
     tnsPackAs(GL_LINE_LOOP);
     tnsPackAs(GL_LINE_LOOP);
 }
 }
 
 
-void tnsDrawFloor(int Size, int Span, int *ShowAxis){
-    int i = 0;
-    int Lim = Span * 2 + 1;
-    int Dist = Size * Span;
+void tnsDrawFloor(real* CamPosition, real Far, int *ShowAxis){
+    real OrigFar=Far;
+    real height=CamPosition[2];
+    real dist=sqrt(Far*Far-height*height);
+    real spanstart=Far/10000;
+    real spanl=1e-3,spanh=1e-3;
+    while((spanl*10)<spanstart){ spanl*=10; } spanh=spanl;
+    while((spanh*10)<Far){ spanh*=10; }
 
 
     tnsFlush();
     tnsFlush();
 
 
-    glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1,1); glEnable(GL_BLEND);
+    glEnable(GL_BLEND); glDepthMask(GL_FALSE); glEnable(GL_DEPTH_TEST);
     tnsUseShader(T->FloorShader);
     tnsUseShader(T->FloorShader);
     tnsEnableShaderv(T->FloorShader);
     tnsEnableShaderv(T->FloorShader);
-
-    tnsVertex3d(-10000,10000,0);
-    tnsVertex3d(10000,10000,0);
-    tnsVertex3d(10000,-10000,0);
-    tnsVertex3d(-10000,-10000,0);
-    tnsPackAs(GL_TRIANGLE_FAN);
-    tnsFlush();
-
-    tnsUseShader(T->immShader);
-    tnsEnableShaderv(T->immShader);
-    glDisable(GL_POLYGON_OFFSET_FILL);
+    glUniform3f(T->FloorShader->uViewPos,LA_COLOR3(CamPosition));
+    glUniform1f(T->FloorShader->uFar,Far);
+    
+    real sp=spanh;
+    while(sp>=spanl){
+        real xmin=((int)((CamPosition[0]-dist-sp)/sp))*sp,xmax=((int)((CamPosition[0]+dist+sp)/sp))*sp;
+        real ymin=((int)((CamPosition[1]-dist-sp)/sp))*sp,ymax=((int)((CamPosition[1]+dist+sp)/sp))*sp;
+        real ux=xmin; while(ux<xmax){ tnsVertex2d(ux,ymin),tnsVertex2d(ux,ymax); ux+=sp; }
+        real uy=ymin; while(uy<ymax){ tnsVertex2d(xmin,uy),tnsVertex2d(xmax,uy); uy+=sp; }
+        tnsPackAs(GL_LINES);
+        tnsFlush();
+        Far/=4; if(Far<height) break;
+        dist=sqrt(Far*Far-height*height); sp/=10;
+        glUniform1f(T->FloorShader->uFar,Far);
+    }
 
 
     //for (i; i < Lim; i++){
     //for (i; i < Lim; i++){
     //    if (i == Span && ShowAxis[0]) continue;
     //    if (i == Span && ShowAxis[0]) continue;
@@ -4401,24 +4411,25 @@ void tnsDrawFloor(int Size, int Span, int *ShowAxis){
 
 
     //tnsPackAs(GL_LINES);
     //tnsPackAs(GL_LINES);
 
 
-    if (ShowAxis[0]){
-        tnsColor4d(1, 0, 0, 1);
-        tnsVertex3d(-Dist, 0, 0);
-        tnsVertex3d(Dist, 0, 0);
+    glUniform1f(T->FloorShader->uFar,OrigFar);
+
+    if (ShowAxis[0]){ tnsColor4d(1, 0, 0, 1);
+        tnsVertex3d(-OrigFar, 0, 0); tnsVertex3d(OrigFar, 0, 0);
         tnsPackAs(GL_LINES);
         tnsPackAs(GL_LINES);
     }
     }
-    if (ShowAxis[1]){
-        tnsColor4d(0, 1, 0, 1);
-        tnsVertex3d(0, -Dist, 0);
-        tnsVertex3d(0, Dist, 0);
+    if (ShowAxis[1]){ tnsColor4d(0, 1, 0, 1);
+        tnsVertex3d(0, -OrigFar, 0); tnsVertex3d(0, OrigFar, 0);
         tnsPackAs(GL_LINES);
         tnsPackAs(GL_LINES);
     }
     }
-    if (ShowAxis[2]){
-        tnsColor4d(0, 0, 1, 1);
-        tnsVertex3d(0, 0, -Dist);
-        tnsVertex3d(0, 0, Dist);
+    if (ShowAxis[2]){ tnsColor4d(0, 0, 1, 1);
+        tnsVertex3d(0, 0, -OrigFar); tnsVertex3d(0, 0, OrigFar);
         tnsPackAs(GL_LINES);
         tnsPackAs(GL_LINES);
     }
     }
+    tnsFlush();
+
+    tnsUseShader(T->immShader);
+    tnsEnableShaderv(T->immShader);
+    glDepthMask(GL_TRUE);
 }
 }
 
 
 void tnsDraw2DGrid10(real L, real R, real U, real B, real xmin, real xmax, real ymin, real ymax, real MostDenseW, real MostDenseH,
 void tnsDraw2DGrid10(real L, real R, real U, real B, real xmin, real xmax, real ymin, real ymax, real MostDenseW, real MostDenseH,

+ 9 - 7
resources/la_tns_shaders.cpp

@@ -692,22 +692,24 @@ uniform mat4 mProjection;
 uniform mat4 mModel;
 uniform mat4 mModel;
 uniform mat4 mView;
 uniform mat4 mView;
 in vec4 vVertex;
 in vec4 vVertex;
+in vec4 vColor;
 out vec3 fGPos;
 out vec3 fGPos;
+out vec4 fColor;
 void main(){
 void main(){
     gl_Position=mProjection * mView * mModel * vVertex;
     gl_Position=mProjection * mView * mModel * vVertex;
     fGPos=vec3((mModel * vVertex).xyz);
     fGPos=vec3((mModel * vVertex).xyz);
+	fColor=vColor;
 })";
 })";
 
 
 extern "C" const char* LA_FLOOR_FRAGMENT_SHADER = R"(#version 330
 extern "C" const char* LA_FLOOR_FRAGMENT_SHADER = R"(#version 330
-layout(location = 0) out vec4 outColor;
+uniform vec3 uViewPos;
+uniform float uFar;
+in vec4 fColor;
 in vec3 fGPos;
 in vec3 fGPos;
-float line(float center, float width,float s){
-	return smoothstep(center-width,center-width+1.e-1,s)-smoothstep(center+width-1.e-1,center+width,s);
-}
+layout(location = 0) out vec4 outColor;
 void main(){
 void main(){
-	float s=sin(fGPos.x/10);
-	float v=line(0,0.1,s);
-	outColor=v*vec4(0.5,0.5,0.5,1);
+	float fac=1-pow(clamp(length(uViewPos-fGPos)/uFar,0,1),0.4);
+	outColor=vec4(fColor.rgb,fColor.a*fac);
 })";
 })";
 
 
 extern "C" const char* LA_RAY_VERTEX_SHADER = R"(#version 330
 extern "C" const char* LA_RAY_VERTEX_SHADER = R"(#version 330

+ 4 - 4
resources/la_widgets_viewers.c

@@ -95,9 +95,9 @@ void la_RootObjectDraw(laBoxedTheme *bt, tnsObject *root, laUiItem* ui){
         //tnsVertexArray3d(p, 4); tnsPackAs(GL_TRIANGLE_FAN);
         //tnsVertexArray3d(p, 4); tnsPackAs(GL_TRIANGLE_FAN);
         //tnsMakeQuad3d(p, -100,-100, 74,-100,100, 74,100,100, 74,100,-100, 74);
         //tnsMakeQuad3d(p, -100,-100, 74,-100,100, 74,100,100, 74,100,-100, 74);
         //tnsVertexArray3d(p, 4); tnsPackAs(GL_TRIANGLE_FAN);
         //tnsVertexArray3d(p, 4); tnsPackAs(GL_TRIANGLE_FAN);
-        ////tnsDrawFloor(e->GridSize, e->GridSpan, e->ShowAxis);
+        ////tnsDrawFloor(e->ViewingCamera->Base.GLocation, e->ViewingCamera->ZMax, e->ShowAxis);
         //tnsColor4dv(laThemeColor(bt,LA_BT_BORDER));
         //tnsColor4dv(laThemeColor(bt,LA_BT_BORDER));
-        //tnsDrawFloor(e->GridSize, e->GridSpan, e->ShowAxis);
+        //tnsDrawFloor(e->ViewingCamera->Base.GLocation, e->ViewingCamera->ZMax, e->ShowAxis);
         //tnsEnableShaderv(T->immShader);
         //tnsEnableShaderv(T->immShader);
         //tnsFlush();
         //tnsFlush();
 
 
@@ -123,7 +123,7 @@ void la_RootObjectDraw(laBoxedTheme *bt, tnsObject *root, laUiItem* ui){
         //tnsUseNoTexture();
         //tnsUseNoTexture();
         //if (!e->LineDrawingMode && e->ShowFloorGrid){
         //if (!e->LineDrawingMode && e->ShowFloorGrid){
         //    tnsColor4dv(laThemeColor(bt,LA_BT_BORDER));
         //    tnsColor4dv(laThemeColor(bt,LA_BT_BORDER));
-        //    tnsDrawFloor(e->GridSize, e->GridSpan, e->ShowAxis);
+        //    tnsDrawFloor(e->ViewingCamera->Base.GLocation, e->ViewingCamera->ZMax, e->ShowAxis);
         //    tnsFlush();
         //    tnsFlush();
         //}
         //}
 //
 //
@@ -171,7 +171,7 @@ void la_RootObjectDraw(laBoxedTheme *bt, tnsObject *root, laUiItem* ui){
     if (!e->LineDrawingMode && e->ShowFloorGrid){
     if (!e->LineDrawingMode && e->ShowFloorGrid){
         tnsUseNoTexture();
         tnsUseNoTexture();
         real* color=laThemeColor(bt,LA_BT_BORDER); tnsColor4d(LA_COLOR3(color),0.4);
         real* color=laThemeColor(bt,LA_BT_BORDER); tnsColor4d(LA_COLOR3(color),0.4);
-        tnsDrawFloor(e->GridSize, e->GridSpan, e->ShowAxis);
+        tnsDrawFloor(e->ViewingCamera->Base.GLocation, e->ViewingCamera->ZMax, e->ShowAxis);
         tnsFlush();
         tnsFlush();
     }
     }