yuv420p scale issue with libyuv, getting wrong image out - yuv

I have a raw yuv420p file at 1860x1920, fill it to cv::Mat, then use libyuv::I420Scale
shrunk to a quarter of its original sizeļ¼Œbut get wired outout, for example there are some green stripes like this:
Use ffplay -f rawvideo -i yuv_image.yuv -video_size 1860x1920, the origin image here:
The scaled image is not normal, it looks like the yuv data is not aligned
What am I doing wrong, code below:
#define ALIGN32(n) (((n) >> 5) << 5)
// yuv420_image is yuv420 cv::Mat
int src_width = yuv420_image.cols, src_height = yuv420_image.rows / 3 * 2;
const uint8_t* src = yuv420_image.data;
const uint8_t* src_y = src;
const uint8_t* src_u = src_y + src_width * src_width;
const uint8_t* src_v = src_u + src_width * src_width / 4;
int dst_width = ALIGN32(src_width / kScaleRatio), dst_height = ALIGN32(src_height / kScaleRatio);
cv::Mat yuv420_scaled(dst_height * 3 / 2, dst_width,
CV_8UC1);
uint8_t* dst = yuv420_scaled.data;
uint8_t* dst_y = dst;
uint8_t* dst_u = dst_y + dst_width * dst_height;
uint8_t* dst_v = dst_u + dst_width * dst_height / 4;
int result = libyuv::I420Scale(src_y, src_width,
src_u, src_width / 2,
src_v, src_width / 2,
src_width, src_width,
dst_y, dst_width,
dst_u, dst_width / 2,
dst_v, dst_width / 2,
dst_width, dst_height,
libyuv::FilterModeEnum::kFilterNone);

