• C++ opengl 纹理映射


    1. #include
    2. #include
    3. #include
    4. #include
    5. #define stripeImageWidth 32
    6. GLubyte stripeImage[4 * stripeImageWidth];
    7. bool mouseLeftDown;
    8. bool mouseRightDown;
    9. float mouseX, mouseY;
    10. float cameraAngleX;
    11. float cameraAngleY;
    12. void mouseCB(int button, int state, int x, int y);
    13. void mouseMotionCB(int x, int y);
    14. // create 2D texture
    15. void makeStripeImage(void)
    16. {
    17. int j;
    18. for (j = 0; j < stripeImageWidth; j++)
    19. {
    20. stripeImage[4 * j + 0] = (GLubyte)((j <= 4) ? 255 : 0);
    21. stripeImage[4 * j + 1] = (GLubyte)((j > 4) ? 255 : 0);
    22. stripeImage[4 * j + 2] = (GLubyte)0;
    23. stripeImage[4 * j + 3] = (GLubyte)255;
    24. }
    25. }
    26. static GLfloat xequalzero[] = { 1.0, 1.0, 1.0, 1.0 };
    27. static GLfloat slanted[] = { 1.0, 1.0, 1.0, 1.0 };
    28. static GLfloat* currentCoeff;
    29. static GLenum currentPlane;
    30. static GLint currentGenMode;
    31. void init(void)
    32. {
    33. glClearColor(1.0, 1.0, 1.0, 1.0);
    34. glEnable(GL_DEPTH_TEST);
    35. //glShadeModel(GL_SMOOTH);
    36. makeStripeImage();
    37. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    38. //glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    39. //glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    40. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    41. glTexImage1D(GL_TEXTURE_1D, 0, 4, stripeImageWidth, 0, GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
    42. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    43. currentCoeff = xequalzero;
    44. currentGenMode = GL_OBJECT_LINEAR;
    45. currentPlane = GL_OBJECT_PLANE;
    46. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
    47. glTexGenfv(GL_S, currentPlane, currentCoeff);
    48. glEnable(GL_TEXTURE_GEN_S);
    49. glEnable(GL_TEXTURE_1D);
    50. glEnable(GL_LIGHTING);
    51. glEnable(GL_LIGHT0);
    52. //glEnable(GL_AUTO_NORMAL);
    53. //glEnable(GL_NORMALIZE);
    54. //glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
    55. }
    56. void display(void) {
    57. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    58. glPushMatrix();
    59. glRotatef(cameraAngleX, 1, 0, 0);
    60. glRotatef(cameraAngleY, 0, 1, 0);
    61. glutSolidTeapot(2.0);
    62. glPopMatrix();
    63. glFlush();
    64. };
    65. void reshape(int w, int h) {
    66. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    67. glMatrixMode(GL_PROJECTION);
    68. glLoadIdentity();
    69. if (w <= h)
    70. glOrtho(-3.5, 3.5, -3.5 * (GLfloat)h / (GLfloat)w, 3.5 * (GLfloat)h / (GLfloat)w, -3.5, 3.5);
    71. else
    72. glOrtho(-3.5 * (GLfloat)w / (GLfloat)h, 3.5 * (GLfloat)w / (GLfloat)h, -3.5, 3.5, -3.5, 3.5);
    73. glMatrixMode(GL_MODELVIEW);
    74. glLoadIdentity();
    75. }
    76. int main(int argc, char** argv) {
    77. glutInit(&argc, argv);
    78. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    79. glutInitWindowSize(256, 256);
    80. glutInitWindowPosition(100, 100);
    81. glutCreateWindow(argv[0]);
    82. glutMouseFunc(mouseCB);
    83. glutMotionFunc(mouseMotionCB);
    84. init();
    85. glutDisplayFunc(display);
    86. glutReshapeFunc(reshape);
    87. glutMainLoop();
    88. return 0;
    89. }
    90. void mouseCB(int button, int state, int x, int y)
    91. {
    92. mouseX = x;
    93. mouseY = y;
    94. if (button == GLUT_LEFT_BUTTON)
    95. {
    96. if (state == GLUT_DOWN)
    97. {
    98. mouseLeftDown = true;
    99. }
    100. else if (state == GLUT_UP)
    101. mouseLeftDown = false;
    102. }
    103. else if (button == GLUT_RIGHT_BUTTON)
    104. {
    105. if (state == GLUT_DOWN)
    106. {
    107. mouseRightDown = true;
    108. }
    109. else if (state == GLUT_UP)
    110. mouseRightDown = false;
    111. }
    112. }
    113. void mouseMotionCB(int x, int y)
    114. {
    115. if (mouseLeftDown)
    116. {
    117. cameraAngleY += (x - mouseX);
    118. cameraAngleX += (y - mouseY);
    119. mouseX = x;
    120. mouseY = y;
    121. }
    122. glutPostRedisplay();
    123. }

    老样子,改出来的,问GPT

  • 相关阅读:
    【Spring Security】安全框架学习(一)
    Linux安装Anaconda(Anaconda3-2022.10-Linux-x86_64.sh版本)
    74 应急响应-win&linux分析后门&勒索病毒&攻击
    C++ 重载运算符和重载函数
    【面试题】CSS响应式
    使用代码产生标准的软件架构图之C4
    echarts 容器自适应
    凌恩客户文章|宏基因组揭示大蒜素对抗生素抗性基因的影响
    Ansible 快速入门
    【C++ Efficiency】理解虚函数、多重继承、虚基类和RTTI
  • 原文地址:https://blog.csdn.net/sss_brs/article/details/139628271