当前位置 : 主页 > 编程语言 > c++ >

c – 使用OpenGL绘制随机点/星星?

来源:互联网 收集:自由互联 发布时间:2021-06-23
我试图绘制一些随机点作为窗口中的星星, 但积分没有显示出来.但其他对象正确显示. 我的源代码: #includewindows.h#include GL\glut.h#include math.h // For math routines (such as sqrt trig).GLfloat xRotated
我试图绘制一些随机点作为窗口中的星星,
但积分没有显示出来.但其他对象正确显示.

我的源代码:

#include<windows.h>
#include <GL\glut.h>
#include <math.h>      // For math routines (such as sqrt & trig).
GLfloat xRotated, yRotated, zRotated;
GLdouble  radius=3;

GLfloat qaBlack[] = {0.0, 0.0, 0.0, 1.0}; //Black Color
GLfloat qaGreen[] = {0.0, 1.0, 0.0, 1.0}; //Green Color
GLfloat qaWhite[] = {1.0, 1.0, 1.0, 1.0}; //White Color
GLfloat qaRed[] = {1.0, 0.0, 0.0, 1.0}; //Red Color

// Set lighting intensity and color

GLfloat qaSpecularLight[]    = {1.0, 1.0, 1.0, 1.0};
GLfloat emitLight[] = {0.9, 0.9, 0.9, 0.9};
GLfloat Noemit[] = {0.0, 0.0, 0.0, 1.0};

// Light source position

GLfloat qaLightPosition[] = {1, 1, 1, 1};

void display(void);
void reshape(int x, int y);

void idleFunc(void)
{
    if ( zRotated > 360.0 ) {
        zRotated -= 360.0*floor(zRotated/360.0);   // Don't allow overflow
      }

    if ( yRotated > 360.0 ) {
        yRotated -= 360.0*floor(yRotated/360.0);   // Don't allow overflow
      }
    zRotated += 0.05;
    yRotated +=0.01;

    display();
}

void initLighting()
{

    // Enable lighting
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glLightfv(GL_LIGHT0, GL_SPECULAR, qaSpecularLight);

}




void display(void){






    glMatrixMode(GL_MODELVIEW);
    // clear the drawing buffer.
     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    // clear the identity matrix.
    glLoadIdentity();



    glTranslatef(0.0,0.0,-40.0);

    glPushMatrix();

    glutSolidSphere(radius,25,25);

    glPopMatrix();



    glPopMatrix();


    glPushMatrix();

    glRotatef(yRotated,0.0,2.0,0.0);
    glTranslatef(5.0,0.0,0.0);

    // Set the light position
    glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emitLight);   // Make sphere glow (emissive)
    glutSolidSphere(radius/6,25,25);
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Noemit);

    glPopMatrix();

    glTranslatef(0.0,0.0,0.0);


    glPushMatrix();

    glColor3f(1.0, 1.0, 1.0);
    glPointSize(3);
    for(int i=1;i<100;i++){
         int x = rand()%640 ;
       int  y = rand()%480;
        glBegin(GL_POINTS);
        glVertex2i (x,y);
    glEnd();
    }

    glPopMatrix();

    glLoadIdentity();

    glColor3f(1.0, 1.0, 1.0);
    glPointSize(3);
    for(int i=1;i<100;i++){
int          x = rand()%640 ;
    int     y = rand()%480;
        glBegin(GL_POINTS);
        glVertex2i (x,y);
    glEnd();
    }


    glFlush(); //FOR RENDERING
    glutSwapBuffers();
}

void reshape(int x, int y){
    if(y == 0 || x == 0) return;
    glMatrixMode(GL_PROJECTION);

    gluPerspective(20.0,(GLdouble)x/(GLdouble)y,0.6,40.0);
    glMatrixMode(GL_MODELVIEW);
    glViewport(0,0,x,y);  //Use the whole window for rendering
}


int main (int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    glutInitWindowSize(1000,600);
    glutCreateWindow("Project_KD");
    initLighting();

    xRotated = yRotated = zRotated = 0.0;

    glutIdleFunc(idleFunc);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}
