Tuesday, April 20, 2010

Javascript foreach equivalent

Javascript foreach equivalent



Javascript has no foreach statement. Instead, we use the following syntax.


var letters = ["a","b","c"];

for(var i in letter) {
alert(letters[i]);
}


Note the variable i, is an index into the array, and not a reference to the array element.

Javascript FOR loop

A for loop in Javascript


A for loop in java or javascript is similar to a Java or C for loop




for(i = 0; i < 3; i++){
alert(i);
}


the variable i is the counter variable. It starts at 0. This loop runs, which will alert 0. Next it increments i, then returns to the start.

Javascript HTML tags

Javascript HTML


In order to use javascript in a html document, you need a HTML file. A typical HTML file looks like this




<html>
<body>
This is a HTML document
</body>
</html>


Inline


You can either put your script in the html document like so




<html>
<body>
<script>
alert('Hello World');
</script>
This is a HTML document
</body>
</html>


External


or in an external file names script.js




<html>
<body>
<script language="JavaScript" src="script.js"></script>
This is a HTML document
</body>
</html>

Thursday, April 15, 2010

64 Bit OS for Intel

Found an interesting site about a new 64 bit operating system that is purely 64 bit. Seems to be an interesting project

http://www.losethos.com/

Sadly there is a blurb about the GPU and how it won't be supported. This of course makes me saddest of all! Interesting 3D graphics tho in the video on the site. Wonder if the rasterization code was written from scratch?

Although I wont be loading this onto all of my 64 bit servers anytime soon, it does look like an interesting beginning.

Wednesday, April 14, 2010

Depth texturing in OpenGL GLSL Shader Program

Depth Texturing in GLSL



This GLSL shader program is used to enable depth texturing. The depth texture is located under texture unit 5 as shown:


uniform sampler2DShadow texture_5;


Note we had to pass the light's translation matrix in gl_TextureMatrix element 0 as shown:

ShadowCoord = gl_TextureMatrix[0] * gl_Vertex;


Here are the shaders

Vertex Program



varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
varying vec4 ShadowCoord;

void main()
{
ShadowCoord = gl_TextureMatrix[0] * gl_Vertex;

/* first transform the normal into eye space and
normalize the result */
normal = normalize(gl_NormalMatrix * gl_Normal);

/* now normalize the light's direction. Note that
according to the OpenGL specification, the light
is stored in eye space. Also since we're talking about
a directional light, the position field is actually direction */
lightDir = normalize(vec3(gl_LightSource[0].position));

/* Normalize the halfVector to pass it to the fragment shader */
halfVector = normalize(gl_LightSource[0].halfVector.xyz);

/* Compute the diffuse, ambient and globalAmbient terms */
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}


Fragment Program



varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
varying vec4 ShadowCoord;
uniform sampler2D texture_0;
uniform sampler2DShadow texture_5;

void main()
{
vec3 n,halfV;
float NdotL,NdotHV;
diffuse = texture2D(texture_0, gl_TexCoord[0].xy);
ambient = vec4(0.0, 0.0, 0.0, 0.0);


vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w;
shadowCoordinateWdivide.z += 0.0001;
float distanceFromLight = shadow2DProj(texture_5, shadowCoordinateWdivide).a;

vec4 tc = shadowCoordinateWdivide;
/*
MultiSample depth texture
float s = 0.00005;
//float s = 1.0 / 10.0;
//float d = distanceFromLight;
float d1 = shadow2DProj(texture_5, vec4(tc.r+s, tc.g+s, tc.b, tc.a)).a;
float d2 = shadow2DProj(texture_5, vec4(tc.r+s, tc.g-s, tc.b, tc.a)).a;
float d3 = shadow2DProj(texture_5, vec4(tc.r-s, tc.g+s, tc.b, tc.a)).a;
float d4 = shadow2DProj(texture_5, vec4(tc.r-s, tc.g-s, tc.b, tc.a)).a;
float d5 = shadow2DProj(texture_5, vec4(tc.r+s, tc.g, tc.b, tc.a)).a;
float d6 = shadow2DProj(texture_5, vec4(tc.r+s, tc.g, tc.b, tc.a)).a;
float d7 = shadow2DProj(texture_5, vec4(tc.r-s, tc.g, tc.b, tc.a)).a;
float d8 = shadow2DProj(texture_5, vec4(tc.r-s, tc.g, tc.b, tc.a)).a;*/

/* The ambient term will always be present */
vec4 color = ambient;

/* a fragment shader can't write a varying variable, hence we need
a new variable to store the normalized interpolated normal */
n = normalize(normal);

/* compute the dot product between normal and ldir */
NdotL = max(dot(n,lightDir),0.0);

if (NdotL > 0.0) {
color += diffuse * NdotL;
halfV = normalize(halfVector);
NdotHV = max(dot(n,halfV),0.0);
color += gl_FrontMaterial.specular *
gl_LightSource[0].specular *
pow(NdotHV, gl_FrontMaterial.shininess);
}

gl_FragColor = color;
gl_FragColor.a = diffuse.a;

float shadow = 1.0;
if(ShadowCoord.w > 0.0) {
shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.2 : 1.0 ;
}

gl_FragColor.rgb = shadow * gl_FragColor.rgb;

}