The issue is mixing width and height in 3 places.
Issues:
Mixing src_width and src_height:
const uint8_t* src_u = src_y + src_width * src_width;
const uint8_t* src_v = src_u + src_width * src_width / 4;
Supposed to be:
const uint8_t* src_u = src_y + src_width * src_height;
const uint8_t* src_v = src_u + src_width * src_height / 4;
Mixing src_width and src_height:
int result = libyuv::I420Scale(src_y, src_width,
src_u, src_width / 2,
src_v, src_width / 2,
src_width, src_width,
Supposed to be:
int result = libyuv::I420Scale(src_y, src_width,
src_u, src_width / 2,
src_v, src_width / 2,
src_width, src_height,
For testing I created a synthetic video frame in YUV420p (I420) pixel format using FFmpeg:
ffmpeg -f lavfi -i testsrc=1860x1920:rate=1:duration=1 -vf scale=out_color_matrix=bt709:out_range=full -pix_fmt yuv420p yuv_image.yuv
The following code sample downscales the image to 448x480.
The code uses OpenCV for converting the result to BGR (for testing).
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "opencv2/opencv.hpp"
#include "libyuv.h"
#define ALIGN32(n) (((n) >> 5) << 5)
//Building a sample input image:
//ffmpeg -f lavfi -i testsrc=1860x1920:rate=1:duration=1 -vf scale=out_color_matrix=bt709:out_range=full -pix_fmt yuv420p yuv_image.yuv
int main()
{
const int kScaleRatio = 4; //Set to 4 for example
const int width = 1860;
const int height = 1920;
const int stride = width; //Assume rows are continuous
uint8_t *frameData = new uint8_t[stride*height*3/2]; //Buffer for storing raw I420 input image.
//Read image from file.
FILE* f = fopen("yuv_image.yuv", "rb");
fread(frameData, 1, stride*height*3/2, f); //Assume rows are continuous (assume stride = width)
fclose(f);
//Make OpenCV Mat wrapper
cv::Mat yuv420_image(height*3/2, width, CV_8UC1, (void*)frameData, stride);
// yuv420_image is yuv420 cv::Mat
int src_width = yuv420_image.cols, src_height = yuv420_image.rows / 3 * 2;
const uint8_t* src = yuv420_image.data;
const uint8_t* src_y = src;
const uint8_t* src_u = src_y + src_width * src_height; //const uint8_t* src_u = src_y + src_width * src_width; <-- Supposed to be src_width * src_height
const uint8_t* src_v = src_u + src_width * src_height / 4; //const uint8_t* src_v = src_u + src_width * src_width / 4;
int dst_width = ALIGN32(src_width / kScaleRatio), dst_height = ALIGN32(src_height / kScaleRatio);
cv::Mat yuv420_scaled(dst_height * 3 / 2, dst_width, CV_8UC1);
uint8_t* dst = yuv420_scaled.data;
uint8_t* dst_y = dst;
uint8_t* dst_u = dst_y + dst_width * dst_height;
uint8_t* dst_v = dst_u + dst_width * dst_height / 4;
//int result = libyuv::I420Scale(src_y, src_width,
// src_u, src_width / 2,
// src_v, src_width / 2,
// src_width, src_width, //<-- Supposed to be src_width, src_height
// dst_y, dst_width,
// dst_u, dst_width / 2,
// dst_v, dst_width / 2,
// dst_width, dst_height,
// libyuv::FilterModeEnum::kFilterNone);
int result = I420Scale(src_y, //const uint8_t * src_y,
src_width, //int src_stride_y,
src_u, //const uint8_t * src_u,
src_width / 2, //int src_stride_u,
src_v, //const uint8_t * src_v,
src_width / 2, //int src_stride_v,
src_width, //int src_width,
src_height, //int src_height,
dst_y, //uint8_t * dst_y,
dst_width, //int dst_stride_y,
dst_u, //uint8_t * dst_u,
dst_width / 2, //int dst_stride_u,
dst_v, //uint8_t * dst_v,
dst_width / 2, //int dst_stride_v,
dst_width, //int dst_width,
dst_height, //int dst_height,
libyuv::FilterModeEnum::kFilterNone);//enum FilterMode filtering);
if (result != 0)
{
return result;
}
cv::Mat bgr_scaled;
//Convert YUV420p to BGR using OpenCV (note: the conversion may not result accurate colors).
//It looks like the is a bug in OpenCV (use COLOR_YUV420p2RGB instead of COLOR_YUV420p2BGR).
cv::cvtColor(yuv420_scaled, bgr_scaled, cv::COLOR_YUV420p2RGB);
//Show bgr_scaled for testing.
cv::imshow("bgr_scaled", bgr_scaled);
cv::waitKey();
cv::destroyAllWindows();
delete[] frameData;
cv::imwrite("bgr_scaled.png", bgr_scaled);
return 0;
}
Output (after converting to BGR):

Related

How to convert camera image to image?

I want to convert camera image from function startImageStream of camera plugin in Flutter to Image to crop that image but I only find the way to convert to FirebaseVisionImage.
Edit For Color Image
if I understand you clear. You are trying to covnert YUV420 format. The following is code snippet from: https://github.com/flutter/flutter/issues/26348
const shift = (0xFF << 24);
Future<Image> convertYUV420toImageColor(CameraImage image) async {
try {
final int width = image.width;
final int height = image.height;
final int uvRowStride = image.planes[1].bytesPerRow;
final int uvPixelStride = image.planes[1].bytesPerPixel;
print("uvRowStride: " + uvRowStride.toString());
print("uvPixelStride: " + uvPixelStride.toString());
// imgLib -> Image package from https://pub.dartlang.org/packages/image
var img = imglib.Image(width, height); // Create Image buffer
// Fill image buffer with plane[0] from YUV420_888
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
final int uvIndex = uvPixelStride * (x / 2).floor() + uvRowStride * (y / 2).floor();
final int index = y * width + x;
final yp = image.planes[0].bytes[index];
final up = image.planes[1].bytes[uvIndex];
final vp = image.planes[2].bytes[uvIndex];
// Calculate pixel color
int r = (yp + vp * 1436 / 1024 - 179).round().clamp(0, 255);
int g = (yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91).round().clamp(0, 255);
int b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255);
// color: 0x FF FF FF FF
// A B G R
img.data[index] = shift | (b << 16) | (g << 8) | r;
}
}
imglib.PngEncoder pngEncoder = new imglib.PngEncoder(level: 0, filter: 0);
List<int> png = pngEncoder.encodeImage(img);
muteYUVProcessing = false;
return Image.memory(png);
} catch (e) {
print(">>>>>>>>>>>> ERROR:" + e.toString());
}
return null;
}
I found, sometimes planes[0] bytes per row is not same as width. In that case, you should do something like the code below.
static image_lib.Image convertYUV420ToImage(CameraImage cameraImage) {
final width = cameraImage.width;
final height = cameraImage.height;
final yRowStride = cameraImage.planes[0].bytesPerRow;
final uvRowStride = cameraImage.planes[1].bytesPerRow;
final uvPixelStride = cameraImage.planes[1].bytesPerPixel!;
final image = image_lib.Image(width, height);
for (var w = 0; w < width; w++) {
for (var h = 0; h < height; h++) {
final uvIndex =
uvPixelStride * (w / 2).floor() + uvRowStride * (h / 2).floor();
final index = h * width + w;
final yIndex = h * yRowStride + w;
final y = cameraImage.planes[0].bytes[yIndex];
final u = cameraImage.planes[1].bytes[uvIndex];
final v = cameraImage.planes[2].bytes[uvIndex];
image.data[index] = yuv2rgb(y, u, v);
}
}
return image;
}
static int yuv2rgb(int y, int u, int v) {
// Convert yuv pixel to rgb
var r = (y + v * 1436 / 1024 - 179).round();
var g = (y - u * 46549 / 131072 + 44 - v * 93604 / 131072 + 91).round();
var b = (y + u * 1814 / 1024 - 227).round();
// Clipping RGB values to be inside boundaries [ 0 , 255 ]
r = r.clamp(0, 255);
g = g.clamp(0, 255);
b = b.clamp(0, 255);
return 0xff000000 |
((b << 16) & 0xff0000) |
((g << 8) & 0xff00) |
(r & 0xff);
}

