未定义标识符 "glewInit"怎么解决
时间: 2023-10-03 11:05:41 浏览: 85
这个问题通常是由于缺少 GLEW 库导致的。GLEW 是一个开源的 C/C++ 库,用于管理 OpenGL 扩展。要解决这个问题,你需要确保正确安装了 GLEW 库并将其链接到你的项目中。
以下是一些可能的解决方法:
1. 安装 GLEW 库:你可以从 GLEW 的官方网站下载适用于你的操作系统的库,并按照其说明进行安装。
2. 链接 GLEW 库:在你的项目中,你需要将 GLEW 库链接到你的代码中。这可以通过在编译器的命令行参数中添加 `-lGLEW` 或在代码中添加 `#pragma comment(lib, "GLEW.lib")` 来实现。
3. 确保 GLEW 头文件正确包含:你需要在你的代码中包含 GLEW 头文件。这可以通过在代码中添加 `#include <GL/glew.h>` 来实现。
希望这些解决方法能帮助你解决这个问题。
相关问题
int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(512, 512); glutInitWindowPosition(300, 200); glutCreateWindow(argv[0]); GLenum err = glewInit(); if (GLEW_OK != err) { printf("出错!\r\n”); return 0; } init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMainLoop(); }请帮我逐行注释这段代码
好的,以下是代码的逐行注释:
```c++
int main(int argc, char** argv) { // 主函数,接收命令行参数
glutInit(&argc, argv); // 初始化 GLUT 库
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // 设置显示模式,使用 RGB 颜色模式,双缓冲和深度缓冲
glutInitWindowSize(512, 512); // 设置窗口大小为 512x512
glutInitWindowPosition(300, 200); // 设置窗口位置
glutCreateWindow(argv[0]); // 创建一个窗口,窗口名字为命令行参数的第一个字符串
GLenum err = glewInit(); // 初始化 GLEW 库,用于加载 OpenGL 的扩展函数
if (GLEW_OK != err) // 检查是否初始化成功
{
printf("出错!\r\n”); // 如果初始化失败,则输出错误信息
return 0; // 退出程序
}
init(); // 初始化 OpenGL 环境
glutDisplayFunc(display); // 设置窗口的显示回调函数
glutReshapeFunc(reshape); // 设置窗口的reshape回调函数
glutMouseFunc(mouse); // 设置窗口的鼠标回调函数
glutMainLoop(); // 进入 GLUT 的事件循环
}
```
#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(); }怎么修改
这段代码存在一些错误,需要进行修改,具体如下:
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) 来绘制圆形。
修改后的代码如下: