Show
Ignore:
Timestamp:
06/21/05 18:10:04 (10 years ago)
Author:
robert
Message:

Added option for terrain texturing.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgparametric/osgparametric.cpp

    r4348 r4349  
    1414 
    1515#include <osgProducer/Viewer> 
     16 
     17// for the grid data.. 
     18#include "../osghangglide/terrain_coords.h" 
    1619 
    1720/////////////////////////////////////////////////////////////////// 
     
    4447    "}\n"; 
    4548 
     49////////////////////////////////////////////////////////////////// 
     50// vertex shader using texture read 
     51char vertexShaderSource_texture[] =  
     52    "uniform sampler2D vertexTexture; \n" 
     53    "\n" 
     54    "void main(void) \n" 
     55    "{ \n" 
     56    "\n" 
     57    "    gl_TexCoord[0] = gl_Vertex; \n" 
     58    "    gl_Vertex.z = texture2D( vertexTexture, gl_TexCoord[0].xy).x*0.0001; \n" 
     59    "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" 
     60    "}\n"; 
     61 
    4662 
    4763////////////////////////////////////////////////////////////////// 
     
    6884}; 
    6985 
    70 osg::Node* createModel(const std::string& shader) 
     86osg::Node* createModel(const std::string& shader, const std::string& textureFileName, const std::string& terrainFileName) 
    7187{ 
    7288    osg::Geode* geode = new osg::Geode; 
     
    7894    unsigned int num_x = 708; 
    7995    unsigned int num_y = 708; 
     96 
     97    // set up state 
     98    { 
     99     
     100        osg::StateSet* stateset = geom->getOrCreateStateSet(); 
     101 
     102        osg::Program* program = new osg::Program; 
     103        stateset->setAttribute(program); 
     104 
     105        if (shader=="simple") 
     106        { 
     107            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_simple); 
     108            program->addShader(vertex_shader); 
     109 
     110            osg::Uniform* coeff = new osg::Uniform("coeff",osg::Vec4(1.0,-1.0f,-1.0f,1.0f)); 
     111            coeff->setUpdateCallback(new UniformVarying); 
     112            stateset->addUniform(coeff); 
     113        } 
     114        else if (shader=="matrix") 
     115        { 
     116            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_matrix); 
     117            program->addShader(vertex_shader); 
     118 
     119            osg::Uniform* origin = new osg::Uniform("origin",osg::Vec4(0.0f,0.0f,0.0f,1.0f)); 
     120            stateset->addUniform(origin); 
     121 
     122            osg::Uniform* coeffMatrix = new osg::Uniform("coeffMatrix", 
     123                osg::Matrix(1.0f,0.0f,1.0f,0.0f, 
     124                            0.0f,0.0f,-1.0f,0.0f, 
     125                            0.0f,1.0f,-1.0f,0.0f, 
     126                            0.0f,0.0f,1.0f,0.0f)); 
     127 
     128            stateset->addUniform(coeffMatrix); 
     129        } 
     130        else if (shader=="texture") 
     131        { 
     132            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture); 
     133            program->addShader(vertex_shader); 
     134 
     135            osg::Image* image = 0; 
     136 
     137            if (terrainFileName.empty()) 
     138            { 
     139                image = new osg::Image; 
     140                unsigned int tx = 38; 
     141                unsigned int ty = 39; 
     142                image->allocateImage(tx,ty,1,GL_LUMINANCE,GL_FLOAT,1); 
     143                for(unsigned int r=0;r<ty;++r) 
     144                { 
     145                    for(unsigned int c=0;c<tx;++c) 
     146                    { 
     147                        *((float*)image->data(c,r)) = vertex[r+c*39][2]*0.1; 
     148                    } 
     149                } 
     150 
     151                num_x = tx; 
     152                num_y = tx; 
     153            } 
     154            else 
     155            { 
     156                image = osgDB::readImageFile(terrainFileName); 
     157 
     158                num_x = image->s(); 
     159                num_y = image->t(); 
     160            } 
     161 
     162            osg::Texture2D* vertexTexture = new osg::Texture2D(image); 
     163 
     164 
     165            vertexTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); 
     166            vertexTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); 
     167            vertexTexture->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI); 
     168            stateset->setTextureAttributeAndModes(1,vertexTexture); 
     169 
     170            osg::Uniform* vertexTextureSampler = new osg::Uniform("vertexTexture",1); 
     171            stateset->addUniform(vertexTextureSampler); 
     172 
     173        } 
     174 
     175        osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); 
     176        program->addShader(fragment_shader); 
     177 
     178        osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile(textureFileName)); 
     179        stateset->setTextureAttributeAndModes(0,texture); 
     180 
     181        osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); 
     182        stateset->addUniform(baseTextureSampler); 
     183 
     184    } 
     185 
     186 
     187    // set up geometry data. 
    80188 
    81189    osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y ); 
     
    115223     
    116224     
    117     osg::StateSet* stateset = geom->getOrCreateStateSet(); 
    118  
    119     osg::Program* program = new osg::Program; 
    120     stateset->setAttribute(program); 
    121  
    122     if (shader=="simple") 
    123     { 
    124         osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_simple); 
    125         program->addShader(vertex_shader); 
    126  
    127         osg::Uniform* coeff = new osg::Uniform("coeff",osg::Vec4(1.0,-1.0f,-1.0f,1.0f)); 
    128         coeff->setUpdateCallback(new UniformVarying); 
    129         stateset->addUniform(coeff); 
    130     } 
    131     else if (shader=="matrix") 
    132     { 
    133         osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_matrix); 
    134         program->addShader(vertex_shader); 
    135  
    136         osg::Uniform* origin = new osg::Uniform("origin",osg::Vec4(0.0f,0.0f,0.0f,1.0f)); 
    137         stateset->addUniform(origin); 
    138  
    139         osg::Uniform* coeffMatrix = new osg::Uniform("coeffMatrix", 
    140             osg::Matrix(1.0f,0.0f,1.0f,0.0f, 
    141                         0.0f,0.0f,-1.0f,0.0f, 
    142                         0.0f,1.0f,-1.0f,0.0f, 
    143                         0.0f,0.0f,1.0f,0.0f)); 
    144  
    145         stateset->addUniform(coeffMatrix); 
    146     } 
    147     else if (shader=="texture") 
    148     { 
    149          
    150     } 
    151  
    152     osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); 
    153     program->addShader(fragment_shader); 
    154      
    155     osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile("lz.rgb")); 
    156     texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); 
    157     texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); 
    158     stateset->setTextureAttributeAndModes(0,texture); 
    159     
    160     osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); 
    161     stateset->addUniform(baseTextureSampler); 
    162225 
    163226 
     
    185248    while(arguments.read("-s",shader)) {} 
    186249     
     250    std::string textureFileName("lz.rgb"); 
     251    while(arguments.read("-t",textureFileName)) {} 
     252 
     253    std::string terrainFileName(""); 
     254    while(arguments.read("-d",terrainFileName)) {} 
    187255 
    188256    // get details on keyboard and mouse bindings used by the viewer. 
     
    207275 
    208276    // load the nodes from the commandline arguments. 
    209     osg::Node* model = createModel(shader); 
     277    osg::Node* model = createModel(shader,textureFileName,terrainFileName); 
    210278    if (!model) 
    211279    {