朱丽叶—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中环境的安装网上教程很多,都是大同小异的,认真操作容易成 ...
随机推荐
- CODE[VS]-保留两位小数-浮点数处理-天梯青铜
题目描述 Description 保留两位小数输出一个浮点数. 输入描述 Input Description 一个浮点数.double范围内 输出描述 Output Description 保留两位小 ...
- GZIP压缩、解压缩工具类
GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...
- 移动端 web开发心得
吐槽:华为p9 内置浏览器就是个渣,各种奇葩的bug,1.如果将一个输入框的最大长度设为4,然后输入4个后,js将输入的值置空,就无法再输入了 手机上浏览器比较蛋疼的是,点击一个输入框,输入法将页面挡 ...
- Another Array of Orz Pandas
Another Array of Orz Pandas 题目链接:http://acm.xidian.edu.cn/problem.php?id=1187 线段树 线段树维护区间和以及区间内各个数平方 ...
- Android使用Eclipse遇到"java.lang.ClassNotFoundException"
最近遇到个Android Jar的问题,找了几天才找到root cause. 在此记录下. 我们的Android项目需要使用一个供应商的Jar. 我们使用的开发环境为:Eclipse + ADT插件( ...
- DB2导入导出 学习笔记
db2pd -osinfodb2mtrk -i -d (for aix)db2 get dbm cfg show detaildb2 get db cfg show detaildb2 get sna ...
- Ddos 攻击
title: Ddos 防御相关 tags: Ddos, 安全, 防御 grammar_cjkRuby: true --- 防御基础 1.1. 攻击流量到底多大 谈到DDoS防御,首先就是要知道到底遭 ...
- git在webstorm中的使用
打开webstorm新建项目,这里新建的项目名称我起为lianxi 打开设置选项里的插件栏 搜索gitignore,并安装,我这里已安装,所以显示X,没有安装的会显示一个绿色的下载箭头.安装完后需要重 ...
- python 学习 异常处理
异常处理实例 while True: num1 = input('num1:') num2 = input('num2:') try: num1 = int(num1) num2 = int(num2 ...
- 一、java自带的观察者模式
Observer对象是观察者,Observable对象是被观察者. 官网api文档:http://docs.oracle.com/javase/7/docs/api/ 1. 实现观察者模式 实现观察者 ...