很多不满:

>无法匹配的glPushMatrix()/ glPopMatrix()调用的几个实例;避免矩阵堆过度/不足;我喜欢使用额外的范围来直观地指示矩阵堆栈嵌套.
>您的点图是假设正投影,而您只设置了透视投影.
>在尝试绘制点时,您启用了灯光,除了左下角之外,所有地方都会产生非常暗的点.
>由于某种原因,你的点绘制循环是重复的;如果你想要双倍的星星调整for循环结束值而不是复制粘贴循环.
>您应该在空闲回调中使用glutPostRedisplay()而不是直接调用display().
>每次通过display()设置投影/模型视图矩阵,而不是在调整大小回调中设置它们;有助于减少神秘矩阵错误的来源.默认的调整大小回调为您调用glViewport(),因此您不必担心这样做.
>你在3D球体之后绘制了点(“星星”?);我认为其意图是在它们“下面”之前绘制它们.
>代码格式的邪恶混杂;建议像clang-format这样的东西来控制它.
>如果您在Windows上使用FreeGLUT(您应该是它;它实际上是唯一保留的GLUT实现),您不需要#include< Windows.h>.
>建议使用计时器回调而不是空闲回调来更新模拟/动画.没有vsync,将经常调用空闲回调.通过计时器回调,您可以模拟vsync’d系统为您提供的~16毫秒帧.

全部一起:

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

GLfloat xRotated, yRotated, zRotated;
GLdouble radius = 3;

void timer( int value )
{
    if( zRotated > 360.0 )
    {
        zRotated -= 360.0 * floor( zRotated / 360.0 ); // Don't allow overflow
    }

    if( yRotated > 360.0 )
    {
        yRotated -= 360.0 * floor( yRotated / 360.0 ); // Don't allow overflow
    }

    zRotated += 5.0;
    yRotated += 1.0;

    glutTimerFunc( 16, timer, 0 );
    glutPostRedisplay();
}

void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glDepthMask( GL_FALSE );
    glDisable( GL_DEPTH_TEST );
    glDisable( GL_LIGHTING );

    // 2D rendering
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, 640, 0, 480, -1, 1 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glPushMatrix();
    {
        glColor3f( 1.0, 1.0, 1.0 );
        glPointSize( 3 );
        glBegin( GL_POINTS );
        for( int i = 1; i < 100; i++ )
        {
            int x = rand() % 640;
            int y = rand() % 480;
            glVertex2i( x, y );
        }
        glEnd();
    }
    glPopMatrix();


    glDepthMask( GL_TRUE );
    glEnable( GL_DEPTH_TEST );

    // Enable lighting
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    GLfloat qaSpecularLight[] = {1.0, 1.0, 1.0, 1.0};
    glLightfv( GL_LIGHT0, GL_SPECULAR, qaSpecularLight );

    // 3D rendering
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    gluPerspective( 20.0, w / h, 0.1, 80.0 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0.0, 0.0, -40.0 );

    glPushMatrix();
    {
        glutSolidSphere( radius, 25, 25 );
    }
    glPopMatrix();

    glPushMatrix();
    {
        glRotatef( yRotated, 0.0, 2.0, 0.0 );
        glTranslatef( 5.0, 0.0, 0.0 );
        GLfloat qaLightPosition[] = {1, 1, 1, 1};
        glLightfv( GL_LIGHT0, GL_POSITION, qaLightPosition );
        GLfloat emitLight[] = {0.9, 0.9, 0.9, 0.9};
        glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, emitLight ); // Make sphere glow (emissive)
        glutSolidSphere( radius / 6, 25, 25 );
        GLfloat Noemit[] = {0.0, 0.0, 0.0, 1.0};
        glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, Noemit );
    }
    glPopMatrix();

    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    glutInitWindowSize( 1000, 600 );
    glutCreateWindow( "Project_KD" );

    xRotated = yRotated = zRotated = 0.0;

    glutDisplayFunc( display );
    glutTimerFunc( 0, timer, 0 );
    glutMainLoop();
    return 0;
}
网友评论