Beginning Ray Tracing

As a side-project, I’m working on writing a ray-tracer, with much help (and source code) from Scratch-a-Pixel. This is very basic at the moment – still need to add lighting, shadows, reflections and triangle intersection.

my first ray tracer

Although ray-tracing is not used in real-time rendering (and probably never will, due to the algorithmic complexity of ray-intersection tests, and not until we get a revolution in hardware at any rate), being able to cast rays is often very useful in other areas, such as AI. Also, it’s a good way of learning the fundamentals of sampling and anti-aliasing since everything is done in software.

Rhythm Blaster – my first game

Recently I got to the second round of the Search for a Star games coding competition, for which I had 2 weeks to a) learn Unity and b) turn something that looked like this:

original

into something that looks like this:

screenshot 2

The game is a 2D space-shooter with a rhythmic twist, inspired by music games such as Rhythm Heaven and Love Live: School Idol Project. Enemies arrive in rhythmic patterns and you have to shoot them down in time with the music. There are two levels, and at the end, you get a score depending on how many hits and misses you got:

screenshot 3_800x450

Here’s a gameplay video:

Download the game (27Mb):
https://onedrive.live.com/redir?resid=56CD19EB372DF550!1221&authkey=!ABi3Sw61HZraNUk&ithint=file%2czip

Download the Unity project (87Mb):
https://onedrive.live.com/redir?resid=56CD19EB372DF550!1222&authkey=!AJvC1OotBXipPgw&ithint=file%2czip

Fireworks particle system set to music with Direct3D 11

In January, I made a particle system to display a realistic fireworks show, in order to learn DirectX 11 and geometry shaders.

The program was written from scratch in C++ and DirectX 11.

Particles are implemented as camera-facing textured quads with transparency generated by the geometry shader. Gravity and atmospheric drag are simulated for each particle on the CPU. Particle states are stored in a set of ring buffers (implemented as C++ vectors where the index wraps around).

I followed a data-oriented programming paradigm for performance. My PC can run up to 1.2 million particles at 60fps, dropping to 26fps during the peak load of 2.8 million particles.

The debugging display in the top left corner shows new particles being added to the end of the buffer and dead particles being removed from the front of the buffer.

Firework data, such as timing, firing location, direction, velocity and colour, were scripted by hand using Excel, saved to a text file and read in by the program at initialisation time.

Music playback is handled by the SFML library.

Total time taken: 10 days

Progression:
Day 1: wireframe cube
Day 2-3: textured cube with transparency:hakase cube_770x600

Day 4: array of 8 million camera-facing quads with the geometry shader:8 million hakases_800x420

Day 5-6: particle system with explosions:explosion of catgirls

Day 7: music playback
Day 8-9: script fireworks display to music
Day 10: add particle trails

Download the Visual Studio project (12Mb) (build with x86 Release configuration):
https://onedrive.live.com/redir?resid=56CD19EB372DF550!1220&authkey=!AGTzG8dHFBkgeME&ithint=file%2czip

 

Lego House in Direct3D 9

I made this project in January to model and render a Lego model, coded from scratch in C++ and DirectX 9.0c, with the invaluable assistance of Luna’s book, Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach.

Bricks are procedurally generated from a custom text file that specifies the size, position and colour of each brick.

Phong shading was used as the lighting model, implemented using a pixel shader. There is only one directional light in the scene.

Download the Visual Studio project (89Kb) (build with x86 Debug/Release configuration): 
https://onedrive.live.com/redir?resid=56CD19EB372DF550!1218&authkey=!AC0hjMKlGn9EK9g&ithint=file%2czip

Git repository:
https://bitbucket.org/handyj443/graphics-assignment-1-lego/

A very basic software wireframe renderer – source code

I wrote this a few months ago to consolidate my learning on 3D maths. In the spirit of Handmade Hero, it uses no off-the-shelf maths or graphics libraries. The program reads in an OFF model file, triangulates it, and draws the wireframe as a bitmap to a section of memory which is sent to the screen using the Windows API StretchDIBits function:

