2010-08-20 86 views
0

對這段很長的代碼感到抱歉,但我認爲包含所有代碼是有意義的。無論我玩什麼,我都無法讓我的網格呈現全屏。視圖是全屏的,GL視圖也是。這裏是發生了什麼:iPhone opengl網格渲染在屏幕的一個角落?

http://img191.imageshack.us/img191/247/screenshot2010082000163.png

這是在我的繪畫循環運行的代碼:

int verticalDivisions = 16; 
int horizontalDivisions = 16; 
GLfloat *verticesArr; 
GLfloat *textureCoordsArr; 
GLuint textureID = texture[0]; 

unsigned int verticesArrsize = (verticalDivisions * ((2 + horizontalDivisions * 2) * 3)); 
unsigned int textureCoordsArraySize = verticalDivisions * ((2 + horizontalDivisions * 2) * 2); 
verticesArr = (GLfloat *)malloc(verticesArrsize * sizeof(GLfloat)); 
textureCoordsArr = (GLfloat*)malloc(textureCoordsArraySize * sizeof(GLfloat)); 

float height = 1.0f/verticalDivisions; 
float width = 1.0f/horizontalDivisions; 

int i,j, count; 

count = 0; 

for (j=0; j<verticalDivisions; j++) { 
    for (i=0; i<=horizontalDivisions; i++, count+=6) { //2 vertices each time... 
     float currX = i * width; 
     float currY = j * height; 
     verticesArr[count] = currX;///backingWidth; 
     verticesArr[count+1] = (currY + height);///backingHeight; 
     verticesArr[count+2] = 0.0f; 

     verticesArr[count+3] = currX;///backingWidth; 
     verticesArr[count+4] = currY;///backingHeight; 
     verticesArr[count+5] = 0.0f; 
     //NSLog(@"v1: (%f,%f) v2:(%f,%f)",currX,currY+height,currX,currY); 
    } 
} 


count = 0; 

float xIncrease = 0.625f/(float)horizontalDivisions; 
float yIncrease = 0.898f/(float)verticalDivisions; 

int x,y; 
//int elements; 
count = 0; 

for (y=0; y<verticalDivisions; y++) { 
    for (x=0; x<horizontalDivisions+1; x++, count+=4) { 
     float currX = (float)x * xIncrease; 
     float currY = (float)y * yIncrease; 
     textureCoordsArr[count] = (float)currX; 
     textureCoordsArr[count+1] = (float)currY + yIncrease; 

     textureCoordsArr[count+2] = (float)currX; 
     textureCoordsArr[count+3] = (float)currY; 
     //NSLog(@"v1: (%f,%f) v2:(%f,%f)",currX,currY+yIncrease,currX,currY); 
    } 
} 

NSLog(@"expected %i vertices, and %i vertices were done",(verticalDivisions * ((2 + horizontalDivisions*2) * 2)) , count); 


glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer); 
glLoadIdentity(); 
glColor4f(255.0, 255.0, 255.0, 0.0); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

glEnable(GL_TEXTURE_2D); 
glEnableClientState(GL_VERTEX_ARRAY); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY); 


glBindTexture(GL_TEXTURE_2D, textureID); 

glTexCoordPointer(2, GL_FLOAT, 0, textureCoordsArr); 
glVertexPointer(3, GL_FLOAT, 0, verticesArr); 


glPushMatrix();{ 
    int i; 
    for (i=0; i<verticalDivisions; i++) { 
     //glDrawArrays(GL_LINE_STRIP, i*(horizontalDivisions*2+2), horizontalDivisions*2+2); 
     glDrawArrays(GL_TRIANGLE_STRIP, i*(horizontalDivisions*2+2), (horizontalDivisions*2+2)); 

    } 
}glPopMatrix(); 


glDisableClientState(GL_VERTEX_ARRAY); 
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 



glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer); 
[context presentRenderbuffer:GL_RENDERBUFFER_OES]; 

謝謝!

+0

你的GL設置代碼是什麼樣的? – Goz 2010-08-20 09:38:14

+0

設置代碼不是問題,我的其他繪圖工作正常。謝謝 – 2010-08-20 15:01:51

回答

0

試着改變一點你的代碼到:

float xStart = -0.5; 
float yStart = -0.5; 
for (j=0; j<verticalDivisions; j++) { 
    for (i=0; i<=horizontalDivisions; i++, count+=6) { //2 vertices each time... 
     float currX = xStart + i * width; 
     float currY = yStart + j * height; 
     verticesArr[count] = currX;///backingWidth; 
    .... 

這應該讓你走動的網格。對齊到一個角落後(更改xStart和yStart),可以縮放寬度和高度,直到其適合。

+0

謝謝!這工作完美。 – 2010-08-20 18:55:57