Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6.  
  7. #define stripeImageWidth 32
  8. GLubyte stripeImage[4*stripeImageWidth];
  9.  
  10. static GLuint texName;
  11.  
  12. void makeStripeImage(void)
  13. {
  14. int j;
  15.  
  16. for (j = 0; j < stripeImageWidth; j++) {
  17. stripeImage[4*j] = (GLubyte) ((j<=4) ? 255 : 0);
  18. stripeImage[4*j+1] = (GLubyte) ((j>4) ? 255 : 0);
  19. stripeImage[4*j+2] = (GLubyte) 0;
  20. stripeImage[4*j+3] = (GLubyte) 255;
  21. }
  22. }
  23.  
  24. /* planes for texture coordinate generation */
  25. static GLfloat xequalzero[] = {1.0, 0.0, 0.0, 0.0};
  26. static GLfloat slanted[] = {1.0, 1.0, 1.0, 0.0};
  27. static GLfloat *currentCoeff;
  28. static GLenum currentPlane;
  29. static GLint currentGenMode;
  30.  
  31. void init(void)
  32. {
  33. glClearColor (0.0, 0.0, 0.0, 0.0);
  34. glEnable(GL_DEPTH_TEST);
  35. glShadeModel(GL_SMOOTH);
  36.  
  37. makeStripeImage();
  38. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  39.  
  40. glGenTextures(1, &texName);
  41. glBindTexture(GL_TEXTURE_1D, texName);
  42. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  43. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER,
  44. GL_LINEAR);
  45. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER,
  46. GL_LINEAR);
  47. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, stripeImageWidth, 0,
  48. GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
  49.  
  50. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  51. currentCoeff = xequalzero;
  52. currentGenMode = GL_OBJECT_LINEAR;
  53. currentPlane = GL_OBJECT_PLANE;
  54. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  55. glTexGenfv(GL_S, currentPlane, currentCoeff);
  56.  
  57. glEnable(GL_TEXTURE_GEN_S);
  58. glEnable(GL_TEXTURE_1D);
  59. glEnable(GL_CULL_FACE);
  60. glEnable(GL_LIGHTING);
  61. glEnable(GL_LIGHT0);
  62. glEnable(GL_AUTO_NORMAL);
  63. glEnable(GL_NORMALIZE);
  64. glFrontFace(GL_CW);
  65. glCullFace(GL_BACK);
  66. glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
  67. }
  68.  
  69. void display(void)
  70. {
  71. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  72.  
  73. glPushMatrix ();
  74. glRotatef(45.0, 0.0, 0.0, 1.0);
  75. glBindTexture(GL_TEXTURE_1D, texName);
  76. glutSolidTeapot(2.0);
  77. glPopMatrix ();
  78. glFlush();
  79. }
  80.  
  81. void reshape(int w, int h)
  82. {
  83. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  84. glMatrixMode(GL_PROJECTION);
  85. glLoadIdentity();
  86. if (w <= h)
  87. glOrtho (-3.5, 3.5, -3.5*(GLfloat)h/(GLfloat)w,
  88. 3.5*(GLfloat)h/(GLfloat)w, -3.5, 3.5);
  89. else
  90. glOrtho (-3.5*(GLfloat)w/(GLfloat)h,
  91. 3.5*(GLfloat)w/(GLfloat)h, -3.5, 3.5, -3.5, 3.5);
  92. glMatrixMode(GL_MODELVIEW);
  93. glLoadIdentity();
  94. }
  95.  
  96. void keyboard (unsigned char key, int x, int y)
  97. {
  98. switch (key) {
  99. case 'e':
  100. case 'E':
  101. currentGenMode = GL_EYE_LINEAR;
  102. currentPlane = GL_EYE_PLANE;
  103. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  104. glTexGenfv(GL_S, currentPlane, currentCoeff);
  105. glutPostRedisplay();
  106. break;
  107. case 'o':
  108. case 'O':
  109. currentGenMode = GL_OBJECT_LINEAR;
  110. currentPlane = GL_OBJECT_PLANE;
  111. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  112. glTexGenfv(GL_S, currentPlane, currentCoeff);
  113. glutPostRedisplay();
  114. break;
  115. case 's':
  116. case 'S':
  117. currentCoeff = slanted;
  118. glTexGenfv(GL_S, currentPlane, currentCoeff);
  119. glutPostRedisplay();
  120. break;
  121. case 'x':
  122. case 'X':
  123. currentCoeff = xequalzero;
  124. glTexGenfv(GL_S, currentPlane, currentCoeff);
  125. glutPostRedisplay();
  126. break;
  127. case 27:
  128. exit(0);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134.  
  135. int main(int argc, char** argv)
  136. {
  137. glutInit(&argc, argv);
  138. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  139. glutInitWindowSize(256, 256);
  140. glutInitWindowPosition(100, 100);
  141. glutCreateWindow (argv[0]);
  142. init ();
  143. glutDisplayFunc(display);
  144. glutReshapeFunc(reshape);
  145. glutKeyboardFunc(keyboard);
  146. glutMainLoop();
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement