• Create Account

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

# Mr. Big

Member Since 02 Apr 2011
Offline Last Active May 02 2013 08:34 PM

### Drawing 2D in 3D World (2.5D)

27 April 2013 - 12:03 AM

Basically I've come across a few games that use this convention, and I'm curious on how its done and want to implement it.

Something similar to this;

This video seems to have accomplished it really well. There are a lot of pretty bad implementations of it too.

I just want to know where to start, what to look out for, any tips or advice.

Cheers

### Terrain generation in 3D Tile Based Game

23 April 2013 - 07:37 AM

Hello everyone. First time XNA project here, so still learning the ropes.

My first idea for a starter project was to make a tile based game. Each movable terrain square would be one quad (or 2 triangles).

Then use some method to determine which tile the mouse is over and clicking it moves the player there.

So what would be the ideal way to do this in XNA? My first thought was to create a triangle strip and a text file with all the vertices and heights and load it in and draw it.

Any other suggestions?

### Passing Lua Array to C++ Array

28 October 2011 - 08:45 PM

I have no idea why this wont work. Needs a new set of (more experienced) eyes to take a look at it.

Lua table;
`maplist = {"sand.obj", "jungle.obj", "cubes.obj"}`

C++
```
//populate the map list array
stringw maplist[50];
int i = 0;

lua_getglobal(L, "maplist");
lua_pushnil(L);

while(lua_next(L, -2))
{
maplist[i] = (stringw)lua_tostring(L, -1);
i++;

lua_pop(L, 1);
}
lua_pop(L, 1);
```

Basically its just filling the array with random garbage.
If i change it to maplist[0] it works. (obviously overwrites it twice though)

Any tips? Maybe even a better way to do it?

Edit: Ok? Not initializing the array was apparently the problem. *shrug* Resolved now, I guess?

### CG Shader with Textures problems.

02 April 2011 - 05:59 AM

So its come to this. Posting a help topic on GameDev!

I'm pretty new to CG, and my work is based on about 3 of my projects merged with mixed success.

So whats happening is the fragment shader is sortof working, I can see its doing something. However when I try to add the textures into the mix it decides there is an Invalid Program Handle.

The .cg file;
```struct vertex
{
float4 position : POSITION;
float4 normal   : NORMAL;
float4 color0   : COLOR0;
float2 texCoord : TEXCOORD0;
};

struct fragment
{
float4 position : POSITION;
float4 color0   : COLOR0;
};

fragment main( vertex IN,
uniform float4x4 modelViewProjection,
uniform float4x4 modelViewInverse,
uniform float4 eyePosition,
uniform float4 lightVector,
uniform sampler2D decal : TEX0)
{
fragment OUT;

OUT.position = mul( modelViewProjection, IN.position );

// Transform normal from model-space to view-space
float4 normal = normalize( mul( modelViewInverse, IN.normal ).xyzz );

// Store normalized light vector
float4 light = normalize( lightVector );

// Calculate half angle vector
float4 eye = eyePosition;
float4 half = normalize( light + eye );

// Calculate diffuse component
float diffuse = dot( normal, light );

// Calculate specular component
float specular = dot( normal, half );
specular = pow( specular, 32 );

// A little ambient color
float4 ambientColor = float4( 0.2, 0.2, 0.2, 0.2 );

// White specular
float4 specularMaterial = float4( 1.0, 1.0, 1.0, 1.0 );

// Combine diffuse and specular contributions and output final vertex color
OUT.color0 = diffuse * IN.color0 + specular * specularMaterial + ambientColor;
//OUT.color0 = diffuse * tex2D(decal,IN.texCoord) + specular * specularMaterial + ambientColor;

return OUT;
}
```

So see where I commented out "OUT.color0 = diffuse * tex2D(decal,IN.texCoord) + specular * specularMaterial + ambientColor;". If i leave it uncommented I get the model rendered with what looks like some form of shading. If I uncomment, I get Invalid Program Handle. I dont know if this is the best way to be doing things, I've had very little theory on shaders and just reading others' code.

