请教opengl里光照与坐标系的问题

dukelee 2010-01-17 12:59:39
程序主要是个纹理贴图,然后对图像做一些处理,其中我加了个光照,出现问题是启用光照后图片就没了,图像是2D的

/************************************************************************/
unsigned int g_texture1; /* texture #1 */

rgb_image_struct *g_input_image1; /* image #1 */

rgb_image_struct *g_input_image2;
RGB *g_output_image;
BOOL g_bLight = FALSE;
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 4.0f, 1.0f };//这里改成-4可以,但光照没有效果

int main(int argc, char** argv)
{
Tiff2bmp();
/* GLUT initialization */
glutInit(&argc, argv);
/* set the displaying-window size */
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(300, 0);
/* set the display mode */
/* -------------------- */
/* GLUT_DOUBLE: double buffer */
/* GLUT_DEPTH: enable depth buffer */
/* GLUT_RGB: the RGB pixel format */
/* GLUT_ALPHA: enable the alpha channel */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_ALPHA);
/* create the displaying window with the desired caption */
glutCreateWindow("Image PsendoColor");

/* do some initialization for OpenGL */
init_opengl();
glutDisplayFunc(display_callback);
glutKeyboardFunc(key_callback);
glutReshapeFunc(resize_callback);
glutMainLoop();
return 0;
}

void init_opengl()
{
/* set the RGBA color for background clearing */
/* In OpenGL, the range of a color channel is [0, 1] */
glClearColor(0.0, 0.0, 0.0, 1.0);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT1);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
/* read in the input image */
g_input_image1 = read_bmp_image("2.bmp");

g_input_image2 = read_bmp_image("2.bmp");

/* generate a texture ID */
glGenTextures(1, &g_texture1);
/* Bind the texture to a rectangle texture */
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, g_texture1);
/* set the texture properties */
/* scaling filter, GL_NEAREST or GL_LINEAR */
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
/* uploading the image to the texture memory of the GPU */

glTexImage2D(
GL_TEXTURE_RECTANGLE_ARB, /* texture target */
0, /* mipmap level */
GL_RGB8, /* texture internal format */
g_input_image1->width, /* width */
g_input_image1->height, /* height */
0, /* border */
GL_RGB, /* pixel format of the image */
/* (matching internal format is better) */
GL_UNSIGNED_BYTE, /* channel type */
*g_input_image1->pixel /* image data */
);
/* restore the texture state */
glDisable(GL_TEXTURE_RECTANGLE_ARB);

/************************************************************************/
}

void display_callback(void)
{
/* clear the framebuffer and the depth buffer */
//glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glLoadIdentity();
//glTranslatef(0.0f,0.0f,-5.0f);//加入这句即使不启用光照也看不到图片
//glPushMatrix();
//glScalef(0.33f,0.33f,1.0f);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, g_texture1);
glBegin(GL_QUADS);
{
glNormal3f( 0.0f, 0.0f, 1.0f);
/* vertex #1 */
glTexCoord2i (0, g_input_image1->height);
glVertex3i (0,0,2);
/* vertex #2 */
glTexCoord2i (g_input_image1->width, g_input_image1->height);
glVertex3i (g_input_image1->width, 0, 2);
/* vertex #3 */
glTexCoord2i (g_input_image1->width, 0);
glVertex3i (g_input_image1->width, g_input_image1->height, 2);
/* vertex #4 */
glTexCoord2i (0, 0);
glVertex3i (0, g_input_image1->height , 2);
}
glEnd();
glDisable(GL_TEXTURE_RECTANGLE_ARB);
//glPopMatrix();
glutSwapBuffers();
}

void resize_callback(int width, int height)
{
glViewport(
100, /* x */
0, /* y */
WINDOW_WIDTH, /* width */
WINDOW_HEIGHT /* height */
);
/* model-view-projection transformation */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* orthogonal projection */
glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, -100, 100);
//gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
/* viewport transformation */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}

void key_callback(unsigned char key_code, int x, int y)
{
char filename[256];

switch (key_code)
{
/* exit */
/* Esc */
case 27:
/* Enter */
case 13:
/* Q */
case 'Q':
/* q */
case 'q':
/* before exiting, release allocated resources */
on_release();
exit(0);
break;
case 'f':
if(FALSE == g_bLight)
{
glEnable(GL_LIGHTING);
g_bLight = TRUE;
}
else
{
//glDisable(GL_LIGHTING);
g_bLight = FALSE;
}
break;
}

/* redraw the screen */
glutPostRedisplay();
}

上边是主要的程序,代码和网上的光照程序的例子差不多,现在就是光照启用后图像就没了
之前的图片载入没有问题,还有2维的彩色图片加入光照会有明显的效果改变么
帮忙看看,谢谢
...全文
144 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangda12 2010-01-19
  • 打赏
  • 举报
回复
应该是图片的材料属性里面设置成双面光照的那个样子,就是
mat[]={}
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,mat);
mat是ambient和diffuse的数值

光源方向改成负数的话看不到无光照效果应该是光源在图片和视角的中间,图片把光挡住了。
希望对你有帮助·····
dukelee 2010-01-18
  • 打赏
  • 举报
回复
二维的图片和深度有关系么,我改改试试
我对opengl也是一知半解,还有就是这张图有没有光照效果会有影响么?
还有会的人帮忙看看吧
wuhuwy 2010-01-17
  • 打赏
  • 举报
回复
是不是深度缓存的问题

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