Skip to content

Commit

Permalink
CPlot: frustum culling for osmesa
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit128 committed Oct 15, 2024
1 parent 5dad62c commit 86a95fc
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cassiopee/CPlot/CPlot/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ void gmenuArrows(int key, int x, int y);

// frustum
void computeFrustumPlanes(ViewInfo& view);
void computeFrustumPlanes2();
int isInFrustum(Zone* z, ViewInfo& view);

// files
Expand Down
2 changes: 1 addition & 1 deletion Cassiopee/CPlot/CPlot/Display/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void Data::display()
_view.xeye, _view.yeye, _view.zeye,
_view.dirx, _view.diry, _view.dirz);
computeFrustumPlanes(_view);

glEnable(GL_MULTISAMPLE);

// Display following mode
Expand Down
97 changes: 91 additions & 6 deletions Cassiopee/CPlot/CPlot/Display/frustum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
*/
#include "../ViewInfo.h"
#include "../Zone.h"
#include <GL/gl.h>
#include <math.h>
#include <stdio.h>

#define TOL 1.e-6
#define TOL 1.e-4

//=============================================================================
// Compute frustum planes
// Compute frustum planes from view
//=============================================================================
void computeFrustumPlanes(ViewInfo& view)
{
Expand Down Expand Up @@ -154,6 +155,90 @@ void computeFrustumPlanes(ViewInfo& view)
view.RightNx = -Nx;
view.RightNy = -Ny;
view.RightNz = -Nz;

//printf("Nx=%g; Ny=%g; Nz=%g; di=%g\n", view.RightNx, view.RightNy, view.RightNz, view.RightD);
//printf("Nx=%g; Ny=%g; Nz=%g; di=%g\n", view.LeftNx, view.LeftNy, view.LeftNz, view.LeftD);
//printf("Nx=%g; Ny=%g; Nz=%g; di=%g\n", view.TopNx, view.TopNy, view.TopNz, view.TopD);
//printf("Nx=%g; Ny=%g; Nz=%g; di=%g\n", view.BottomNx, view.BottomNy, view.BottomNz, view.BottomD);
//printf("Nx=%g; Ny=%g; Nz=%g; di=%g\n", view.NearNx, view.NearNy, view.NearNz, view.NearD);
//printf("Nx=%g; Ny=%g; Nz=%g; di=%g\n", view.FarNx, view.FarNy, view.FarNz, view.FarD);
}

