iOS上的OpenGL ES 2.0对象选取(使用颜色编码) [英] OpenGL ES 2.0 Object Picking on iOS (Using Color Coding)

查看:92
本文介绍了iOS上的OpenGL ES 2.0对象选取(使用颜色编码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个相关问题:

iOS上的OpenGL ES 2.0对象选取

哪个说拾色器"是一个很好的解决方案,并且在了解了它之后的事实:

http://www.lighthouse3d.com/opengl/picking/index .php?color1

这似乎是一个非常简单的解决方案,所以这使我想到了这个问题

在iPhone上进行OpenGL ES配色

不幸的是,我使用的是opengl es 1.0,我试图在2.0中使用它,所以我无权访问 该问题中描述的功能.

但是理论似乎很简单,这就是我应该做的:

在触摸时,我用一种独特的颜色渲染对象.

在触摸结束时,我从该位置获取像素并检查其颜色 得到我的对象. (可能使用glReadPixels)

问题是我不知道如何执行渲染到后台缓冲区并从中读取".

到目前为止,我的代码仅使用绘图",我怀疑我必须做类似glBind 另一个缓冲区的事情,但我希望获得一些帮助.

我的绘图代码是这样的:

glClearColor(0, 0, 0, 0.0); 
glClear(GL_COLOR_BUFFER_BIT);

// Set the Projection Matrix
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(60), 2.0/3.0, 0, 50);


glUseProgram(_programHD);

glBindVertexArrayOES(_vao);

glActiveTexture(GL_TEXTURE1); 
glBindTexture(GL_TEXTURE_2D, _textureBuffer[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glUniform1i(uniforms[UNIFORM_TEXTURE_HD], 1);

// Drawing starts here //

// Pass the Model View Matrix to Open GL
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix,rotationMatrix);

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX_HD], 1, GL_FALSE, _modelViewProjectionMatrix.m);

// Change texture coordinates to draw a different image

glUniform2fv(uniforms[TEXTURE_OFFSET_HD], 1, offSet.v);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);

//glUniform2i(uniforms[TEXTURE_OFFSET], 7, -5);
glUniform2fv(uniforms[TEXTURE_OFFSET_HD], 1, borderHD.v);

glDrawElements(GL_LINE_STRIP, 6, GL_UNSIGNED_SHORT, 0);



glBindVertexArrayOES(0);
glUseProgram(0);

我已经删除了图纸计算以使其更易于理解. 关键是我看不到我指定要绘制到哪里"的任何地方.

感谢您的帮助.

解决方案

我实际上已经完成了使用openGL ES 2.0在我的iPhone游戏中实现选色功能,并且使用了有趣的灯塔教程.

您应该绘制到帧缓冲区.

如果您想从帧缓冲区中读取数据,那是正确的,因为您想使用glReadPixels.更多信息在这里:

http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml

与灯塔教程的唯一不同之处在于,您还想存储Alpha值. 这是获取特定像素颜色的快速功能.随时对其进行改进或更改,但确实可以完成工作.

+ (void) ProcessColourPick : (GLubyte*) out : (Float32) x : (Float32) y
{
    GLint viewport[4];
    //Get size of screen
    glGetIntegerv(GL_VIEWPORT,viewport);

    GLubyte pixel[4];
    //Read pixel from a specific point
    glReadPixels(x,viewport[3] - y,1,1,
             GL_RGBA,GL_UNSIGNED_BYTE,(void *)pixel);

    out[0] = pixel[0];
    out[1] = pixel[1];
    out[2] = pixel[2];
    out[3] = pixel[3];
}

希望这会有所帮助.

This might appear as a related question:

OpenGL ES 2.0 Object Picking on iOS

Which says Color picker is a good solution, and in deed after reading about it:

http://www.lighthouse3d.com/opengl/picking/index.php?color1

It does seem like a very simple solution so this brings me to this question

OpenGL ES color picking on the iPhone

Which unfortunately uses opengl es 1.0, I am trying to do it in 2.0 so I have no access to the functions described in that question.

But the theory seems simple and here is what I think I should do:

On touches begin I render my objects with a unique color.

On touches ended I get the pixel from that position and check it for the color to get my object. (probably with glReadPixels)

The problem is that I dont know how to do the "Render to the back buffer and read from it".

My code so far simply uses "Draw", I suspect I have to do something like glBindthe other buffer but I would appreciate some help.

My Drawing code is like this:

glClearColor(0, 0, 0, 0.0); 
glClear(GL_COLOR_BUFFER_BIT);

// Set the Projection Matrix
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(60), 2.0/3.0, 0, 50);


glUseProgram(_programHD);

glBindVertexArrayOES(_vao);

glActiveTexture(GL_TEXTURE1); 
glBindTexture(GL_TEXTURE_2D, _textureBuffer[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glUniform1i(uniforms[UNIFORM_TEXTURE_HD], 1);

// Drawing starts here //

// Pass the Model View Matrix to Open GL
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix,rotationMatrix);

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX_HD], 1, GL_FALSE, _modelViewProjectionMatrix.m);

// Change texture coordinates to draw a different image

glUniform2fv(uniforms[TEXTURE_OFFSET_HD], 1, offSet.v);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);

//glUniform2i(uniforms[TEXTURE_OFFSET], 7, -5);
glUniform2fv(uniforms[TEXTURE_OFFSET_HD], 1, borderHD.v);

glDrawElements(GL_LINE_STRIP, 6, GL_UNSIGNED_SHORT, 0);



glBindVertexArrayOES(0);
glUseProgram(0);

I have stripped the drawing calculations to make it more understandable. The point is I do not see anywhere where I specify to "where" am i drawing.

Thanks for your help.

解决方案

I've actually just finished implementing a colour picking function into my iPhone game, using openGL ES 2.0, using the lighthouse tutorial funny enough.

You should be drawing to the frame buffer.

If you want to read from the frame buffer, then you're correct in that you want to use glReadPixels. More information is here:

http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml

The only thing that's different from the lighthouse tutorial is that you also want to store the alpha values. Here's a quick function to get the colour of a specific pixel. Feel free to improve it or change it, but it does the job.

+ (void) ProcessColourPick : (GLubyte*) out : (Float32) x : (Float32) y
{
    GLint viewport[4];
    //Get size of screen
    glGetIntegerv(GL_VIEWPORT,viewport);

    GLubyte pixel[4];
    //Read pixel from a specific point
    glReadPixels(x,viewport[3] - y,1,1,
             GL_RGBA,GL_UNSIGNED_BYTE,(void *)pixel);

    out[0] = pixel[0];
    out[1] = pixel[1];
    out[2] = pixel[2];
    out[3] = pixel[3];
}

Hope this helps.

这篇关于iOS上的OpenGL ES 2.0对象选取(使用颜色编码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