2012-01-03 57 views
0

当我尝试glTranslatef(1,-1,0);它会将我的四边形左手角推到屏幕的中心,而不是我试图做的事情,将它向左移1像素,向下移1像素。林肯定这是因为我的视口没有正确设置,但我不明白为什么。图片,查看下面的设置代码和绘图代码。glTranslatef/2D视口设置问题

enter image description here

setupView函数:

-(void)setupView:(GLView*)view 
{ 
printf("setup view"); 
glClearColor(0,1,1, 1); 
// Enable Smooth Shading, default not really needed. 
glShadeModel(GL_SMOOTH); 
// Depth buffer setup. 
glClearDepthf(1.0f); 


//enable textures. 
glEnable(GL_TEXTURE_2D); 
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST); 
glEnable(GL_BLEND); 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
CGRect rect = view.bounds; 
glOrthof(0,rect.size.width,-rect.size.height, 0, -1, 1) ; 
glViewport(0, 0,rect.size.width,rect.size.height); 

glMatrixMode(GL_PROJECTION); 


// Bind the number of textures we need, in this case one. 
glGenTextures(1, &texture[0]); 
glBindTexture(GL_TEXTURE_2D, texture[0]); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP,GL_TRUE); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glLoadIdentity(); 


NSString *path = [[NSBundle mainBundle] pathForResource:@"cm2" ofType:@"jpg"]; 
NSData *texData = [[NSData alloc] initWithContentsOfFile:path]; 
UIImage *image = [[UIImage alloc] initWithData:texData]; 

if (image == nil) 
    NSLog(@"Do real error checking here"); 

GLuint width = CGImageGetWidth(image.CGImage); 
GLuint height = CGImageGetHeight(image.CGImage); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
void *imageData = malloc(height * width * 4); 
CGContextRef context = CGBitmapContextCreate(imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 

// Flip the Y-axis 
CGContextTranslateCTM (context, 0, height); 
CGContextScaleCTM (context, 1.0, -1.0); 

CGColorSpaceRelease(colorSpace); 
CGContextClearRect(context, CGRectMake(0, 0, width, height)); 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage); 

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); 

CGContextRelease(context); 

free(imageData); 
[image release]; 
[texData release]; 


} 

drawView函数:

- (void)drawView:(GLView*)view 
{ 
//draw calls 

glColor4f(1,1,1,1); 
glClear(GL_COLOR_BUFFER_BIT); 

glLoadIdentity(); 

glEnableClientState(GL_VERTEX_ARRAY); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

static const Vertex3D vertices[] = { 
    {0, 0, 1}, //TL 
    { 1024.0f,0, 1}, //TR 
    {0, -1024.0f, 1}, //BL 
    { 1024.0f, -1024.0f, 1} //BR 
}; 


static const GLfloat texCoords[] = { 
    0.0, 1.0, 
    1.0, 1.0, 
    0.0, 0.0, 
    1.0, 0.0 
}; 
glTranslatef(1,-1, 1); 
glScalef(scale,scale,1); 

glBindTexture(GL_TEXTURE_2D, texture[0]); 
glVertexPointer(3, GL_FLOAT, 0, vertices); 
glTexCoordPointer(2, GL_FLOAT, 0, texCoords); 
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 
glDisableClientState(GL_VERTEX_ARRAY); 
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 

} 

回答

1

您需要先设置你的口,然后设置矩阵模式投影,然后调用glOrtho,像这样:

glViewPort (0, 0, width, height); 
glMatrixMode (GL_PROJECTION); 
glLoadIdentity(); 
glOrtho (0, width, 0, height, -1, 1); // Usually this is -width/2,width/2,-height/2,height/2 

此外,您可能需要将矩阵模式设置为ModelView,然后绘制模型。