Hi there,

I'm using different glsl shader (phong, etc.) to create different visualizations of my models in my scene. In my shaders I would like to use the original material properties resp. textures that belong to the model and were loaded with the geometry from file.

When I load the tie fighter model I would like to use the original material colors in my shader. I tried to do so by using the built-in gl_FrontColor.diffuse in the Vertex Shader and pass it through to the Fragment Shader. But the gl_FrontColor (diffuse/ambient/specular) is all the time (0.0,0.0,0.0,0.0)/black, meaning that its not available. It seems that the OpenGL material isn't anymore valid for any reason when I use a shader.

Afterwards I tried to use the textures of the model in my shader. In Performer the corresponding texture of the currently rendered geometry was bind to texture unit 0. Thus it was easy to use the textures in the shader. It seams that OpenSG binds the textures to different textures units. Because when I used texture unit 0 in my shader, the wrong textures were visualized with wrong parts of my model. So my question is how to acces the textures in my shader, which were loaded with the model? Because I don't know the exact texture units OpenSG binds them to.

Perhaps I don't set up the shader corrctly and the OpenGL State isn't anymore available.

I attached an example to show the problem.

thanks for any comment.

Sebastian Knödel
// OpenSG Tutorial Example: Shader
//
// This example shows how to use GLSL shaders.
// It creates a bouncing ball animation (completely calculated on the GPU).
// You need a graphics card supporting the GL_ARB_shading_language_100 extension
// to run this tutorial.

// Headers
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGBaseFunctions.h>
#include <OpenSG/OSGTransform.h>
#include <OpenSG/OSGGroup.h>
#include <OpenSG/OSGPointLight.h>
#include <OpenSG/OSGRenderAction.h>
#include <OpenSG/OSGSceneFileHandler.h>

#include <OpenSG/OSGSimpleMaterial.h>
#include <OpenSG/OSGImage.h>
#include <OpenSG/OSGTextureChunk.h>
#include <OpenSG/OSGSHLChunk.h>
#include <OpenSG/OSGMaterialGroup.h>
#include <OpenSG/OSGGradientBackground.h>

// Activate the OpenSG namespace
OSG_USING_NAMESPACE

using namespace std;

// The SimpleSceneManager to manage simple applications
SimpleSceneManager *_mgr        = NULL;
NodePtr _scene                          = NullFC;
SHLChunkPtr _shl                        = NullFC;

NodePtr model;

MaterialGroupPtr mg;

//----------------------------------------------------
//Phong Shader
//----------------------------------------------------
// vertex shader program.
static std::string _vp_program =
"\n"
"varying vec2 Texcoord;\n"
"varying vec3 ViewDirection;\n"
"varying vec3 fvObjectPosition;\n"
"varying vec3 Normal;\n"
"   \n"
"void main( void )\n"
"{\n"
"   gl_Position = ftransform();\n"
"   Texcoord    = gl_MultiTexCoord0.xy;\n"
"    \n"
"   vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;\n"
"   \n"
"   ViewDirection  = - fvObjectPosition.xyz;\n"
"   Normal         = gl_NormalMatrix * gl_Normal;\n"
"       gl_FrontColor  = gl_FrontMaterial.diffuse;\n"
"   \n"
"}\n";

// fragment shader program for bump mapping in surface local coordinates
static std::string _fp_program =
"vec4 fvAmbient = vec4(0.36, 0.36, 0.36, 1.0);\n"
"vec4 fvSpecular = vec4(0.7, 0.7, 0.7, 1.0);\n"
"vec4 fvDiffuse = vec4(0.5, 0.5, 0.5, 1.0);\n"
"float fSpecularPower = 25.0;\n"
"\n"
"uniform sampler2D baseMap;\n"
"uniform int useTexture;\n"
"\n"
"varying vec2 Texcoord;\n"
"varying vec3 ViewDirection;\n"
"varying vec3 fvObjectPosition;\n"
"varying vec3 Normal;\n"
"\n"
"void main( void )\n"
"{\n"
"   vec3  fvLightDirection = normalize( gl_LightSource[0].position.xyz - 
fvObjectPosition.xyz);\n"
"   vec3  fvNormal         = normalize( Normal );\n"
"   float fNDotL           = dot( fvNormal, fvLightDirection ); \n"
"   \n"
"   vec3  fvReflection     = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - 
fvLightDirection ); \n"
"   vec3  fvViewDirection  = normalize( ViewDirection );\n"
"   float fRDotV           = max( 0.0, dot( fvReflection, fvViewDirection ) 
);\n"
"   \n"
"       vec4  fvBaseColor;"
"       if(useTexture>0)\n"
"               fvBaseColor  = texture2D( baseMap, Texcoord );\n"
"       else\n"
"               fvBaseColor    = gl_Color;\n"
"   \n"
"   vec4  fvTotalAmbient   = fvAmbient * fvBaseColor; \n"
"   vec4  fvTotalDiffuse   = fvDiffuse * fNDotL * fvBaseColor; \n"
"   vec4  fvTotalSpecular  = fvSpecular * ( pow( fRDotV, fSpecularPower ) );\n"
"  \n"
"   gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );\n"
"}\n";

