Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi thread #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,7 @@ ModelManifest.xml
/noise
/VectorVisualization/snapshotOut
/VectorVisualization/video
*.html
*.csv
/VectorVisualization/glew-1.11.0
/VectorVisualization/lib
210 changes: 183 additions & 27 deletions VectorVisualization/3DLIC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <thread>
#include <mutex>
#include <condition_variable>

#include "texture.h"
#include "illumination.h"
Expand Down Expand Up @@ -101,6 +104,7 @@ void display(void)
//double timetest = timer();

renderer.render(updateScene || updateSceneCont);


//std::cout << "cost for render" << timetest - timer() << std::endl;
//updateScene = true;
Expand All @@ -118,9 +122,11 @@ void display(void)
snprintf(fpsStr, 10, "%2.2f", fpsCounter.getFPS());
hud.DrawHUD(fpsStr, 420, 2);
//hud.DrawHUD();
//if(updateScene)
// std::cout << fpsCounter << std::endl;

CHECK_FOR_OGL_ERROR();

//updateScene = false;
glutSwapBuffers();
fpsCounter.frameFinished();
}
Expand All @@ -135,16 +141,32 @@ void idle(void)
//vd.getVolumeData()->newData = vd.getVolumeData()->dataSets[vd.NextTimeStep()];

//vd.createTexture("VectorData_Tex", GL_TEXTURE2_ARB, true);
if(animationMode && (renderTechnique == VOLIC_SLICING || renderTechnique == VOLIC_RAYCAST || renderTechnique == VOLIC_LICVOLUME))
if (animationMode && (renderTechnique == VOLIC_SLICING || renderTechnique == VOLIC_RAYCAST /*|| renderTechnique == VOLIC_LICVOLUME*/))
{
vd.createTextureIterp("VectorData_Tex", GL_TEXTURE2_ARB, true);
if(animationMode && renderTechnique == VOLIC_LICVOLUME)
renderer.updateLICVolume();
vd.checkInterpolateStage();

//renderer.setDataTex(vd.getTextureSetRef(idx));
//renderer.setDataTex(vd.getTextureSetRef(vd.getNextTimeStep()));
if (vd.checkInterpolateStage()==1)
renderer.setVolumeData(vd.getVolumeData());
}
if (animationMode && renderTechnique == VOLIC_LICVOLUME)
{
licParams.interpStep++;
updateScene = true;
if (licParams.interpStep == licParams.interpSize)
{
renderer.setDataTex(vd.getTextureSetRef(vd.getNextTimeStep()));
renderer.setNextDataTex(vd.getTextureSetRef(vd.NextTimeStep()));
//vd.createTexture("VolumeLIC_Tex", GL_TEXTURE2_ARB, true);
renderer.updateLICVolume();
licParams.interpStep = 0;
}
}

//int i = 0;
//renderer.setDataTex(vd.getTextureSetRef(vd.getNextTimeStep()));

//Update Render Animation source
renderer.setVolumeData(vd.getVolumeData());
//renderer.setVolumeData(vd.getVolumeData());

// check whether to change to high res rendering
if (requestHighRes && renderer.isLowResEnabled())
Expand Down Expand Up @@ -272,6 +294,8 @@ void keyboard(unsigned char key, int x, int y)
hud.SetVisible(!hud.IsVisible());
break;
case 'r':
renderer.loadGLSLShader("#define NOISE_LAO_COMPUTE");
renderer.computeNoiseLAO();
renderer.loadGLSLShader();
updateScene = true;
break;
Expand Down Expand Up @@ -357,14 +381,14 @@ void keyboard(unsigned char key, int x, int y)
updateHUD(); updateScene = true;
break;
case 'h': // frequency scaling
licParams.freqScale += 0.2f; //0.5f
licParams.freqScale += 0.1f; //0.5f
updateHUD();
updateScene = true;
break;
case 'n':
licParams.freqScale -= 0.2f; //0.5f
if (licParams.freqScale < 0.5f)
licParams.freqScale = 0.5f;
licParams.freqScale -= 0.1f; //0.5f
if (licParams.freqScale < 0.1f)
licParams.freqScale = 0.1f;
updateHUD();
updateScene = true;
break;
Expand Down Expand Up @@ -394,7 +418,15 @@ void keyboard(unsigned char key, int x, int y)
break;
// update 3D Lic calculation
case 'u':
renderer.updateLICVolume();
//renderer.updateLICVolume();
updateScene = true;
break;
case '-':
renderer.loadGLSLShader("#define STREAMLINE_DISTANCE");
updateScene = true;
break;
case '=':
renderer.loadGLSLShader("#define AMBIENT_OCCULUSION");
updateScene = true;
break;

Expand Down Expand Up @@ -456,31 +488,53 @@ void keyboard(unsigned char key, int x, int y)

