OpenGL有两个循环,一个循环不起作用

OpenGL with two loops, one loop is not working

本文关键字:循环 一个 不起作用 OpenGL 两个      更新时间:2023-10-16

我的OpenGL程序不能正常工作。在drawScene()函数中,我创建了两个循环。一个环是GL_LINES,另一个环是GL_POINTSGL_LINES循环工作良好,但GL_POINTS循环不工作。谢谢你的帮助。

#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h> 
#define PI 3.14159265
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
    switch (key) {
        case 27: //Escape key
            exit(0);
    }
}
//Initializes 3D rendering
void initRendering() {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL); //Enable color
    glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //Change the background to sky blue
}
//Called when the window is resized
void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = 0.0f;
float _cameraAngle = 0.0f;
float x = -1.5;
float y = -0.5;
//Draws the 3D scene
void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glLineWidth (9.0f);
    glBegin(GL_LINES);
    glColor3f(1.0f, 0.0f, 0.0f);
    double r = 0.5;
    for(int c = 0;c<=360;c++){
        double y = r*cos (c*PI/180);
        double x = r*sin (c*PI/180);
        glVertex3d(x,y,-5.0);
        glVertex3d(0.0,0.0,-5.0);
    }
    glEnd();

    glPointSize (7.0f);
    glBegin(GL_POINTS);
    glColor3f(0.0f, 1.0f, 0.0f);
    double r = 1.0;
    for(int c = 0;c<=360;c++){
        double y = r*cos (c*PI/180);
        double x = r*sin (c*PI/180);
        glVertex3d(x,y,-5.0);
        //glVertex3d(0.0,0.0,-5.0);
    }
    glEnd();
    glutSwapBuffers();
    //glutPostRedisplay();
}
void update(int value) {
    _angle += 2.0f;
    if (_angle > 360) {
        _angle -= 360;
    }
    glutPostRedisplay();
    glutTimerFunc(60, update, 0);
}
int main(int argc, char** argv) {
    //Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);
    //Create the window
    glutCreateWindow("two circle");
    initRendering();
    //Set handler functions
    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25, update, 0); //Add a timer
    glutMainLoop();
    return 0;
}

现在,您在两个地方声明变量r:您设置double r = 0.5;double r = 1.0;

试试改成:

double r = 0.5;
//First loop
r = 1.0;
//Second loop