• Create Account

Banner advertising on our site currently available from just \$5!

# FrozenSnake

Member Since 07 Aug 2008
Offline Last Active Jan 14 2015 07:41 AM

### Topics I've Started

06 January 2015 - 10:48 AM

I am trying to convert some of my c++ code to c# to make the same program in C#.

Here is my C++ code

```MyVector operator+(const MyVector& vec)
{
return MyVector(mX + vec.mX, mY + vec.mY, mZ + vec.mZ);
}

void operator += (const MyVector& vec)
{
mX += vec.mX;
mY += vec.mY;
mZ += vec.mZ;
}

MyVector operator-(const MyVector& vec)
{
return MyVector(mX - vec.mX, mY - vec.mY, mZ - vec.mZ);
}

MyVector operator-()
{

return MyVector(-mX, -mY, -mY);
}

void operator -=(const MyVector& vec)
{
mX -= vec.mX;
mY -= vec.mY;
mZ -= vec.mZ;
}

float operator*(const MyVector& vec)
{
return (mX * vec.mX + mY * vec.mY + mZ * vec.mZ);
}

MyVector operator*(const float& f)
{
return MyVector(mX * f, mY * f, mZ * f);
}

void operator*=(const float& f)
{
mX *= f;
mY *= f;
mZ *= f;
}```
As you can see I do different stuff on the + and += for example. Is there any good way to get this to work in C#?

### Hierarchical finite state machine

08 December 2014 - 05:37 PM

Hello! I am currently working on a FSM for a project at my university. But I have problem with figuring out if I meet the criteria for this program.

I need to have a state that have sub-states (Hierarchical states). This code is about an AI I know, but the topic itself I feel is more related to general programming.

For example Combat leads to Shooting OR TakingCover. And no other state except for Combat should be able to reach these two states directly they need to go through Combat. This is how I have interpreted Hierarchical Finite State Machine anyway, so I would really like to have some feedback on this. It is not the most pretty piece of code out there so I apologize in advance. I also made a bitbucket repository if someone feel they need to dig deeper to help me, I do not wanna bloat this post with all the code. This is the code I think is the most important one and I only included the rest for people that feel they wanna study the code more.

TLDR; Is the code below hierarchical or have I misinterpreted something?

Here is my PonyStates.h file

```#include "State.h"
class Pony;

class AgentIsHungry : public State
{
public:
static	AgentIsHungry*	Instance		();
virtual void			EnterState		(Pony* p);
virtual void			ExitState		(Pony* p);
virtual void			ExecuteState	(Pony* p);
private:
// Copy construtor and assignment.
AgentIsHungry()							{}
AgentIsHungry	(const AgentIsHungry&);
AgentIsHungry&			operator=		(const AgentIsHungry&);
};

class AgentIsWorking : public State
{
public:
static AgentIsWorking* Instance();
virtual void   EnterState(Pony* p);
virtual void   ExitState(Pony* p);
virtual void   ExecuteState(Pony* p);
private:
AgentIsWorking()       { }
// Copy construtor and assignment.
AgentIsWorking(const AgentIsWorking&);
AgentIsWorking&   operator=  (const AgentIsWorking&);
};

class AgentIsTierd : public State
{
public:
static AgentIsTierd*	Instance	();
virtual void			EnterState	(Pony* p);
virtual void			ExitState	(Pony* p);
virtual void			ExecuteState(Pony* p);
AgentIsTierd()			{ }
private:
// Copy construtor and assignment.
AgentIsTierd(const AgentIsTierd&);
AgentIsTierd&			operator=	(const AgentIsTierd&);
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////
class AgentIsHavingANightmare : public AgentIsTierd
{
public:
static AgentIsHavingANightmare* Instance();
virtual void			EnterState(Pony* p);
virtual void			ExitState(Pony* p);
virtual void			ExecuteState(Pony* p);
private:
// Copy construtor and assignment.
AgentIsHavingANightmare() {}
AgentIsHavingANightmare(const AgentIsHavingANightmare&);
AgentIsHavingANightmare& operator= (const AgentIsHavingANightmare&);
};

{
public:
virtual void			EnterState(Pony* p);
virtual void			ExitState(Pony* p);
virtual void			ExecuteState(Pony* p);
private:
// Copy construtor and assignment.
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////
```

Here is my PonyStates.cpp file

```////////////////////////////////
///////// AgentIsTierd /////////
////////////////////////////////
AgentIsTierd* AgentIsTierd::Instance()
{
static AgentIsTierd instance;
return &instance;
}

void AgentIsTierd::EnterState(Pony* p)
{
if (p->WhereAmI() != Bedroom)
{
std::cout << GetName(p->GetID()) << " is walking to the bedroom; it is time to sleep.\n";
p->ChangeRoom(Bedroom);
}
}

void AgentIsTierd::ExitState(Pony* p)
{
std::cout << GetName(p->GetID()) << " is now leaving the bedroom.\n";
}

void AgentIsTierd::ExecuteState(Pony *p)
{
if (p->IsTierd())
{
std::cout << GetName(p->GetID()) << " wishes to sleep. " << GetName(p->GetID()) << " lay down in bed to sleep! Zzz...\n";
if ((rand() % 100) % 2)
else
p->ChangeState(AgentIsHavingANightmare::Instance());
}
else
{
std::cout << GetName(p->GetID()) << " wakes up refreshed! Time to go to work!\n";
p->ChangeState(AgentIsWorking::Instance());
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

// Good dream
{
return &instance;
}
{
}
{
std::cout << "\t" << GetName(p->GetID()) << " had a nice dream and is feeling refreshed.\n";
//p->ChangeState(AgentIsHungry::Instance());
}
{
p->Sleep();
p->ChangeState(AgentIsHungry::Instance());
}
// Nightmare
AgentIsHavingANightmare* AgentIsHavingANightmare::Instance()
{
static AgentIsHavingANightmare instance;
return &instance;
}
void AgentIsHavingANightmare::EnterState(Pony* p)
{
}
void AgentIsHavingANightmare::ExitState(Pony* p)
{
std::cout << "\t" << GetName(p->GetID()) << " had a nightmare and didn't sleep very well.\n";

}
void AgentIsHavingANightmare::ExecuteState(Pony* p)
{
p->ChangeState(AgentIsHungry::Instance());
}
```

