Changes

Jump to: navigation, search

GAM670/DPS905 Weekly Schedule 20121

13,264 bytes added, 18:33, 8 March 2012
Week 7 - Feb 20
=== To Do ===
=== Resources ===
 
== Week 8 - Mar 4 ==
=== This Week ===
* Design.cpp
: Design::initialize() - create the skybox object
<syntaxhighlight lang="cpp">
// initialize initializes the general display design coordinator, creates the
// primitive sets, textures, objects, lights, sounds, cameras, and text items
//
void Design::initialize() {
 
// ...
 
// create textures
iTexture* sunset = CreateCubeTexture(L"Islands.dds");
 
// ...
 
iObject* skybox = CreateSkybox();
skybox->rotatex(-1.5708f);
skybox->attach(sunset);
setSkybox(skybox);
 
// ...
}
</syntaxhighlight>
* Coordinator.cpp
: Coordinator::render() - using different techniques for different objects
<syntaxhighlight lang="cpp">
void Coordinator::render() {
 
// adjust framecount and fps
if (now - lastReset <= unitsPerSec)
framecount++;
else {
// recalculate the frame rate
fps = framecount * unitsPerSec / (now - lastReset);
framecount = 0;
lastReset = now;
if (timerText) {
wchar_t str[MAX_DESC + 1];
sprintf(str, fps, L" fps");
timerText->set(str);
}
}
// update the user input devices
userInput->update();
Coordinator::update();
// update the model
update();
// update the audio
audio->setVolume(volume);
audio->setFrequencyRatio(frequency);
audio->update(Camera::getView());
 
// start rendering
display->beginDrawFrame(Camera::getView());
display->setAmbientLight(ambient.r, ambient.g, ambient.b);
unsigned nPasses;
// render all of the opaque unlit objects
display->beginEffect("opaque", nPasses);
for (unsigned i = 0; i < nPasses; i++) {
display->beginPass(i);
render(OPAQUE_OBJECT);
display->endPass();
}
display->endEffect();
// render all of the translucent unlit objects
display->beginEffect("translucent", nPasses);
for (unsigned i = 0; i < nPasses; i++) {
display->beginPass(i);
render(TRANSLUCENT_OBJECT);
display->endPass();
}
display->endEffect();
// render all of the lit objects
display->beginEffect("litObjects", nPasses);
for (unsigned i = 0; i < nPasses; i++) {
display->beginPass(i);
render(LIT_OBJECT);
display->endPass();
}
display->endEffect();
// render the skybox
display->beginEffect("skybox", nPasses);
if (background && !skybox) {
Rectf fullScreen(0, 0, 1, 1);
display->beginDrawHUD(0);
background->render(fullScreen, true);
display->endDrawHUD();
}
else if (skybox) {
for (unsigned i = 0; i < nPasses; i++) {
display->beginPass(i);
render(SKYBOX);
display->endPass();
}
}
display->endEffect();
display->set(ALPHA_BLEND, false);
display->beginDrawHUD(HUD_ALPHA);
render(ALL_HUDS);
display->endDrawHUD();
display->endDrawFrame();
render(ALL_SOUNDS);
}
</syntaxhighlight>
: Coordinator::render(iObject*) - render a single object one subset at a time
<syntaxhighlight lang="cpp">
void Coordinator::render(iObject* object) {
 
display->setWorld(&object->world());
unsigned nSubsets = object->noSubsets();
for (unsigned i = 0; i < nSubsets; i++) {
iTexture* texture = object->getTexture(i);
if (texture) texture->attach();
display->setReflectivity(object->getReflectivity(i));
object->render(i);
if (texture) texture->detach();
}
}
</syntaxhighlight>
* Skybox class
: iObject interface - CreateSkybox declaration
<syntaxhighlight lang="cpp">
class iObject : public Shape, public Base {
public:
// initialization
virtual void attach(iTexture* t) = 0;
virtual void attach(iTexture** t) = 0;
// execution
virtual unsigned noSubsets() const = 0;
virtual void render(unsigned) = 0;
virtual void setTextureFilter(unsigned) = 0;
virtual iTexture* getTexture(unsigned) const = 0;
virtual const void* getReflectivity(unsigned) const = 0;
virtual bool belongsTo(Category category) const = 0;
};
 
iObject* CreateObject(iGraphic*, const Reflectivity* = nullptr, unsigned = 1u);
iObject* CreateBillboard(BillboardType, iGraphic*,
const Reflectivity* = nullptr);
iObject* CreateSkybox();
 
