Author Topic: Using a Sprite as a Pixel Buffer  (Read 1781 times)

Offline ProN3rd

  • Mc. Print
  • *
  • Posts: 38
    • View Profile
Using a Sprite as a Pixel Buffer
« on: 2015-Aug-24 »
EDIT: solved it, i figured some pixel values were not set as INTEGERs, what caused the errors in the assignment to the array

I have been working on a portal based rendering graphics engine, like DukeNukem, Shadow Warrior and the like, where the screen gets renderened in vertical lines. For each x coordinate there are at least three y- lines (floor,main,ceiling), and often more when other sectors are seen through respective portals.

Everything works fine and smooth, until i started using the setpixel instruction instead of drawline. I figured out from the forum that setpixel does not use opengl and is quite slow therefore.

However to apply textures and lightning, i need individual pixel access, so i had the idea to write the pixels to a pixel buffer instead of drawing immediate points. I think this is also the way its done with SDL c++.

Anyway the following code works horrible. It works somehow and has quite good speed compared to setpixel but I can barley recognize the scene: between 50k and 70k pixels are lost out of 307.000 (640 x 480) and dont get rendered at all, it seems like.

How can i deal with this?

Code: (glbasic) [Select]
GLOBAL W = 640, H = 480
LOADSPRITE "texture.png", 0  //640x480 dummy
GLOBAL pix%[];SPRITE2MEM(pix%[], 0);
MEM2SPRITE(pix%[], 1, W, H)  //work with this copy

  CalculatePixels()  // All pixels get assigned in here
« Last Edit: 2015-Aug-24 by ProN3rd »

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1618
  • Particle Hawk
    • View Profile
Re: Using a Sprite as a Pixel Buffer
« Reply #1 on: 2015-Aug-25 »
Well, you are reading a very large array (307200 indices).
Also generating a large image, AND rendering it, in *every frame*, 640x480 is a bit excesive.

That is mistake #1 and #2, and #2.5

Mistake #3....depending on getpixel for the help file for more info on this, so you can better plan your next tests.

Also consider make your images a power of 2 in size eg 8x8 16x16 32x32 64x64 128x128, opengl seems to work more effectively with the right dimensions.

oh ya almost forgot....use polyvector command to get more speed with rendering 2d in glbasic.
you can even use it to make a pixel, or a line, plus you can fade colors across them, because they have color nodes for each corner vertex. i recently shared a bit of code that creates texturable stars, using polyvectors.
That project is in the 2d codesnippets section of the forums,

ps. love your ideas, i tried that same exact thing many years the end i built a program that generates 3d objects for glbasic, from an image as a template.
Its tricky though, and the current state of that code is a mess, and out of date....theoretically it still works.
its called image2object in the download section of
« Last Edit: 2015-Aug-26 by Hemlos »
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4349
    • View Profile
    • FUED
Re: Using a Sprite as a Pixel Buffer
« Reply #2 on: 2015-Aug-26 »
Yep, I love this idea too, post a video of it running whenever you can, I (+ others I bet) would like to see it in action ;)