Sunday, April 11, 2010

Fable the last chapters under wine

Hi Everyone,

So I am attempting to set up fable using wine v 1.1.37. During the install, I get the error message "Error loading the PID generator DLL. The DLL could not be found!".

To fix, get the winetricks script.


wget http://www.kegel.com/wine/winetricks


Then, we need to run the script to install MFC42 package.


winetricks vcrun6


And voila, the installer runs no problem

Saturday, April 10, 2010

Oblivion in Wine : Bypass Securom

Installing Oblivion under linux wine Ubuntu



Attempting to install oblivion in linux to run under wine causes some problems for me. The problem is securom, or saferom. When you attempt to install oblivion under wine (I am using Ubuntu) the installer fails. We need to find a method to bypass securom so we can use wine to play the game.

Since the install files are ultimately just tucked away inside some cab files, installation without using the actual installer is possible. All you have to do is use a tool called "Unsheild".

Users who are using playonlinux should still be able to use this guide, and install the game. Later use playonlinux to actually run the game.

What you will need to set up Oblivion



You will only need an open terminal and an ISO version of the disk. You could try this method with the DVD itself in the drive.

Mounting the ISO



I had to try this method in my attempt to install oblivion from some ISOs I had saved on my hard drive. What I ended up doing was mounting the ISO to my cdrom mount point:


sudo mount -o loop /path/to/oblivion.iso /media/cdrom


replacing /path/to/oblivion.iso with the path to the actual ISO file.

Setting up Unsheild



Then you need to install "Unsheild"


sudo apt-get install unsheild


Installing Oblivion



Then you run the command to "install" the game


unshield -d /path/to/install/to x /media/cdrom/data1.cab


replacing /path/to/install/to with the path you wish to install the game. This will extract all the cab contents to the specified folder.

Running the Game



Next we run the game


wine /path/to/install/to/Oblivion/OblivionLauncher.exe


Enjoy!

Wednesday, April 7, 2010

PHP's case sensitivity in function names

Hi Everyone,

Just thought I'd toss out a random tidbit. I've found a program that tried to define a function name that conflicted with a global function name, but not like you would expect.

They used camel case for the function name, when the system defined function didn't. Unfortunately function names in PHP are NOT case sensitive, as shown in the following experiment:


<?php

function test_func() {
echo 'test_func';
}

function Test_Func() {
echo 'Test_Func';
}


Running this script gives:


bill-hamiltons-imac:~ bill$ php test.php

Fatal error: Cannot redeclare test_func() (previously declared in /Users/bill/test.php:4) in /Users/bill/test.php on line 9


So there you have it. PHP function names are not case sensitive which some of you may already know but this is obviously the proof.

Thanks!

Thursday, April 1, 2010

Programming Games in Python

Programming Linux Games - Prototyping in Python



Hi Everyone,


Its been awhile since I've posted on topics related to game programming. Basically I am here to talk about programming games in Python programming language. I have programmed in many languages including C and C++ and even developed graphics engines in these languages. Eventually I get to a point where the code is large and basically I sit pondering where to go next in order to add features, and also get lost in semantics of the languages and how I will accomplish these goals.

Next I started trying out python, in order to prototype new features. I have written rather large programs in python before, and the one thing I got out of it is that its very fast for writing programs, and also very type safe. So if you try to compare and int with a float, it will raise an exception. This sounds like a headache, but really its a dream for debugging later on!

Some problems you may be thinking is 1) Python is slow. Right you are, python is rather slow compared to languages like C++ and C, but only in specific cases. For example, iterating through arrays can be much faster in C and C++. Memory usage is also less because you are working with more primitive data types. Also python does constant checking of variable types, etc.

When it comes to games, I feel that today's processors are very fast. Not only that, but many of the heaviest tasks are getting put onto the GPU, which has limited choice in programming languages, and they are mostly low level. Tasks like physics, rasterization are now completely done on the GPU, and now the trend seems to be even using the GPU for general processing tasks (GPGPU).

Back to the point, python's speed limitations no longer apply considering we are pushing all the data to the GPU and allowing it to do all the work with its languages. There are some things left to the CPU, like event handling, playing sounds, and some basic math (debatable as to perform on GPU or CPU) which I think my quad core can handle quite easily thank you!

Further more, threading in python is very easy however you still need to be very careful as to how you set up variable access etc.

As I will show in future blog posts, using python for game development should become a reality because its so type safe, it allows you to program things quickly (functionally) and then allows you to push all the data to the GPU very easily. Currently I am working on a GPGPU processing program written in python, and it should come in under 1000 lines of code!

Wow that is a large starbuck's coffee



Just found a link to starbucks doing some market research. Im glad they are listening to their customers but I think this size "Plenta" is a little over the top. I mean seriously you should not need that much coffee, what is that an entire pot?!? Check out the link. This is real folks!!

http://www.starbucks.com/blog/10113/starbucks-listens-to-customer-request-for-more-sizes.aspx

Happy april fools I guess haha!