iObject* Clone(const iObject*);
</syntaxhighlight>
: Skybox class - derived from Object
<syntaxhighlight lang="cpp">
//-------------------------------- Skybox -------------------------------------
//
// A Skybox is an inverted Object that translates with the viewpoint
//
class Skybox : public Object {
 
public:
Skybox();
void render(unsigned);
};
</syntaxhighlight>
: CreateSkybox
<syntaxhighlight lang="cpp">
iObject* CreateSkybox() {
 
return new Skybox();
}
</syntaxhighlight>
: Skybox::Skybox - SKYBOX category, 2 x 2 x 2 cube
<syntaxhighlight lang="cpp">
Skybox::Skybox() : Object(SKYBOX, CreateIBox(-1, -1, -1 * MODEL_Z_AXIS, 1, 1,
1 * MODEL_Z_AXIS)) {
}
</syntaxhighlight>
: Skybox::render(unsigned) - move skybox centroid to current camera position
<syntaxhighlight lang="cpp">
void Skybox::render(unsigned) {
 
Camera* camera = *(Camera**)(Camera::getCurrent());
Vector disp = camera->position() - position();
translate(disp.x, disp.y, disp.z);
 
Object::render(0);
}
</syntaxhighlight>
* Texture class
: iTexture interface - CreateCubeTexture declaration
<syntaxhighlight lang="cpp">
class iTexture : public Base {
public:
virtual void attach() const = 0;
virtual void setFilter(unsigned) const = 0;
virtual void detach() = 0;
virtual void render(const Rectf&, bool = false) = 0;
};
 
iTexture* CreateTexture(const wchar_t* file, unsigned filter = 0);
iTexture* CreateCubeTexture(const wchar_t* file, unsigned filter = 0);
 
iTexture* Clone(const iTexture*);
</syntaxhighlight>
: Texture class - add the cube parameter to constructor
<syntaxhighlight lang="cpp">
class Texture : public iTexture {
 
iAPITexture* apiTexture; // points to the api texture
 
Texture(const Texture&);
virtual ~Texture();
 
public:
Texture(const wchar_t* file, unsigned filter = 0, bool cube = false);
Texture& operator=(const Texture&);
void* clone() const { return new Texture(*this); }
// execution
void attach() const;
void setFilter(unsigned) const;
void detach();
void render(const Rectf&, bool);
// termination
void suspend();
void release();
};</syntaxhighlight>
: CreateCubeTexture - call constructor with true flag for cube
<syntaxhighlight lang="cpp">
iTexture* CreateCubeTexture(const wchar_t* file, unsigned filter) {
 
return new Texture(file, filter, true);
}
</syntaxhighlight>
: Texture::Texture() - create APICubeTexture()
<syntaxhighlight lang="cpp">
Texture::Texture(const wchar_t* file, unsigned filter, bool cube) {
 
coordinator->add(this);
 
wchar_t* fileWithPath = nullptr;
if (file) {
// add the directory to create the relative filename
int len = strlen(file) + strlen(TEXTURE_DIRECTORY) + 1;
fileWithPath = new wchar_t[len + 1];
::nameWithDir(fileWithPath, TEXTURE_DIRECTORY, file, len);
}
 
// apiTexture on the graphics device
if (cube)
apiTexture = CreateAPICubeTexture(fileWithPath);
else
apiTexture = CreateAPITexture(fileWithPath, filter);
 
if (fileWithPath) delete [] fileWithPath;
}
</syntaxhighlight>
: iAPITexture interface - add CreateAPICubeTexture declaration
<syntaxhighlight lang="cpp">
class iAPITexture {
public:
virtual iAPITexture* clone() const = 0;
// execution
virtual void attach() = 0;
virtual void setFilter(unsigned flags) = 0;
virtual void detach() = 0;
virtual void render(const Rectf&, unsigned char, bool = false) = 0;
// termination
virtual void suspend() = 0;
virtual void release() = 0;
virtual void Delete() const = 0;
};
 
