13 Replies - 904 Views - Last Post: 30 August 2013 - 06:21 PM Rate Topic: -----

#1 Heartl3ss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-August 13

Help with a C++/C Project (Ray Tracer, Canvas)

Posted 21 August 2013 - 01:28 PM

Hey guys. Me and my teacher are working on a project to make a simple Ray Tracer application (give realistic lightning effects to simple 3D objects, like spheres and cubes from various angles). Before he gives me any kind of code of the ray tracer he asked me to create a simple application on visual studio that when i run it, will open a empty white/black window which will be a canvas or even give to each pixel of this canvas a random colour. He told me that on the web there are huge ammounts of examples for something like this but i searched a lot and didn't find anything. Probably because i don't know how to search or what to search for as it's something very specific.
Does any of you programming master's have any idea's to help me or give me some good starting tips or even useful links with examples that i can follow? I will greatly appreciate it!
Thanks in advance and have a nice day!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with a C++/C Project (Ray Tracer, Canvas)

#2 mustardpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 02-December 12

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 21 August 2013 - 02:14 PM

You are probably going to need an OpenGL tutorial. I have created a very similar program and this is how I did it anyway.

Here is a good OpenGL guide that I used.

My link

Here is a Win32 API tutorial

My link
Was This Post Helpful? 1
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 21 August 2013 - 04:19 PM

Also tell your teacher that ray tracing is inefficient and superseded by shaders for real time rendering. :)
Was This Post Helpful? 0
  • +
  • -

#4 Heartl3ss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-August 13

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 21 August 2013 - 05:59 PM

View PostButchDean, on 21 August 2013 - 04:19 PM, said:

Also tell your teacher that ray tracing is inefficient and superseded by shaders for real time rendering. :)/>

i guess he already knows that...we are just doing this small little project to present it on a programming event so i can get some good extra marks on my degree... :D
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 21 August 2013 - 06:06 PM

Yes, you certainly wouldn't use ray tracing for some kind of game application. However, people do still like to render pretty pictures, which ray tracing is a good choice for.

If it's Visual Studio and C++/C, your choices would be either Win32 or MFC. If you're looking for speed, you'll want something that leverages DirectX. But for just an image, a DIB will do.

Hmm... googling "win32 dib" gets me these as hit three and four respectively:
http://www.dreaminco...ial-in-c-win32/
http://www.functionx...32/Lesson14.htm

Both seem like a reasonable place to start.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 21 August 2013 - 07:40 PM

You can do so much more now with shaders vs. ray tracing. Even non-realtime rendering work is done with them now.
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,363
  • Joined: 20-March 10

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 21 August 2013 - 08:44 PM

View Postbaavgai, on 22 August 2013 - 01:06 AM, said:

If it's Visual Studio and C++/C, your choices would be either Win32 or MFC. If you're looking for speed, you'll want something that leverages DirectX. But for just an image, a DIB will do.

Hmm... googling "win32 dib" gets me these as hit three and four respectively:
http://www.dreaminco...ial-in-c-win32/
http://www.functionx...32/Lesson14.htm

Both seem like a reasonable place to start.


Seriously you googled it and all you could come up with was me !

That is just so sad :)

However,
If you want directX there is the SimpleDirect2dApplication in the directX SDK, this includes an example using Windows Imaging Component (WIC) to display a bitmap on screen.

If you want OpenGL you might be better looking at my example in the Tutorial pointed to by baavgai.
as you need the (LPBYTE) lpBits from the PBITMAPINFOHEADER using GetDIBits for displaying a Bitmap in OpenGL.

As for making a Win32 window black or white

WNDCLASSEX wincl;        /* Data structure for the windowclass */
........

wincl.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(0,0,0)); // for black screen
wincl.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(255,255,255)); // for white screen
........



I really wouldnt touch MFC.

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 22 August 2013 - 05:49 AM

:P I thought you'd be happy.

View PostButchDean, on 21 August 2013 - 10:40 PM, said:

You can do so much more now with shaders vs. ray tracing. Even non-realtime rendering work is done with them now.


Because ray tracing fundamentally hasn't changed. It still provides the most realistic rendering at the simple cost of time. A whole lot of time. Other methods ask the questions "is that time worth it" and "how close can we get to ray tracing without actually paying the same cost as ray tracing."

