旋转5圈的问题 [英] rotate 5 circle problem

查看:587
本文介绍了旋转5圈的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  如何旋转这个OpenGL的code…

下面是我的code,它做工精细..我画的这个5环。

below is my code and it work fine.. i draw 5 ring in this.

#include <GL/glut.h>
#include <math.h>

static void redraw(void);
#define PI 3.14159265 
#define EDGES 90
#define factor 10


void display (void) 
{
    glClearColor(1.0,1.0,1.0,0.0);
     glMatrixMode (GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();   


//ring1 
glPushMatrix();
glColor3f (0.0, 0.0 ,1.0);
glTranslatef(-30.0,10.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

//ring2
glPushMatrix();
glColor3f (0.0, 0.0, 0.0);
glTranslatef(-8.0,10.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

//ring3
glPushMatrix();
glColor3f (1.0, 0.0 ,0.0);
glTranslatef(14.0,10.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

//ring4
glPushMatrix();
glColor3f (1.0, 1.0, 0.0);
glTranslatef(-19.0,-2.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

//ring5
glPushMatrix();
glColor3f (0.0, 1.0, 0.0);
glTranslatef(4.0,-2.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

}

static void redraw(void)
{
for (int i = 0; i < EDGES; i++)
    { 
      glBegin(GL_LINE_LOOP); 
      glVertex2f(factor*cos((2*PI*i)/EDGES),factor*sin((2*PI*i)/EDGES)); 
      glVertex2f(factor*cos((2*PI*(i+1))/EDGES),factor*sin((2*PI*(i+1))/EDGES)); 
      glEnd();
     } 
}

int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(110*3, 110*3);
glutCreateWindow("draw circle");
glPointSize(3); 
glShadeModel (GL_FLAT); 
glutDisplayFunc(display);
glMatrixMode(GL_PROJECTION);    
gluPerspective(45,1.0,10.0,200.0);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0; 
}

所以我要旋转所有这个圈子..我该怎么办?

so i want to rotate all of this circle.. how can i do??

推荐答案

您应该在绘图函数中调用glutSwapBuffers只有一次。

You should call glutSwapBuffers only once in the drawing function.

有关的动画必须调用glutPostRedisplay,以保证显示功能将被再次调用。

For an animation you have to call glutPostRedisplay, to ensure that the display function will be called again.

该在glBegin和glEnd调用只需要调用一次每循环。在fo​​r循环中,你只需提供顶点调用glVertex。

The glBegin and glEnd calls only have to be called once per circle. In the for loop you just supply vertices with calls to glVertex.

要简化时,ESC键是pressed调试我安装的键盘处理程序,关闭窗口。

To simplify debugging I installed a keyboard handler that closes the window when the escape key is pressed.

 // compile in linux: gcc ogl.c -lglut -lGLU

#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>

static void redraw(void);
#define PI 3.14159265 

enum{
  EDGES=90,
};

static void 
circle(float radius)
{
  int i;
  glBegin(GL_LINE_LOOP); 
  for (i = 0; i < EDGES; i++){ 
    glVertex2f(radius*cos((2*PI*i)/EDGES),
           radius*sin((2*PI*i)/EDGES)); 
    glVertex2f(radius*cos((2*PI*(i+1))/EDGES),
           radius*sin((2*PI*(i+1))/EDGES)); 
  } 
  glEnd();
}


int count=0;
void display (void) 
{
  float r=10;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();   

  glRotated(count,0,0,1);
  count++;
  if(count>360)
    count=0;

  glPushMatrix();
  // ring1
  glColor3f (0.0, 0.0 ,1.0);
  glTranslatef(-30.0,10.0,-100.0);
  circle(r);
  glPopMatrix();

  glPushMatrix();
  // ring2
  glColor3f (0.0, 0.0, 0.0);
  glTranslatef(-8.0,10.0,-100.0);
  circle(r);
  glPopMatrix();

  glPushMatrix();
  //ring3
  glColor3f (1.0, 0.0 ,0.0);
  glTranslatef(14.0,10.0,-100.0);
  circle(r);
  glPopMatrix();

  glPushMatrix();
  //ring4
  glColor3f (1.0, 1.0, 0.0);
  glTranslatef(-19.0,-2.0,-100.0);
  circle(r);
  glPopMatrix();

  glPushMatrix();
  //ring5
  glColor3f (0.0, 1.0, 0.0);
  glTranslatef(4.0,-2.0,-100.0);
  circle(r);
  glPopMatrix();

  glutSwapBuffers();
  glutPostRedisplay();
}

static void 
keyb(unsigned char key, int x, int y)
{
  switch (key) {
  case 27:  /* Escape key */
    exit(0);
  }
}


void
init()
{
  glPointSize(3); 
  glShadeModel (GL_FLAT); 
  glMatrixMode(GL_PROJECTION);    
  gluPerspective(45,1.0,10.0,200.0);
  glMatrixMode(GL_MODELVIEW);
  glClearColor(1.0,1.0,1.0,0.0);
  glLineWidth(2);
}

int
main(int argc, char **argv)
{
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowPosition(100,100);
  glutInitWindowSize(110*3, 110*3);
  glutCreateWindow("draw circle");
  glutDisplayFunc(display);
  glutKeyboardFunc(keyb);
  init();
  glutMainLoop();
  return 0; 
}

这篇关于旋转5圈的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