We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › OpenGL & Video Capture (The fast solution)
Page Index Toggle Pages: 1
OpenGL & Video Capture (The fast solution) (Read 710 times)
OpenGL & Video Capture (The fast solution)
May 17th, 2008, 3:54pm
 
I was always searching for this combination: Having a video capture in the background and the OpenGL Renderer above it with more than 9fps. Here it is, have fun:

Quote:

import javax.media.opengl.*;
import processing.opengl.*;
import java.nio.*;
import com.sun.opengl.util.*;
import processing.video.*;

Capture cam;
boolean BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
IntBuffer buf;

void setup() {
 size(640, 480, OPENGL);
 cam = new Capture(this, 640, 480);
 buf = BufferUtil.newIntBuffer(cam.width*cam.height);
}

void draw() {
 background(0);
 println(frameRate);
 PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
 GL gl = pgl.beginGL();
 gl.glRasterPos2f(0.01, height-0.01);
 //gl.glPixelZoom(2, 2);
 gl.glDrawPixels(cam.width, cam.height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, buf);
 gl.glClear(GL.GL_DEPTH_BUFFER_BIT );
 pgl.endGL();
 
 if (cam.available() == true) {
   cam.read();
   javaToNativeRGB(cam);
   buf.put(cam.pixels);
   buf.rewind();
 }
 fill(255,50);
 translate(frameCount%width, height/2);
 rotateY(frameCount/30.0);
 rotateX(frameCount/70.0);
 box(100);
}

void javaToNativeRGB(PImage image) {
   int width = image.width;
   int height = image.height;
   int pixels[] = image.pixels;

   int index = 0;
   int yindex = (height - 1) * width;
   for (int y = 0; y < height/2; y++) {
     if (BIG_ENDIAN) {
       // and convert ARGB back to opengl RGBA components (big endian)
       for (int x = 0; x < image.width; x++) {
         int temp = pixels[index];
         pixels[index] = ((pixels[yindex] << 8) & 0xffffff00) | 0xff;
         pixels[yindex] = ((temp << 8) & 0xffffff00) | 0xff;

         index++;
         yindex++;
       }

     } else {
       // convert ARGB back to native little endian ABGR
       for (int x = 0; x < width; x++) {
         int temp = pixels[index];

         pixels[index] = 0xff000000 |
           ((pixels[yindex] << 16) & 0xff0000) |
           (pixels[yindex] & 0xff00) |
           ((pixels[yindex] >> 16) & 0xff);

         pixels[yindex] = 0xff000000 |
           ((temp << 16) & 0xff0000) |
           (temp & 0xff00) |
           ((temp >> 16) & 0xff);

         index++;
         yindex++;
       }
     }
     yindex -= width*2;
   }
 }


greetings ascorbin
Page Index Toggle Pages: 1