2012-08-23 79 views
2

我想为mac设置一个openGL窗口的背景。背景将需要一个JPG或PNG文件。如何在mac中使用纹理.jpg图像作为openGL背景窗口?

这里是我的代码..

GLuint texture; //the array for our texture 

GLfloat angle = 0.0; 

GLuint LoadTexture (const char * filename, int width, int height){ 

// GLuint texture; 
unsigned char * data; 
FILE * file; 

//The following code will read in our RAW file 
file = fopen(filename, "rb"); 
if (file == NULL) return 0; 
data = (unsigned char *)malloc(width * height * 3); 
fread(data, width * height * 3, 1, file); 
fclose(file); 

glGenTextures(1, &texture); 
glBindTexture(GL_TEXTURE_2D, texture); 
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

// // when texture area is small, bilinear filter the closest mipmap 
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
//     GL_LINEAR_MIPMAP_NEAREST); 
// // when texture area is large, bilinear filter the original 
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
//  
// // the texture wraps over at the edges (repeat) 
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
//  
// //Generate the texture 
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,GL_RGB, GL_UNSIGNED_BYTE, data); 



// select modulate to mix texture with color for shading 
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

// when texture area is small, bilinear filter the closest mipmap 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
       GL_LINEAR_MIPMAP_NEAREST); 
// when texture area is large, bilinear filter the first mipmap 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

// // the texture wraps over at the edges (repeat) 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 

// build our texture mipmaps 
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, 
        GL_RGB, GL_UNSIGNED_BYTE, data); 

free(data); 

return texture; //return whether it was successful 

} 

void FreeTexture(GLuint texture){ 
glDeleteTextures(1, &texture); 
} 

void cube() { 
glEnable(GL_TEXTURE_2D); 
glBindTexture(GL_TEXTURE_2D, texture); //bind the texture 

glPushMatrix(); 
glRotatef(angle, 0.0f, 0.0f, 1.0f); 
glBegin(GL_QUADS); 
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0); 
glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0); 
glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0); 
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0); 
glEnd(); 
glPopMatrix(); 
glutSwapBuffers(); 
//glutSolidCube(2); 
} 

void display() { 
    glClearColor (0.0,0.0,0.0,1.0); 
    glClear (GL_COLOR_BUFFER_BIT); 
    glLoadIdentity(); 
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 
    texture = LoadTexture("/Users/macbook/MatrixEngineClientSample/Fighters/Sunset03.jpg", 256, 256 ); //load the texture 
    glEnable(GL_TEXTURE_2D); //enable 2D texturing 
// glEnable(GL_TEXTURE_GEN_S); //enable texture coordinate generation 
// glEnable(GL_TEXTURE_GEN_T); 
    cube(); 
    FreeTexture(texture); 
    //glutSwapBuffers(); 
    //angle ++; 
} 

void reshape (int w, int h) { 
    glViewport (0, 0, (GLsizei)w, (GLsizei)h); 
    glMatrixMode (GL_PROJECTION); 
    //glLoadIdentity(); 
    gluPerspective (50, (GLfloat)w/(GLfloat)h, 1.0, 100.0); 
    glMatrixMode (GL_MODELVIEW); 
} 

int main (int argc, char **argv) { 
    glutInit (&argc, argv); 
    glutInitDisplayMode (GLUT_DOUBLE); 
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (100, 100); 
    glutCreateWindow ("A basic OpenGL Window"); 
    glutDisplayFunc (display); 
    glutIdleFunc (display); 
    glutReshapeFunc (reshape); 
    glutMainLoop(); 
    return 0; 
} 

编辑

我希望看到这个图像作为OpenGL窗口的背景...图像低于..

enter image description here

但它显示此...

enter image description here

+0

你需要说这么多。什么失败?你期望看到什么?你在代码中哪里看到了问题? –

+2

FWIW,乍一看,您的代码正在加载文件的字节并尝试将其用作纹理位图。如果您说的文件是PNG或JPG文件,那么这将不起作用;这些文件类型需要先解码成位图。 –

+0

@quixoto我已更新我的问题...你能告诉我我的问题在哪里?你可以给我任何帮助,通过提供任何代码或链接,因为我在OpenGL中有点新。 – Emon

回答

3

我咨询this Apple guide

问题是文件被压缩并且有一个头文件,并且您的代码甚至使用文件头的一部分作为图像源。

我将这段代码:

// GLuint texture; 
unsigned char * data; 
FILE * file; 

//The following code will read in our RAW file 
file = fopen(filename, "rb"); 
if (file == NULL) return 0; 
data = (unsigned char *)malloc(width * height * 3); 
fread(data, width * height * 3, 1, file); 
fclose(file); 

更多的东西与此类似:

CFURLRef urlRef = (CFURLRef)[NSURL fileURLWithPath:@"/Users/macbook/MatrixEngineClientSample/Fighters/Sunset03.jpg"]; 
CGImageSourceRef myImageSourceRef = CGImageSourceCreateWithURL(url, NULL); 
CGImageRef myImageRef = CGImageSourceCreateImageAtIndex(myImageSourceRef, 0, NULL); 
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(myImageRef)); 
unsigned char *data = CFDataGetBytePtr(data); 

您可能需要核芯显卡和Core Foundation的添加到您的链接框架的名单。

+0

据我所知,你正试图添加Objective-C代码到C++项目。我认为这不会起作用。 – michaellindahl

+0

你可以混合使用Obj-C和C++。 Xcode使用文件扩展名“.hh”和“.mm”作为使用两者的源代码。 – Tyler

+0

以下Apple技术问答提供了示例代码,用于了解使用上述代码访问的原始像素数据的格式:https://developer.apple.com/library/mac/qa/qa1509/_index.html – Tyler

2

PNG和JPG图像(与大多数其他图像格式一样)需要某种形式的解压缩/解码,因此从文件加载原始文件不会产生预期结果。它应该在读取文件头之后使用bmp或未压缩的tga图像:/。无论如何,这里有一些图像加载库,应该使加载图片轻松:

SOIL

DevIL

FreeImage

GLI

+1

当操作系统本身支持它时,无需诉诸一些难以编译的第三方库只需使用Apple提供的工具 – user1118321

+0

i'如果你使用苹果的,你的用户将自动获得苹果公司在操作系统更新中增加的任何新格式的支持,例如:苹果公司提供的工具是什么? –

+0

操作系统内置了对图像格式的支持。以及b ug修正等。当您使用第三方库时,只有在将应用程序重新链接到第三方库并重新发布时才会修复错误。 – user1118321

相关问题