void keyboardSpecial(int key, int x, int y)
{
double begint, ent;
switch (key)
{
case GLUT_KEY_F1:
renderTechnique = VOLIC_VOLUME;
renderer.setDataTex(vd.getTextureRef());
break;
case GLUT_KEY_F2:
renderTechnique = VOLIC_RAYCAST;
renderer.setDataTex(vd.getTextureRef());
break;
case GLUT_KEY_F3:
renderTechnique = VOLIC_SLICING;
renderer.setDataTex(vd.getTextureRef());
renderer.updateSlices();
break;
case GLUT_KEY_F4:
renderTechnique = VOLIC_LICVOLUME;
renderer.updateLICVolume();
renderer.setDataTex(vd.getTextureRef());
//begint = timer();
renderer.renderLICVolume();
renderer.computeVolumeNormal();
//renderer.updateLICVolume();
renderer.computeLAOVolume();
//renderer.updateLICVolume();
//ent = timer();
//std::cout<<"pre-computed LIC: "<< ent-begint << std::endl;
break;
case GLUT_KEY_F5:
animationMode = !animationMode;
renderer.setAnimationFlag(animationMode);
if(animationMode)
if (animationMode)
{
std::cout << "Playing animation!" << std::endl;
renderer.loadGLSLShader("#define VOLUME_ANIMATION");
}
else
{
std::cout << "Stop animation!" << std::endl;
renderer.loadGLSLShader();
}
//renderer.updateLICVolume();
break;
case GLUT_KEY_F6:
renderTechnique = VOLIC_SCATTER;
renderer.setDataTex(vd.getTextureRef());
break;
}
renderer.setTechnique(renderTechnique);
updateHUD();
Expand Down Expand Up @@ -593,7 +647,7 @@ void mouseMotionInteract(int x, int y)
if (mouseBtnDown)
{
// change to low res mode immediately
//renderer.enableLowRes(true);
renderer.enableLowRes(true);
//requestHighRes = false;
mouseBtnDown = false;

Expand Down Expand Up @@ -673,6 +727,39 @@ void initGL(void)
CHECK_FOR_OGL_ERROR();
}

void loadVecterData(VectorDataSet * vdd)
{
wglMakeCurrent(dc, glrc_load);
std::deque<VolumeData*>& vds = vdd->getVolumeDataSet();
std::vector<Texture*> & vts = vdd->getTextureSet();
for (int i = vdd->getTimeStepBegin(); i <= vdd->getTimeStepEnd(); i++)
{
void* datap = vdd->loadTimeStep(i);
VolumeData * vdp = new VolumeData(*(vdd->getVolumeData()));
vdp->data = datap;
if (!vds.empty())
vds.back()->newData = datap;
vds.push_back(vdp);
//std::cout << "vector data set size: " << vds.size() << std::endl;
}
vds.back()->newData = vds.back()->data;

//initial first volume
vdd->setVolumeDataIndex(0);
vds.pop_front();

std::unique_lock<std::mutex> lck_load(mt_load);
load_volume_buffer = true;
cv_load.notify_one();


for (int i = 0; i < vds.size(); i++)
{
vts.push_back(vdd->createTextures(i, "LoadThread_Tex", GL_TEXTURE2_ARB, true));
//std::cout << "create vector texture: " << vts.size() << std::endl;
}
}


void init(void)
{
Expand All @@ -689,20 +776,23 @@ void init(void)
std::cerr << "Could not load data ..." << std::endl;
exit(1);
}
//for (int i = vd.getTimeStepBegin(); i <= vd.getTimeStepEnd(); i++)
//{
// void* datap = vd.loadTimeStep(vd.getCurTimeStep());
// vd.getVolumeData()->dataSets.push_back(datap);
// vd.getNextTimeStep();
//}
std::thread load_thread(loadVecterData, &vd);
load_thread.join();
//load_thread.detach();

auto pred = []() {
return load_volume_buffer;
};
std::unique_lock<std::mutex> lck_load(mt_load);
while (!pred())
cv_load.wait(lck_load);

//vd.getVolumeData()->data = vd.getVolumeData()->dataSets[vd.getCurTimeStep()];
//vd.getVolumeData()->newData = vd.getVolumeData()->dataSets[vd.NextTimeStep()];
vd.getVolumeData()->data = vd.loadTimeStep(vd.getCurTimeStep());
vd.getVolumeData()->newData = vd.loadTimeStep(vd.NextTimeStep());
std::cout << "start initil texture" << std::endl;
//vd.createTextures("VectorData_Tex", vd.getVolumeData()->dataSets.size(), GL_TEXTURE2_ARB, true);
// Set Interpolation step size
vd.setInterpolateSize(10);
licParams.interpSize = 10;

//vd.createTexture("VectorData_Tex", GL_TEXTURE2_ARB, true);
vd.createTextureIterp("VectorData_Tex", GL_TEXTURE2_ARB, true);
vd.checkInterpolateStage();
Expand All @@ -714,13 +804,22 @@ void init(void)
std::cout << std::endl;

// load secondary scalar volume data
if (!scalar.loadData("..\\data\\outputraw\\out_64_0_temperature.dat"))
if (!scalar.loadData("..\\data\\327output\\out_64_0_Lorentz_work.dat"))
{
std::cerr << "Could not load data ..." << std::endl;
exit(1);
}
scalar.createTexture("Scalar_Tex", GL_TEXTURE4_ARB);

//load secondary vector data
if (!vector.loadData("..\\data\\327output\\out_64_0_vorticity.dat"))
{
std::cerr << "Could not load data ..." << std::endl;
exit(1);
}
vector.getVolumeData()->data = vector.loadTimeStep(0);
vector.createTexture("Vector_Tex", GL_TEXTURE6_ARB, true);

// load LIC filter kernel
if (!licFilter.loadData(arguments.getLicFilterFileName()))
{
Expand Down Expand Up @@ -760,6 +859,21 @@ void init(void)
CHECK_FOR_OGL_ERROR();
//std::cout << std::endl;

//Test of Algorithm using CPU for instance
//double begint = timer();
//cpuLic = new CPULIC(256, 256, 256);
//cpuLic->setkernelData(licFilter.getFilterData());
//cpuLic->setVectorTexture(vd.getTextureRef());
//cpuLic->getVectorTextureData();
//cpuLic->setNoiseData(&noise);
//cpuLic->getNoiseTextureData();
////cpuLic->gather3DLIC(vd.getVolumeDataSet());
//cpuLic->scatter3DLIC(vd.getVolumeDataSet());
//double endt = timer();
//cpuLic->generateTexture();
//CHECK_FOR_OGL_ERROR();
//std::cout << "CPU LIC:" << endt - begint << std::endl;

clipPlanes[0].rotate(Quaternion_fromAngleAxis(static_cast<float>(M_PI / 2.0), Vector3_new(0.0f, 1.0f, 0.0f)));
clipPlanes[1].rotate(Quaternion_fromAngleAxis(static_cast<float>(M_PI / 2.0), Vector3_new(-1.0f, 0.0f, 0.0f)));

Expand All @@ -784,7 +898,12 @@ void init(void)
renderer.setLICFilter(&licFilter);

renderer.setDataTex(vd.getTextureRef());
//renderer.setDataTex(cpuLic->getTextureRef());
//renderer.setDataTex(noise.getTextureRef());
//renderer.setDataTex(vd.getTextureSetRef(vd.getCurTimeStep()));
renderer.setNextDataTex(vd.getTextureSetRef(vd.NextTimeStep()));
renderer.setScalarTex(scalar.getTextureRef());
renderer.setVectorTex(vector.getTextureRef());
renderer.setNoiseTex(noise.getTextureRef());
renderer.setTFrgbTex(tfEdit.getTextureRGB());
renderer.setTFalphaOpacTex(tfEdit.getTextureAlphaOpac());
Expand All @@ -793,11 +912,42 @@ void init(void)
renderer.setIllumMalloSpecTex(illum.getTexMalloSpecular());

renderer.setLICParams(&licParams);
renderer.setLAOParams(&laoParams);

//compute once noise LAO
renderer.loadGLSLShader("#define NOISE_LAO_COMPUTE");
renderer.computeNoiseLAO();
renderer.loadGLSLShader();

//renderer.renderLICVolume();
//renderer.updateLICVolume();
CHECK_FOR_OGL_ERROR();
renderer.updateLightPos();
renderer.updateSlices();


}

void initMultiThread(void)
{
//Create Multi-Thread for loading and Texture generation
dc = wglGetCurrentDC();
glrc_main = wglCreateContext(dc);
glrc_load = wglCreateContext(dc);
BOOL error = wglShareLists(glrc_main, glrc_load);
if (error == FALSE)
{
DWORD errorCode = GetLastError();
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
MessageBox(NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION);
LocalFree(lpMsgBuf);
//Destroy the GL context and just use 1 GL context
wglDeleteContext(glrc_load);
}
wglMakeCurrent(dc, glrc_main);
}

int main(int argc, char **argv)
{
Expand Down Expand Up @@ -829,9 +979,15 @@ int main(int argc, char **argv)
fprintf(stderr, "GLEW error");
return 1;
}

initMultiThread();
initGL();
init();

// Load a thread to calculate LIC volume
//std::thread load_thread(loadVecterData, &vd);
//load_thread.join();

glutMainLoop();

return 0;
Expand Down
Loading