Hi again,
I am VERY new to OpenSG, I haven't studied OpenGL or anything, but I am OKish
with C and C++.
I am trying to adapt the "withoutSSM" demo so that I can import the
terrain.wrl, and move the camera around it and zoom in etc...
I have managed to get the 'w' and 's' keys to zoom in and out but they only do
it once each (I cannot keep zooming)
I'm not sure if I'm multiplying the matrices wrong, or have my tree all mixed
up but could someone point me in the right direction?
Thanks
Bits of code that do stuff
This makes the cam beacon
beginEditCP(leftCamBeacon);
beginEditCP(rightCamBeacon);
beginEditCP(lightBeacon);
//create Transformations
TransformPtr leftCamTrans, rightCamTrans, lightTrans;
leftCamTrans = Transform::create();
rightCamTrans = Transform::create();
lightTrans = Transform::create();
beginEditCP(leftCamTrans);
beginEditCP(rightCamTrans);
beginEditCP(lightTrans);
Matrix leftM, rightM, lightM;
leftM.setTransform(Vec3f(0,50,500));
rightM.setTransform(Vec3f(0,50,500));
lightM.setTransform(Vec3f(0,500,1000));
leftCamTrans->setMatrix(leftM);
rightCamTrans->setMatrix(rightM);
lightTrans->setMatrix(lightM);
endEditCP(leftCamTrans);
endEditCP(rightCamTrans);
endEditCP(lightTrans);
leftCamBeacon->setCore(leftCamTrans);
rightCamBeacon->setCore(rightCamTrans);
lightBeacon->setCore(lightTrans);
endEditCP(leftCamBeacon);
endEditCP(rightCamBeacon);
endEditCP(lightBeacon);
// -- end of camera beacon creation
//i think this creates a transformation matrix
//this one is declared globally
ct = ComponentTransform::create();
beginEditCP(ct, ComponentTransform::TranslationFieldMask |
ComponentTransform::ScaleFieldMask |
ComponentTransform::RotationFieldMask);
ct->setTranslation(Vec3f(0,0,0));
ct->setScale(Vec3f(1,1,1));
ct->setRotation(Quaternion(Vec3f(0,1,0),0));
endEditCP(ct, ComponentTransform::TranslationFieldMask |
ComponentTransform::ScaleFieldMask |
ComponentTransform::RotationFieldMask);
//add a simple Transformation
/*ct = Transform::create();
beginEditCP(ct);
Matrix m;
m.setIdentity();
ct->setMatrix(m);
endEditCP(ct);*/
beginEditCP(ctNode);
ctNode->setCore(ct);
ctNode->addChild(rightCamBeacon); //important line
endEditCP(ctNode);
// end component transform ********************************
//add it to the root
beginEditCP(root);
root->addChild(ctNode);
endEditCP(root);
void display(void)
{
frame++;
Real32 time = glutGet(GLUT_ELAPSED_TIME);
beginEditCP(ct, ComponentTransform::TranslationFieldMask |
ComponentTransform::ScaleFieldMask |
ComponentTransform::RotationFieldMask);
switch (mode){
// this translates the camera ie. zoom forward
case 0 :
ct->setTranslation(Vec3f(0,0,100));
break;
// this is left over from the demo app
case 1 :
ct->setRotation(Quaternion(Vec3f(0,1,0), time/2000));
break;
// this translates the camera to zoom out
case 2 :
ct->setTranslation(Vec3f(0,0,-100));
break;
}
endEditCP(ct, ComponentTransform::TranslationFieldMask |
ComponentTransform::ScaleFieldMask |
ComponentTransform::RotationFieldMask);
window->render(renderAction);
}
This is all of the program, its includes some exerts of other tutorials
// all needed include files
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSolidBackground.h>
#include <OpenSG/OSGDirectionalLight.h>
#include <OpenSG/OSGPerspectiveCamera.h>
#include <OpenSG/OSGTransform.h>
#include <OpenSG/OSGRenderAction.h>
#include <OpenSG/OSGViewport.h>
#include <OpenSG/OSGGradientBackground.h>
#include <OpenSG/OSGImageBackground.h>
#include <OpenSG/OSGImage.h>
#include <OpenSG/OSGImageForeground.h>
#include <OpenSG/OSGFileGrabForeground.h>
#include <OpenSG/OSGSceneFileHandler.h>
#include <OpenSG/OSGComponentTransform.h>
OSG_USING_NAMESPACE
using namespace std;
NodePtr scene;
//create global transform
ComponentTransformPtr ct;
//this is something from the demo app i modified
UInt32 frame = 0;
// 0 = translation
// 1 = rotation
// 2 = scalation
UInt8 mode = 0;
// 2 cameras for 2 viewports
PerspectiveCameraPtr leftCamera;
PerspectiveCameraPtr rightCamera;
// 2 viewports so i can keep one as a comparison
ViewportPtr leftViewport;
ViewportPtr rightViewport;
WindowPtr window;
NodePtr leftCamBeacon, rightCamBeacon, lightBeacon, lightNode;
RenderAction *renderAction;
int setupGLUT( int *argc, char *argv[] );
NodePtr createScenegraph(){
//create geometry - just a simple torus
//NodePtr n = makeTorus(1,5,8,16);
//load the smallish demo terrain from
http://www.vrac.iastate.edu/~dkabala/Doc/tutorial/progs/data/
NodePtr n = SceneFileHandler::the().read("data/terrain.wrl");
// //we check the result
// if (n != NullFC)
// return n;
// else{
// cout << "Loading the specified file was not possible!" << endl;
// return NullFC;
//}
//create transformations & beacons for cameras & light
leftCamBeacon = Node::create();
rightCamBeacon = Node::create();
lightBeacon = Node::create();
// the following style is a bit different than from before
// this is only to remind you that beginEditCP()'s can also
// be interleaved
beginEditCP(leftCamBeacon);
beginEditCP(rightCamBeacon);
beginEditCP(lightBeacon);
//create Transformations
TransformPtr leftCamTrans, rightCamTrans, lightTrans;
leftCamTrans = Transform::create();
rightCamTrans = Transform::create();
lightTrans = Transform::create();
beginEditCP(leftCamTrans);
beginEditCP(rightCamTrans);
beginEditCP(lightTrans);
//Maticies need to be here for this wrl, for the iraq one they are in the wrong
place but iraq takes about 5x londer to load
Matrix leftM, rightM, lightM;
leftM.setTransform(Vec3f(0,50,500));
rightM.setTransform(Vec3f(0,50,500));
lightM.setTransform(Vec3f(0,500,1000));
leftCamTrans->setMatrix(leftM);
rightCamTrans->setMatrix(rightM);
lightTrans->setMatrix(lightM);
endEditCP(leftCamTrans);
endEditCP(rightCamTrans);
endEditCP(lightTrans);
leftCamBeacon->setCore(leftCamTrans);
rightCamBeacon->setCore(rightCamTrans);
lightBeacon->setCore(lightTrans);
endEditCP(leftCamBeacon);
endEditCP(rightCamBeacon);
endEditCP(lightBeacon);
// -- end of camera beacon creation
//create the light source
DirectionalLightPtr dLight = DirectionalLight::create();
beginEditCP(dLight);
dLight->setDirection(Vec3f(0,50,1000));
//color information
dLight->setDiffuse(Color4f(1,1,1,1));
dLight->setAmbient(Color4f(0.2,0.2,0.2,1));
dLight->setSpecular(Color4f(1,1,1,1));
//set the beacon
dLight->setBeacon(lightBeacon);
endEditCP (dLight);
// create the node that will contain the light source
lightNode = Node::create();
beginEditCP(lightNode);
lightNode->setCore(dLight);
lightNode->addChild(n);
endEditCP(lightNode);
// now create the root and add all children
NodePtr root = Node::create();
beginEditCP(root);
root->setCore(Group::create());
root->addChild(lightNode);
root->addChild(leftCamBeacon);
root->addChild(rightCamBeacon);
//root->addChild(rightCamTrans);
root->addChild(lightBeacon);
endEditCP(root);
// component transform ************************************
NodePtr ctNode = Node::create();
//i think this creates a transformation matrix
//this one is declared globally
ct = ComponentTransform::create();
beginEditCP(ct, ComponentTransform::TranslationFieldMask |
ComponentTransform::ScaleFieldMask |
ComponentTransform::RotationFieldMask);
ct->setTranslation(Vec3f(0,0,0));
ct->setScale(Vec3f(1,1,1));
ct->setRotation(Quaternion(Vec3f(0,1,0),0));
endEditCP(ct, ComponentTransform::TranslationFieldMask |
ComponentTransform::ScaleFieldMask |
ComponentTransform::RotationFieldMask);
//add a simple Transformation
/*ct = Transform::create();
beginEditCP(ct);
Matrix m;
m.setIdentity();
ct->setMatrix(m);
endEditCP(ct);*/
beginEditCP(ctNode);
ctNode->setCore(ct);
ctNode->addChild(rightCamBeacon); //important line
endEditCP(ctNode);
// end component transform ********************************
//add it to the root
beginEditCP(root);
root->addChild(ctNode);
endEditCP(root);
return root;
}
int main(int argc, char **argv)
{
osgInit(argc,argv);
int winid = setupGLUT(&argc, argv);
scene =createScenegraph();
//we beginn with creating our cameras
leftCamera = PerspectiveCamera::create();
rightCamera = PerspectiveCamera::create();
beginEditCP(leftCamera);
leftCamera->setBeacon(leftCamBeacon);
leftCamera->setFov(deg2rad(60));
leftCamera->setNear(0.1);
leftCamera->setFar(2000);
endEditCP(leftCamera);
beginEditCP(rightCamera);
rightCamera->setBeacon(rightCamBeacon);
rightCamera->setFov(deg2rad(60));
rightCamera->setNear(0.1);
rightCamera->setFar(2000);
endEditCP(rightCamera);
//next we create the backgrounds
GradientBackgroundPtr leftBkg = GradientBackground::create();
beginEditCP(leftBkg);
leftBkg->addLine(Color3f(0,0,0),1);
leftBkg->addLine(Color3f(1,1,1),0);
endEditCP(leftBkg);
//load the image file
ImagePtr bkgImage = Image::create();
beginEditCP(bkgImage);
bkgImage->read("data/front.jpg");
endEditCP(bkgImage);
ImageBackgroundPtr rightBkg = ImageBackground::create();
beginEditCP(rightBkg);
rightBkg->setImage(bkgImage);
rightBkg->setScale(true);
endEditCP(rightBkg);
//now the viewports
leftViewport = Viewport::create();
rightViewport = Viewport::create();
beginEditCP(leftViewport);
leftViewport->setCamera(leftCamera);
leftViewport->setBackground(leftBkg);
leftViewport->setRoot(scene);
leftViewport->setSize(0,0,0.5,1);
endEditCP(leftViewport);
beginEditCP(rightViewport);
rightViewport->setCamera(rightCamera);
rightViewport->setBackground(rightBkg);
rightViewport->setRoot(scene);
rightViewport->setSize(0.5,0,1,1);
endEditCP(rightViewport);
// -- end of viewport creation
// add an logo foreground to the right viwport
//load the logo image file
ImagePtr frgImage = Image::create();
beginEditCP(frgImage);
frgImage->read("data/logo.png");
endEditCP(frgImage);
ImageForegroundPtr imgFrg = ImageForeground::create();
beginEditCP(imgFrg);
//NOTE: the position values are between 0 and 1
//and are relative to the viewport!
imgFrg->addImage(frgImage,Pnt2f(0.1,0));
endEditCP(imgFrg);
//add the created foreground by appending it to
//the vieports foreground multifield
rightViewport->getMFForegrounds()->push_back(imgFrg);
//create the foreground for screenshot functionality
FileGrabForegroundPtr fileGrab = FileGrabForeground::create();
beginEditCP(fileGrab);
fileGrab->setActive(false);
fileGrab->setName("data/screenshot%04d.jpg");
endEditCP(fileGrab);
//add this one to the right viewport, too
rightViewport->getMFForegrounds()->push_back(fileGrab);
//to make a screenshot the foreground has to be set to active
//were doing that if the user pressen the 's' key
//have a look at the keyboard callback function
//and the render action - more on that later
renderAction = RenderAction::create();
//create the window now
GLUTWindowPtr gwin= GLUTWindow::create();
gwin->setId(winid);
gwin->setSize(300,300);
window = gwin;
window->addPort(leftViewport);
window->addPort(rightViewport);
window->init();
glutMainLoop();
return 0;
}
void reshape(int w, int h)
{
window->resize(w, h);
glutPostRedisplay();
}
void display(void)
{
frame++;
Real32 time = glutGet(GLUT_ELAPSED_TIME);
beginEditCP(ct, ComponentTransform::TranslationFieldMask |
ComponentTransform::ScaleFieldMask |
ComponentTransform::RotationFieldMask);
switch (mode){
// this translates the camera ie. zoom forward
case 0 :
//multiply or something
/*Matrix m,n,o;
m.setTransform(Vec3f(0,0,50));
n.setTransform(ct);
n.mult(m);
o.setValue(n);
ct->setMatrix(o);*/
ct->setTranslation(Vec3f(0,0,100));
break;
// this is left over from the demo app
case 1 :
ct->setRotation(Quaternion(Vec3f(0,1,0), time/2000));
break;
// this translates the camera to zoom out
case 2 :
ct->setTranslation(Vec3f(0,0,-100));
break;
}
endEditCP(ct, ComponentTransform::TranslationFieldMask |
ComponentTransform::ScaleFieldMask |
ComponentTransform::RotationFieldMask);
window->render(renderAction);
}
void mouse(int button, int state, int x, int y)
{
/*
if (state)
mgr->mouseButtonRelease(button, x, y);
else
mgr->mouseButtonPress(button, x, y);
*/
glutPostRedisplay();
}
void motion(int x, int y)
{
//mgr->mouseMove(x, y);
glutPostRedisplay();
}
void keyboard(unsigned char k, int x, int y){
switch(k){
case 27: {
osgExit();
exit(1);
}
break;
// move cam forward
case 'w' : mode = 0; break;
// left over from demo
case 'r' : mode = 1; break;
// move cam back
case 's' : mode = 2; break;
//if you press 'u' close the app straight away or it will fill up your system
with screenshots
case 'u':
// The return value is actually a pointer to on osgPtr class
// I don't know if that makes much sense at all...
MFForegroundPtr foregrounds = *(window->getPort(1)->getMFForegrounds());
FileGrabForegroundPtr fg = FileGrabForegroundPtr::dcast(foregrounds[1]);
if (!fg->getActive()){
std::cout << "start recording..." << std::endl;
fg->setActive(true);
}else{
std::cout << "stopped" << std::endl;
fg->setActive(false);
}
break;
}
}
int setupGLUT(int *argc, char *argv[])
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
int winid = glutCreateWindow("Amazing First Application");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(display);
return winid;
}
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users