查看: 1408|回复: 1

为什么我用OSG对几何体进行三维渲染,图片总贴不上去,求大虾们路过看一下,求帮忙

[复制链接]

该用户从未签到

发表于 2013-11-12 17:53:02 | 显示全部楼层 |阅读模式
我用OSG想做一个旋转的地球模型,现在只做出了旋转的球,地球的图片总贴不上去 提示总是无法打开文件
// earth.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


/*int _tmain(int argc, _TCHAR* argv[])
{
        return 0;
}*/
#include <osg/Shape>
#include <osg/ShapeDrawable>
#include <osg/Geode>

osg::ref_ptr<osg::Geode> createSphere(float radius)
{
        osg::ref_ptr<osg::Sphere> sphereShape = new osg::Sphere(osg::Vec3(2.5,0.0,0.0),radius);
        osg::ref_ptr<osg::ShapeDrawable> sphereShapeDrawable = new osg::ShapeDrawable(sphereShape.get());
        osg::ref_ptr<osg::Geode> sphereGeode = new osg::Geode;
        sphereGeode->addDrawable(sphereShapeDrawable.get());
        return sphereGeode;

}

#include <osg/Group>
#include <osgDB/Readfile>
#include <osgViewer/Viewer>
#include <osg/TexEnv>
#include <osg/Texture2D>
#include <osg/PositionAttitudeTransform>
#include <iostream>
#ifdef _DEBUG
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgd.lib")
#else
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osg.lib")
#endif


int main()
{
        //Declear the root node
        osg::ref_ptr<osg::Group> root = new osg::Group;
        //Declear the viewer instance
        osgViewer::Viewer myViewer;
        //Declear texture
        osg::ref_ptr<osg::Texture2D> fogTexture = new osg::Texture2D;
        fogTexture->setDataVariance(osg::Object:YNAMIC);
        osg::ref_ptr<osg::Image> smokeImage = osgDB::readImageFile("land_shallow_topo_2048.jpg");
*************************************************************************************************
        if (!smokeImage.get())
        {
                std::cout<<"open file is failed!"<<std::endl;
                return -1;
        }//就是这边总是提示无法打开文件,是什么原因
***************************************************************************************************
        fogTexture->setImage(smokeImage.get());
        osg::ref_ptr<osg::Geode> sphereOne = createSphere(2.0);
        //Create a TexEnv instance for binding the StateSet
        osg::ref_ptr<osg::TexEnv> sphereOneTexEnv = new osg::TexEnv;
        sphereOneTexEnv->setMode(osg::TexEnv::DECAL);
        osg::ref_ptr<osg::StateSet> sphereOneStateSet = new osg::StateSet;
        sphereOneStateSet->setAttribute(sphereOneTexEnv.get());
        sphereOneStateSet->setAttributeAndModes(fogTexture.get());
        sphereOne->setStateSet(sphereOneStateSet.get());
       
        //the second sphere
        osg::ref_ptr<osg::Geode> sphereTwo = createSphere(0.5);
        osg::ref_ptr<osg:ositionAttitudeTransform> sphereTwoPositionTransform = new osg::PositionAttitudeTransform;
        sphereTwoPositionTransform->setPosition(osg::Vec3(5.0,0.0,0.0));
        sphereTwoPositionTransform->addChild(sphereTwo.get());
        osg::ref_ptr<osg::Texture2D> moonTexture = new osg::Texture2D;
        osg::ref_ptr<osg::Image> moonImage = osgDB::readImageFile("moon256128.jpg");
*******************************************************************************************
        /*if (!moonImage.get())
        {
                std::cout<<"moon file open error"<<std::endl;
                return -1;
        }*/
********************************************************************************************
        moonTexture->setImage(moonImage.get());
        osg::ref_ptr<osg::TexEnv> texEnvSphereTwo = new osg::TexEnv;
        texEnvSphereTwo->setMode(osg::TexEnv::BLEND);
        osg::ref_ptr<osg::StateSet> sphereTwoStateSet = new osg::StateSet;
        sphereTwoStateSet->setAttribute(texEnvSphereTwo.get());
        sphereTwoStateSet->setAttributeAndModes(moonTexture.get());
        sphereTwoStateSet->setMode(GL_FOG,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
        sphereTwo->setStateSet(sphereTwoStateSet.get());
       
        //the third sphere
        osg::ref_ptr<osg::Geode> sphereThree = createSphere(1.6);
        osg::ref_ptr<osg::PositionAttitudeTransform> sphereThreePositionTrans = new osg::PositionAttitudeTransform;
        sphereThreePositionTrans->setPosition(osg::Vec3(30,0,0));
        sphereThreePositionTrans->addChild(sphereThree.get());
        osg::ref_ptr<osg::Texture2D> marsTexture = new osg::Texture2D;
        marsTexture->setDataVariance(osg::Object::DYNAMIC);
        osg::ref_ptr<osg::Image> marsImage = osgDB::readImageFile("mars256128.jpg");
****************************************************************************************
        /*if (!marsImage.get())
        {
                std::cout<<"moon file open error"<<std::endl;
                return -1;
        }*/
********************************************************************************************
        marsTexture->setImage(marsImage.get());
        osg::ref_ptr<osg::StateSet> sphereThreeStateSet = new osg::StateSet;
        osg::ref_ptr<osg::TexEnv> sphereThreeTexEnv = new osg::TexEnv;
        sphereThreeTexEnv->setMode(osg::TexEnv::DECAL);
        sphereThreeStateSet->setAttributeAndModes(marsTexture.get());
        sphereThreeStateSet->setAttribute(sphereThreeTexEnv.get());
        sphereThreeStateSet->setMode(GL_FOG,osg::StateAttribute::OFF);
        sphereThree->setStateSet(sphereThreeStateSet.get());


        root->setStateSet(sphereOneStateSet.get());
        root->addChild(sphereOne.get());
        root->addChild(sphereTwoPositionTransform.get());
        root->addChild(sphereThreePositionTrans.get());


        myViewer.setSceneData(root.get());
        myViewer.realize();
        myViewer.run();

}

该用户从未签到

发表于 2013-11-15 14:39:50 | 显示全部楼层
可能是osgdb_jpg插件的问题,你把图像转换为bmp或者其他格式看看。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表