1
我正在開發一款使用OpenGL ES的iPhone遊戲。我想用使用類似的代碼觸摸座標移動播放器執行以下操作:如何將窗口座標轉換爲2D 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];
}
使用的窗口,我無法翻譯對象:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point=[[touches anyObject]locationInView:self.view];
[eaglView render:point.x];
}// end
然後用下面的代碼我的OpenGL ES視圖中繪製座標,所以我必須將其轉換爲標準化的設備座標。我將如何從窗口座標轉換爲可用於翻譯我的OpenGL ES模型的座標?