Skip to main content
added 1078 characters in body
Source Link
user18490
  • 656
  • 4
  • 8

EDIT

I am editing the answer to respond to your comment. The algorithm would look like this:

Color *buffer = Color  char [w * h];
// create a texture that you can map onto the quad stretched over the area
// of the window. You will need to create a quad rendered in the space of the
// window, aka you in 2D not 3D.
...
// now render your image using you ray-tracer
for (j = 0; j < h; ++h) {
    for (i =0; i < w; ++i) {
        buffer[j * w + i] =  trace(orig, dir, ...);
    }
    // it is not very efficient to update your window each time you have 
    // a new pixel so do this every row or every 10 rows ... you get the idea
    // copy content of your buffer into the GL texture
    // I am not using the right calls here, too lazy to find the right ones
    glCopyTexture(myGLTextureId, buffer, (sizeof(char) * 3 * w * h); 
    // do the texture binding, etc... all GL or DX stuff
    ...
    // this will update your window with the content of the buffer
    glSwapBuffer(); 
}
// now save content of buffer to image file
saveToDisk(buffer);

Windows? Arg(. You will have to use DirectX calls indeed but they 2 APIs are very close.

EDIT

I am editing the answer to respond to your comment. The algorithm would look like this:

Color *buffer = Color  char [w * h];
// create a texture that you can map onto the quad stretched over the area
// of the window. You will need to create a quad rendered in the space of the
// window, aka you in 2D not 3D.
...
// now render your image using you ray-tracer
for (j = 0; j < h; ++h) {
    for (i =0; i < w; ++i) {
        buffer[j * w + i] =  trace(orig, dir, ...);
    }
    // it is not very efficient to update your window each time you have 
    // a new pixel so do this every row or every 10 rows ... you get the idea
    // copy content of your buffer into the GL texture
    // I am not using the right calls here, too lazy to find the right ones
    glCopyTexture(myGLTextureId, buffer, (sizeof(char) * 3 * w * h); 
    // do the texture binding, etc... all GL or DX stuff
    ...
    // this will update your window with the content of the buffer
    glSwapBuffer(); 
}
// now save content of buffer to image file
saveToDisk(buffer);

Windows? Arg(. You will have to use DirectX calls indeed but they 2 APIs are very close.

Source Link
user18490
  • 656
  • 4
  • 8

This is a very common question that most people in your situation (interested or fascinated shall I say in Computer Graphics) have and a problem they want to figure out. The solution is pretty simple, though not necessarily that straightforward to implement.

I can't make a specific answer because it depends on the OS you are using, and as I said this is not something you can do in 2 lines of code.

The answer is to use some sort of library that:

  1. allows you to create a window on your system
  2. allows you to display some content (an image) in the window you created and displayed to the screen

The first task is generally quite complex regardless of the OS you use (MacOS, Windows, Linux). For the second step, you need to generally go through some sort of graphics API (like OpenGL).

So there is hope for you. One thing you can do is use some other library that works as a wrapper around 1) and 2) In other words they help you especially with the creation of the window, and then if you know OpenGL you can do the rest of the work. You will need to create some texture that you will fill with the content of your image as it gets rendered and display this texture to the screen (that's pretty basic in OpenGL though you need to know this API in order to do so).

GLFW is a great library that will allow you to do that. It's simple to use and compile and install, etc. You can alternatively use Qt but I don't recommend it unless you want to become a professional programmer.

I had some documentation about the whole process but it's offline right now. I might put it back in a few weeks but you will need to wait I am afraid.