// Compute frustum planes from view matrices
void computeFrustumPlanes2()
{
// need further check
float proj[16]; float mod[16];
glGetFloatv(GL_PROJECTION_MATRIX, proj);
glGetFloatv(GL_MODELVIEW_MATRIX, mod);

float clip[16];
clip[0] = mod[0] * proj[0] + mod[1] * proj[4] + mod[2] * proj[8] + mod[3] * proj[12];
clip[1] = mod[0] * proj[1] + mod[1] * proj[5] + mod[2] * proj[9] + mod[3] * proj[13];
clip[2] = mod[0] * proj[2] + mod[1] * proj[6] + mod[2] * proj[10] + mod[3] * proj[14];
clip[3] = mod[0] * proj[3] + mod[1] * proj[7] + mod[2] * proj[11] + mod[3] * proj[15];

clip[4] = mod[4] * proj[0] + mod[5] * proj[4] + mod[6] * proj[8] + mod[7] * proj[12];
clip[5] = mod[4] * proj[1] + mod[5] * proj[5] + mod[6] * proj[9] + mod[7] * proj[13];
clip[6] = mod[4] * proj[2] + mod[5] * proj[6] + mod[6] * proj[10] + mod[7] * proj[14];
clip[7] = mod[4] * proj[3] + mod[5] * proj[7] + mod[6] * proj[11] + mod[7] * proj[15];

clip[8] = mod[8] * proj[0] + mod[9] * proj[4] + mod[10] * proj[8] + mod[11] * proj[12];
clip[9] = mod[8] * proj[1] + mod[9] * proj[5] + mod[10] * proj[9] + mod[11] * proj[13];
clip[10] = mod[8] * proj[2] + mod[9] * proj[6] + mod[10] * proj[10] + mod[11] * proj[14];
clip[11] = mod[8] * proj[3] + mod[9] * proj[7] + mod[10] * proj[11] + mod[11] * proj[15];

clip[12] = mod[12] * proj[0] + mod[13] * proj[4] + mod[14] * proj[8] + mod[15] * proj[12];
clip[13] = mod[12] * proj[1] + mod[13] * proj[5] + mod[14] * proj[9] + mod[15] * proj[13];
clip[14] = mod[12] * proj[2] + mod[13] * proj[6] + mod[14] * proj[10] + mod[15] * proj[14];
clip[15] = mod[12] * proj[3] + mod[13] * proj[7] + mod[14] * proj[11] + mod[15] * proj[15];

struct Plane { float a, b, c, d; };
Plane planes[6];

// Right plane
planes[0].a = clip[3] - clip[0];
planes[0].b = clip[7] - clip[4];
planes[0].c = clip[11] - clip[8];
planes[0].d = clip[15] - clip[12];

// Left plane
planes[1].a = clip[3] + clip[0];
planes[1].b = clip[7] + clip[4];
planes[1].c = clip[11] + clip[8];
planes[1].d = clip[15] + clip[12];

// Top plane
planes[2].a = clip[3] - clip[1];
planes[2].b = clip[7] - clip[5];
planes[2].c = clip[11] - clip[9];
planes[2].d = clip[15] - clip[13];

// Bottom plane
planes[3].a = clip[3] + clip[1];
planes[3].b = clip[7] + clip[5];
planes[3].c = clip[11] + clip[9];
planes[3].d = clip[15] + clip[13];

// near plane
planes[4].a = clip[3] - clip[2];
planes[4].b = clip[7] - clip[6];
planes[4].c = clip[11] - clip[10];
planes[4].d = clip[15] - clip[14];

// far plane
planes[5].a = clip[3] + clip[2];
planes[5].b = clip[7] + clip[6];
planes[5].c = clip[11] + clip[10];
planes[5].d = clip[15] + clip[14];

//printf("a=%g; b=%g; c=%g; d=%g\n", planes[0].a, planes[0].b, planes[0].c, planes[0].d);
//printf("a=%g; b=%g; c=%g; d=%g\n", planes[1].a, planes[1].b, planes[1].c, planes[1].d);
//printf("a=%g; b=%g; c=%g; d=%g\n", planes[2].a, planes[2].b, planes[2].c, planes[2].d);
//printf("a=%g; b=%g; c=%g; d=%g\n", planes[3].a, planes[3].b, planes[3].c, planes[3].d);
//printf("a=%g; b=%g; c=%g; d=%g\n", planes[4].a, planes[4].b, planes[4].c, planes[2].d);
//printf("a=%g; b=%g; c=%g; d=%g\n", planes[5].a, planes[5].b, planes[5].c, planes[3].d);
return;
}

//=============================================================================
Expand All @@ -164,7 +249,7 @@ E_Int isInFrustum(Zone* z, ViewInfo& view)
{
#ifdef __MESA__
// to avoid abusive clipping in osmesa
return 1;
//return 1;
#endif

E_Int out;
Expand Down Expand Up @@ -223,7 +308,7 @@ E_Int isInFrustum(Zone* z, ViewInfo& view)
}
if (out == 8) return 0;

// all points roght of righ plane
// all points right of right plane
out = 0;
for (E_Int k = 0; k < 8; k++)
{
Expand All @@ -232,7 +317,7 @@ E_Int isInFrustum(Zone* z, ViewInfo& view)
}
if (out == 8) return 0;

// all points top of top plane
// all points above top plane
out = 0;
for (E_Int k = 0; k < 8; k++)
{
Expand All @@ -241,7 +326,7 @@ E_Int isInFrustum(Zone* z, ViewInfo& view)
}
if (out == 8) return 0;

