Saturday, October 25, 2008

Odd Magic Square NxN C++ code

The blogger interface is stupid it keeps formatting my input so I am attaching the whole cpp file. Get the C++ Code. Ofcourse rename the file remove the .zip extension

Magic Squares Algorithm & C++ implementation

Hi all,
This time around its something a bit tricky generating an odd magic square. Details are given everywhere. The wiki entry is sufficient, check it out

I base on the same logic which is commonly followed the one up one right stepping strategy. Here is the pseudocode,

//n is the dimension of magic square for a 3*3 square n=3;

int magic[n][n]={0};

curRow=0, curCol=1;

oldRow=0, oldCol=0;

count =0;





//See if we are into an empty cell

if( magic[curRow][curCol]==0 )


magic[curRow][curCol] = ++count;




//Move down from the previous row but keep the same column

magic[(oldRow+1)%n][oldCol] = ++count;

curRow = (oldRow+1)%n;

curCol = oldCol;


//Store the last row and column index



//Move one up and one right



//Wrap around the size


curRow =n-1;



Thursday, June 19, 2008

Getting a list of files of a specific extension in a specified folder in Win32 API for c/c++

Often in a Win32/C/C++ application, we need a function to go through a specific folder and snatch all files matching a specified extension. While there is support for doing so in C# but there is no convenient function to do it in Win32/C/C++. Here I am sharing the function I use quite often in my day to day applications. It might help others also.

void GetFileList (std::vector& list, std::string dir, std::string extension)
WIN32_FIND_DATA findData;
HANDLE fileHandle;
int flag = 1;
std::string search ("*.");
if (dir.length() == 0) dir = ".";
dir += "/";
//search = dir + search + extension;
std::string temp = dir;
search = temp;
// SetCurrentDirectory(dir.c_str());
fileHandle = FindFirstFile(search.c_str(), &findData);

if (fileHandle == INVALID_HANDLE_VALUE) return;
while (flag)
flag = FindNextFile(fileHandle, &findData);

Monday, June 9, 2008

Light makes a very big difference.

Don't believe me take at a look at the snapshots yourself. I have added gradient based lighting on the 2D object based texture slice volume rendering.

Lit engine volume rendering
Lit engine volume rendering

Sunday, June 8, 2008

Transfer function loaded into 2D object based texture slicing

Today, I added the transfer functions. First I made a simple class to wrap a transfer function. This class has a very easy interface. To add color point curve, simply call TransferFunction::AddPoint(int intensity, float opacity). Then I added it to my object based 2D texture slicer. See the snapshots for details.

Transfer function object based texture slicing
Transfer function object based texture slicing
Transfer function object based texture slicing

Saturday, June 7, 2008

View aligned 3D Texture Slicing

Today I did the View aligned 3D Texture Slicing. The only thing different here is that we have to get the intersection test between a plane parallel to the viewing plane and intersect it to the unit cube placed at origin.

I will add transfer functions to this and the object aligned version later.

View aligned 3d texture slicing

Thursday, June 5, 2008

Object aligned slice based volume rendering

I just finished 2D texture slices based volume rendering today. Here are the snapshots of the application. Its damn fast. I will be adding some transfer function editing facility into it tomorrow.

A cool idea though will now work on transfer functions and then view aligned slicing of 3D texture.

Object aligned slicing

Implementation details:

I used glut and OpenGL for this b/c I think its much more fast to develop applications in OpenGL and glut. First I determine the current view direction vector which is simply obtained using the following code

GLfloat matView[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matView);
GLfloat viewDir[3]={ -matView[2], -matView[6], -matView[10]};

Next a 3d texture is loaded from the CT/MRI scans and then using this code, object space quad slices are generated. Here I am only giving the code for the PosZ axis. A slight modification is needed for the other axes.

void DrawSliceZPos()
float zPos = 0.5;
float zAmt = -1.0f/zSlices;


//offset the slice so its in the middle
zPos += zAmt/2.0f;
for(int i=zSlices-1;i>=0;i--)
float tex = (((float)i)/zSlices);
glTexCoord3f(0,0,tex); glVertex3f(-0.5, -0.5, zPos);
glTexCoord3f(0,1,tex); glVertex3f(-0.5, 0.5, zPos);
glTexCoord3f(1,1,tex); glVertex3f( 0.5, 0.5, zPos);
glTexCoord3f(1,0,tex); glVertex3f( 0.5, -0.5, zPos);
zPos += zAmt;

The appropriate slices are drawn based on the maximum abs value of the current view direction as

case 0:
if(viewDir[maxDim] > 0.0f)
case n: ... and so on

Thats it and finally the result is blended to produce the rendering as shown above.

Monday, June 2, 2008

A simple 3d mesh viewer using C++, JUCE and OpenGL

This is the current project I am working on. Its made completely in C++ using JUCE and OpenGL.
I had a lot of fun making this.

3d mesh viewer c++ JUCE OpenGL

Saturday, May 24, 2008

A simple 3d wave CG shader

I have been doing GPU programming for a while and during my learning I made this small demo showing a 3D wave using OpenGL and GLSL. The idea is pretty simple though.

Just for fun.

3d wave shader

Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.