如何旋转这个 openGl 代码 [英] how to rotate this openGl code

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

问题描述

在这段代码中,我尝试绘制简单的奥运戒指并旋转它......下面的工作正常,但我无法旋转戒指......帮助我解决这个问题......

in this code i'm try to draw simple olympic ring and rotate it... the below work fine but i can't rotate the rings.. help me to solve this problme...

void myReshape (int width, int height)
{
  glViewport (0, 0, width, height);    
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D (-5, 105, -5, 105);
  glMatrixMode (GL_MODELVIEW);
  glLoadIdentity ();
  glTranslatef (0.375, 0.375, 0.0);
}


int main (int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
  glutInitWindowPosition(100,100); 
  glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE);
  glutCreateWindow ("Olymipc Rings  ||  rotation  "); 
  glClearColor(1.0, 1.0, 1.0, 0.0);  
  glPointSize(PIXEL_SIZE);          
  glShadeModel (GL_FLAT);       
  glutDisplayFunc(display);
  glutReshapeFunc(myReshape);
  glutMainLoop(); 
  return 0; 
}

推荐答案

试试这个:

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

#define PIXEL_SIZE 3 
#define MESSAGE     "hello world !"

void draw_circle(int x, int y, int r);

int ring_radius = 19; 
int color[5][3]={{0,0,1}, {0,0,0},{1,0,0}, {1,1,0},{0,1,0}};
int center[5][2]={{15,60},{50,60},{85,60},{33,45},{68,45}};
//=========================================================

void drawText(const char * message)
{
    glRasterPos2f((GLfloat)0, (GLfloat)-400);
    while (*message) 
    {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *message);
        glutStrokeCharacter(GLUT_STROKE_ROMAN,*message++);
    }
}

void display (void) 
{
    int ms = glutGet(GLUT_ELAPSED_TIME);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D (-5, 105, -5, 105);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    glTranslatef (0.375, 0.375, 0.0);

    glClear(GL_COLOR_BUFFER_BIT); 
    glBegin(GL_LINE_LOOP);
    glVertex2i(-1,-1);
    glVertex2i(100,-1);
    glVertex2i(100,100);
    glVertex2i(-1,100);
    glEnd();

    const float deg_per_sec = 60.0f;
    float angle = deg_per_sec * ( (float)ms / 1000.0f );

    for(int i = 0 ; i <5; i++)
    {
        glColor3f(color[i][0],color[i][1],color[i][2]);

        glPushMatrix();
        glTranslatef( center[i][0], center[i][1], 0 );
        glRotatef(angle, 0, 0, 1);
        glTranslatef( -center[i][0], -center[i][1], 0 );
        draw_circle(center[i][0],center[i][1],ring_radius);
        glPopMatrix();
    }
    glScalef(0.001, 0.001, 0.001);
    drawText(MESSAGE);
    glFlush();             

    glutSwapBuffers();
}

void draw_circle(int center_x, int center_y , int radius)
{
    int r = radius;
    int h = 1 - r ;    /*initialization */
    int x = 0;
    int y = r;
    int dU=3;
    int dD = 5 - 2*r;
    int i = center_x;
    int j = center_y;

    glPointSize(6);
    glBegin(GL_POINTS);

    while( y > x )
    {
        if (h<0)
        {
            dU= dU + 2;
            h = h + dU;
            x = x + 1;
            dD = dD + 2;
        }
        else
        {
            dD = 2*(x-y) + 5;
            h = h + dD;
            x = x + 1;
            y = y - 1;
            dU = dU + 2;
            dD = dD + 4;
        }

        glVertex2i(x+i, y+j);
        glVertex2i(-x+i, y+j);
        glVertex2i(x+i, -y+j);
        glVertex2i(-x+i,-y+j);
        glVertex2i(y+i, x+j);
        glVertex2i(y+i, -x+j);
        glVertex2i(-y+i, x+j);
        glVertex2i(-y+i, -x+j);

    }

    glEnd();
} 

void myReshape (int width, int height)
{
    glViewport (0, 0, width, height);    
}

void idle()
{
    glutPostRedisplay();
}


int main (int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowPosition(100,100); 
    glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE);
    glutCreateWindow ("Olymipc Rings  ||  rotation  "); 
    glClearColor(1.0, 1.0, 1.0, 0.0);  
    glPointSize(PIXEL_SIZE);          
    glShadeModel (GL_FLAT);       
    glutDisplayFunc(display);
    glutReshapeFunc(myReshape);
    glutIdleFunc(idle);
    glutMainLoop(); 
    return 0; 
}

这篇关于如何旋转这个 openGl 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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