iAPITexture* CreateAPITexture(const wchar_t* file, unsigned filter);
iAPITexture* CreateAPICubeTexture(const wchar_t* file);
</syntaxhighlight>
* APITexture.h
: APICubeTexture class definition
<syntaxhighlight lang="cpp">
class APICubeTexture : public iAPITexture, public APIBase {
 
wchar_t* file; // points to file with texture image
unsigned filter; // default texture filtering flags
 
IDirect3DCubeTexture9* tex; // interface to texture COM object
 
virtual ~APICubeTexture();
 
void setup();
 
public:
APICubeTexture(const wchar_t* file);
APICubeTexture(const APICubeTexture&);
iAPITexture& operator=(const APICubeTexture&);
iAPITexture* clone() const { return new APICubeTexture(*this); }
// execution
void attach();
void setFilter(unsigned filter) {}
void detach();
void render(const Rectf&, unsigned char, bool) {}
// suspension
void suspend();
// termination
void release();
void Delete() const { delete this; }
};
</syntaxhighlight>
: APICubeTexture class implementation
<syntaxhighlight lang="cpp">
//-------------------------------- APICubeTexture -----------------------------
//
// The APICubeTexture class implements a texture at the API level
//
iAPITexture* CreateAPICubeTexture(const wchar_t* file) {
 
return new APICubeTexture(file);
}
 
// constructor initializes the texture identifier
//
APICubeTexture::APICubeTexture(const wchar_t* file) {
 
if (file) {
int len = strlen(file);
this->file = new wchar_t[len + 1];
strcpy(this->file, file, len);
}
else
this->file = nullptr;
 
tex = nullptr;
}
 
APICubeTexture::APICubeTexture(const APICubeTexture& src) {
 
file = nullptr;
tex = nullptr;
*this = src;
}
 
iAPITexture& APICubeTexture::operator=(const APICubeTexture& src) {
 
if (this != &src) {
if (file)
delete [] file;
if (src.file) {
int len = strlen(src.file);
file = new wchar_t[len + 1];
strcpy(file, src.file, len);
}
else
file = nullptr;
suspend();
tex = nullptr;
}
 
return *this;
}
 
// setup creates the api texture from the texture file
//
void APICubeTexture::setup() {
 
// create a texture COM object from the texture file
//
HRESULT hr;
if (file && FAILED(hr = D3DXCreateCubeTextureFromFileEx(d3dd, file,
0, D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, 0, nullptr, nullptr, &tex))) {
error(L"APICubeTexture::11 Failed to create texture COM object from file");
tex = nullptr;
}
}
 
// attach attaches the api texture to sampling stage i
//
void APICubeTexture::attach() {
 
if (!tex) setup();
 
if (tex)
d3dd->SetTexture(0, tex);
}
 
// detach detaches the api texture from sampling stage 0
//
void APICubeTexture::detach() {
 
if (tex)
d3dd->SetTexture(0, nullptr);
}
 
// suspend releases the api texture
//
void APICubeTexture::suspend() {
 
// release the Interface to the texture COM object
if (tex) {
tex->Release();
tex = nullptr;
}
}
 
// releases suspends the api texture
//
void APICubeTexture::release() {
 
suspend();
}
 
// destructor releases the api texture
//
APICubeTexture::~APICubeTexture() {
 
release();
}
</syntaxhighlight>
* effects.fx
: vertex shader output
<syntaxhighlight lang="cpp">
//-------------------------------- Skybox -------------------------------------
//
struct FS_Skybox {
float4 pos : POSITION;
float3 tex : TEXCOORD0;
};
<syntaxhighlight lang="cpp">
: vertex shader
</syntaxhighlight>
FS_Skybox skyboxVertexShader(float3 pos : POSITION) {
FS_Skybox output = (FS_Skybox) 0;
output.pos = mul(float4(pos, 0), world);
output.pos = mul(output.pos, viewProjection).xyww;
output.tex = pos.xzy;
return output;
}
<syntaxhighlight lang="cpp">
: skybox sampler state
</syntaxhighlight>
texture skyBox;
samplerCUBE skySampler = sampler_state {
texture = <skyBox>;
MagFilter = LINEAR;
Minfilter = LINEAR;
Mipfilter = LINEAR;
AddressU = MIRROR;
AddressV = MIRROR;
AddressW = MIRROR;
};
<syntaxhighlight lang="cpp">
: fragment shader
</syntaxhighlight>
float4 skyboxFragmentShader(FS_Skybox input) : COLOR0 {
return texCUBE(skySampler, input.tex);
}
<syntaxhighlight lang="cpp">
: skybox technique
</syntaxhighlight>
technique skybox {
 
pass {
AlphaBlendEnable = false;
ZENABLE = true;
ZWRITEENABLE = false;
CullMode = None;
VertexShader = compile vs_3_0 skyboxVertexShader();
PixelShader = compile ps_3_0 skyboxFragmentShader();
}
}
</syntaxhighlight>
<syntaxhighlight lang="cpp">
</syntaxhighlight>
<!--

Navigation menu