朱丽叶—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中环境的安装网上教程很多,都是大同小异的,认真操作容易成 ...
随机推荐
- HTML学习总结(四)【canvas绘图、WebGL、SVG】
一.Canvas canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3D图像技术.<canvas> 标记和 ...
- 最近一段时间get到的小知识(c++的)
(1)查看一个程序运行的时间 int main() { clock_t start,end; start=clock(); ... end=clock(); cout<<"Run ...
- phonegap 随笔
开发者论坛 http://bbs.phonegapcn.com/forum.php phone调用android本地方法 http://blog.csdn.net/crazyman2010/artic ...
- express mongoose 新手上路 问题记录
1.npm start启动报错,提示端口占用 app.js中添加了app.listen(3000),删掉即可 2.跳转到html页面 //ejs改为html app.engine('.html', r ...
- 从键盘或文件中获取标准输入:read命令
文件的描述符和重定向 文件描述符是和文件的输入.输出相关联的非负整数,Linux内核(kernel)利用文件描述符(file descriptor)来访问文件.打开现存文件或新建文件时,内核会返回一个 ...
- java读写锁源码分析(ReentrantReadWriteLock)
读锁的调用,最终委派给其内部类 Sync extends AbstractQueuedSynchronizer /** * 获取读锁,如果写锁不是由其他线程持有,则获取并立即返回: * 如果写锁被其他 ...
- 在ubuntu14.04上安装openstack mitaka
最近在工作环境安装部署了juno版本,在GE口测试网络性能不太满意,发现mitaka版本支持ovs-dpdk,于是抽时间安装实验一番. 参考官网的安装文档,先准备将mitaka版本安装好再配置ovs. ...
- Intellij Idea使用频率较高的几个快捷键
自动补全参数定义: Ctrl+Alt+V 运行断点Expression: Alt+F8 选择具体的方法以断点步入:Shift+F7 智能操作: Alt+Enter 打开最近文件:Ctrl+E 打开最近 ...
- React native android 最常见的10个问题
这里逐条记录下最容易遇到的React native android 相关case: 1. app启动后,红色界面,unable load jsbundle : 解决办法:一般来说就是,你是用dev-s ...
- 三大框架之hibernate
在DB Browser视图里新建一个数据库连接 Connection URL可填 jdbc:oracle:thin:@localhost:1521:orcl 或 jdbc:oracle:thin: ...