#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 16:06:09 浏览: 103
duojikongzhi.rar_Unsigned_c++include <reg51>
这段代码存在一些错误,需要进行修改,具体如下:
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) 来绘制圆形。
修改后的代码如下:
阅读全文