opengl怎么显示出旋转的角度值?

时间:2022-09-10 18:49:17
#include <string>
#include <iostream>
#include <strstream>

#include <gl\glut.h>
#include <gl\gl.h>
#include <gl\glaux.h>

using namespace std;


 int g_WindowWidth = 800;
 int g_WindowHeight = 500;
 string s;
 char ch[2];
 strstream ss;

static int spin=0;
void init(void)
{
 glClearColor(0.0,0.0,0.0,0.0);
 glShadeModel(GL_SMOOTH);
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
 glEnable(GL_DEPTH_TEST);
}
 // 在屏幕上显示文本,x 和 y 为屏幕坐标
 void DrawText(const char* text, float x, float y)
  {
    // 检查OpenGL状态
     bool isDepthOpen = false;
     bool isStencilOpen = false;
     bool isLightOpen = false;
     bool isFogOpen = false;
     
      if(glIsEnabled(GL_DEPTH_TEST)) 
  {
        isDepthOpen = true;
         glDisable(GL_DEPTH_TEST);
      }
     if(glIsEnabled(GL_STENCIL_TEST))
     {
         isStencilOpen = true;
         glDisable(GL_STENCIL_TEST);
     }
     if(glIsEnabled(GL_LIGHTING)) 
     {
         isLightOpen = true;
         glDisable(GL_LIGHTING);
     }
     if(glIsEnabled(GL_FOG))
     {
         isFogOpen = true;
         glDisable(GL_FOG);
     }
    
     int font = (int)GLUT_BITMAP_8_BY_13;
 
    // 设置字体颜色
     glColor3f(1.0, 1.0, 0.0);
     
     /*
      * 设置正投影
      */
 
     glMatrixMode(GL_PROJECTION);
 
     // 保存当前投影矩阵
     glPushMatrix();
 
         glLoadIdentity();
         gluOrtho2D( 0, g_WindowWidth, 0, g_WindowHeight );
 
         // 反转Y轴(朝下为正方向)(与窗口坐标一致)
         glScalef(1, -1, 1);
         // 将原点移动到屏幕左上方(与窗口坐标一致)
         glTranslatef(0, -g_WindowHeight, 0);
         glMatrixMode(GL_MODELVIEW);
         
         // 保存当前模型视图矩阵
         glPushMatrix();
             glLoadIdentity();
             
             // 设置文字位置
             glRasterPos2f( x, y );
             
             // 依次描绘所有字符(使用显示列表)
             for(const char* c = text; *c != '\0'; c++)
             {
                 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
             }
 
         // 恢复之前保存的模型视图矩阵
         glPopMatrix();
         
         glMatrixMode(GL_PROJECTION);
    
     // 恢复之前保存的投影矩阵
     glPopMatrix();
 
     // 返回模型视图矩阵状态
     glMatrixMode(GL_MODELVIEW);
     
     // 恢复OpenGL状态
     if(isDepthOpen) 
     {
         glEnable(GL_DEPTH_TEST);
     }
     if(isStencilOpen)
     {
         glEnable(GL_STENCIL_TEST);
     }
     if(isLightOpen) 
    {
        glEnable(GL_LIGHTING);
     }
     if(isFogOpen)
     {
         glEnable(GL_FOG);
     }
 }
 
void display(void)
{
 
    // ss<<spin;
    // ss>>s; 
  
    ch=(char)spin;//???怎么修改一下,显示出(spin的值)旋转的角度
GLfloat position[]={0.0,0.0,1.5,1.0};
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
float x = 50.0f;
    float y = 20.0f;

    DrawText(ch, x, y);

glPushMatrix();
glTranslatef(0.0,0.0,-5.0);
glPushMatrix();
glRotated((GLdouble)spin,1.0,0.0,0.0);
glLightfv(GL_LIGHT0,GL_POSITION,position);
glTranslated(0.0,0.0,1.5);
glDisable(GL_LIGHTING);
glColor3f(0.0,1.0,1.0);
glutWireCube(0.1); //绘制实心立方体或线框立方体
glEnable(GL_LIGHTING);
glPopMatrix();

glutSolidTorus(0.275,0.85,18,15);
glPopMatrix();
glFlush();
}
void reshape(int w,int h)
{
 glViewport(0,0,(GLsizei)w,(GLsizei)h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(40.0,(GLfloat)w/(GLfloat)h,1.0,20.0);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}
void mouse(int button,int state,int x,int y)//鼠标函数
{
 switch(button)
 {
 case GLUT_LEFT_BUTTON:  //按下左键
if(state==GLUT_DOWN)
 {
  spin=(spin+30)%360;
  glutPostRedisplay(); //位置重置
 }
 break;
 default:
 break;
 }
}

int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(50,50);
glutCreateWindow("移动光源");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

2 个解决方案

#1


char ch[10];//长度多定义点,2太短了!
sprintf(ch,"%d",spin);代替 ch=(char)spin;

#2


现在可以了,谢谢!

#1


char ch[10];//长度多定义点,2太短了!
sprintf(ch,"%d",spin);代替 ch=(char)spin;

#2


现在可以了,谢谢!