2012-01-03 4 views
0

私はglTranslatef(1、-1,0)を試してみます。それは私のクワッドの左側のコーナーを、画面の中心に、何をしようとしているのではなく、1ピクセル左に移動し、1つ下に移動します。私のビューポートisntは正しく設定されているが、理由はわかりません。 pic、以下の設定コードと描画コードを参照してください。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に設定したいとします。