• 8.6.4编程实例-纹理映射


    clip_image002

      1 #include <GL/glut.h>
      2 
      3 #include <stdlib.h>
      4 
      5 #include <stdio.h>
      6 
      7 #define stripeImageWidth 32
      8 
      9 GLubyte stripeImage[4*stripeImageWidth];
     10 
     11 void makeStripeImage(void) //生成纹理
     12 
     13 { int j;
     14 
     15 for (j=0; j<stripeImageWidth; j++)
     16 
     17 { stripeImage[4*j+0] = (GLubyte) ((j<=4) ? 255 :0);
     18 
     19 stripeImage[4*j+1] = (GLubyte) ((j>4) ? 255 : 0);
     20 
     21 stripeImage[4*j+2] = (GLubyte) 0;
     22 
     23 stripeImage[4*j+3] = (GLubyte) 255;
     24 
     25 }
     26 
     27 }
     28 
     29 // 平面纹理坐标生成
     30 
     31 static GLfloat xequalzero[] = {1.0, 1.0, 1.0, 1.0};
     32 
     33 static GLfloat slanted[] = {1.0, 1.0, 1.0, 0.0};
     34 
     35 static GLfloat *currentCoeff;
     36 
     37 static GLenum currentPlane;
     38 
     39 static GLint currentGenMode;
     40 
     41 static float roangles;
     42 
     43 void init(void)
     44 
     45 { glClearColor (1.0, 1.0, 1.0, 1.0);
     46 
     47 glEnable(GL_DEPTH_TEST);
     48 
     49 glShadeModel(GL_SMOOTH);
     50 
     51 makeStripeImage();
     52 
     53 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
     54 
     55 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     56 
     57 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     58 
     59 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     60 
     61 glTexImage1D(GL_TEXTURE_1D, 0, 4, stripeImageWidth, 0,
     62 
     63 GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
     64 
     65 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
     66 
     67 currentCoeff = xequalzero;
     68 
     69 currentGenMode = GL_OBJECT_LINEAR;
     70 
     71 currentPlane = GL_OBJECT_PLANE;
     72 
     73 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
     74 
     75 glTexGenfv(GL_S, currentPlane, currentCoeff);
     76 
     77 glEnable(GL_TEXTURE_GEN_S);
     78 
     79 glEnable(GL_TEXTURE_1D);
     80 
     81 glEnable(GL_LIGHTING);
     82 
     83 glEnable(GL_LIGHT0);
     84 
     85 glEnable(GL_AUTO_NORMAL);
     86 
     87 glEnable(GL_NORMALIZE);
     88 
     89 glFrontFace(GL_CW);
     90 
     91 glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
     92 
     93 roangles=45.0f;
     94 
     95 }
     96 
     97 void display(void)
     98 
     99 { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    100 
    101 glPushMatrix ();
    102 
    103 glRotatef(roangles,0.0,0.0,1.0);
    104 
    105 glutSolidSphere(2.0,32,32 );
    106 
    107 glPopMatrix ();
    108 
    109 glFlush();
    110 
    111 }
    112 
    113 void reshape(int w, int h)
    114 
    115 { glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    116 
    117 glMatrixMode(GL_PROJECTION);
    118 
    119 glLoadIdentity();
    120 
    121 if (w <= h)
    122 
    123 glOrtho (-3.5, 3.5, -3.5*(GLfloat)h/(GLfloat)w,
    124 
    125 3.5*(GLfloat)h/(GLfloat)w, -3.5, 3.5);
    126 
    127 else
    128 
    129 glOrtho (-3.5*(GLfloat)w/(GLfloat)h,
    130 
    131 3.5*(GLfloat)w/(GLfloat)h, -3.5, 3.5, -3.5, 3.5);
    132 
    133 glMatrixMode(GL_MODELVIEW);
    134 
    135 glLoadIdentity();
    136 
    137 }
    138 
    139 void idle()
    140 
    141 { roangles += 0.05f;
    142 
    143 glutPostRedisplay();
    144 
    145 }
    146 
    147 int main(int argc, char** argv)
    148 
    149 {
    150 
    151 glutInit(&argc, argv);
    152 
    153 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    154 
    155 glutInitWindowSize(256, 256);
    156 
    157 glutInitWindowPosition(100, 100);
    158 
    159 glutCreateWindow (argv[0]);
    160 
    161 glutIdleFunc(idle);
    162 
    163 init ();
    164 
    165 glutDisplayFunc(display);
    166 
    167 glutReshapeFunc(reshape);
    168 
    169 glutMainLoop();
    170 
    171 return 0;
    172 
    173 }

    附上本实验的VC++工程代码(VC++2008)

  • 相关阅读:
    操作系统实验报告-信号量的实现和应用
    操作系统实验报告-系统调用
    操作系统实验报告-熟悉实验环境
    Linux下JDK环境变量配置
    BATMAN.adv系列07 过度泛洪的遏制策略与网络重组
    BATMAN.adv系列06 协议栈结构
    BATMAN.adv系列05 数据结构分析
    BATMAN.adv系列04 TVLV包
    BATMAN.adv系列01:BATMAN.adv IV 概述
    Kudu、Hudi和Delta Lake的比较
  • 原文地址:https://www.cnblogs.com/opengl/p/3790472.html
Copyright © 2020-2023  润新知