Of course, that amazing 3D simulation generated is ultimately squished down into a finite 2D view port. At that time, it often comes out that the effort really wasn't worth the reward. However, the results of ray tracing are still the goal of other methods.

Note, I've enjoyed may hours of amusement with POV-Ray. Prior to that, I wrote a simple ray tracer for college ( Turbo Pascal .)
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 22 August 2013 - 08:12 AM

The easiest way to do it, avoiding all of the nasty microsoft gui code, is to create a array of pixels to represent the output image, and display it using opengl. I suggest simply writing the image to a file first.
Was This Post Helpful? 0
  • +
  • -

#10 Heartl3ss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-August 13

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 22 August 2013 - 08:21 AM

Indeed my teacher told me yesterday that the Libraries of C++ in Visual Studio have already the Canvas integrated. I just need to use the specific code so when i run it, it will open a new window that will be the canvas and fill it with random pixels, that's my first step. The interface is in C++ and my purpose for now is just to show something on the screen as a comfirmation. Then me and my teacher will try to convert it so a simpler language © so that we can transfer the algorith on an FPGA chip and this is what we will present...
Was This Post Helpful? 0
  • +
  • -

#11 Heartl3ss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-August 13

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 28 August 2013 - 02:02 PM

ok guys i found this code that allows me to make a simple small window without even using OpenGL, cause i can't seem to make it work as it doesn't recognise a lot of it's functions when i try to run my program.

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	//Step 1: Registering the Window Class
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	// Step 2: Creating the Window
	hwnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		g_szClassName,
		"The title of my window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	// Step 3: The Message Loop
	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}



anybody knows if there is a built-in way function that i can call to give a random RGB color to each pixel of this window?

i found out about the SetPixel function but im not sure in where or how to integrate it in this piece of code to make it work..till now i failed...
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,363
  • Joined: 20-March 10

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 28 August 2013 - 08:52 PM

Yes,

you have found a simple Win32 window code,

however finding code and understanding code are two different things.

I would suggest some Tutorials

http://www.dreaminco...-to-the-winapi/

or

http://www.dreaminco...he-windows-api/

once you have understood all that.

in win32 terminology all the painting of a window is done in WM_PAINT:

lets look at a typical WM_PAINT routine

case WM_PAINT:
	        {
	 
	            BeginPaint(hwnd,&ps);
	 
	             
	            //draw something here using gdi functions
	 
	             
	            EndPaint(hwnd,&ps);
	 
	            return 0;
	        }


a link to gdi functions

http://msdn.microsof...y/aa923590.aspx

what you want to do is set up two loops one for screen width and one for screen height

loop through each pixel using as you Say SetPixel and rand() like this.

int R = rand()%255;
int G = rand()%255;
int B = rand()%255;

SetPixel(hdc,X,Y,RGB(R,G,B)/>);



where hdc is a HDC (handle to a device context) and X and Y are for - loop variables.

http://msdn.microsof...v=vs.85%29.aspx

you probably want to put all this in a function and not actually in WM_PAINT as it will lead to slow drawing routines

so function for drawing

HDC DrawScreen(HDC hdc, int width, int height)
{
  HDC tempDC;
  tempDC = CreateCompatibleDC(hdc)
  for (int Y = 0; Y< height; Y++)
  {
  for (int X = 0; X< width; X++)
  {
  int R = rand()%255;
  int G = rand()%255;
  int B = rand()%255;

  SetPixel(tempDC,X,Y,RGB(R,G,B)/>);
  }
  }
  return tempDC;
}

call this function in say WM_CREATE then pass this HDC to WM_PAINT and BitBlt to screen as a one time screen draw.

something like this

case WM_PAINT:
	        {
	            PAINTSTRUCT ps;
	            BeginPaint(hwnd,&ps);
	 
	             
	            BitBlt(ps.hdc,0,0,width,height,memhdc,0,0,SRCCOPY);
	 
	             
	            EndPaint(hwnd,&ps);
	 
	            return 0;
	        }

where memhdc is a memory hdc that is the return value of your function.
of course all of this is just rough code, you will have to fill in the blanks a bit, but it will give you a good idea on how to approach this.

Regards

Snoopy.
Was This Post Helpful? 3
  • +
  • -

#13 Heartl3ss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-August 13

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 30 August 2013 - 12:45 PM

