本文介绍了在opengl中旋转时闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码

#include "stdafx.h"
#include <GL/glut.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <math.h>
#define pi 3.14;
float angle=3;
void reshape(int w, int h)
{
if(h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1,1);

}
void display(void)
{
    glDepthMask(GL_TRUE);
    glClear(GL_COLOR_BUFFER_BIT);
    glRotatef(angle++,0,1.0,1.0);
    glBegin(GL_POLYGON);
    glVertex2f(10,10);
    glVertex2f(30,20);
    glVertex2f(30,30);
    glVertex2f(10,30);
    glEnd();
    glRotatef(angle++,0,1.0,1.0);
    glBegin(GL_TRIANGLE_FAN);
        glVertex2f(300,300);
        int segments=20;
        GLfloat angle=0;
        for(int i=0;i<=segments; i++)
        {
            angle=i*2*pi; angle=angle / (segments);
            glVertex2f(300+cos(angle)*30,300+sin(angle)*30);

        }
        glEnd();

    glFlush();
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc , argv);
    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
    glutInitWindowSize(640,480);
    glutCreateWindow("first");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(display);
    glutMainLoop();
    return(0);
}

这里的问题是


  1. 只是闪烁矩形,无法正确查看旋转。不知道它是否旋转。

2.即使有旋转代码,圆也不旋转。为什么?

2.the circle is not rotating even there is code for rotation. why?

3.如果我只想旋转矩形或圆形,我做了哪些更改?

3.what change I made if I want only rectangle or circle is rotated?

推荐答案

你可能运行waaaaay太快。使用计时器回调每16ms左右发出 glutPostRedisplay()

You're probably running waaaaay too fast. Use a timer callback to issue glutPostRedisplay()s every 16ms or so.



glRotatef(angle++,0,1.0,1.0);
                    ^^^

您正在X / Y平面外旋转并超出深度您的 glOrtho()调用的限制导致剪辑的片段。将 nearVal / 以容纳:

You're rotating outside the X/Y plane and exceeding the depth limits of your glOrtho() call resulting in clipped fragment. Bump your nearVal/farVal to accommodate:

glOrtho(0,w,0,h,-1000,1000);



删除 glRotatef )

共同:

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

using namespace std;

const double pi = 3.14159;

float angle=3;
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    glOrtho(-w,w,-h,h,-1000,1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glDepthMask(GL_TRUE);

    //glRotatef(angle++,0,1.0,1.0);
    glBegin(GL_POLYGON);
    glVertex2f(10,10);
    glVertex2f(30,20);
    glVertex2f(30,30);
    glVertex2f(10,30);
    glEnd();

    glRotatef(angle++,0,1.0,1.0);
    glBegin(GL_TRIANGLE_FAN);
    glVertex2f(300,300);
    const int segments=20;
    for(int i=0;i<=segments; i++)
    {
        const GLfloat angle = ( i*2*pi ) / (float)segments;
        glVertex2f(300+cos(angle)*30,300+sin(angle)*30);
    }
    glEnd();

    glutSwapBuffers();
}

void timer( int value )
{
    glutPostRedisplay();
    glutTimerFunc( 16, timer, 0 );
}

int main(int argc, char** argv)
{
    glutInit(&argc , argv);
    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
    glutInitWindowSize(640,480);
    glutCreateWindow("first");
    glutDisplayFunc(display);
    glutTimerFunc( 0, timer, 0 );
    glutMainLoop();
    return(0);
}

这篇关于在opengl中旋转时闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:59