Skip to content

Instantly share code, notes, and snippets.

@deltaluca
Last active December 15, 2015 15:08
Show Gist options
  • Save deltaluca/5278981 to your computer and use it in GitHub Desktop.
Save deltaluca/5278981 to your computer and use it in GitHub Desktop.
Creating window with OpenCV and capturing video feed from file, converted to greyscale. Rendering video feed to a rotating cube of cubes with OpenGL.
package;
import cv.Core;
import cv.HighGUI;
import cv.ImgProc;
import ogl.GL;
class Main {
static var v2 = GL.v2;
static var v3 = GL.v3;
static function main() {
HighGUI.namedWindow("main", HighGUI.CV_WINDOW_AUTOSIZE | HighGUI.CV_WINDOW_OPENGL);
HighGUI.resizeWindow("main", 800, 600);
HighGUI.setOpenGlContext("main");
var vertexArrayID = GL.genVertexArrays(1)[0];
GL.bindVertexArray(vertexArrayID);
var g_vertex_buffer_data = GL.buffer([
[-1, 1, 1],[-1, 1,-1],[-1,-1, 1], [-1, 1,-1],[-1,-1,-1],[-1,-1, 1],
[-1, 1,-1],[ 1, 1,-1],[-1,-1,-1], [ 1, 1,-1],[ 1,-1,-1],[-1,-1,-1],
[ 1, 1, 1],[ 1,-1, 1],[ 1, 1,-1], [ 1, 1,-1],[ 1,-1, 1],[ 1,-1,-1],
[-1,-1, 1],[ 1, 1, 1],[-1, 1, 1], [-1,-1, 1],[ 1,-1, 1],[ 1, 1, 1],
[-1, 1, 1],[ 1, 1,-1],[-1, 1,-1], [-1, 1, 1],[ 1, 1, 1],[ 1, 1,-1],
[-1,-1,-1],[ 1,-1, 1],[-1,-1, 1], [-1,-1,-1],[ 1,-1,-1],[ 1,-1, 1],
], GL.FLOAT);
var vertexBuffer = GL.genBuffers(1)[0];
GL.bindBuffer(GL.ARRAY_BUFFER, vertexBuffer);
GL.bufferData(GL.ARRAY_BUFFER, g_vertex_buffer_data, GL.STATIC_DRAW);
trace(g_vertex_buffer_data);
var t = 1/3; var T = 2/3;
var h = 1/2;
var g_uv_buffer_data = GL.buffer([
[1,0],[T,0],[1,h], [T,0],[T,h],[1,h],
[T,0],[t,0],[T,h], [t,0],[t,h],[T,h],
[0,0],[0,h],[t,0], [t,0],[0,h],[t,h],
[T,1],[t,h],[t,1], [T,1],[T,h],[t,h],
[t,1],[0,h],[0,1], [t,1],[t,h],[0,h],
[1,1],[T,h],[T,1], [1,1],[1,h],[T,h],
], GL.FLOAT);
var uvBuffer = GL.genBuffers(1)[0];
GL.bindBuffer(GL.ARRAY_BUFFER, uvBuffer);
GL.bufferData(GL.ARRAY_BUFFER, g_uv_buffer_data, GL.STATIC_DRAW);
var n0 = [-1,0,0]; var n1 = [0,0,-1]; var n2 = [1, 0,0];
var n3 = [ 0,0,1]; var n4 = [0,1, 0]; var n5 = [0,-1,0];
var g_normal_buffer_data = GL.buffer([
n0,n0,n0, n0,n0,n0,
n1,n1,n1, n1,n1,n1,
n2,n2,n2, n2,n2,n2,
n3,n3,n3, n3,n3,n3,
n4,n4,n4, n4,n4,n4,
n5,n5,n5, n5,n5,n5,
], GL.FLOAT);
var nBuffer = GL.genBuffers(1)[0];
GL.bindBuffer(GL.ARRAY_BUFFER, nBuffer);
GL.bufferData(GL.ARRAY_BUFFER, g_normal_buffer_data, GL.STATIC_DRAW);
var textureID = GL.genTextures(1)[0];
GL.bindTexture(GL.TEXTURE_2D, textureID);
GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR);
GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR);
var vertexShaderID = GL.createShader(GL.VERTEX_SHADER);
var fragmentShaderID = GL.createShader(GL.FRAGMENT_SHADER);
GL.shaderSource(vertexShaderID, "
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 normal_modelspace;
uniform mat4 MVP;
out vec2 UV;
out vec3 N;
void main() {
vec4 v = vec4(vertexPosition_modelspace, 1);
gl_Position = v * MVP;
UV = vertexUV;
N = normalize(normal_modelspace * inverse(transpose(mat3(MVP))));
}
");
GL.shaderSource(fragmentShaderID, "
#version 330 core
in vec2 UV;
in vec3 N;
out vec3 color;
uniform sampler2D tex;
void main() {
color = texture(tex, UV).rgb * dot(N, vec3(0,0,-1));
}
");
GL.compileShader(vertexShaderID);
GL.compileShader(fragmentShaderID);
var programID = GL.createProgram();
GL.attachShader(programID, vertexShaderID);
GL.attachShader(programID, fragmentShaderID);
GL.linkProgram(programID);
GL.deleteShader(vertexShaderID);
GL.deleteShader(fragmentShaderID);
var matrixID = GL.getUniformLocation(programID, "MVP");
GL.enable(GL.DEPTH_TEST);
GL.depthFunc(GL.LESS);
GL.viewport(0, 0, 800, 600);
var capture = HighGUI.captureFromFile("video.avi");
var width = Std.int(HighGUI.getCaptureProperty(capture, HighGUI.CV_CAP_PROP_FRAME_WIDTH));
var height = Std.int(HighGUI.getCaptureProperty(capture, HighGUI.CV_CAP_PROP_FRAME_HEIGHT));
var working = Core.createImage(Core.size(width, height), Core.IPL_DEPTH_8U, 1);
HighGUI.setOpenGlDrawCallback("main", function () {
GL.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
GL.useProgram(programID);
GL.enableVertexAttribArray(0);
GL.bindBuffer(GL.ARRAY_BUFFER, vertexBuffer);
GL.vertexAttribPointer(0,3, GL.FLOAT, false, 0,0);
GL.enableVertexAttribArray(1);
GL.bindBuffer(GL.ARRAY_BUFFER, uvBuffer);
GL.vertexAttribPointer(1,2, GL.FLOAT, false, 0,0);
GL.enableVertexAttribArray(2);
GL.bindBuffer(GL.ARRAY_BUFFER, nBuffer);
GL.vertexAttribPointer(2,3, GL.FLOAT, false, 0,0);
var view = Mat4.lookAt([40.0,100.0,100.0], [0.0,0.0,0.0]);
var proj = Mat4.perspective(45, 4/3, 0.1, 1000);
var t = Sys.cpuTime();
var rot = Mat4.rotateY(t*1)*
Mat4.rotateX(t*-3)*
Mat4.rotateZ(t*-2);
for (x in 0...3) {
for (y in 0...3) {
for (z in 0...3) {
var dx = 50*(x-1);
var dy = 50*(y-1);
var dz = 50*(z-1);
var model = Mat4.rotateY(x*Math.PI)*
Mat4.rotateX(z*Math.PI)*
Mat4.rotateZ(y*Math.PI)*
Mat4.scale(15,15,15);
model = rot * Mat4.translate(dx,dy,dz) * model;
GL.uniformMatrix4fv(matrixID, 1, false, proj * view * model);
GL.drawArrays(GL.TRIANGLES, 0, 6*2*3);
}}}
GL.disableVertexAttribArray(0);
GL.disableVertexAttribArray(1);
GL.disableVertexAttribArray(2);
});
var fst = true;
while (true) {
HighGUI.waitKey(1);
var f = HighGUI.queryFrame(capture);
if (f == null) return;
ImgProc.cvtColor(f, working, ImgProc.CV_RGB2GRAY);
var g_tex_data = GL.rawBuffer(working.raw, width*height, GL.UNSIGNED_BYTE, true);
GL.bindTexture(GL.TEXTURE_2D, textureID);
if (fst) {
GL.texImage2D(GL.TEXTURE_2D, 0, GL.RGB, width, height, 0, GL.LUMINANCE, g_tex_data.type, g_tex_data);
fst = false;
} else
GL.texSubImage2D(GL.TEXTURE_2D, 0, 0, 0, width, height, GL.LUMINANCE, g_tex_data.type, g_tex_data);
HighGUI.updateWindow("main");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment