2012-11-09 3 views
0

PNG 파일을로드하고이를 텍스처로 사용하여 구형에 바인딩하는 방법에 대한 튜토리얼을 찾을 수 없습니다. 이를 수행 할 라이브러리 함수가 있습니까? 어떻게 구체에 바인딩할까요? 나는 이걸로 해봤지만 효과가 없다. 오류는 없지만 텍스처가 구에로드되어 있지 않다. 나는 위해 glutMainLoop() 전에png를 텍스처로로드하여 구형으로 바인딩

다음

파일의 로딩 내 코드, 특정 파일로 LoadTexture 전화 : 내가 구를 만들 경우

여기가

GLuint LoadTexture(const char * filename, int width, int height) 
    { 
GLuint texture; 
unsigned char * data; 
FILE * file; 

//The following code will read in our PNG 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); //generate the texture with 

glBindTexture(GL_TEXTURE_2D, texture); //bind the texture 

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, 
GL_MODULATE); //set texture environment parameters 



//even better quality, but this will do for now. 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
GL_LINEAR); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
GL_LINEAR); 

//Here we are setting the parameter to repeat the texture 
//to the edge of our shape. 
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, width, height, 0, 
GL_RGB, GL_UNSIGNED_BYTE, data); 
free(data); //free the texture 
return texture; //return whether it was successfull 

}

void renderScene(void) { 

// Clear Color and Depth Buffers 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

glEnable(GL_TEXTURE_2D); 
// Reset transformations 
glLoadIdentity(); 
glBindTexture(GL_TEXTURE_2D, texture); 
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

glPushMatrix(); 
glTranslatef(0.0,2.0,-6); 
glRotatef(angle, 0.0f, 2.0, -6.0f); 
glutSolidSphere(1,50,50); 
glPopMatrix(); 


angle+=0.4f; 
glDisable(GL_TEXTURE_2D); 
glutSwapBuffers(); 
} 

나는 그것을 올바르게 했습니까?

답변

4

PNG는 압축 된 파일이므로 일부 이미지 라이브러리를 사용하여 압축을 풀 수 있습니다. PNG는 압축 파일이므로 OpenGL에서이를 디코딩하는 방법을 알고 있어야합니다. PNG를로드하는 데 권장되는 방법은 libpng입니다.

여기에 example은 libpng를 사용하여 PNG 파일을 2D 배열로 동시에 읽는 방법을 보여줍니다. OpenGL은 평평한 1D 배열을 기대하기 때문에 직접 평면화해야하지만 꽤 간단합니다.

4

압축 된 PNG 데이터를 OpenGL에 공급하고 있습니다. OpenGL 텍스처 함수가 PNG를 이해할 수 없기 때문에 먼저 압축을 풀어야합니다. stb_image.c

0

나는 you.Also에 대한 모든 더러운 일이 내가 modern의 OpenGL API를 사용하는 것이 좋습니다 않는 DevIL 같은 이미지 로딩 라이브러리를 사용하는 것이 좋습니다 것입니다,하지만 만 라이브러리가 마침내 결정 : 내가 실현

3

입니다 당신에게 던져지고 있지만, 나는 강력하게 SOIL을 추천한다. PNG를로드하는 것은 쉽습니다.

GLuint tex_2d = SOIL_load_OGL_texture("img.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y); 
관련 문제