2012-08-23 30 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

相關問題