### Pros and Cons with Specular and Diffuse lighting

04 May 2013 - 08:44 AM

First, I hope I post in the right forum for this question!

I am working on a shader in RenderMonkey and the shader is complete, so now I am writing a small report on the shader. My problem here is that I can't find any sources to read about pros and cons about "Specular light" and "Diffuse light". I have googled but I mainly find sources that is about what specular/diffuse light is, and these sources do not talk about the pros of each light model.

I have also checked in my course books to for some clues but I cannot find any. Do anyone here have any suggestion about where I can read about the pros and cons of these to light models?

Best regards!

### Problem with shader in RenderMonkey

20 April 2013 - 11:03 AM

I hope this is in the correct forum, since I am programming the shader I decided to put the topic in here.

I have been working on a shader in RenderMonkey on my desktop computer, and when I transferred the rfx-file to my laptop to continue the work in school I get two error messages that I do not get at home.

OpenGL Preview Window: Linking program .. failure

The fragment shader uses varying uDiffuseColor, but previous shader does not write to it.
The fragment shader uses varying uOpacity, but previous shader does not write to it.

This is the Fragment program

```// Ljus 1
uniform vec3  uLight1Position;
uniform float uLight1DiffIntent, Light1SpecIntent;
float uLight1RimIntent;

// Ljus 2
uniform vec3  uLight2Position;
uniform float uLight2DiffIntent, uLight2SpecIntent, uLight2RimIntent;

varying vec3 uLight1Color, uDiffuseColor, vColor, vNormal, vLightDir, vEyeVec;
varying float uLight1Intent, luminosity1, luminosity2, uLight1RimLight, uOpacity;
varying vec2 vTexCoord;

uniform sampler2D baseMap;

void main(void)
{
vec3 N = normalize(vNormal);
vec3 L = normalize(vLightDir);
vec3 E = normalize(vEyeVec);
vec3 H = normalize(-L + E);

// Lambert Diffuse Lighting.
float diffuse = clamp(dot(N, -L), 0.0, 1.0);

// Blinn Specular
float specular = pow(max(dot(N, H), 0.0), 120.0);
//float diffuse = dot(N, -L);

// Rim Lighting.
float rimLight = smoothstep(0.5, 0.05, dot(N,E));// * (uLight1RimIntent + uLight2RimIntent);
specular += rimLight;

// Diffuse Attenuation
specular *= diffuse;

vec3 base = texture2D( baseMap, vTexCoord ).xyz;

// Lägg till kantljus till den diffusa komponenten.
//vec4 finalColor = vec4(diffuse + rimLight, rimLight, rimLight, uLight1Intent) / (length(uLight1Position) * 0.02);
vec3 vColor = mix(uDiffuseColor.xyz, base, uOpacity);// diffuse + finalColor;
vec4 finalColor = vec4(base * diffuse, 2.0);
gl_FragColor = vec4(vColor * finalColor + rimLight, 0.5);
//gl_FragColor = finalColor;
}
```

The Vertex Program

```uniform vec3 uLight1Position;
uniform vec3 uLight2Position;
uniform vec3 uEyePosition;

varying vec3 vColor;
varying vec3 vLightDir;
varying vec3 vNormal;
varying vec3 vEyeVec, uOpacity, uDiffuseColor;
varying float uLight1Intent, luminosity1, luminosity2;
varying vec2 vTexCoord;

void main( void )
{
vec3 N = normalize(vNormal);
vec3 L = normalize(vLightDir);

vec4 vertex = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = vertex;

uLight1Intent = distance(gl_Position.xyz, uLight1Position.xyz);
//uLight1RimIntent = distance(gl_Position.xyz, uLight1Position.xyz);
luminosity1 = smoothstep(120, 0, uLight1Intent);

vLightDir = -vec3(gl_ModelViewMatrix * vec4(uLight1Position, 0.0));
vNormal = gl_NormalMatrix * gl_Normal;
vEyeVec = -vec3(gl_ModelViewMatrix * gl_Vertex);
vTexCoord = vec2(gl_MultiTexCoord0);
//float diffuse = dot(N, -L) * luminosity1;
//vColor=vec3(diffuse,0.5,0.3);
}
```

I use the exact same rfx file (the exact same code) on the two different computers, I tried it in school as well and the same problem is there as well. We just started to work with hardware shaders so I dont know how rendermonkey work and why this is happening. It only work at home, all the files are the same on the different systems. Anyone that has any idea what I can try to get this to work?