screen capture2

It’s very rudimentary and I’m sharing this more for those who are interested in looking at the code than for general consumption! With those warnings in mind, you can download the Visual Studio project here. Build with F7, run with F5, and you should see a beautiful sea shell. You have to change the source code to open different models 😉

UPDATE (28 March 2016): As a SIMD learning exercise, I’ve re-written my maths functions to use Intel SSE intrinsics. The frame time is dominated by the time it takes to draw of lines to the screen, so although the vertex transform stage is about 15% faster, the overall frame rate doesn’t go up noticeably. Download the updated Visual studio project here.

How to open Introduction to 3D Game Programming With DirectX 9.0c: A Shader Approach sample projects with Visual Studio 2013

Frank Luna’s DirectX 9.0c book is excellent, but it can be a pain trying to build the code samples using the latest versions of Visual Studio. Here’s what I did to get it working with Visual Studio 2013 on Windows 7:

First, ensure you have installed the DirectX SDK (June 2010): https://www.microsoft.com/en-gb/download/details.aspx?id=6812

Then add the following directories to your project in Project Properties:

Add the following to C/C++ > General > Additional Include Directories:
$(DXSDK_DIR)Include;C:\Program Files %28×86%29\Microsoft DirectX SDK %28June 2010%29\Include;%(AdditionalIncludeDirectories)

Add the following to Linker > General > Additional Library Directories:
$(DXSDK_DIR)Lib\x86;%(AdditionalLibraryDirectories)

In Linker > Input > Additional Dependencies, change dxerr9.lib to dxerr.lib.

Finally, in the source code, change dxerr9.h to dxerr.h. The project should now compile.

Bitwise Multiplication with Shifts and Adds

I’ve been learning some x86 assembly over the past week.

Something that assembly language does really well (even better than a higher level language such as C) is bit manipulation. For example, here is a way to multiply two 32 bit unsigned integers using only shifts and adds. This is an interesting exercise because shifts and adds are among the most low-level operations a CPU can perform, and it seems likely that the MUL instruction is in practice implemented using some combination of shifts and adds.

To see how this is possible, we first realise that any number can be written as a sum of powers of 2:

E.g. 42 = 32 + 8 + 2 = 2^5 + 2^3 + 2^1 = \text{0001 0101b}

Due to the distributive property of multiplication, when we multiply a number, we can multiply by the powers of two and sum:

20 \times 42 = 20 \times (32 + 8 + 2) = 20 \times (2^5 + 2^3 + 2^1)

And in binary, multiplying by a power of two is simply a bit-shift to the left:

20 \times 2^1 = \text{0000 0010 1000b}
20 \times 2^3 = \text{0000 1010 0000b}
20 \times 2^5 = \text{0010 1000 0000b}

So for each power of two of 42, we can multiply by shifting 20 by the required number of bits and accumulating the result:

20 \times 42 = \text{0011 0100 1000b}

Here is the listing in MASM. The actual multiplication loop is just 5 lines of code.

; Binary Multiplication
.code
main PROC
	mov		eax,42
	mov		ebx,20
	call	bitmul
						; EAX = 840
	exit
main ENDP


; Multiplies an unsigned 32 bit integer in EAX by EBX
; Receives: EAX = uint32, EBX = uint32
; Returns: EAX
bitmul PROC
	push	edx			; save used registers
	push	ecx
	mov		edx, eax	; store original multiplicand into EDX
	mov		ecx, 32		; loop for 32 bits
	mov		eax, 0		; eax will accumulate the result

BeginLoop:
	shr		edx, 1		; shift lowest bit into carry flag
	jnc		DontAdd		; jump if carry flag not set
	add		eax, ebx
DontAdd:
	shl		ebx, 1		; shifting multiplies EBX by 2
	loop	BeginLoop

	pop		ecx			; restore used registers
	pop		edx
	ret
bitmul ENDP

END main