#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的更多相关文章

  1. osg + cuda

    #include <osg/Notify> #include <osgViewer/Viewer> #include <osgCompute/Memory> #in ...

  2. CUDA[2] Hello,World

    Section 0:Hello,World 这次我们亲自尝试一下如何用粗(CU)大(DA)写程序 CUDA最新版本是7.5,然而即使是最新版本也不兼容VS2015 ...推荐使用VS2012 进入VS ...

  3. CUDA[1] Introductory

    Section 0 :Induction of CUDA CUDA是啥?CUDA®: A General-Purpose Parallel Computing Platform and Program ...

  4. 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 ...

  5. OSG计时器与时间戳

    static osg::Timer* sendMsgTimer = new osg::Timer; if (sendMsgTimer->time_m()>100)//100ms {// d ...

  6. ubuntu 16.04 + N驱动安装 +CUDA+Qt5 + opencv

    Nvidia driver installation(after download XX.run installation file) 1. ctrl+Alt+F1   //go to virtual ...

  7. OSG消息机制之消息分析

    OSG消息接收在头文件有各种事件的相关参数

  8. OSG消息机制之事件处理概述

    OSG的消息机制包括好多个头文件预定义及多个类. 首先,消息接收相关的类当属osgGA::GUIEventHandler和osgGA::GUIEventAdapter这两个类了.前者处理OSG程序与用 ...

  9. OSG 3D场景渲染编程概述

    OSG是Open Scene Graphic的缩写,是基于C++平台的使用OpenGL技术的开源3D场景开发. vs环境安装或者是在Ubuntu中环境的安装网上教程很多,都是大同小异的,认真操作容易成 ...

随机推荐

  1. Hololens Rest API

    通过Hololens提供的 Rest API 可以对Hololens进行远程控制和获取信息 ,可以通过第三方程序对Hololens重启或者关机,当然,还有更多更丰富的API,例如可以在PC上分流显示H ...

  2. EmbossMaskFilter BlurMaskFilter 学习

    MaskFilter类可以为Paint分配边缘效果.对MaskFilter的扩展可以对一个Paint边缘的alpha通道应用转换.Android包含了下面几种MaskFilter: BlurMaskF ...

  3. 【Python】Python中对象管理与垃圾回收中两个很有意思的问题

    再Python中是利用引用计数来实现对象管理和垃圾回收的,即其他对象引用该对象时候,其引用计数加1,反之减1,当引用计数为0时候,被垃圾收集器回收. Python解释器对对象以及计数器的管理分为以下两 ...

  4. [O]打印时闪退问题

    1. 使用的是Office批量打印精灵1.2版,软件可以打开 2. Win8.1 MSDN原版操作系统,系统重装了,.NET Framework也装了 3. 使用真实打印机打印,打印时闪退,没有任何提 ...

  5. VB调用WebService(SOA2.0接口)(直接Post方式)并解析返回的XML

    SOA 2.0接口 Function GetDepartmentCode(reqDeptCode) Dim soaRequestXML : soaRequestXML = "" D ...

  6. 12C RMAN 备份参考v1

    windows bat 1,C:\dba\utility\rman\rman.bat del C:\dba\utility\rman\full_db_* /qset TNSNAME=ceipuatrm ...

  7. 使用vs2010打开VS2013的工程文件

    在开发团队,会出现vs工具使用版本的不一样的情况.我的电脑使用的是VS2010,可是其他人员使用的是vs2013. 要打开其他人员上传的工程文件,可以通过三种方式: 1.下载一个vs2013版本. 2 ...

  8. Linux文件系统简介及常用命令

    在linux系统中一切皆是文件,下面简要总结了一下linux文件系统中分区类型.文件系统类型以及常用命令. 一.分区类型1.主分区:最多只能有四个2.扩展分区:只能有一个,也可以看做是主分区的一种.即 ...

  9. SQL优化 总结 精简

    索引: 考虑在 where 及 order by 涉及的列上建立索引 经常同时存取多列,且每列都含有重复值可考虑建立组合索引,且查询越频繁的字段放前面 按需使用聚集与非聚集索引,聚集不适合频繁更新.适 ...

  10. Design Pattern——单一职责原理

    在类的职责分离上多考虑,做到单一职责,这样的代码才能做到易于维护,易扩展,灵活多样.