19,468
社区成员
发帖
与我相关
我的任务
分享
/************************************************************************/
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();
}