朱丽叶—Cuda+OSG
#include <cuda_runtime.h>
#include <osg/Image>
const int DIM = ; typedef struct cuComplex
{
float r;
float i;
__device__ cuComplex(float a, float b)
:r(a)
,i(b)
{
} __device__ float magnitude2(void)
{
return r * r + i * i;
} __device__ cuComplex operator * (const cuComplex & a)
{
return cuComplex(r * a.r - i * a.i, i * a.r + r * a.i);
} __device__ cuComplex operator + (const cuComplex & a)
{
return cuComplex(r + a.r, i + a.i);
}
}; __device__ int julia(int x, int y)
{
const float scale = 1.5;
float jx = scale * (float)(DIM/ - x)/(DIM/);
float jy = scale * (float)(DIM/ -y)/(DIM/);
cuComplex c(-0.8, 0.156);
cuComplex a(jx, jy); for (int i = ; i < ; ++i)
{
a = a * a + c;
if (a.magnitude2() > )
return ;
}
return ;
} __global__ void kernel(unsigned char * ptr)
{
int x = blockIdx.x;
int y = blockIdx.y;
int offset = x + y * gridDim.x;
int juliaValue = julia(x, y);
ptr[offset * + ] = * juliaValue;
ptr[offset * + ] = ;
ptr[offset * + ] = ;
ptr[offset * + ] = ; } extern "C" void SetUp(osg::Image * image)
{
unsigned char * dev_bitmap;
cudaMalloc((void **)&dev_bitmap, DIM * DIM * );
dim3 grid(DIM, DIM);
kernel<<<grid, >>>(dev_bitmap);
cudaMemcpy(image->data(), dev_bitmap, DIM * DIM * , cudaMemcpyDeviceToHost);
cudaFree(dev_bitmap);
}
#include <osgViewer/Viewer>
#include <osg/Texture2D>
#include <osg/Image>
#include <osgDB/WriteFile>
#include <osgViewer/ViewerEventHandlers>
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgGAd.lib")
const int DIM = ; osg::ref_ptr<osg::Geode> CreateQuad()
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vArray = new osg::Vec3Array;
osg::ref_ptr<osg::Vec2Array> tArray = new osg::Vec2Array;
osg::ref_ptr<osg::Vec3Array> nArray = new osg::Vec3Array;
vArray->push_back(osg::Vec3(-1.0, 0.0, -1.0));
vArray->push_back(osg::Vec3(1.0, 0.0, -1.0));
vArray->push_back(osg::Vec3(1.0, 0.0, 1.0));
vArray->push_back(osg::Vec3(-1.0, 0.0, 1.0)); tArray->push_back(osg::Vec2(0.0, 0.0));
tArray->push_back(osg::Vec2(1.0, 0.0));
tArray->push_back(osg::Vec2(1.0, 1.0));
tArray->push_back(osg::Vec2(0.0, 1.0)); nArray->push_back(osg::Vec3(0.0, 1.0, 0.0));
geometry->setVertexArray(vArray.get());
geometry->setTexCoordArray(, tArray.get());
geometry->setNormalArray(nArray.get());
geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, , vArray->size()));
geode->addDrawable(geometry.get());
return geode.get();
} osg::ref_ptr<osg::StateSet> CreateStateSet(osg::Image * image)
{
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet; osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D();
texture->setImage(image);
stateSet->setTextureAttributeAndModes(, texture.get(), osg::StateAttribute::ON);
return stateSet.get();
}
extern "C" void SetUp(osg::Image * image);
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage(DIM, DIM, , GL_RGBA, GL_UNSIGNED_BYTE);
SetUp(image.get()); osg::ref_ptr<osg::StateSet> stateSet = CreateStateSet(image.get());
osg::ref_ptr<osg::Geode> quad = CreateQuad();
quad->setStateSet(stateSet.get());
viewer->setSceneData(quad.get());
viewer->getCamera()->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
viewer->addEventHandler(new osgViewer::StatsHandler);
viewer->setUpViewInWindow(, , , );
viewer->run();
return ;
}
朱丽叶—Cuda+OSG的更多相关文章
- osg + cuda
#include <osg/Notify> #include <osgViewer/Viewer> #include <osgCompute/Memory> #in ...
- CUDA[2] Hello,World
Section 0:Hello,World 这次我们亲自尝试一下如何用粗(CU)大(DA)写程序 CUDA最新版本是7.5,然而即使是最新版本也不兼容VS2015 ...推荐使用VS2012 进入VS ...
- CUDA[1] Introductory
Section 0 :Induction of CUDA CUDA是啥?CUDA®: A General-Purpose Parallel Computing Platform and Program ...
- Couldn't open CUDA library cublas64_80.dll etc. tensorflow-gpu on windows
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_load ...
- OSG计时器与时间戳
static osg::Timer* sendMsgTimer = new osg::Timer; if (sendMsgTimer->time_m()>100)//100ms {// d ...
- ubuntu 16.04 + N驱动安装 +CUDA+Qt5 + opencv
Nvidia driver installation(after download XX.run installation file) 1. ctrl+Alt+F1 //go to virtual ...
- OSG消息机制之消息分析
OSG消息接收在头文件有各种事件的相关参数
- OSG消息机制之事件处理概述
OSG的消息机制包括好多个头文件预定义及多个类. 首先,消息接收相关的类当属osgGA::GUIEventHandler和osgGA::GUIEventAdapter这两个类了.前者处理OSG程序与用 ...
- OSG 3D场景渲染编程概述
OSG是Open Scene Graphic的缩写,是基于C++平台的使用OpenGL技术的开源3D场景开发. vs环境安装或者是在Ubuntu中环境的安装网上教程很多,都是大同小异的,认真操作容易成 ...
随机推荐
- 最强DE 战斗力 (nyoj 541)
题解链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=541 几天前百度题解后用数学知识AC的,后来大牛说这是一道动态规划题. 网上的数学解题链接 ...
- C语言指针、地址、赋值三者含义
先来一个观点.大家先看看对不对 按:在CSDN论坛上,有位坛友提到这个问题: ==================================== 先看一段代码: #include<stdi ...
- MVC-Area
ASP.NET MVC中,是依靠某些文件夹以及类的固定命名规则去组织model实体层,views视图层和控制层的.如果是大规模的应用程序,经常会由不同功能的模块组成,而每个功能模块都由MVC中的三层所 ...
- cuda编程(一)
环境安装和例程运行 显卡主要有两家,ATI.NVIDIA,简称A卡和N卡.随着GPU计算能力的上升,采用GPU并行计算来加速的应用越来越多. Nvidia创立人之一,黄仁勋(Jen-Hsun Huan ...
- pack://application:,,,/
FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid)); gridFactory.SetValu ...
- sass 安装和使用
1,安装ruby :检查本地是否安装ruby: #ruby -v 2,安装sass: #gem install sass 3,检查是否安装成功:#sass -v 4,.scss文件不能直接被浏览器解析 ...
- Redis同步操作失败的原因
今天弄了下 Redis 的主从同步,设置方法其实很简单的,但崩溃的是遇到个莫名其妙的问题,始终同步不了.. 看了看错误日志: Unable to connect to MASTER: Invalid ...
- Android设置窗体Activity背景透明
背景透明 style.xml <item name="android:windowBackground">@color/transparent</item> ...
- magento里get与post传值如何接收
$this->getRequest()->getParam('customer_id');这个方法就是获取post和get的值就不用$_POST['']了.$this->getReq ...
- PAT乙级1027. 打印沙漏(20)
本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对齐:相邻两 ...