扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
#include <stdlib.h> #include <GL/glut.h> #define imageWidth 64 #define imageHeight 64 GLubyte image[imageWidth][imageHeight][3]; /*绘制一个简单的二维纹理图*/ void makeImage(void) { int i,j,r,g,b; /*根据点的位置设置不同的颜色*/ for(i = 0;i < imageWidth;i++) { for(j = 0;j <imageHeight;j++) { r = (i*j)%255; g = (i*i)%255; b = (j*j)%255; image[i][j][0] = (GLubyte)r; image[i][j][1] = (GLubyte)g; image[i][j][2] = (GLubyte)b; } } } void myInit(void) { glClearColor(0.0,0.0,0.0,0.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); makeImage(); glPixelStorei(GL_UNPACK_ALIGNMENT,1); /*指定二维纹理映射*/ glTexImage2D(GL_TEXTURE_2D,0,3,imageWidth,imageHeight,0,GL_RGB,GL_UNSIGNED_BYTE,&image[0][0][0]); /*设置纹理参数*/ glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); /*设置纹理环境参数*/ glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL); glEnable(GL_TEXTURE_2D); glShadeModel(GL_FLAT); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /*将纹理映射到四边形上*/ glBegin(GL_QUADS); /*纹理的坐标和四边形顶点的对应*/ 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(1.0,-1.0,0.0); glTexCoord2f(1.0,0.0); glVertex3f(1.0,1.0,0.0); glEnd(); glFlush(); } void myReshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80.0,1.0-(GLfloat)w/(GLfloat)h,1.0,30.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc,char **argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,300); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow(" Texture "); /*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return 0; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者