// all points bottom of bottom plane
// all points below bottom plane
out = 0;
for (E_Int k = 0; k < 8; k++)
{
Expand Down
6 changes: 3 additions & 3 deletions Cassiopee/CPlot/CPlot/Plugins/screenDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ char* Data::export2Image(E_Int exportWidth, E_Int exportHeight)
// resolution
GLuint fb, rb, db;
#ifdef __SHADERS__
if (ptrState->offscreen == 2 || ptrState->offscreen == 3 || ptrState->offscreen == 4) // openGL offscreen rendering
if (ptrState->offscreen == 0 || ptrState->offscreen == 2 || ptrState->offscreen == 3 || ptrState->offscreen == 4) // openGL offscreen rendering
{
glGenFramebuffersEXT(1, &fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
Expand Down Expand Up @@ -108,14 +108,14 @@ char* Data::export2Image(E_Int exportWidth, E_Int exportHeight)
// // Attach depth buffer to FBO
// glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER_EXT, db);
//}
else if (ptrState->offscreen == 2) // opengl offscreen
else if (ptrState->offscreen == 0 || ptrState->offscreen == 2) // opengl offscreen
{
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, exportWidth, exportHeight);
// Attach depth buffer to FBO
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, db);
}

if (ptrState->offscreen == 2 || ptrState->offscreen == 3 || ptrState->offscreen == 4) // opengl offscreen
if (ptrState->offscreen == 0 || ptrState->offscreen == 2 || ptrState->offscreen == 3 || ptrState->offscreen == 4) // opengl offscreen
{
int status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status == GL_FRAMEBUFFER_COMPLETE_EXT)
Expand Down
2 changes: 1 addition & 1 deletion Cassiopee/CPlot/CPlot/display1.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
int bgColor, shadow, dof, offscreen, stereo, frameBuffer, panorama;
E_Float lightOffsetX, lightOffsetY;
E_Float dofPower, gamma; int toneMapping;
PyObject* posCamList; PyObject* posEyeList; PyObject* dirCamList; PyObject* exportList; // only for ODS
PyObject* posCamList; PyObject* posEyeList; PyObject* dirCamList; // only for ODS
if (!PyArg_ParseTuple(args, "OiOOOOOiiiiiiiddiiiiisssOidO(ii)(ddd)(ddd)(ddd)disi(dd)iddiidissOOiiOOO",
&arrays, &dim, &modeObject, &scalarFieldObject,
&vectorFieldObject1, &vectorFieldObject2, &vectorFieldObject3,
Expand Down
22 changes: 4 additions & 18 deletions Cassiopee/CPlot/CPlot/panorama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ PyObject* K_CPLOT::panoramaODS(PyObject* self, PyObject* args)

char* varString;
E_Int ni, nj, nk, res;
FldArrayF* array; FldArrayI* cn; char* eltType;
FldArrayI* cn; char* eltType;
std::vector<FldArrayF*> frontImages(nangles);
for (E_Int i = 0; i < nangles; i++)
{
Expand Down Expand Up @@ -541,7 +541,6 @@ PyObject* K_CPLOT::panoramaODS(PyObject* self, PyObject* args)
imb4[i] = botImages[i]->begin(7);
}


// final image pointers
E_Float* final1 = final->begin(4); // r,g,b,a
E_Float* final2 = final->begin(5);
Expand All @@ -553,16 +552,9 @@ PyObject* K_CPLOT::panoramaODS(PyObject* self, PyObject* args)
if (type360 == 0) { tinf = -M_PI; tsup = 2*M_PI; } // 360
else { tinf = -M_PI/2.; tsup = M_PI; } // 180

// fov of each cube image
E_Float fov = 90.;

E_Int nijl = nil*njl; // final image
E_Int nil1 = nil-1;
E_Int njl1 = njl-1;
E_Int ni1 = ni-1; // cube image
E_Int nj1 = nj-1;
//printf("ni=%d, nj=%d\n", ni, nj);


E_Int ind;

// direct sliting
Expand All @@ -583,8 +575,7 @@ PyObject* K_CPLOT::panoramaODS(PyObject* self, PyObject* args)
//return Py_None;

// transformation
E_Float theta = 0.;
E_Float x, y, z, scale, py, phi, ty;
E_Float y, z, scale, py, phi, ty;

for (E_Int i = 0; i < nangles; i++)
{
Expand All @@ -596,7 +587,6 @@ PyObject* K_CPLOT::panoramaODS(PyObject* self, PyObject* args)

ind = i + j*nil;

x = 0.;
y = sin(phi); // entre -1 et 1
z = cos(phi); // entre 0 et 0

Expand Down Expand Up @@ -646,15 +636,12 @@ PyObject* K_CPLOT::panoramaODS(PyObject* self, PyObject* args)
void accumulateSlit(E_Int ni, E_Int nj, char* imf, char* imt, char* imb,
E_Int i, E_Int nil, E_Int njl, char* imOut)
{
E_Int nijl = nil*njl; // final image
E_Int nil1 = nil-1;
E_Int njl1 = njl-1;
E_Int ni1 = ni-1; // slit image
E_Int nj1 = nj-1;

{
E_Int ind;
E_Float scale, ty, py, phi, x, y, z;
E_Float scale, ty, py, phi, y, z;

// 1D interpolation
for (E_Int j = 0; j < njl; j++)
Expand All @@ -663,7 +650,6 @@ void accumulateSlit(E_Int ni, E_Int nj, char* imf, char* imt, char* imb,
phi = M_PI/2. - ty * M_PI; // between pi/2 and -pi/2
ind = i + (njl-j-1)*nil;

x = 0.;
y = sin(phi); // entre -1 et 1
z = cos(phi); // entre 0 et 0

Expand Down

0 comments on commit 86a95fc

Please sign in to comment.