[英]ios opengl es 2.0 2D + 3D combination
我正在嘗試在OpenGL ES“ 2.0”中結合2D和3D,盡管在openGL es 1.0上存在許多問題,而在2.0上存在一些問題,但我仍難以設法解決。 因此,對於2D,我將退出本教程: http : //www.raywenderlich.com/9743/how-to-create-a-simple-2d-iphone-game-with-opengl-es-2-0-and -glkit-part-1 ,對於3D,我正在使用現有的多維數據集旋轉xcode模板...
我在第二個glDrawArray上得到了EXC_BAD_ACCESS(以某種方式認為原始緩沖區仍然適用?在繪制2D紋理之前是否要取消綁定此緩沖區?)使用以下渲染函數出錯。 如果我禁用以下行,它將起作用:glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition,3,GL_FLOAT,GL_FALSE,24,BUFFER_OFFSET(0)); glEnableVertexAttribArray(GLKVertexAttribNormal); glVertexAttribPointer(GLKVertexAttribNormal,3,GL_FLOAT,GL_FALSE,24,BUFFER_OFFSET(12));
這是怎么回事? 提前致謝。
[碼]
glEnable(GL_DEPTH_TEST);
// glGenVertexArraysOES(1, &_vertexArrayNum);
// glBindVertexArrayOES(_vertexArrayNum);
glGenBuffers(1, &_vertexBufferNum);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferNum);
glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
glBindVertexArrayOES(0);
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f);
// baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);
// Compute the model view matrix for the object rendered with GLKit
// GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 1.5f);
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeRotation(_rotation, 0, 1, 0);
//modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
self.effect.transform.modelviewMatrix = modelViewMatrix;
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(_vertexArrayNum);
// Render the object with GLKit
// [self.effect prepareToDraw];
// glDrawArrays(GL_TRIANGLES, 0, 36);
modelViewMatrix = GLKMatrix4MakeScale(2.0f, 2.0f, 2.0f);
projectionMatrix = GLKMatrix4MakeOrtho(0, 480, 0, 320, -1024, 1048);
self.effect.transform.projectionMatrix = projectionMatrix;
self.effect.transform.modelviewMatrix = modelViewMatrix;
[self.effect prepareToDraw];
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
[self.player render];
[/碼]
更新:添加了glEnableClientState(GL_VERTEX_ARRAY); 包裹在多維數據集繪制部分周圍...擺脫了嚴重的訪問錯誤,但是沒有繪制多維數據集或精靈(除非引起了我將代碼部分全部注釋掉)...
UPDATE2:所以問題很明顯是在我調用glDrawArray繪制立方體之后(工作正常)……我再次如下調用glDrawArray。 但是不知何故它仍在嘗試渲染以前的數組? // 1 self.effect.texture2d0.name = self.textureInfo.name; self.effect.texture2d0.enabled =是;
// 2
[self.effect prepareToDraw];
// 3
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
// 4
long offset = (long)&_quad;
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
// 5
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
愚蠢的我。 我可以立即使用:
glBindBuffer(GL_ARRAY_BUFFER, 0);
清除緩沖區。 我一直在尋找glUnbind東西...忘記了openGL不能使用內存引用... 2D texutre現在正在應用到多維數據集,但是我敢肯定那是一個簡單的解決方法:)....大家都在尋求幫助> :o
紋理問題已修復:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.