//以下是我的程序,但是我想让画出的图形按规律旋转,但是它始终不变啊?怎么回事?// s.cpp
// Demonstrates a simple animated rectangle program with GLUT
// OpenGL SuperBible, 3rd Edition
// Richard S. Wright Jr.
// [email protected]#include "../../shared/gltools.h" // OpenGL toolkit#include "stdafx.h"
#include "s.h"
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
int i,j;
float n;
float t;
float tex[K];
float x,y,z,angle;
float dx,dy,dz,dangle;
float x00=0.0f;
float y00=0.0f;
float z00=0.0f;
float x01=0.5f;
float y01=0.0f;
float angle00=0;void myInit()
{
 glClearColor(1.0,1.0,1.0,1.0);
 glColor3f(250.0f,1.0f,1.0f);
 glPointSize(1.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
glOrtho(-5.0,5.0,-5.0,5.0,-5.0,5.0); }
void myDisplay1(void){
  glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(0.2f,0.5f,0.3f);
 glutSolidTeapot(200);
 glColor3f(0.5f,0.5f,0.5f);
 glutSolidTeapot(2); 
glRecti(0,0,1,1); 
glFlush();
 
}int main(int argc,char**argv)
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 glutInitWindowSize(640,480);
 glutInitWindowPosition(100,150);
 glutCreateWindow("my first attempt");
for (i=0;i<2;i++)
{
t=i/100;
x=t*t+2*t+0.03;
y=t+0.02;
z=t*t*3+0.2;
        angle=i*90;
    //计算增量
    dx=x-x00;
    dy=y-y00;
dz=z-z00;
    dangle=angle-angle00; glTranslatef(dx,dy,dz);//平移
        glRotatef(dangle,0.0f,0.0f,1.0f);//旋转
        glutDisplayFunc(myDisplay1);
        myInit();
for (j=0;j<400000000;j++)
{
}
} glutMainLoop();

解决方案 »

  1.   

    没仔细看
    不过你确定
    glRotatef(dangle,0.0f,0.0f,1.0f);//旋转
    这句执行到了并且dangle的值有变化么?
      

  2.   

    dangle的值是有变化的,但是没有执行旋转的功能
      

  3.   

    for (i=0;i<2;i++)
    循环改大点试试看,有可能是已经旋转了但是看不出来效果
      

  4.   

    //旋转问题解决了一点,但是新问题出来了,它在旋转,但是为什么它在循环完成以后它才画图呢?
    // s.cpp
    // Demonstrates a simple animated rectangle program with GLUT
    // OpenGL SuperBible, 3rd Edition
    // Richard S. Wright Jr.
    // [email protected]#include "../../shared/gltools.h" // OpenGL toolkit#include "stdafx.h"
    #include "s.h"
    #include <math.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <GL/glut.h>
    int i=0,j=0;
    float n=0;
    float m=0;
    float x,y,z,angle;
    float dx,dy,dz,dangle;
    float x00=0;
    float y00=0;
    float z00=0;
    float angle00=0;void myInit()
    {
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(250.0f,1.0f,1.0f);
    glPointSize(1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-5.0,5.0,-5.0,5.0,-5.0,5.0); 
    }void myDisplay1(void)
    {
    glTranslatef(dx,dy,dz);//平移
    glRotatef(dangle,0.0f,1.0f,1.0f);//旋转
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.2f,0.5f,0.3f);
    glutSolidTeapot(200);
    glColor3f(0.5f,0.5f,0.5f);
    glutSolidTeapot(2); 
    glRecti(0,0,1,1); 
    glFlush();
    }int main(int argc,char**argv)
    {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutCreateWindow("my first attempt");
    for (i=0;i<3;i++)
    {
    m=i*0.1;
    x=m*m+2*m+0.03;
    y=m+0.02;
    z=m*m*3+0.2;
            angle=i*27;
        //计算增量
        dx=x-x00;
        dy=y-y00;
    dz=z-z00;
        dangle=angle-angle00;
            printf("%f\t%f\t%f\t%f\n",dx,dy,dz,dangle);
    //画模型或者载入模型
            glutDisplayFunc(myDisplay1);
            myInit();
    //延时
    for (j=0;j<40000000;j++)
    {
    }
    } glutMainLoop();
      

  5.   

    和画图有关的函数都因该放到myDisplay1中
      

  6.   

    以下代码简单实现了你的需求
    你参考一下吧int nAngle = 0;void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(nAngle, 1.,0.,0.);
    glScalef (0.5, 0.5, 0.5);
    glColor3f(0.5f,0.5f,0.5f);
    glutSolidTeapot(2);
    nAngle += 2; glPopMatrix();
    glFlush();
    }void reshape(int w, int h)
    {
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0); glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef (0.0, 0.0, -5.0);
    }int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode (/*GLUT_SINGLE | */GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow(argv[0]);
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
    return 0; 
    }