// forward declaration so we can have the interesting stuff upfront
int setupGLUT(int *argc, char *argv[]);

// redraw the window
void display(void)
{
    _mgr->redraw();
}

void update(void)
{
    glutPostRedisplay();
}

SimpleMaterialPtr createSimpleShaderMaterial(){

        _shl = SHLChunk::create();

        beginEditCP(_shl);
        _shl->setVertexProgram(_vp_program);
        _shl->setFragmentProgram(_fp_program);
    endEditCP(_shl);

    SimpleMaterialPtr simpleShadMat = SimpleMaterial::create();
    beginEditCP(simpleShadMat);
                simpleShadMat->addChunk(_shl);
    endEditCP(simpleShadMat);

        return simpleShadMat;
}

// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{

    // OSG init
    osgInit(argc,argv);

    // GLUT init
    int winid = setupGLUT(&argc, argv);

    // create the scene
    _scene = makeCoredNode<Group>();
        
    //model = makeLatLongSphere(50, 50, 1.0);
        model = SceneFileHandler::the().read("data/tie.wrl");
        model->updateVolume();

        // set Material group
        mg = MaterialGroup::create();
        beginEditCP(mg);
                mg->setMaterial(createSimpleShaderMaterial());
        endEditCP(mg);
        
    beginEditCP(_scene);
                _scene->setCore(mg);
        _scene->addChild(model);
    endEditCP(_scene);

    // the connection between GLUT and OpenSG
    GLUTWindowPtr gwin= GLUTWindow::create();
    beginEditCP(gwin);//Window
        gwin->setId(winid);
        gwin->init();
    endEditCP(gwin);

    // create the SimpleSceneManager helper
    _mgr = new SimpleSceneManager;

    // tell the manager what to manage
    _mgr->setWindow(gwin );
    _mgr->setRoot  (_scene);
    _mgr->turnHeadlightOn();

        // show the whole scene
    _mgr->showAll();

        // create a gradient background.
    GradientBackgroundPtr gbg = GradientBackground::create();
    beginEditCP(gbg, GradientBackground::LineFieldMask);
        gbg->clearLines();
        gbg->addLine(Color3f(0.7, 0.7, 0.8), 0);
        gbg->addLine(Color3f(0.0, 0.1, 0.3), 1);
    endEditCP(gbg, GradientBackground::LineFieldMask);
        
        //set gradient background
        beginEditCP(gwin);
                ViewportPtr vp = gwin->getPort(0);
        beginEditCP(vp);
                        vp->setBackground(gbg);
        endEditCP(vp);
    endEditCP(gwin);

    // GLUT main loop
    glutMainLoop();

    return 0;
}

//
// GLUT callback functions
//

// react to size changes
void reshape(int w, int h)
{
    _mgr->resize(w, h);
    glutPostRedisplay();
}

// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
    if (state)
        _mgr->mouseButtonRelease(button, x, y);
    else
        _mgr->mouseButtonPress(button, x, y);
        
    glutPostRedisplay();
}

// react to mouse motions with pressed buttons
void motion(int x, int y)
{
    _mgr->mouseMove(x, y);
    glutPostRedisplay();
}

// react to keys
void keyboard(unsigned char k, int x, int y)
{
    switch(k)
    {
                case '1':       _scene->subChild(model);
                                        model = makeLatLongSphere(50, 50, 1.0);
                                        _scene->addChild(model);
                                        _mgr->showAll();
                                        cout<<"Sphere model loaded"<<endl;
                                break;
                case '2':       _scene->subChild(model);
                                        model = 
SceneFileHandler::the().read("tie.wrl");
                                        _scene->addChild(model);
                                        _mgr->showAll();
                                        cout<<"Tie model loaded"<<endl;
                                break;
                case 'q':       // create the shader material                   
                
                                        
mg->setMaterial(createSimpleShaderMaterial());
                                        cout<<"Phong Shader"<<endl;
                                        break;
                case 'w':       // standard OpenGL
                                        mg->setMaterial(NullFC);
                                        cout<<"Standard OpenGL"<<endl;
                                        break;
        case 27:
                                        OSG::osgExit();
                                        exit(0);
        break;
        case 'x':
                                        SceneFileHandler::the().write(_scene, 
"scene.osb.gz", true);
                                        printf("wrote scene.\n");
        break;
    }
}

int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    int winid = glutCreateWindow("OpenSG Material");
    
        cout<<"OpenSG Material"<<endl;
        cout<<"Press Keys 1/2 to switch between Tie Fighter and Sphere 
model"<<endl;
        cout<<"Press Keys q/w to switch between Standard OpenGL and Phong 
Shader visualization"<<endl;

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    glutIdleFunc(update);

    return winid;
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to