Heres the rest of the code.

```...
static CGparameter modelViewProjection;

static CGparameter myCgFragmentParam_decal;

static CGparameter modelViewInverse;
static CGparameter CGeyePosition;
static CGparameter CGlightVector;

static CGprofile g_CGprofile;
static CGcontext g_CGcontext;
static CGprogram g_CGprogram;
...
```

Snippet from the Initializer;
```...
g_CGprofile = CG_PROFILE_ARBVP1;

g_CGcontext = cgCreateContext();
checkForCgError("creating context");

g_CGprogram = cgCreateProgramFromFile( g_CGcontext,
CG_SOURCE,
"cg_lighting.cg",
g_CGprofile,
NULL,
NULL );
checkForCgError("creating program");

modelViewProjection = cgGetNamedParameter( g_CGprogram, "modelViewProjection" );
modelViewInverse = cgGetNamedParameter( g_CGprogram, "modelViewInverse" );
CGeyePosition = cgGetNamedParameter( g_CGprogram, "eyePosition" );
CGlightVector = cgGetNamedParameter( g_CGprogram, "lightVector");
checkForCgError("naming params");

myCgFragmentParam_decal =cgGetNamedParameter(g_CGprogram, "decal"); //texture
checkForCgError("getting decal parameter");//texture

cgGLSetTextureParameter(myCgFragmentParam_decal, 1);//texture
checkForCgError("setting decal 2D texture");//texture
...
```

The error CG throws is "creating program" Invalid Program Handle.

Snippet from the Rendering loop;
```	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode( GL_MODELVIEW );
glTranslatef( 0.0f, 0.0f, -200.0f );
glRotatef(-50,1,0,0);

cgGLSetStateMatrixParameter(modelViewProjection,
CG_GL_MODELVIEW_PROJECTION_MATRIX,
CG_GL_MATRIX_IDENTITY );

cgGLSetStateMatrixParameter( modelViewInverse,
CG_GL_MODELVIEW_MATRIX,
CG_GL_MATRIX_INVERSE_TRANSPOSE );

float fEyePosition[] = { 0.0f, 0.0f, 0.0f, 0.0f };
float fLightVector[] = { 0.0f, 0.0f, 1.0f, 0.0f };

// Normalize light vector
float fLength = sqrtf( fLightVector[0]*fLightVector[0] +
fLightVector[1]*fLightVector[1] +
fLightVector[2]*fLightVector[2] );
fLightVector[0] /= fLength;
fLightVector[1] /= fLength;
fLightVector[2] /= fLength;

cgGLSetParameter4fv( CGeyePosition, fEyePosition );
cgGLSetParameter4fv( CGlightVector, fLightVector );

cgGLBindProgram( g_CGprogram );
cgGLEnableProfile( g_CGprofile );

cgGLEnableTextureParameter(myCgFragmentParam_decal);
checkForCgError("enable decal texture");

//now draw all the stuff
glPushMatrix();
glTranslatef(md5.getxPos(),md5.getyPos(),md5.getzPos());
cgGLSetStateMatrixParameter(modelViewProjection,CG_GL_MODELVIEW_PROJECTION_MATRIX,CG_GL_MATRIX_IDENTITY);
md5.render();
glPopMatrix();

cgGLDisableProfile( g_CGprofile );
checkForCgError("disabling decal texture");

cgGLDisableTextureParameter(myCgFragmentParam_decal);
checkForCgError("disabling decal texture");

SwapBuffers(hDC);
```

Any help here would be greatly appreciated. At least a point in the right direction.
Originally I had a separate vertex and fragment shader but I was having some problems and ended up rewriting it like this. It was working all well and good until I decided to implement textures. I'm thinking this could be the problem, because at the creating program part there is no entry function declared - does there need to be one for it to work with textures? I can't do it the way I have it now?

PARTNERS