iPhone opengl网格渲染在屏幕的一角?

时间:2010-08-20 04:22:14

标签: iphone objective-c opengl-es mesh

对于这长段代码感到抱歉,但我认为包含所有代码是有意义的。无论我玩什么,我都无法让我的网格渲染全屏。视口是全屏的,GL视图也是如此。这是发生的事情:

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];

谢谢!

1 个答案:

答案 0 :(得分: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)后,您可以缩放宽度和高度,直到它适合。