void VRMaterial::setTexture(VRTexturePtr img, bool alpha) { if (img == 0) return; auto md = mats[activePass]; if (md->texChunk == 0) { md->texChunk = TextureObjChunk::create(); md->mat->addChunk(md->texChunk); } if (md->envChunk == 0) { md->envChunk = TextureEnvChunk::create(); md->mat->addChunk(md->envChunk); } md->texture = img; md->texChunk->setImage(img->getImage()); md->envChunk->setEnvMode(GL_MODULATE); if (alpha && img->getImage()->hasAlphaChannel() && md->blendChunk == 0) { md->blendChunk = BlendChunk::create(); md->mat->addChunk(md->blendChunk); } if (alpha && img->getImage()->hasAlphaChannel()) { md->blendChunk->setSrcFactor ( GL_SRC_ALPHA ); md->blendChunk->setDestFactor ( GL_ONE_MINUS_SRC_ALPHA ); } if (img->getInternalFormat() != -1) md->texChunk->setInternalFormat(img->getInternalFormat()); }
void VRSSAO::setSSAOparams(float radius, int kernelSize, int noiseSize) { if (!ssao_mat) return; int kernelSize2 = kernelSize*kernelSize; int noiseSize2 = noiseSize*noiseSize; ssao_mat->setActivePass(0); ssao_mat->setShaderParameter<int>("KernelSize", kernelSize); ssao_mat->setShaderParameter<float>("uRadius", radius); ssao_mat->setShaderParameter<float>("texScale", 1.0/noiseSize); ssao_mat->setShaderParameter<int>("NoiseSize", noiseSize); ssao_mat->setShaderParameter<float>("uRadius", radius); // kernel vector<float> kernel(3*kernelSize2); vector<float> noise(3*noiseSize2); srand(0); for (int i = 0; i < kernelSize2; i++) { Vec3f k(random(-1,1), random(-1,1), random(0,1)); k.normalize(); k *= random(0,1); float scale = float(i) / float(kernelSize2); k *= lerp(0.1, 1, scale * scale); kernel[i*3+0] = k[0]; kernel[i*3+1] = k[1]; kernel[i*3+2] = k[2]; } // noise texture for (int i = 0; i < noiseSize2; i++) { Vec3f n(random(-1,1), random(-1,1), 0); n.normalize(); noise[i*3+0] = n[0]; noise[i*3+1] = n[1]; noise[i*3+2] = n[2]; } // kernel texture VRTexturePtr img = VRTexture::create(); img->getImage()->set(OSG::Image::OSG_RGB_PF, kernelSize, kernelSize, 1, 0, 1, 0.0, (const uint8_t*)&kernel[0], OSG::Image::OSG_FLOAT32_IMAGEDATA); ssao_mat->setTextureAndUnit(img, 3); ssao_mat->setMagMinFilter("GL_NEAREST", "GL_NEAREST"); // noise texture VRTexturePtr imgN = VRTexture::create(); imgN->getImage()->set(OSG::Image::OSG_RGB_PF, kernelSize, kernelSize, 1, 0, 1, 0.0, (const uint8_t*)&kernel[0], OSG::Image::OSG_FLOAT32_IMAGEDATA); ssao_mat->setTextureAndUnit(imgN, 4); ssao_mat->setMagMinFilter("GL_NEAREST", "GL_NEAREST"); // blur size ssao_mat->setActivePass(1); ssao_mat->setShaderParameter<int>("uBlurSize", noiseSize); }
void transcode(AVFrame *frame, AVCodecContext* codec_context, SwsContext* sws_context, int i) { valid = 0; if (!capture) return; AVPacket pkt; av_init_packet(&pkt); pkt.data = NULL; // packet data will be allocated by the encoder pkt.size = 0; const unsigned char* data = capture->getImage()->getData(); if (codec_context->pix_fmt == AV_PIX_FMT_YUV420P) { const int in_linesize[1] = { 3 * codec_context->width }; sws_scale(sws_context, (const uint8_t * const *)&data, in_linesize, 0, codec_context->height, frame->data, frame->linesize); } /* encode the image */ frame->pts = i; int ret = avcodec_encode_video2(codec_context, &pkt, frame, &valid); if (ret < 0) { fprintf(stderr, "Error encoding frame\n"); return; } pktSize = pkt.size; pktData = new char[pktSize]; memcpy(pktData, pkt.data, pktSize); av_free_packet(&pkt); capture = 0; }
void VRMaterial::setTexture(char* data, int format, Vec3i dims, bool isfloat) { VRTexturePtr img = VRTexture::create(); int pf = Image::OSG_RGB_PF; if (format == 4) pf = Image::OSG_RGBA_PF; int f = Image::OSG_UINT8_IMAGEDATA; if (isfloat) f = Image::OSG_FLOAT32_IMAGEDATA; img->getImage()->set( pf, dims[0], dims[1], dims[2], 1, 1, 0, (const UInt8*)data, f); if (format == 4) setTexture(img, true); if (format == 3) setTexture(img, false); setShaderParameter<int>("is3DTexture", dims[2] > 1); }
void VRMaterial::setTextureAndUnit(VRTexturePtr img, int unit) { if (img == 0) return; auto md = mats[activePass]; auto texChunk = getTexChunk(unit); texChunk->setImage(img->getImage()); }
VRMatData* copy() { VRMatData* m = new VRMatData(); m->mat = ChunkMaterial::create(); if (colChunk) { m->colChunk = dynamic_pointer_cast<MaterialChunk>(colChunk->shallowCopy()); m->mat->addChunk(m->colChunk); } if (blendChunk) { m->blendChunk = dynamic_pointer_cast<BlendChunk>(blendChunk->shallowCopy()); m->mat->addChunk(m->blendChunk); } if (envChunk) { m->envChunk = dynamic_pointer_cast<TextureEnvChunk>(envChunk->shallowCopy()); m->mat->addChunk(m->envChunk); } if (texChunk) { m->texChunk = dynamic_pointer_cast<TextureObjChunk>(texChunk->shallowCopy()); m->mat->addChunk(m->texChunk); } if (genChunk) { m->genChunk = dynamic_pointer_cast<TexGenChunk>(genChunk->shallowCopy()); m->mat->addChunk(m->genChunk); } if (lineChunk) { m->lineChunk = dynamic_pointer_cast<LineChunk>(lineChunk->shallowCopy()); m->mat->addChunk(m->lineChunk); } if (pointChunk) { m->pointChunk = dynamic_pointer_cast<PointChunk>(pointChunk->shallowCopy()); m->mat->addChunk(m->pointChunk); } if (polygonChunk) { m->polygonChunk = dynamic_pointer_cast<PolygonChunk>(polygonChunk->shallowCopy()); m->mat->addChunk(m->polygonChunk); } if (twoSidedChunk) { m->twoSidedChunk = dynamic_pointer_cast<TwoSidedLightingChunk>(twoSidedChunk->shallowCopy()); m->mat->addChunk(m->twoSidedChunk); } if (clipChunk) { m->clipChunk = dynamic_pointer_cast<ClipPlaneChunk>(clipChunk->shallowCopy()); m->mat->addChunk(m->clipChunk); } if (stencilChunk) { m->stencilChunk = dynamic_pointer_cast<StencilChunk>(stencilChunk->shallowCopy()); m->mat->addChunk(m->stencilChunk); } if (shaderChunk) { m->shaderChunk = ShaderProgramChunk::create(); m->mat->addChunk(m->shaderChunk); } if (texture) { ImageRecPtr img = dynamic_pointer_cast<Image>(texture->getImage()->shallowCopy()); m->texture = VRTexture::create(img); m->texChunk->setImage(img); } if (vProgram) { m->vProgram = dynamic_pointer_cast<ShaderProgram>(vProgram->shallowCopy()); m->shaderChunk->addShader(m->vProgram); } if (fProgram) { m->fProgram = dynamic_pointer_cast<ShaderProgram>(fProgram->shallowCopy()); m->shaderChunk->addShader(m->fProgram); } if (gProgram) { m->gProgram = dynamic_pointer_cast<ShaderProgram>(gProgram->shallowCopy()); m->shaderChunk->addShader(m->gProgram); } if (video) ; // TODO m->vertexScript = vertexScript; m->fragmentScript = fragmentScript; m->geometryScript = geometryScript; return m; }