How to get Sprite pixel alpha information in cocos2d js/c++

I am working on a scratch and win game, I used clipper node for this.
But I want to know the event when whole sprite is clippe?
Is there any other way to know it, plz help me
I solved this issue by using following method:-
I create one rendure texture and add an sprite on it.
I found answer here:- http://discuss.cocos2d-x.org/t/render-texture-get-percentage-of-transparent/21123
here is the code:-
var WINDOW_WIDTH = cc.director.getWinSize().width;
var WINDOW_HEIGHT = cc.director.getWinSize().height;
rt = new cc.RenderTexture(WINDOW_WIDTH, WINDOW_HEIGHT,
sprite.getTexture().getPixelFormat());
rt.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
this.addChild(rt, 5);
getPercentageTransparent: function () {
//
var s = rt.getSprite().getContentSize();
var tx = s.width;
var ty = s.height;
var bitsPerPixel = 4 * 8;
var bytesPerPixel = bitsPerPixel / 8;
var bytesPerRow = bytesPerPixel * tx;
var myDataLength = bytesPerRow * ty;
var numberOfPixels = tx * ty;
var numberOfTransparent = 0;
var rawImagePixels = new Uint8Array(myDataLength);
rt.begin();
gl.readPixels(0, 0, tx, ty, gl.RGBA, gl.UNSIGNED_BYTE, rawImagePixels);
rt.end();
var x, y;
for (y = 0; y < ty; y++) {
// just want the last byte (alpha) for each pixel
for (x = 0; x < tx; x++) {
var alpha = rawImagePixels[(y * 4 * tx + ((x * 4) + 3))];
if (alpha < 1) {
numberOfTransparent++;
}
}
}
cc.log("Number of pixels" + numberOfPixels);
cc.log("Number of Trasparent" + numberOfTransparent);
cc.log("percentage " + (numberOfTransparent / numberOfPixels) * 100);
return (numberOfTransparent / numberOfPixels) * 100;
},

Making a screenshot using Xlib and Cairo libs [fail]

I'm trying to make a screenshot using Xlib and Cairo, however I'm not sure to do it the good way, "stride" is really confusing me.
Here's the code :
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <cairo.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(int argc, char** argv) {
int x, y;
Display *disp;
Window root;
XWindowAttributes watts;
XImage *image;
cairo_surface_t *surface;
unsigned int width;
unsigned int height;
int stride;
disp = XOpenDisplay(NULL);
root = DefaultRootWindow(disp);
XGetWindowAttributes(disp, root, &watts);
width = watts.width;
height = watts.height;
image = XGetImage(disp, root, watts.x, watts.y, width, height, AllPlanes, ZPixmap);
stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, width);
unsigned char *data = malloc(width * height * 3);
for (y = 0; y < height; ++y)
for (x = 0; x < width; ++x) {
unsigned long pixel = XGetPixel(image, x, y);
unsigned char red = (image->red_mask & pixel);
unsigned char green = (image->green_mask & pixel) >> 8;
unsigned char blue = (image->blue_mask & pixel) >> 16;
data[(y * width + x) * 3] = red;
data[(y * width + x) * 3 + 1] = green;
data[(y * width + x) * 3 + 2] = blue;
}
surface = cairo_image_surface_create_for_data(
data,
CAIRO_FORMAT_RGB24,
width, height,
stride);
cairo_surface_write_to_png(
surface,
"test.png");
cairo_surface_destroy(surface);
free(data);
return (EXIT_SUCCESS);
}
When I compile and run the program, everything seems to work just fine. However here's the resulting image :
quite a mess right ?..
What am I possibly doing wrong ?
Instead of doing all this complicated magic, let cairo do it for you:
#include <cairo.h>
#include <cairo-xlib.h>
#include <X11/Xlib.h>
int main(int argc, char** argv) {
Display *disp;
Window root;
cairo_surface_t *surface;
int scr;
disp = XOpenDisplay(NULL);
scr = DefaultScreen(disp);
root = DefaultRootWindow(disp);
surface = cairo_xlib_surface_create(disp, root, DefaultVisual(disp, scr),
DisplayWidth(disp, scr), DisplayHeight(disp, scr));
cairo_surface_write_to_png(
surface,
"test.png");
cairo_surface_destroy(surface);
return 0;
}
TFM:
CAIRO_FORMAT_RGB24
each pixel is a 32-bit quantity, with the upper 8 bits unused
TFM:
stride = cairo_format_stride_for_width (format, width);
data = malloc (stride * height);
Hence, the correct index calculation is
data[y * stride + x * 4 + 0] = blue;
data[y * stride + x * 4 + 1] = green;
data[y * stride + x * 4 + 2] = red; /* yes, in this order */
Also, masks are taken from the image and shifts are hard-coded, which makes absolutely no sense. Calculate the shifts from the masks.