I understood how im gonna do it..and i had the same idea about the dual loop in order to give a random color for each pixel on the screen..the outer loop for height and the inner for the width..the only thing that i can't understand is where to integrate this raw code in my programm and making it compilable...i also understood that i have to call the HDC that includes the loop in the WM_CREATE cause it's when the window will be created, so at the same time it will assign a random color for each pixel of the window. What i didn't understand is the last part about WM_PAINT..where do i have to put this code and how i will call it? I am asking cause i didn't understand in what sense it helps me and why its neccesary.

And i really appreciate your help Snoopy. Your post helped a lot and every help is appreciated deeply. What if i should change it to something more simple? For example just view something in that window like a message or color a specific pixel or number of pixels? Don't know what other kind of stuff i can do on a window. What im trying to do it view practically anything on a window as a comfirmation...it can be a pixel , an image, a phrase...
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,363
  • Joined: 20-March 10

Re: Help with a C++/C Project (Ray Tracer, Canvas)

Posted 30 August 2013 - 06:21 PM

View PostHeartl3ss, on 30 August 2013 - 07:45 PM, said:

the only thing that i can't understand is where to integrate this raw code in my programm and making it compilable...i also understood that i have to call the HDC that includes the loop in the WM_CREATE cause it's when the window will be created, so at the same time it will assign a random color for each pixel of the window. What i didn't understand is the last part about WM_PAINT..where do i have to put this code and how i will call it? I am asking cause i didn't understand in what sense it helps me and why its neccesary.


Yeah,

The important thing is not too panic this is still C.

and

// Step 4: the Window Procedure
   LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)


is just a function, its like any other function and the same C rules apply to this function as to any other.

Your program entry point is WinMain or to give it it's full title

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow
)

this is like your program entry point in C

int main()


in WinMain there is a WNDCLASSEX structure this is for registering the window with the OS.

as part of setting up the structure

we have this line.

wc.lpfnWndProc   = WndProc;


this tells the OS that the Window Procedure for this window is called WndProc.

Are you catching on yet ?

// Step 4: the Window Procedure
   LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)


Do you see a resemblance ?

for instance if we do this in registering our class

wc.lpfnWndProc   = WindowProcedure;


then we have to do this

// Step 4: the Window Procedure
   LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)


inside the Window Procedure (or function)

we have a switch statement and that switch statement is switching msg

msg is just a parameter of the Window Procedure that contains Window Messages from the OS.

We could call it anything we like it doesnt have to be msg it could be message for instance like this

// Step 4: the Window Procedure
   LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch(message)
   {
     //In here each case statement is a Windows Message
    default:
    return DefWindowProc(hwnd, message, wParam, lParam);
   }
   return 0;
}



Each Window Message starts with WM_ for simplicity

Now our Window Procedure is responsible for handling those window messages if process one we return 0;
if we do not process a message we return DefWindowProc(hwnd, msg, wParam, lParam);
and let the OS handle it.

So WM_CREATE this message is sent by the OS to the function when our application is started.

WM_PAINT this message is sent by the OS to the function when our application requires repainting perhaps another window has been dragged across it, perhaps it has been minimised etc.

We want to do our painting here as if we do it elsewhere our drawing wont stick, it will be temporary, if we drag a window across it etc it will be wiped out.

Remember this Procedure is just a normal function, so dont panic. We will go back to it.

// Step 4: the Window Procedure
   LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC memhdc, hdc;
    RECT rect;
    switch(message)
   {
     case WM_CREATE:
    {
      GetWindowRect(hwnd, &rect);
      hdc = GetDC(hwnd);
      memhdc = CreateCompatibleDC(hdc);
      memhdc = DrawScreen(hdc,rect.right, rect.bottom);
      UpdateWindow(hwnd);
      return 0; // we processed this so return 0;
    }
    default:
    return DefWindowProc(hwnd, message, wParam, lParam);
   }
   return 0;
}



So now we know this is just a function and C rules still apply,
and inside the function is just one switch statement switching windows messages

Where do you think WM_PAINT is going to go ?

I will leave that up to you.

Quote

Don't know what other kind of stuff i can do on a window.


Practically anything display pictures, display text, display graphics, display primitives, display a webcam, display movies etc.

Regards

Snoopy.

This post has been edited by snoopy11: 30 August 2013 - 09:56 PM
Reason for edit:: Spelling mistakes

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1