正文

(OPENGL编程指南第四版)OPENGL范例程序9.12008-04-03 21:11:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yuqiexing/33908.html

分享到:

// 范例P250.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <gl/glut.h>

#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageWidth][checkImageHeight][4];

static GLuint texName;

void makeCheckImage(void)
{
 int i,j,c;

 for(i=0;i<checkImageWidth;i++)
  for(j=0;j<checkImageHeight;j++)
  {
   c=(((i&0x8)==0)^((j&0x8)==0))*255;
   for(int k=0;k<3;k++)
    checkImage[i][j][k]=(GLubyte)c;
   checkImage[i][j][3]=(GLubyte)255;
  }
}


void display()
{
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 glColor3f(1.0,1.0,1.0);       //设置画笔白色
 
 glEnable(GL_TEXTURE_2D);
 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
 glBindTexture(GL_TEXTURE_2D,texName);

 glBegin(GL_QUADS);
 glTexCoord2f(0.0,0.0);glVertex3f(-2.0,-1.0,0.0);
 glTexCoord2f(0.0,1.0);glVertex3f(-2.0,1.0,0.0);
 glTexCoord2f(1.0,1.0);glVertex3f(0.0,1.0,0.0);
 glTexCoord2f(1.0,0.0);glVertex3f(0.0,-1.0,0.0);

 glTexCoord2f(0.0,0.0);glVertex3f(1.0,-1.0,0.0);
 glTexCoord2f(0.0,1.0);glVertex3f(1.0,1.0,0.0);
 glTexCoord2f(1.0,1.0);glVertex3f(2.41421,1.0,-1.41421);
 glTexCoord2f(1.0,0.0);glVertex3f(2.41421,-1.0,-1.41421);
 glEnd();
 glFlush();
 glDisable(GL_TEXTURE_2D);

 glFlush();          //立即显示出来
}

void init()
{
 glClearColor(0.0,0.0,0.0,0.0);     //用黑色清除背景
 glShadeModel(GL_FLAT);
 glEnable(GL_DEPTH_TEST);
 makeCheckImage();
 glPixelStorei(GL_UNPACK_ALIGNMENT,1);

 glGenTextures(1,&texName);
 glBindTexture(GL_TEXTURE_2D,texName);

 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

 glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,checkImageWidth,checkImageHeight,0,GL_RGBA,
  GL_UNSIGNED_BYTE,checkImage);
}

void reshape(int w,int h)
{
 glViewport(0,0,(GLsizei)w,(GLsizei)h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,30.0);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(0.0,0.0,-3.6);
}

int main(int argc,char ** argv)
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 glutInitWindowSize(250,250);     //初始化窗口大小
 glutInitWindowPosition(0,0);    //初始化窗口位置
 glutCreateWindow("hello");      //创建标题为“hello”的窗口
 init();           //调用初始化函数
 glutDisplayFunc(display);      //注册回调函数
 glutReshapeFunc(reshape);
 glutMainLoop();         //进入主循环处理事件
 return 0;
}


阅读(2706) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册