Unable to Draw Triangles Through glDrawElements in OpenGL ES 2.0

I'm trying to draw a procedural sphere referenced here.
I modified it a bit so that I can use the glDrawElements method of OpenGL ES 2.0
Here's my version of createSphere:
GLfloat sphereVerticies[10000]={0.0};
GLubyte triangleIndices[15000]={0};
int createSphere (GLfloat spherePoints[], GLubyte triangleIndices[], GLfloat fRadius, GLfloat step)
{
int points = 0;
GLfloat uStep = DEGREES_TO_RADIANS (step);
GLfloat vStep = uStep;
unsigned long index=0;
for (GLfloat u = 0.0f; u <= (2 * M_PI); u += uStep)
{
for (GLfloat v = -M_PI_2; v <= M_PI_2; v += vStep)
{
triangleIndices[index++]=points;
triangleIndices[index++]=points+1;
triangleIndices[index++]=points+2;
triangleIndices[index++]=points+2;
triangleIndices[index++]=points+3;
triangleIndices[index++]=points;
points++;
spherePoints[(points - 1) * 3] = fRadius * cosf(v) * cosf(u); // x
spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v) * sinf(u); // y
spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v); // z
points++;
spherePoints[(points - 1) * 3] = fRadius * cosf(v) * cosf(u + uStep); // x
spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v) * sinf(u + uStep); // y
spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v); // z
points++;
spherePoints[(points - 1) * 3] = fRadius * cosf(v + vStep) * cosf(u); // x
spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v + vStep) * sinf(u); // y
spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v + vStep); // z
points++;
spherePoints[(points - 1) * 3] = fRadius * cosf(v + vStep) * cosf(u + uStep); // x
spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v + vStep) * sinf(u + uStep); // y
spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v + vStep); // z
}
}
return points;
}
In SetupGL I have:
..
glEnable(GL_CULL_FACE);
..
..
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
numPoints=createSphere(sphereVerticies, triangleIndices, 30.0f, 20.0f);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(sphereVerticies), sphereVerticies, GL_STATIC_DRAW);
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangleIndices), triangleIndices, GL_STATIC_DRAW);
// New lines (were previously in draw)
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) sphereVerticies);
glBindVertexArrayOES(0);
and finally in drawInRect:
glBindVertexArrayOES(_vertexArray);
glDrawElements(GL_TRIANGLES, (int)(numPoints*1.5), GL_UNSIGNED_BYTE, 0);
Now what am I doing wrong here? I don't see any visual output. Any help will be greatly appreciated. Thanks.
I can see two things:
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) sphereVerticies);
the last argument there should be 0, since the vertices are already buffered and bound in the VBO.
OpenGL ES - glVertexAttribPointer documentation
If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target (see glBindBuffer) while a generic vertex attribute array is specified, pointer is treated as a byte offset into the buffer object's data store...
And,
glDrawElements(GL_TRIANGLES, (int)(numPoints*1.5), GL_UNSIGNED_BYTE, 0);
you should use GL_UNSIGNED_SHORT because you have more than 255 vertices.
As for the indices, this is how you index them:
(0,1,2) (2,3,0)
2 3 2 2 3
o-------o o o-------o
| | | \ | /
| | => | \ | /
| | | \ | /
o-------o o-------o o
0 1 0 1 0
So, from this chart we can see two problems:
1) The triangles do not close up the polygon
2) Notice the winding, the first triangle is CCW, the second CW.
Try this code:
triangleIndices[index++]=points;
triangleIndices[index++]=points+1;
triangleIndices[index++]=points+2;
triangleIndices[index++]=points+3;
triangleIndices[index++]=points+2;
triangleIndices[index++]=points+1;

OpenGL ES (iPhone) Touch Picking

