View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Code Snippets

Keyboard Mapping in C++

The mapping of keyboard could be done either by script, e.g. startup.tcl or in C++

nInputServer inputServer = nInputServer::Instance();
inputServer->BeginMap();
inputServer->Map("keyb0:down.pressed","arrow_down");
inputServer->Map("keyb0:up.pressed","arrow_up");
inputServer->EndMap();


Using the VariableServer

The nVariableServer is a convenient way, to store values application wide. These could include int, float, matrices, textures etc.
The following code, gets a variable from the nVariableServer and updates it's value. If the variable is unknown, it's declared automatically.
A variable is identified by its handle and has a name.


//get nVariableServer Singelton
nVariableServer variableServer = nVariableServer::Instance();
//get handle, or declare new variable
nVariable::Handle hVideoTex = variableServer->GetVariableHandleByName("videotexture");
//create new variable object with specified handle and value, in this case a pointer to an nTexture2 object
nVariable vVideoTexture = nVariable(hVideoTex, GetVideoFrameTexture());
//hand over this variable to the nVariableServer
variableServer->SetGlobalVariable(vVideoTexture);


Animate a texture

Animating a texture means to exhange pre-registered textures on a time or event triggered basis. On every frame, nTextureAnimator::Animate() should be called, which specifies the RenderContext and SceneNode to be animated.

//create animator object
m_TextureAnimator = (nTextureAnimator)ks->New("ntextureanimator","/res/texanim");
//register textures
m_TextureAnimator->AddTexture("home:export/textures/presar/animtest_1.png");
m_TextureAnimator->AddTexture("home:export/textures/presar/animtest_2.png");
m_TextureAnimator->AddTexture("home:export/textures/presar/animtest_3.png");
//specify texture channel, Diffuse Map 0 or Normal Map etc.
m_TextureAnimator->SetShaderParam("DiffMap0");
//The Animator is controled by a nVariable, specified in the nVariableServer. SetChannel sets the name of this variable
m_TextureAnimator->SetChannel("textureIndex");


Useful

// get mangled path name
nString mangledPath = nFileServer2::Instance()->ManglePath(fileName);
hr = D3DXSaveSurfaceToFile(mangledPath.Get(), D3DXIFF_JPG, this->captureSurface, NULL, NULL);

Pass custom variables / values to shader

Shader variables can be altered at runtime. This requires a special declaration in Nebula, in order to update the values for every frame. This is done via nShaderState. The parameter list already consists of various values like textures, time etc. but every custom parameter has to be specified here.

Load custom model

void presarState::LoadCustomModel() {
nKernelServerks = nKernelServer::Instance();
nVariableServer variableServer = nVariableServer::Instance();
//nRoot resourceRoot = ks->Lookup("/res");
//ks-PushCwd(resourceRoot);
m_myObject = (nShapeNode)ks->New("nshapenode","/res/toby");
m_myObject->SetPosition(vector3(0.0f,1.2f,0.0f));
m_myObject->SetEuler(vector3(0.0f,0.0f,-1.57f));
m_myObject->SetScale(vector3(0.5f,0.5f,0.5f));
m_myObject->SetMesh("meshes:canvas.nvx2");
m_myObject->SetShader("videodisplay");
m_myObject->SetTexture(nShaderState::DiffMap0,"home:export/textures/presar/animtest_1.png");
m_myObject->LoadResources();
m_RenderContext.SetRootNode(m_myObject);
m_myObject->RenderContextCreated(&m_RenderContext);
}

Link to this Page

  • Nebula Device last edited on 23 February 2006 at 7:55 pm by leadbelly-win.cc.gatech.edu