update volume plot functions

This commit is contained in:
wyq 2022-01-18 10:43:29 +08:00
parent 3418ded3da
commit 4e44a4ba1e
8 changed files with 158 additions and 18 deletions

View File

@ -41,7 +41,7 @@ Get in touch
License
-------
Copyright 2010-2020, MeteoInfo Developers
Copyright 2010-2022, MeteoInfo Developers
Licensed under the LGPL License, Version 3.0 (the "License");
you may not use this file except in compliance with the License.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

After

Width:  |  Height:  |  Size: 146 KiB

View File

@ -161,6 +161,10 @@ public class VolumeGraphics extends GraphicCollection3D {
colors[i * 4 + 1] = (byte) Math.round(g * 255);
colors[i * 4 + 2] = (byte) Math.round(b * 255);
colors[i * 4 + 3] = (byte) Math.round(a * 255);
Color color = this.legendScheme.getLegendBreak(i).getColor();
color = new Color(color.getRed(), color.getGreen(), color.getBlue(), (int)(a * 255));
this.legendScheme.getLegendBreak(i).setColor(color);
}
}

View File

@ -1000,8 +1000,8 @@ public class Plot3DGL extends Plot implements GLEventListener {
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
//gl.glEnable(GL2.GL_BLEND);
//gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
//gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE);
if (this.antialias) {
@ -2165,6 +2165,9 @@ public class Plot3DGL extends Plot implements GLEventListener {
}
private void drawVolume(GL2 gl, VolumeGraphics volume) throws Exception {
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glDisable(GL_DEPTH_TEST);
gl.glActiveTexture(GL_TEXTURE1);
gl.glBindTexture(GL_TEXTURE_2D, getTextureID(gl));
@ -2185,7 +2188,12 @@ public class Plot3DGL extends Plot implements GLEventListener {
gl.glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
String vertexShaderCode = Utils.loadResource("/shaders/volume/volume.vert");
String fragmentShaderCode = Utils.loadResource("/shaders/volume/volume.frag");
String fragmentShaderCode;
if (this.orthographic) {
fragmentShaderCode = Utils.loadResource("/shaders/volume/volume.frag");
} else {
fragmentShaderCode = Utils.loadResource("/shaders/volume/volume_perspective.frag");
}
final Program program = new Program("volume", vertexShaderCode, fragmentShaderCode);
try {
program.init(gl);
@ -2274,6 +2282,8 @@ public class Plot3DGL extends Plot implements GLEventListener {
Program.destroyAllPrograms(gl);
gl.glUseProgram(0);
gl.glDisable(GL_BLEND);
gl.glEnable(GL_DEPTH_TEST);
}
private void drawLineString(GL2 gl, Graphic graphic) {
@ -3932,12 +3942,14 @@ public class Plot3DGL extends Plot implements GLEventListener {
break;
}
} else {
float near = 0.1f;
float far = 1000.0f;
switch (this.aspectType) {
case EQUAL:
glu.gluPerspective(45.0f, h, 1.0, 20.0);
glu.gluPerspective(45.0f, h, near, far);
break;
default:
glu.gluPerspective(45.0f, 1.0f, 1.0, 20.0);
glu.gluPerspective(45.0f, 1.0f, near, far);
break;
}
glu.gluLookAt(0.0f, 0.0f, distance, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

View File

@ -43,15 +43,17 @@ Ray makeRay(vec3 origin, vec3 direction) {
}
Ray CreateCameraRay(vec2 uv)
{
float near = -5.0f;
//float near = -5.0f;
float far = 5.0f;
// Transform the camera origin to world space
float zv = 1000.0f;
vec3 direction = (iP * vec4(uv, far, 1.0f)).xyz;
//float zv = 1000.0f;
//vec3 direction = (iP * vec4(uv, far, 1.0f)).xyz;
vec3 origin = (iP * vec4(uv, 0.0f, 1.0f)).xyz;
origin = (iV * vec4(origin.xyz, 1.0f)).xyz;
// Invert the perspective projection of the view-space position
vec3 direction = (iP * vec4(uv, far, 1.0f)).xyz;
// Transform the direction from camera to world space and normalize
direction = (iV* vec4(direction, 0.0f)).xyz;
direction = normalize(direction);
@ -86,6 +88,7 @@ void main(){
intersect(ray, aabb, tmin, tmax);
if (tmax < tmin){
discard;
return;
}
vec3 start = (ray.origin.xyz + tmin*ray.direction.xyz - aabb[0])/(aabb[1]-aabb[0]);
vec3 end = (ray.origin.xyz + tmax*ray.direction.xyz - aabb[0])/(aabb[1]-aabb[0]);

View File

@ -0,0 +1,120 @@
#if __VERSION__ >= 130
#define varying in
out vec4 mgl_FragColor;
#else
#define mgl_FragColor gl_FragColor
#endif
uniform vec2 viewSize;
uniform mat4 iV;
uniform mat4 iP;
uniform sampler3D tex;
uniform sampler2D colorMap;
uniform int depthSampleCount;
uniform vec3 aabbMin;
uniform vec3 aabbMax;
vec3 aabb[2] = vec3[2](
vec3(-1.0, -1.0, -1.0),
vec3(1.0, 1.0, 1.0)
);
struct Ray {
vec3 origin;
vec3 direction;
vec3 inv_direction;
int sign[3];
};
Ray makeRay(vec3 origin, vec3 direction) {
vec3 inv_direction = vec3(1.0) / direction;
return Ray(
origin,
direction,
inv_direction,
int[3](
((inv_direction.x < 0.0) ? 1 : 0),
((inv_direction.y < 0.0) ? 1 : 0),
((inv_direction.z < 0.0) ? 1 : 0)
)
);
}
Ray CreateCameraRay(vec2 uv)
{
// Transform the camera origin to world space
//vec3 origin = (iV* vec4(0.0f, 0.0f, -100.0f, 1.0f)).xyz;
//vec3 origin = (iV* vec4(0.0f, 0.0f, 10.0f, 1.0f)).xyz;
vec3 origin = (iP * vec4(0.0f, 0.0f, 0.0f, -5.0f)).xyz;
//vec3 origin = (iP * vec4(0.0f, 0.0f, 0.0f, 1.0f)).xyz;
origin = (iV * vec4(origin, 0.0f)).xyz;
// Invert the perspective projection of the view-space position
vec3 direction = (iP * vec4(uv, 0.0f, 1.0f)).xyz;
// Transform the direction from camera to world space and normalize
direction = (iV* vec4(direction, 0.0f)).xyz;
direction = normalize(direction);
return makeRay(origin, direction);
}
/*
From: https://github.com/hpicgs/cgsee/wiki/Ray-Box-Intersection-on-the-GPU
*/
void intersect(
in Ray ray, in vec3 aabb[2],
out float tmin, out float tmax
){
float tymin, tymax, tzmin, tzmax;
tmin = (aabb[ray.sign[0]].x - ray.origin.x) * ray.inv_direction.x;
tmax = (aabb[1-ray.sign[0]].x - ray.origin.x) * ray.inv_direction.x;
tymin = (aabb[ray.sign[1]].y - ray.origin.y) * ray.inv_direction.y;
tymax = (aabb[1-ray.sign[1]].y - ray.origin.y) * ray.inv_direction.y;
tzmin = (aabb[ray.sign[2]].z - ray.origin.z) * ray.inv_direction.z;
tzmax = (aabb[1-ray.sign[2]].z - ray.origin.z) * ray.inv_direction.z;
tmin = max(max(tmin, tymin), tzmin);
tmax = min(min(tmax, tymax), tzmax);
}
void main(){
vec2 vUV = 2.0 * gl_FragCoord.xy / viewSize - 1.0;
//vec2 vUV = 2.0 * (gl_FragCoord.xy + vec2(0.5, 0.5)) / viewSize - 1.0;
Ray ray = CreateCameraRay(vUV);
vec3 aabb[2] = vec3[2](aabbMin, aabbMax);
float tmin = 0.0;
float tmax = 0.0;
intersect(ray, aabb, tmin, tmax);
if (tmax <= tmin){
discard;
return;
}
vec3 start = (ray.origin.xyz + tmin*ray.direction.xyz - aabb[0])/(aabb[1]-aabb[0]);
vec3 end = (ray.origin.xyz + tmax*ray.direction.xyz - aabb[0])/(aabb[1]-aabb[0]);
float len = distance(end, start);
int sampleCount = int(float(depthSampleCount)*len);
vec3 increment = (end-start)/float(sampleCount);
float incLength = length(increment);
increment = normalize(increment);
vec3 pos = start;
float px = 0.0;
vec4 pxColor = vec4(0.0, 0.0, 0.0, 0.0);
vec3 texCo = vec3(0.0, 0.0, 0.0);
float last = 0.0;
for(int count = 0; count < sampleCount; count++){
texCo = mix(start, end, float(count)/float(sampleCount));// - originOffset;
px = max(px, texture(tex, texCo).r);
if(px >= 0.99){
break;
}
}
pxColor = texture(colorMap, vec2(px, 0.0));
mgl_FragColor = pxColor;
}

View File

@ -4,6 +4,7 @@ import org.meteoinfo.chart.graphic.VolumeGraphics;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@ -102,7 +103,9 @@ public class VolumeTest {
VolumeGraphics graphics = test.createGraphics();
JFrame frame = new JFrame("Volume Test");
Plot3DGL plot = new Plot3DGL();
plot.setOrthographic(false);
plot.setClipPlane(false);
//plot.setBackground(Color.black);
GLChartPanel canvas = new GLChartPanel(plot);
plot.addGraphic(graphics);
frame.getContentPane().add(canvas);

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\map">
<Path OpenPath="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\toolbox\miml"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\toolbox\miml\model_persistence"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
@ -14,21 +14,19 @@
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite\modis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d_earth"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
</Path>
<File>
<OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d_earth\streamslice_pitch.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d_earth\CALIPSO_L1_3d_axis.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\bar_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\map\convexhull.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\volumeplot_perspective.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\volumeplot_1.py"/>
</OpenedFiles>
<RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d_earth\streamslice_pitch.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d_earth\CALIPSO_L1_3d_axis.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\bar_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\map\convexhull.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\volumeplot_perspective.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\volumeplot_1.py"/>
</RecentFiles>
</File>
<Font>
@ -36,5 +34,5 @@
</Font>
<LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/>
<Figure DoubleBuffering="true"/>
<Startup MainFormLocation="-7,-7" MainFormSize="1293,685"/>
<Startup MainFormLocation="-7,0" MainFormSize="1398,815"/>
</MeteoInfo>