Looking to do classic OpenGL mouse picking in ES. I'd prefer not to use third party libs, GLU ports and OpenGL name stacks, etc, are out. This pretty much leaves inverse view transformation and ray intersection, correct?
I've gotten pretty far with the help of:
http://trac.bookofhook.com/bookofhook/trac.cgi/wiki/MousePicking
http://eigenclass.blogspot.com/2008/10/opengl-es-picking-using-ray-boundingbox.html
. . .but I'm not there yet. This also reeks of THERE MUST BE AN EASIER WAY!!
Here is some code:
-(void)handleTouch:(CGPoint)point {
GLfloat width = backingWidth;
GLfloat height = backingHeight;
GLfloat x = point.x;
GLfloat y = point.y;
GLfloat z = 0.0f;
//viewport -> normalized dev coord -> clip
GLfloat n[] = {
2 * x / width - 1,
2 * y / height,
2 * z - 1,
1
};
float fov = 45.0f * (M_PI / 180.0f);
float near = 0.01, far = 10.0f;
float aspect = (float)backingWidth / (float)backingHeight;
float top = tan(fov) * near;
//float bottom = -top;
//float left = aspect * bottom;
float right = aspect * top;
//I'm a viewing volume symmetric projection matrix
GLfloat P[] = {
near / right, 0, 0, 0,
0, near / top, 0, 0,
0, 0, -(far + near) / (far - near), (-2 * far * near) / (far - near),
0, 0, -1, 0
};
GLfloat Pminus1[] = {
1/P[0], 0, 0, 0,
0, 1/P[5], 0, 0,
0, 0, 0, 1/P[14],
0, 0, 1/P[11], -(P[10]/ (P[11]*P[14]))
};
//clip -> view
GLfloat v[] = {
Pminus1[0] * n[0] + Pminus1[1] * n[1] + Pminus1[2] * n[2] + Pminus1[3] * n[3],
Pminus1[4] * n[0] + Pminus1[5] * n[1] + Pminus1[6] * n[2] + Pminus1[7] * n[3],
Pminus1[8] * n[0] + Pminus1[9] * n[1] + Pminus1[10] * n[2] + Pminus1[11] * n[3],
Pminus1[12] * n[0] + Pminus1[13] * n[1] + Pminus1[14] * n[2] + Pminus1[15] * n[3]
};
//view -> world
GLfloat Rt[] = {
mv[0], mv[4], mv[8],
mv[1], mv[5], mv[9],
mv[2], mv[6], mv[10]
};
GLfloat tPrime[] = {
Rt[0] * mv[3] + Rt[1] * mv[7] + Rt[2] * mv[11],
Rt[3] * mv[3] + Rt[4] * mv[7] + Rt[5] * mv[11],
Rt[6] * mv[3] + Rt[7] * mv[7] + Rt[8] * mv[11]
};
GLfloat Mminus1[] = {
Rt[0], Rt[1], Rt[2], -(tPrime[0]),
Rt[3], Rt[4], Rt[5], -(tPrime[1]),
Rt[6], Rt[7], Rt[8], -(tPrime[2]),
0, 0, 0, 1
};
//point in world space
GLfloat w[] = {
Mminus1[0] * v[0] + Mminus1[1] * v[1] + Mminus1[2] * v[2] + Mminus1[3] * v[3],
Mminus1[4] * v[0] + Mminus1[5] * v[1] + Mminus1[6] * v[2] + Mminus1[7] * v[3],
Mminus1[8] * v[0] + Mminus1[9] * v[1] + Mminus1[10] * v[2] + Mminus1[11] * v[3],
Mminus1[12] * v[0] + Mminus1[13] * v[1] + Mminus1[14] * v[2] + Mminus1[15] * v[3]
};
//r = a + t(w - a)
GLfloat a[] = {0.0f, -0.1f, 0.0f};
GLfloat wminusa[] = {w[0] - a[0], w[1] - a[1], w[2] - a[2]};
vector[0] = a[0];
vector[1] = a[1],
vector[2] = a[2];
vector[3] = w[0];
vector[4] = w[1];
vector[5] = -10.0f;
//3 non-colinear points on the plane
GLfloat p1[] = {rect.origin.x, rect.origin.y, 0};
GLfloat p2[] = {rect.origin.x + rect.size.width, rect.origin.y, 0};
GLfloat p3[] = {rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, 0};
//location plane normal vector, Ax + By + Cz + D = 0
GLfloat lp[] = {
p1[1] * (p2[2] - p3[2]) + p2[1] * (p3[2] - p1[2]) + p3[1] * (p1[2] - p2[2]),
p1[2] * (p2[0] - p3[0]) + p2[2] * (p3[0] - p1[0]) + p3[2] * (p1[0] - p2[0]),
p1[0] * (p2[1] - p3[1]) + p2[0] * (p3[1] - p1[1]) + p3[0] * (p1[1] - p2[1]),
-(p1[0] * (((p2[1] * p3[2]) - (p3[1] * p2[2]))) + p2[0] * (((p3[1] * p1[2]) - (p1[1] * p3[2]))) + p3[0] * (((p1[1] * p2[2]) - (p2[1] * p1[2]))))
};
GLfloat PnRd = (lp[0] * wminusa[0]) + (lp[1] * wminusa[1]) + (lp[2] * wminusa[2]);
if(PnRd != 0) {
GLfloat PnR0D = -((lp[0] * a[0]) + (lp[1] * a[1]) + (lp[2] * a[2]) + lp[3]);
if(PnR0D != 0) {
GLfloat t = PnR0D / PnRd;
if(t >= 0) {
GLfloat p[] = {
a[0] + wminusa[0] * t,
a[1] + wminusa[1] * t,
a[2] + wminusa[2] * t
};
if(p[0] > rect.origin.x &&
p[0] < rect.origin.x + rect.size.width &&
p[1] > rect.origin.y &&
p[1] < rect.origin.y + rect.size.height)
NSLog(#"BOOM!!!");
}
}
}
}
This post is very hard to follow. I'm attempting to roll my own on iOS 5 with GLKView; I've worked out how to touch detect pixel RGBA as I describe here, now I'm trying to work out how to quickly change the colours of my scene objects to be unique, to accompany this method.
I managed to fix it:
-(void)view2WorldPoint:(CGPoint)point :(GLfloat*)worldPoint {
// this is the inverse translation of the modelview
GLfloat width = (GLfloat)backingWidth;
GLfloat height = (GLfloat)backingHeight;
float clickX = point.x;
float clickY = point.y;
float clickZ = 0.0f;
NSLog(#"click point : x = %f, y = %f, z = %f", clickX, clickY, clickZ);
// NSLog(#"Me : x = %f, y = %f, z = %f", a[0], a[1], a[2]);
// NSLog(#"Dev : x = %f, y = %f, z = %f", squareX, squareY, squareZ);
//viewport -> normalized device coord -> clip
GLfloat n[] = {
2 * clickX / width - 1,
2 * (480-clickY) / height - 1,
2 * clickZ - 1,
1
};
// NSLog(#"Obj : x = %f, y = %f, z = %f", rect.origin.x, rect.origin.y, -0.5);
// NSLog(#"N : x = %f, y = %f, z = %f", n[0], n[1], n[2]);
//I'm a viewing volume symmetric projection matrix
// GLfloat P[] = {
// near / right, 0, 0, 0,
// 0, near / top, 0, 0,
// 0, 0, -(far + near) / (far - near), (-2 * far * near) / (far - near),
// 0, 0, -1, 0
// };
GLfloat P[16];
glGetFloatv(GL_PROJECTION_MATRIX, P);
// [self dumpMatrix:P :#"P"];
GLfloat Pminus1[] = {
1/P[0], 0, 0, 0,
0, 1/P[5], 0, 0,
0, 0, 0, 1/P[11],
0, 0, 1/P[14], -(P[10]/ (P[11]*P[14]))
};
// [self dumpMatrix:Pminus1 :#"P-1"];
//clip -> view
GLfloat v[] = {
(Pminus1[0] * n[0]) + (Pminus1[1] * n[1]) + (Pminus1[2] * n[2]) + (Pminus1[3] * n[3]),
(Pminus1[4] * n[0]) + (Pminus1[5] * n[1]) + (Pminus1[6] * n[2]) + (Pminus1[7] * n[3]),
(Pminus1[8] * n[0]) + (Pminus1[9] * n[1]) + (Pminus1[10] * n[2]) + (Pminus1[11] * n[3]),
(Pminus1[12] * n[0]) + (Pminus1[13] * n[1]) + (Pminus1[14] * n[2]) + (Pminus1[15] * n[3])
};
// NSLog(#"v = [%f, %f, %f, %f]", v[0], v[1], v[2], v[3]);
// [self dumpMatrix:mv :#"mv"];
//view -> world
GLfloat Rt[] = {
mv[0], mv[4], -mv[8],
mv[1], mv[5], -mv[9],
-mv[2], -mv[6], mv[10]
};
// NSLog(#"Rt0 = [%f, %f, %f]", Rt[0], Rt[1], Rt[2]);
// NSLog(#"Rt1 = [%f, %f, %f]", Rt[3], Rt[4], Rt[5]);
// NSLog(#"Rt2 = [%f, %f, %f]", Rt[6], Rt[7], Rt[8]);
GLfloat tPrime[] = {
Rt[0] * mv[12] + Rt[1] * mv[13] + Rt[2] * mv[14],
Rt[3] * mv[12] + Rt[4] * mv[13] + Rt[5] * mv[14],
Rt[6] * mv[12] + Rt[7] * mv[13] + Rt[8] * mv[14]
};
// NSLog(#"tPrime = [%f, %f, %f]", tPrime[0], tPrime[1], tPrime[2]);
GLfloat Mminus1[] = {
Rt[0], Rt[1], Rt[2], -(tPrime[0]),
Rt[3], Rt[4], Rt[5], -(tPrime[1]),
Rt[6], Rt[7], Rt[8], -(tPrime[2]),
0, 0, 0, 1
};
//point in world space
GLfloat w[] = {
Mminus1[0] * v[0] + Mminus1[1] * v[1] + Mminus1[2] * v[2] + Mminus1[3] * v[3],
Mminus1[4] * v[0] + Mminus1[5] * v[1] + Mminus1[6] * v[2] + Mminus1[7] * v[3],
Mminus1[8] * v[0] + Mminus1[9] * v[1] + Mminus1[10] * v[2] + Mminus1[11] * v[3],
Mminus1[12] * v[0] + Mminus1[13] * v[1] + Mminus1[14] * v[2] + Mminus1[15] * v[3]
};
NSLog(#"W : x = %f, y = %f, z = %f", w[0], w[1], w[2]);
worldPoint[0] = w[0];
worldPoint[1] = w[1];
worldPoint[2] = w[2];
}
Okay, okay that was still a bit buggy. Here is what is MOSTLY working now:
-(void)view2WorldPoint:(CGPoint)point :(GLfloat*)worldPoint {
float clickX = point.x;
float clickY = point.y;
float clickZ = -near;
//viewport -> normalized device coord -> clip
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
GLfloat n[] = {
(clickX - (float)viewport[0]) / (float)viewport[2] * 2.0 - 1.0,
-((clickY - (float)viewport[1]) / (float)viewport[3] * 2.0 - 1.0),
2.0 * clickZ - 1.0,
1.0
};
GLfloat MP[16], MPInv[16];
MatMatMultiply(MP, projMat, modelMat);
GenerateInverseMatrix4f(MPInv, MP); // replace this one with the whole 1/p thang?
GLfloat w[] = {
(MPInv[0] * n[0]) + (MPInv[4] * n[1]) + (MPInv[8] * n[2]) + (MPInv[12] * n[3]),
(MPInv[1] * n[0]) + (MPInv[5] * n[1]) + (MPInv[9] * n[2]) + (MPInv[13] * n[3]),
(MPInv[2] * n[0]) + (MPInv[6] * n[1]) + (MPInv[10] * n[2]) + (MPInv[14] * n[3]),
(MPInv[3] * n[0]) + (MPInv[7] * n[1]) + (MPInv[11] * n[2]) + (MPInv[15] * n[3])
};
worldPoint[0] = w[0] / w[3];
worldPoint[1] = w[1] / w[3];
worldPoint[2] = w[2] / w[3];
}
float Determinant4f(const float m[16])
{
return
m[12]*m[9]*m[6]*m[3]-
m[8]*m[13]*m[6]*m[3]-
m[12]*m[5]*m[10]*m[3]+
m[4]*m[13]*m[10]*m[3]+
m[8]*m[5]*m[14]*m[3]-
m[4]*m[9]*m[14]*m[3]-
m[12]*m[9]*m[2]*m[7]+
m[8]*m[13]*m[2]*m[7]+
m[12]*m[1]*m[10]*m[7]-
m[0]*m[13]*m[10]*m[7]-
m[8]*m[1]*m[14]*m[7]+
m[0]*m[9]*m[14]*m[7]+
m[12]*m[5]*m[2]*m[11]-
m[4]*m[13]*m[2]*m[11]-
m[12]*m[1]*m[6]*m[11]+
m[0]*m[13]*m[6]*m[11]+
m[4]*m[1]*m[14]*m[11]-
m[0]*m[5]*m[14]*m[11]-
m[8]*m[5]*m[2]*m[15]+
m[4]*m[9]*m[2]*m[15]+
m[8]*m[1]*m[6]*m[15]-
m[0]*m[9]*m[6]*m[15]-
m[4]*m[1]*m[10]*m[15]+
m[0]*m[5]*m[10]*m[15];
}
BOOL GenerateInverseMatrix4f(float i[16], const float m[16])
{
float x=Determinant4f(m);
if (x==0) return FALSE;
i[0]= (-m[13]*m[10]*m[7] +m[9]*m[14]*m[7] +m[13]*m[6]*m[11]
-m[5]*m[14]*m[11] -m[9]*m[6]*m[15] +m[5]*m[10]*m[15])/x;
i[4]= ( m[12]*m[10]*m[7] -m[8]*m[14]*m[7] -m[12]*m[6]*m[11]
+m[4]*m[14]*m[11] +m[8]*m[6]*m[15] -m[4]*m[10]*m[15])/x;
i[8]= (-m[12]*m[9]* m[7] +m[8]*m[13]*m[7] +m[12]*m[5]*m[11]
-m[4]*m[13]*m[11] -m[8]*m[5]*m[15] +m[4]*m[9]* m[15])/x;
i[12]=( m[12]*m[9]* m[6] -m[8]*m[13]*m[6] -m[12]*m[5]*m[10]
+m[4]*m[13]*m[10] +m[8]*m[5]*m[14] -m[4]*m[9]* m[14])/x;
i[1]= ( m[13]*m[10]*m[3] -m[9]*m[14]*m[3] -m[13]*m[2]*m[11]
+m[1]*m[14]*m[11] +m[9]*m[2]*m[15] -m[1]*m[10]*m[15])/x;
i[5]= (-m[12]*m[10]*m[3] +m[8]*m[14]*m[3] +m[12]*m[2]*m[11]
-m[0]*m[14]*m[11] -m[8]*m[2]*m[15] +m[0]*m[10]*m[15])/x;
i[9]= ( m[12]*m[9]* m[3] -m[8]*m[13]*m[3] -m[12]*m[1]*m[11]
+m[0]*m[13]*m[11] +m[8]*m[1]*m[15] -m[0]*m[9]* m[15])/x;
i[13]=(-m[12]*m[9]* m[2] +m[8]*m[13]*m[2] +m[12]*m[1]*m[10]
-m[0]*m[13]*m[10] -m[8]*m[1]*m[14] +m[0]*m[9]* m[14])/x;
i[2]= (-m[13]*m[6]* m[3] +m[5]*m[14]*m[3] +m[13]*m[2]*m[7]
-m[1]*m[14]*m[7] -m[5]*m[2]*m[15] +m[1]*m[6]* m[15])/x;
i[6]= ( m[12]*m[6]* m[3] -m[4]*m[14]*m[3] -m[12]*m[2]*m[7]
+m[0]*m[14]*m[7] +m[4]*m[2]*m[15] -m[0]*m[6]* m[15])/x;
i[10]=(-m[12]*m[5]* m[3] +m[4]*m[13]*m[3] +m[12]*m[1]*m[7]
-m[0]*m[13]*m[7] -m[4]*m[1]*m[15] +m[0]*m[5]* m[15])/x;
i[14]=( m[12]*m[5]* m[2] -m[4]*m[13]*m[2] -m[12]*m[1]*m[6]
+m[0]*m[13]*m[6] +m[4]*m[1]*m[14] -m[0]*m[5]* m[14])/x;
i[3]= ( m[9]* m[6]* m[3] -m[5]*m[10]*m[3] -m[9]* m[2]*m[7]
+m[1]*m[10]*m[7] +m[5]*m[2]*m[11] -m[1]*m[6]* m[11])/x;
i[7]= (-m[8]* m[6]* m[3] +m[4]*m[10]*m[3] +m[8]* m[2]*m[7]
-m[0]*m[10]*m[7] -m[4]*m[2]*m[11] +m[0]*m[6]* m[11])/x;
i[11]=( m[8]* m[5]* m[3] -m[4]*m[9]* m[3] -m[8]* m[1]*m[7]
+m[0]*m[9]* m[7] +m[4]*m[1]*m[11] -m[0]*m[5]* m[11])/x;
i[15]=(-m[8]* m[5]* m[2] +m[4]*m[9]* m[2] +m[8]* m[1]*m[6]
-m[0]*m[9]* m[6] -m[4]*m[1]*m[10] +m[0]*m[5]* m[10])/x;
return TRUE;
}
void MatMatMultiply(GLfloat *result, GLfloat *matrix1, GLfloat *matrix2)
{
result[0]=matrix1[0]*matrix2[0]+
matrix1[4]*matrix2[1]+
matrix1[8]*matrix2[2]+
matrix1[12]*matrix2[3];
result[4]=matrix1[0]*matrix2[4]+
matrix1[4]*matrix2[5]+
matrix1[8]*matrix2[6]+
matrix1[12]*matrix2[7];
result[8]=matrix1[0]*matrix2[8]+
matrix1[4]*matrix2[9]+
matrix1[8]*matrix2[10]+
matrix1[12]*matrix2[11];
result[12]=matrix1[0]*matrix2[12]+
matrix1[4]*matrix2[13]+
matrix1[8]*matrix2[14]+
matrix1[12]*matrix2[15];
result[1]=matrix1[1]*matrix2[0]+
matrix1[5]*matrix2[1]+
matrix1[9]*matrix2[2]+
matrix1[13]*matrix2[3];
result[5]=matrix1[1]*matrix2[4]+
matrix1[5]*matrix2[5]+
matrix1[9]*matrix2[6]+
matrix1[13]*matrix2[7];
result[9]=matrix1[1]*matrix2[8]+
matrix1[5]*matrix2[9]+
matrix1[9]*matrix2[10]+
matrix1[13]*matrix2[11];
result[13]=matrix1[1]*matrix2[12]+
matrix1[5]*matrix2[13]+
matrix1[9]*matrix2[14]+
matrix1[13]*matrix2[15];
result[2]=matrix1[2]*matrix2[0]+
matrix1[6]*matrix2[1]+
matrix1[10]*matrix2[2]+
matrix1[14]*matrix2[3];
result[6]=matrix1[2]*matrix2[4]+
matrix1[6]*matrix2[5]+
matrix1[10]*matrix2[6]+
matrix1[14]*matrix2[7];
result[10]=matrix1[2]*matrix2[8]+
matrix1[6]*matrix2[9]+
matrix1[10]*matrix2[10]+
matrix1[14]*matrix2[11];
result[14]=matrix1[2]*matrix2[12]+
matrix1[6]*matrix2[13]+
matrix1[10]*matrix2[14]+
matrix1[14]*matrix2[15];
result[3]=matrix1[3]*matrix2[0]+
matrix1[7]*matrix2[1]+
matrix1[11]*matrix2[2]+
matrix1[15]*matrix2[3];
result[7]=matrix1[3]*matrix2[4]+
matrix1[7]*matrix2[5]+
matrix1[11]*matrix2[6]+
matrix1[15]*matrix2[7];
result[11]=matrix1[3]*matrix2[8]+
matrix1[7]*matrix2[9]+
matrix1[11]*matrix2[10]+
matrix1[15]*matrix2[11];
result[15]=matrix1[3]*matrix2[12]+
matrix1[7]*matrix2[13]+
matrix1[11]*matrix2[14]+
matrix1[15]*matrix2[15];
}