gpt4 book ai didi

c++ - 具有两个循环的OpenGL,一个循环不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:08 26 4
gpt4 key购买 nike

我的 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

关于c++ - 具有两个循环的OpenGL,一个循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25082903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com