I have gone through this as a example on how to render a normal texture to a canvas using WebGL and successfully implemented it.
Now I am trying to implement a scrolling image where I am getting line by line pixel data from another machine in the same network via Ajax Polling or Websockets and have to render the same using WebGL to canvas.
So, now I know we can easily render 60 frames per second which means if I get 50 lines of pixel data per second from another computer in the network ,I should be easily able to render them without any jerks.
I am able to do pixel by pixel transfer using CPU(javascript),where I render 1st line of data ,and on receiving second line of data ,I move 1st line of pixel data to 2nd and render the new line to the first line . This works but I am unable to see a smooth scrolling , I have also tried to use gl.texSubImage2D by keeping all line data in a single Array and looping through it on getting a new line ,but it also doesn't work as expected.
Basically ,what I am looking for is I will get 1 line of pixel data from another pc in the network and then I will render that as 1st line of the texture ,then when I receive the second line , then the GPU should move the 1st line of pixel data to second line and after I render the 1st new line of pixel data received via network ,I will render the first line by calling gl.texSubImage2D and call gl.drawArrays to make sure we are rendering without any jerks. This should happen until the end of texture, so GPU takes care of moving the pixels and CPU takes care of sending new line of pixel data to GPU.
So, in this way GPU will take care of moving the pixels to accomplish a scrolling image ,instead of CPU doing it and hanging the browser.
I have also gone through what is the most efficient way of moving multiple objects (stored in VBO) in space? should I use glTranslatef or a shader? and https://www.opengl.org/discussion_boards/showthread.php/136155-Scrolling-textures
But still a little confused on how to implement it exactly. Any suggestions?
You have asked this same question 4 times already. Here, here, here, and here.
An working answer was already given here.
It basically showed that while you can render at 60fps you are unlikely to be able to receive data at 60fps. It also showed there's NO REASON TO MOVE THE DATA. Why create more work for yourself?
You apparently want an infinitely scrolling display. First off, as I already pointed out, there's no guarantee your data will arrive fast enough. The user might be on a slow connection or might be using the connection for something else (netflix, youtube, torrents).
So you can't assume you'll have enough data to constantly scroll.
Here's the same answer I gave you before slightly modified for infinite data
Notes: It assumes 1 column is added to the texture per frame. All it does is keep writing a column into the texture. So imagine there are only 8 columns in the texture. The first frame the texture looks like this with only 1 column of data
It then draws this using 2 draw calls for the same texture drawing just the first column on the right and the rest of the texture on the left.
Next frame the texture looks like this with 2 columns of data
And it draws these 2 parts
When the the 9th column of data comes in (our texture is only 8 columns large) we replace the 1st column so the texture looks like this
We then draw this with 2 draw calls
Repeat this forever and it will look like an infinite scrolling texture.
The code pretends it's receiving an infinite set of data by calling
getNextLineData
.getNextLineData
just generates a new column of data. That function would need to be replaced by something that receives data from the network.If you really want this to work though you're going to have to deal with network issues. You're going to have to decide what to do when the network is slow. Do you stop scrolling? Do you keep scrolling with blank data? Repeating the same data? What?
Similarly the network could be fast. If you start receiving data too fast what do you do? Draw extra columns? Scroll faster? I'd guess you probably want to cache the data but only use 1 column of it per frame so you'd need a list of columns.
getNextLineData
would pull off the oldest column and your network code would add columns of data.You mentioned ECG though. If you need the data to be live then you wouldn't want to cache the data, you'd want to show as much of it as you received each frame. The simplest way would be to call
getNextLineData
until you run out of data.Also note nothing about this example requires WebGL. You could just as easily do it with canvas 2d. Create one canvas to be the "texture". Update a column per frame using
ctx.putImageData
then callctx2.drawImage
into a separate canvas calls similar to the 2drawImage
calls in this WebGL example