Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A very simple question is that I need a library for writing raw pixels similar to ByteBuffers in Java (if you are familiar with that), using
a sort of image object, that can be used with the method of pixel drawing (with some raytracing stuff) that I will demonstrate.

The way I raytrace is by writing the rayOrgin, discriminant, and returning the color in a function named PerPixel of type int with a 2-float Vector named "coord" and then suppling the data to the image data via a nested for loop.

This is what it should look like:
C++
for (int y = 0; y < IMAGE_WIDTH; y++)
{
  for (int y = 0; y < IMAGE_HEIGHT; y++)
  {
    Vector2f coord = { x / (float) IMAGE_WIDTH, y / (float) IMAGE_HEIGHT };
    imageData[x + y * IMAGE_WIDTH] = PerPixel(coord);
  }
}

int PerPixel(Vector2f coord)
{
  // RAYTRACING STUFF
  // ORDER CAN BE RGBA, ARGB, RGB and etc. just needs to have Red, Green and  Blue channels.
  if (discriminant >= 0)
     return 0xffff00ff;
  return 0xff000000;
}


What I have tried:

I have tried SDL however when I did the image creation correctly, it just failed when I gone to the raytracing part. I've tried OpenGL
instead but this time it gave me an exception when tried to create an image using VAOs, VBOs, TBOs and etc.

Except for image creation, I don't need any big tutorial, just a code snippet of something similar to the code snippet that I showed earlier working with a library (and also obviously give me the name of the library).
Posted
Comments
0x01AA 24-Dec-23 11:37am    
'raw pixels similar to ByteBuffers in Java' is very general. A ByteBuffer can contain any information, not just bitmaps.

If you like to represent a bitmap in a 'ByteBuffer' - which you can ultimately simply display as a bitmap - you have to put a little more thought into it. In particular about things like bit/pixel, alpha chanel y/n etc. Also, for example, under Windows the native format is BGR...

The pragmatic approach will be: ABGR
An integer (4 bytes) containing the informat about
A: Alpha Chanel (transparency)
B: B RGB Chanel
G: G RGB Chanel
R: R RGB Chanel
P. Mehroof Abid 5-Jan-24 5:02am    
I might be really wrong but I think you don't understand the question my question was that I needed a library for drawing pixels on the screen similar to how ByteBuffers do it (and sorry that I didn't include BufferedImages.) However thanks for telling it's BGR for windows.

Take a look at some of the links at set pixel gdi - Google Search[^] and set pixel directx - Google Search[^].
 
Share this answer
 
What OS are you using? Does your program utilize any particular code framework? These questions are important to determine what libraries are available to you.

If your program is for windows and you use MFC then you can use the excellent CxImage[^] class to create a buffered bitmap, write to individual pixels, and then transfers the entire image to the frame buffer. I do exactly this in my applications and it is quite simple. It is also very easy to access an image buffer and write it out to a file.
 
Share this answer
 
I recommend that you read and try the outstanding CXImage article which covers a great amount of image manipulation.
Best is to manipulate the data in memory first and than do the output.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900