#define _USE_MATH_DEFINES #include <cstdlib> #include <cmath> #include <iostream> #include <GL/glew.h> #include <GL/freeglut.h> // Globals. static float R = 40.0; // Radius of circle. static float X = 50.0; // X-coordinate of center of circle. static float Y = 50.0; // Y-coordinate of center of circle. static const int numVertices = 50; // Number of vertices on circle. static int verticesColors[6 * numVertices]; void generateVertices() { float t = 0; // Angle parameter. for (int i = 0; i < 6*numVertices; i+=6) { verticesColors[] = X + R * cos(t); //x verticesColors[] = Y + R * sin(t); //y verticesColors[] = 0.0; //z verticesColors[] = 1.0; //r verticesColors[] = 0.0; //g verticesColors[] = 0.0; //b t += 2 * M_PI / numVertices; //angle } } // Drawing routine. void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1, 0, 0); glLineWidth(5); glDrawArrays(GL_LINE_LOOP, 0, 50); glFlush(); } // Initialization routine. void setup(void) { glClearColor(1.0, 1.0, 1.0, 0.0); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), &verticesColors[0]); glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), &verticesColors[3]) } // OpenGL window reshape routine. void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } // Keyboard input processing routine. void keyInput(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; default: break; } } // Main routine. int main(int argc, char** argv) { generateVertices(); glutInit(&argc, argv); glutInitContextVersion(4, 3); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("circle.cpp"); glutDisplayFunc(drawScene); glutReshapeFunc(resize); glutKeyboardFunc(keyInput); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }怎么修改
时间: 2023-08-08 11:06:09 浏览: 67
这段代码存在一些错误,需要进行修改,具体如下:
1. 在函数 generateVertices 中,数组 verticesColors[] 的下标没有被正确设置,应该是 verticesColors[i]。
2. 在函数 setup 中,第二个 glEnableClientState(GL_COLOR_ARRAY) 应该是 glEnableClientState(GL_COLOR_ARRAY);。
3. 在函数 setup 中,第一个 glVertexPointer 函数应该是 glColorPointer。
4. 在函数 drawScene 中,应该使用 glDrawArrays(GL_POLYGON, 0, numVertices) 来绘制圆形。
修改后的代码如下:
相关问题
补充代码#include<iostream> #include math.h #define EPS 1E-16 #define PI 3.14159265358979323846 #define _2P
这段代码是C++中的一些常用头文件和宏定义的补充代码。让我逐一介绍一下:
1. `#include<iostream>`:这是C++标准库中的头文件,用于输入输出操作,例如`cout`和`cin`。
2. `#include math.h`:这是C标准库中的头文件,用于数学运算,例如三角函数、指数函数等。
3. `#define EPS 1E-16`:这是一个宏定义,将`EPS`定义为一个非常小的数值,用于比较浮点数的精度。
4. `#define PI 3.14159265358979323846`:这是一个宏定义,将`PI`定义为圆周率π的近似值。
5. `#define _2P`:这是一个未完成的宏定义,缺少后续的代码。
如果你有具体的问题或者需要了解更多关于这段代码的信息,请告诉我。
#include<math.h> #include<process.h> #include <iostream> #define _CRT_SECURE_NO_WARNINGS //#include<stdio.h>这几个头文件分别是什么作用
1. #include<math.h>:这个头文件包含了数学运算的函数和常量,如三角函数、对数函数、指数函数以及常用的数学常量等。
2. #include<process.h>:这个头文件包含了一些与进程(process)相关的函数,如exit()函数用于终止进程、getpid()函数用于获取进程ID等。
3. #include<iostream>:这个头文件包含了输入输出流的相关函数和类,如cout和cin等。
4. #define _CRT_SECURE_NO_WARNINGS:这个宏定义可以用来禁用一些不安全的C/C++函数,如scanf、gets等。在VC++中,如果不使用该宏定义,编译器会给出警告。
5. //#include<stdio.h>:这行代码是一个注释,表示这个头文件被注释掉了,没有被包含进来。stdio.h是一个标准输入输出头文件,包含了一些输入输出函数的定义,如printf、scanf等。