OpenGL有两个循环,一个循环不起作用
OpenGL with two loops, one loop is not working
我的OpenGL程序不能正常工作。在drawScene()
函数中,我创建了两个循环。一个环是GL_LINES
,另一个环是GL_POINTS
。GL_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++-字符串是否包含一个带有简单循环的单词
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 使用一个循环与两个循环
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- 使用一个 for 循环如何迭代和测试迭代器值?
- 我正在尝试创建一个菜单,但我的代码一直在循环
- 转到基于范围的 for 循环中的下一个迭代器
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 如何循环 n 次,在 xml 文件中每个循环一个级别
- 初始化时 num 的哪个值会给这个嵌套循环一个"true"输出?
- 两次相同的 for 循环:一个编译,另一个不编译
- 如何在C 中循环一个语句
- 循环一个级数
- 如何在c++中循环一个函数
- 循环一个精灵以显示不止一次
- C++循环一个菜单,直到选择了所有选项
- QT:如何每秒循环一个方法?C++
- 循环一个mpl::map