朱丽叶—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中环境的安装网上教程很多,都是大同小异的,认真操作容易成 ...
随机推荐
- wifi 模块
#include"STC12C5A.H" #include <string.h> #include<intrins.h> #define uint unsi ...
- table中td内容过长 省略号显示
首先设置 css样式: table { table-layout: fixed;} HTML中的table代码: <tr> <th class="col-md-1" ...
- awk学习笔记二:调用shell、文件执行(转)
awk 'BEGIN {print "Hello"}' 不操作文件直接处理数据流 要调用shell则可以用管道命令 如,打印日期awk 'BEGIN {"date&quo ...
- 技能学习经验与C语言学习调查
技能学习经验与C语言学习调查 前言 要说的话,这还是我第一次写博客.不论是为了作业也好,为了将来的学习工作也好,写博客都是必不可少的,也算是个自我提升的途径吧.不过第一次写博客,就用从来没听说过的ma ...
- js---疑点代码段解析
function count() { var arr = []; for (var i=1; i<=3; i++) { console.log("iii---"+i); ar ...
- HDU 4520 小Q系列故事——最佳裁判
Time Limit : 500/200ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Problem Description 过 ...
- 数据库中Schema、Database、User、Table的关系[转]
数据库的初学者往往会对关系型数据库模式(schema).数据库(database).表(table).用户(user)之间感到迷惘,总感觉他们的关系千丝万缕,但又不知道他们的联系和区别在哪里,对一些问 ...
- openwrt 添加 应用(luci-application)
openwrt 添加应用的几个步骤如下: (1)在目录 ./feeds/luci/applications 下添加要增加的应用,譬如 "luci-test" (2)里面应该包含以下 ...
- 在GNU/Linux下设置与定时更换桌面壁纸
1 简介 在电脑桌面设置一组可以定时更换的壁纸已经不是什么新奇的功能了.比如,Windows 7.KDE桌面环境都可以实现这样的效果.可是,自己目前使用的Debian系统并未安装KDE.GNOME这样 ...
- epoll完整例子
#include <deque> #include <map> #include <vector> #include <pthread.h> #incl ...