void ParamsBasicApp::update(){ receiver.update(); drawCube = receiver.getValue(0); mCameraDistance = receiver.getValue(1,1,100); r = receiver.getValue(2); g = receiver.getValue(3); // b = receiver.getValue(4); a = receiver.getValue(4,-1,1); e = receiver.getValue(5,-1,1); c = receiver.getValue(6,-1,1); // x = receiver.getValue(8,-1,1); // y = receiver.getValue(9,-1,1); // z = receiver.getValue(10,-1,1); mEye = Vec3f(a,e,c); Color( CM_HSV, 0.7f, 0.85f, 1.0f ); mColor.set(r, g, b,1); mColor.set( Color( CM_HSV, r, g, 1 )); Vec3f nDist = mEye * mCameraDistance; mCam.lookAt( nDist,Vec3f( 0, 0, 0 ) ); // mObjOrientation.set(a, e, c, d); // mLightDirection.set(x, y, z); // mLightDirection.rotateX(x); // mLightDirection.rotateX(y); // mLightDirection.rotateX(z); console() << a << e << c << endl; }
/****************************************************************************** * Renders a 2d polyline in the viewport. ******************************************************************************/ void ViewportSceneRenderer::render2DPolyline(const Point2* points, int count, const ColorA& color, bool closed) { OVITO_STATIC_ASSERT(sizeof(points[0]) == 2*sizeof(GLfloat)); // Load OpenGL shader. QOpenGLShaderProgram* shader = loadShaderProgram("line", ":/core/glsl/lines/line.vs", ":/core/glsl/lines/line.fs"); if(!shader->bind()) throw Exception(tr("Failed to bind OpenGL shader.")); bool wasDepthTestEnabled = glIsEnabled(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST); GLint vc[4]; glGetIntegerv(GL_VIEWPORT, vc); QMatrix4x4 tm; tm.ortho(vc[0], vc[0] + vc[2], vc[1] + vc[3], vc[1], -1, 1); OVITO_CHECK_OPENGL(shader->setUniformValue("modelview_projection_matrix", tm)); QOpenGLBuffer vertexBuffer; if(glformat().majorVersion() >= 3) { if(!vertexBuffer.create()) throw Exception(tr("Failed to create OpenGL vertex buffer.")); if(!vertexBuffer.bind()) throw Exception(tr("Failed to bind OpenGL vertex buffer.")); vertexBuffer.allocate(points, 2 * sizeof(GLfloat) * count); OVITO_CHECK_OPENGL(shader->enableAttributeArray("position")); OVITO_CHECK_OPENGL(shader->setAttributeBuffer("position", GL_FLOAT, 0, 2)); vertexBuffer.release(); } else { OVITO_CHECK_OPENGL(glEnableClientState(GL_VERTEX_ARRAY)); OVITO_CHECK_OPENGL(glVertexPointer(2, GL_FLOAT, 0, points)); } if(glformat().majorVersion() >= 3) { OVITO_CHECK_OPENGL(shader->disableAttributeArray("color")); OVITO_CHECK_OPENGL(shader->setAttributeValue("color", color.r(), color.g(), color.b(), color.a())); } else { OVITO_CHECK_OPENGL(glColor4(color)); } OVITO_CHECK_OPENGL(glDrawArrays(closed ? GL_LINE_LOOP : GL_LINE_STRIP, 0, count)); if(glformat().majorVersion() >= 3) { shader->disableAttributeArray("position"); } else { OVITO_CHECK_OPENGL(glDisableClientState(GL_VERTEX_ARRAY)); } shader->release(); if(wasDepthTestEnabled) glEnable(GL_DEPTH_TEST); }
/****************************************************************************** * Determines the display color of a single particle. ******************************************************************************/ ColorA ParticleDisplay::particleColor(size_t particleIndex, ParticlePropertyObject* colorProperty, ParticleTypeProperty* typeProperty, ParticlePropertyObject* selectionProperty, ParticlePropertyObject* transparencyProperty) { OVITO_ASSERT(colorProperty == nullptr || colorProperty->type() == ParticleProperty::ColorProperty); OVITO_ASSERT(typeProperty == nullptr || typeProperty->type() == ParticleProperty::ParticleTypeProperty); OVITO_ASSERT(selectionProperty == nullptr || selectionProperty->type() == ParticleProperty::SelectionProperty); OVITO_ASSERT(transparencyProperty == nullptr || transparencyProperty->type() == ParticleProperty::TransparencyProperty); // Check if particle is selected. if(selectionProperty) { OVITO_ASSERT(particleIndex < selectionProperty->size()); if(selectionProperty->getInt(particleIndex)) return selectionParticleColor(); } ColorA c = defaultParticleColor(); if(colorProperty) { // Take particle color directly from the color property. OVITO_ASSERT(particleIndex < colorProperty->size()); c = colorProperty->getColor(particleIndex); } else if(typeProperty) { // Return color based on particle types. OVITO_ASSERT(particleIndex < typeProperty->size()); ParticleType* ptype = typeProperty->particleType(typeProperty->getInt(particleIndex)); if(ptype) c = ptype->color(); } // Apply alpha component. if(transparencyProperty) { OVITO_ASSERT(particleIndex < transparencyProperty->size()); c.a() = FloatType(1) - transparencyProperty->getFloat(particleIndex); } return c; }
void redEyeApp::update() { //--osc input while(mListener.hasWaitingMessages()) { osc::Message msg; mListener.getNextMessage(&msg); mOsc= msg.getAddress(); for(uint32_t i= 0; i<msg.getNumArgs(); i++) { mOsc= mOsc+" "+msg.getArgAsString(i, true); } if(msg.getAddress()=="/numSamples") { mNumSamples= math<int32_t>::clamp(msg.getArgAsInt32(0, true), 1, 2048); } else if(msg.getAddress()=="/downSample") { mDownSample= math<int32_t>::clamp(msg.getArgAsInt32(0, true), 0, 2047); } else if(msg.getAddress()=="/amplitude") { mAmplitude= msg.getArgAsFloat(0, true); } else if(msg.getAddress()=="/width") { mWidth= math<float>::clamp(msg.getArgAsFloat(0, true), 0, 1000); } else if(msg.getAddress()=="/colorBack") { ColorA col= ColorA(0, 0, 0, 1); for(uint32_t i= 0; i<min(msg.getNumArgs(), 4); i++) { col[i]= msg.getArgAsFloat(i, true); } mColorBack.set(col.r, col.g, col.b, col.a); } else if(msg.getAddress()=="/scale0") { Vec3f sca= Vec3f(1.0f, 1.0f, 1.0f); for(uint32_t i= 0; i<min(msg.getNumArgs(), 3); i++) { msg.getArgAsFloat(i, true); } mScale0.set(sca.x, sca.y, sca.z); } else if(msg.getAddress()=="/scale1") { Vec3f sca= Vec3f(1.0f, 1.0f, 1.0f); for(uint32_t i= 0; i<min(msg.getNumArgs(), 3); i++) { msg.getArgAsFloat(i, true); } mScale1.set(sca.x, sca.y, sca.z); } else if(msg.getAddress()=="/rotate0") { Vec3f rot= Vec3f::zero(); for(uint32_t i= 0; i<min(msg.getNumArgs(), 3); i++) { rot[i]= msg.getArgAsFloat(i, true); } mRotate0.set(rot.x, rot.y, rot.z); } else if(msg.getAddress()=="/rotate1") { Vec3f rot= Vec3f::zero(); for(uint32_t i= 0; i<min(msg.getNumArgs(), 3); i++) { rot[i]= msg.getArgAsFloat(i, true); } mRotate1.set(rot.x, rot.y, rot.z); } else if(msg.getAddress()=="/translate0") { Vec3f tra= Vec3f::zero(); for(uint32_t i= 0; i<min(msg.getNumArgs(), 3); i++) { tra[i]= msg.getArgAsFloat(i, true); } mTranslate0.set(tra.x, tra.y, tra.z); } else if(msg.getAddress()=="/translate1") { Vec3f tra= Vec3f::zero(); for(uint32_t i= 0; i<min(msg.getNumArgs(), 3); i++) { tra[i]= msg.getArgAsFloat(i, true); } mTranslate1.set(tra.x, tra.y, tra.z); } else if(msg.getAddress()=="/color0") { ColorA col= ColorA(0, 0, 0, 1); for(uint32_t i= 0; i<min(msg.getNumArgs(), 4); i++) { col[i]= msg.getArgAsFloat(i, true); } mColor0.set(col.r, col.g, col.b, col.a); } else if(msg.getAddress()=="/color1") { ColorA col= ColorA(0, 0, 0, 1); for(uint32_t i= 0; i<min(msg.getNumArgs(), 4); i++) { col[i]= msg.getArgAsFloat(i, true); } mColor1.set(col.r, col.g, col.b, col.a); } } //--audio input mPcmBuffer= mInput.getPcmBuffer(); if(mPcmBuffer) { mBufferSize= mPcmBuffer->getSampleCount(); //std::cout<<"mBufferSize: "<<mBufferSize<<std::endl; mBufferLeft= mPcmBuffer->getChannelData(audio::CHANNEL_FRONT_LEFT); //mBufferRight= mPcmBuffer->getChannelData(audio::CHANNEL_FRONT_RIGHT); mFftLeft= audio::calculateFft(mPcmBuffer->getChannelData(audio::CHANNEL_FRONT_LEFT), mBufferSize/2); //mFftRight= audio::calculateFft(mPcmBuffer->getChannelData(audio::CHANNEL_FRONT_LEFT), mBufferSize/2); mAmplitude= 0.0f; for(uint32_t i= 0; i<mBufferSize; i++) { mAmplitude += abs(mBufferLeft->mData[i]); } mAmplitude /= float(mBufferSize); //average amplitude Surface32f mSurfaceSnd(mBufferSize, 1, true); Surface32f::Iter sndIter(mSurfaceSnd.getIter()); uint32_t i= 0; while(sndIter.line()) { while(sndIter.pixel()) { sndIter.r()= mBufferLeft->mData[i]; i++; } } mTextureSnd= gl::Texture(mSurfaceSnd); Surface32f mSurfaceFft(mBufferSize/2, 1, true); Surface32f::Iter fftIter(mSurfaceFft.getIter()); uint32_t j= 0; float *fftBuffer= mFftLeft.get(); while(fftIter.line()) { while(fftIter.pixel()) { fftIter.r()= fftBuffer[j]; j++; } } mTextureFft= gl::Texture(mSurfaceFft); } //--shaders if(mShader!=NULL) { if((fs::last_write_time(mPathFrag)>mTimeFrag) || (fs::last_write_time(mPathVert)>mTimeVert)) { loadShader(); //hot-loading shader } } mFps= getAverageFps(); }
void GlslParams::parse( const string &source ) { auto trim = []( const string &input, const string &key ) { string temp = input; size_t foundKey = temp.find( key ); while( foundKey != string::npos ) { temp = temp.replace( foundKey, key.length(), "" ); foundKey = temp.find( key ); } return temp; }; multimap<string, string> uiTypeMap = { { "int", "slider" }, { "int", "dialer" }, { "float", "ui" }, { "float", "slider" }, { "float", "dialer" }, { "vec2", "pad" }, { "vec2", "range" }, { "vec2", "ui" }, { "vec2", "slider" }, { "vec2", "dialer" }, { "vec3", "ui" }, { "vec3", "slider" }, { "vec3", "dialer" }, { "vec4", "ui" }, { "vec4", "slider" }, { "vec4", "color" }, { "vec4", "dialer" }, { "bool", "button" }, { "bool", "toggle" } }; bool ignore = false; vector<string> lines = split( source, '\n' ); for( auto &it : lines ) { string original = it; string line = it; string ignoreStart( "/*" ); string ignoreEnd( "*/" ); if( !ignore && line.find( ignoreStart ) != string::npos ) { ignore = true; } if( ignore && line.find( ignoreEnd ) == string::npos ) { continue; } else { ignore = false; } std::transform( line.begin(), line.end(), line.begin(), ::tolower ); string uniform( "uniform " ); string semicolon( ";" ); string colon( ":" ); string space( " " ); string comment( "//" ); string comma( "," ); string newLine( "/n" ); size_t foundUniform = line.find( uniform ); if( foundUniform == string::npos ) { continue; } size_t foundComment = line.find( comment ); if( foundComment == string::npos || foundUniform > foundComment ) { continue; } size_t foundType = string::npos; size_t foundUIType = string::npos; string type; string uitype; string key; bool valid = false; for( auto &ui : uiTypeMap ) { foundType = line.find( ui.first ); // string tempkey = comment + ui.second + colon; string tempkey = comment + ui.second; foundUIType = line.find( tempkey ); if( foundType != string::npos && foundUIType != string::npos ) { valid = true; type = ui.first; uitype = ui.second; key = tempkey; break; } } if( !valid ) { continue; } string uniformName = original; size_t foundSemicolon = uniformName.find( semicolon ); uniformName = uniformName.substr( 0, foundSemicolon ); uniformName = uniformName.replace( foundType, type.length(), "" ); uniformName = uniformName.replace( foundUniform, uniform.length(), "" ); uniformName = trim( uniformName, " " ); string uiParams = line.substr( foundUIType + key.length() ); uiParams = trim( uiParams, ":" ); vector<string> params = split( uiParams, "," ); if( params.size() == 1 ) { if( params[0].length() == 0 ) { params.clear(); } } int size = params.size(); vector<float> values; bool invalidParams = false; for( auto &it : params ) { try { values.emplace_back( stof( it ) ); } catch( std::exception &exc ) { invalidParams = true; break; } } if( invalidParams ) { continue; } valid = false; if( type == "bool" ) { if( size == 1 ) { bool val = values[0] > 0.5 ? true : false; mBoolParams[uniformName] = val; } else { mBoolParams[uniformName] = true; } valid = true; } else if( type == "int" ) { if( size > 2 ) { mIntParams[uniformName] = values[2]; mIntRanges[uniformName] = { values[0], values[1] }; } else { mIntParams[uniformName] = 50; mIntRanges[uniformName] = { 0, 100 }; } valid = true; } else if( type == "float" ) { if( size > 2 ) { mFloatParams[uniformName] = values[2]; mFloatRanges[uniformName] = { values[0], values[1] }; } else { mFloatParams[uniformName] = 0.5f; mFloatRanges[uniformName] = { 0.0f, 1.0f }; } valid = true; } else if( type == "vec2" ) { if( size > 2 ) { if( uitype == "range" && size > 3 ) { mVec2Params[uniformName] = vec2( values[2], values[3] ); } else { mVec2Params[uniformName] = vec2( values[2] ); } mVec2Ranges[uniformName] = { values[0], values[1] }; } else { mVec2Params[uniformName] = vec2( 0.25f, 0.75f ); mVec2Ranges[uniformName] = { 0.0f, 1.0f }; } valid = true; } else if( type == "vec3" ) { if( size > 2 ) { mVec3Params[uniformName] = vec3( values[2] ); mVec3Ranges[uniformName] = { values[0], values[1] }; } else { mVec3Params[uniformName] = vec3( 0.5f ); mVec3Ranges[uniformName] = { 0.0f, 1.0f }; } valid = true; } else if( type == "vec4" && uitype == "color" ) { ColorA clr; if( size > 3 ) { clr.set( ColorModel::CM_RGB, vec4( values[0], values[1], values[2], values[3] ) ); mColorParams[uniformName] = clr; } else { clr.set( ColorModel::CM_RGB, vec4( 1.0f, 1.0f, 1.0f, 1.0f ) ); mColorParams[uniformName] = clr; } valid = true; } else if( type == "vec4" ) { if( size > 2 ) { mVec4Params[uniformName] = vec4( values[2] ); mVec4Ranges[uniformName] = { values[0], values[1] }; } else { mVec4Params[uniformName] = vec4( 0.5f ); mVec4Ranges[uniformName] = { 0.0f, 1.0f }; } valid = true; } if( valid ) { mTypeMap.insert( { uniformName, { type, uitype } } ); mParamOrder[mParamOrder.size()] = uniformName; } } }