void VHParticlesRender::initParticlesRender(){ // Create the CUDPP radix sort CUDPPConfiguration sortConfig; sortConfig.algorithm = CUDPP_SORT_RADIX; sortConfig.datatype = CUDPP_FLOAT; sortConfig.op = CUDPP_ADD; sortConfig.options = CUDPP_OPTION_KEY_VALUE_PAIRS; cudppPlan(&m_sortHandle, sortConfig, pSys->nParts, 1, 0); //-----shaders /*char currentPath[_MAX_PATH]; getcwd(currentPath, _MAX_PATH); printf("Path : %s", currentPath);*/ simpleSpriteProg = new GLSLProgram("sprite.vs", "sprite.gs", "simpleSprite.ps"); shadowedSpriteProg = new GLSLProgram("sprite.vs", "sprite.gs", "shadowedSprite.ps"); shadowMapSpriteProg = new GLSLProgram("sprite.vs", "sprite.gs", "ShadowMapSprite.ps"); displayTexProg = new GLSLProgram("passThru.vs", "texture2D.ps"); blurProg = new GLSLProgram("passThru.vs", "blur.ps"); if(spritePath) loadSprite(spritePath); initFbos(width, height, true); }
void VHParticlesRender::draw(){ if (pSys->nParts == -1) return; if(displayMode == SHADOWED_SPRITES) { calcVectors(); cu::float3 halfVec = cu::make_float3(halfVector.x,halfVector.y,halfVector.z); calcDepthCu(pSys->dev_pos, pSys->dev_keys, pSys->dev_indices, halfVec, pSys->nParts); if (sortParts) cudppSort(m_sortHandle, pSys->dev_keys, pSys->dev_indices, 32, pSys->nParts); } if((displayMode == SPRITES || displayMode == POINTS) && sortParts) { glGetFloatv(GL_MODELVIEW_MATRIX, (float *) modelView.get_value()); viewVector = -vec3f(modelView.get_row(2)); cu::float3 viewVec = cu::make_float3(viewVector.x, viewVector.y, viewVector.y); //printf("view vec : %f, %f, %f \n", viewVector.x, viewVector.y, viewVector.z); calcDepthCu(pSys->dev_pos, pSys->dev_keys, pSys->dev_indices, viewVec, pSys->nParts); cudppSort(m_sortHandle, pSys->dev_keys, pSys->dev_indices, 32, pSys->nParts); } pSys->posVbo->map(); pSys->colourVbo->map(); pSys->indexVbo->map(); pSys->updateVBOs(); pSys->posVbo->unmap(); pSys->colourVbo->unmap(); pSys->indexVbo->unmap(); switch (displayMode) { case POINTS: glPointSize(pointSize); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); pSys->posVbo->bind(); glVertexPointer(3, GL_FLOAT, 0, 0); glEnableClientState(GL_VERTEX_ARRAY); pSys->colourVbo->bind(); glColorPointer(4, GL_FLOAT, 0, 0); glEnableClientState(GL_COLOR_ARRAY); if (blendingMode == ADD) { glBlendFunc( GL_SRC_ALPHA, GL_ONE ); glDrawArrays(GL_POINTS, 0, pSys->nParts); } else { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); if(sortParts){ pSys->indexVbo->bind(); glDrawElements(GL_POINTS, pSys->nParts, GL_UNSIGNED_INT, 0); pSys->indexVbo->unbind(); } else { glDrawArrays(GL_POINTS, 0, pSys->nParts); } } pSys->posVbo->unbind(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisable(GL_BLEND); break; case LINES: glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc( GL_SRC_ALPHA, GL_ONE ); pSys->posVbo->bind(); glVertexPointer(3, GL_FLOAT, 0, 0); glEnableClientState(GL_VERTEX_ARRAY); pSys->colourVbo->bind(); glColorPointer(4, GL_FLOAT, 0, 0); glEnableClientState(GL_COLOR_ARRAY); glLineWidth(lineWidth); for (int i = 0; i<pSys->nLeadParts; i++) { glDrawArrays(GL_LINE_STRIP, i*pSys->trailLength, pSys->trailLength); } pSys->posVbo->unbind(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisable(GL_BLEND); break; case SPRITES: glDisable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc( GL_SRC_ALPHA, GL_ONE ); pSys->posVbo->bind(); glVertexPointer(3, GL_FLOAT, 0, 0); glEnableClientState(GL_VERTEX_ARRAY); pSys->colourVbo->bind(); glColorPointer(4, GL_FLOAT, 0, 0); glEnableClientState(GL_COLOR_ARRAY); simpleSpriteProg->enable(); simpleSpriteProg->setUniform1f("pointRadius",pointSize); simpleSpriteProg->bindTexture("sDiffuseMap",TextureManager::Inst()->m_texID[id1],GL_TEXTURE_2D,0); if (blendingMode == ADD) { glBlendFunc( GL_SRC_ALPHA, GL_ONE ); glDrawArrays(GL_POINTS, 0, pSys->nParts); } else { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); if(sortParts){ pSys->indexVbo->bind(); glDrawElements(GL_POINTS, pSys->nParts, GL_UNSIGNED_INT, 0); pSys->indexVbo->unbind(); } else { glDrawArrays(GL_POINTS, 0, pSys->nParts); } } simpleSpriteProg->disable(); pSys->posVbo->unbind(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); break; case SHADOWED_SPRITES : GLfloat currentViewport[4]; glGetFloatv(GL_VIEWPORT, currentViewport); if(width != currentViewport[2] || height != currentViewport[3]) initFbos(currentViewport[2],currentViewport[3], false); drawSlices(); //glutReportErrors(); Fbo::unbind(); glViewport(0, 0, width, height); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); int mm; glGetIntegerv ( GL_MATRIX_MODE, &mm ); displayTexProg->enable(); displayTexProg->bindTexture("tex", imageTex, GL_TEXTURE_2D, 0); drawQuad(); displayTexProg->disable(); if(displayLightBuffer) { displayTexProg->bindTexture("tex", lightTex[srcLightTexture], GL_TEXTURE_2D, 0); glViewport(0, 0, lightBufferSize, lightBufferSize); drawQuad(); displayTexProg->disable(); } //calcVectors(); glViewport(0, 0, width, height); if (displayVectors) { debugVectors(); } glutReportErrors(); break; } }
ofxEdenBiosphere& ofxEdenBiosphere::allocate(int _width, int _height){ initFbos(_width, _height); string fragmentShader = "#version 120\n \ #extension GL_ARB_texture_rectangle : enable \n \ \ float kernel[9];\ vec2 offset[9];\ uniform sampler2DRect conditions;\ uniform sampler2DRect biosphere;\ uniform sampler2DRect hydrosphere;\ uniform sampler2DRect change;\ uniform float diffU;\ uniform float diffV;\ uniform float f;\ uniform float k;\ uniform float width;\ \ void main(void) {\ vec2 st = gl_TexCoord[0].st;\ float alt = texture2DRect( conditions, st ).r;\ float nut = texture2DRect( conditions, st ).g;\ float wtr = texture2DRect( conditions, st ).b;\ float hyd = texture2DRect( hydrosphere, st ).b;\ float chn = texture2DRect( change, st ).r;\ float bioV = 0.0;\ float bioU = 0.0;\ \ if ( wtr == 0.0 ){\ bioV = texture2DRect( biosphere, st ).g + hyd * 0.5;\ bioU = texture2DRect( biosphere, st ).b ;\ \ kernel[0] = 0.707106781;\ kernel[1] = 1.0;\ kernel[2] = 0.707106781;\ kernel[3] = 1.0;\ kernel[4] = -6.82842712;\ kernel[5] = 1.0;\ kernel[6] = 0.707106781;\ kernel[7] = 1.0;\ kernel[8] = 0.707106781;\ \ offset[0] = vec2( -1.0, -1.0);\ offset[1] = vec2( 0.0, -1.0);\ offset[2] = vec2( 1.0, -1.0);\ \ offset[3] = vec2( -1.0, 0.0);\ offset[4] = vec2( 0.0, 0.0);\ offset[5] = vec2( 1.0, 0.0);\ \ offset[6] = vec2( -1.0, 1.0);\ offset[7] = vec2( 0.0, 1.0);\ offset[8] = vec2( 1.0, 1.0);\ \ vec2 lap = vec2( 0.0, 0.0 );\ \ for( int i=0; i < 9; i++ ){\ vec2 tmp = vec2(texture2DRect( biosphere, st + offset[i] ).g, texture2DRect( biosphere, st + offset[i] ).b);\ lap += tmp * kernel[i];\ }\ \ float F = f + hyd * 0.025 - 0.0005;\ float K = k + hyd * 0.025 - 0.0005;\ \ float uvv = bioU * bioV * bioV;\ float du = diffU * lap.g - uvv + F * (1.0 - bioU);\ float dv = diffV * lap.r + uvv - (F + K) * bioV;\ bioU += du * 0.6;\ bioV += dv * 0.6;\ \ bioV -= alt*0.001;\ bioU -= alt*0.001;\ \ bioV -= (1.0-nut)*0.001;\ bioU -= (1.0-nut)*0.001;\ \ if (chn <= 0.1) \ chn = 1.0;\ \ bioV *= chn;\ bioU *= chn;\ }\ \ gl_FragColor.rgb = vec3(0.0, clamp( bioV, 0.0, 1.0 ), clamp( bioU, 0.0, 1.0 ));\ gl_FragColor.a = 1.0;\ }"; shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragmentShader); shader.linkProgram(); //loadShader("shaders/biosphere.frag"); flock.allocate(width, height, 640).setParticleSize(20).setTimpeStep(0.0005); return * this; }