Iphone 如何将窗口坐标转换为二维OpenGL ES坐标?
我正在使用OpenGLES开发一个iPhone游戏。我想使用如下代码使用触摸坐标移动播放器:Iphone 如何将窗口坐标转换为二维OpenGL ES坐标?,iphone,opengl-es,Iphone,Opengl Es,我正在使用OpenGLES开发一个iPhone游戏。我想使用如下代码使用触摸坐标移动播放器: - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint point=[[touches anyObject]locationInView:self.view]; [eaglView render:point.x]; }// end 然后使用以下代码在我的OpenGL ES视图中进行渲染: - (
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point=[[touches anyObject]locationInView:self.view];
[eaglView render:point.x];
}// end
然后使用以下代码在我的OpenGL ES视图中进行渲染:
- (void) render:(float )delta
{
if(initialized==0) {
[self initOpenGL];
}
static const GLfloat squareVertices[] = {
-0.5f, -0.33f,
0.5f, -0.33f,
-0.5f, 0.33f,
0.5f, 0.33f,
};
static const GLubyte squareColors[] = {
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
};
float transY = delta;
// This application only creates a single context which is already set current at this point.
// This call is redundant, but needed if dealing with multiple contexts.
[EAGLContext setCurrentContext:context];
// This application only creates a single default framebuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple framebuffers.
//glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Use shader program
//glUseProgram(program);
// Update uniform value
//glUniform1f(uniforms[UNIFORM_TRANSLATE], (GLfloat)transY);
//transY += 0.075f;
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
//transY += 0.0001f;
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
// Update attribute values
// Validate program before drawing. This is a good check, but only really necessary in a debug build.
// DEBUG macro must be defined in your debug configurations if that's not already the case.
// Draw
// This application only creates a single color renderbuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple renderbuffers.
// glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES,renderBuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
我无法使用窗口坐标转换对象,因此必须将其转换为标准化设备坐标。如何将窗口坐标转换为可用于转换OpenGL ES模型的坐标?要将窗口坐标转换为OpenGL坐标,只需翻转y轴(可能是x轴,具体取决于方向)。如果设备处于纵向模式,则使用以下代码:
CGPoint touch_point = [[touches anyObject] locationInView:self.view];
touch_point = CGPointMake(touch_point.x, 480-touch_point.y); //Or you can put touch_point.y = 480-touch_point.y
[eaglview renderTouchAtPoint:touch_point];
现在,如果您的设备处于横向,除了x轴之外,您也可以执行相同的操作:
CGPoint touch_point = [[touches anyObject] locationInView:self.view];
touch_point = CGPointMake(480-touch_point.x, touch_point.y);
[eaglview renderTouchAtPoint:touch_point];
这对我有用,希望对你有用