这是我第一次编写关于openGL的编码,我在使用所有函数时遇到了很多麻烦。这是我们老师给我们的一个程序,它应该没有任何错误或问题。我正在使用MinGW和最新版本的freeglut。我已经在环境变量中创建了路径,并且我还添加了所有库及其路径。
#define GLUT_DISABLE_ATEXIT_HACK
# include <windows.h>
# include <stdlib.h>
# include <GL/freeglut.h>
GLfloat vertices[][3] = {{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0}, {1.0, 1.0, -1.0}, {-1.0, 1.0, -1.0}, {-1.0, -1.0, 1.0}, {1.0, -1.0, 1.0}, {1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0}};
GLfloat colors[][3]= { {0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0} };
bool stop = false;
void face(int a, int b, int c, int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube()
{
face(0, 3, 2, 1);
face(2, 3, 7, 6);
face(0, 4, 7, 3);
face(1, 2, 6, 5);
face(4, 5, 6, 7);
face(0, 1, 5, 4);
}
static GLfloat theta[] = {0.0, 0.0, 0.0};
static GLint axis = 2;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 1.0, 0.0, 1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
void spinCube()
{
theta[axis] += 2.0;
if (theta[axis] > 360,0) theta[axis] -= 360;
glutPostRedisplay();
}
void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 'q' || key == 'Q') exit(0);
if (key == ' ') stop = !stop;
if (stop)
glutIdleFunc(NULL);
else
glutIdleFunc(spinCube);
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0,
2.0, -10.0, 10.0);
//if (w <= h)
// glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
// 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
// else
// glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
// 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("colorcube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
接下来是它给我的控制台中的错误,没有关于无法找到库的错误,只是变量未定义。
16:19:35 **** Incremental Build of configuration Debug for project spinningCube ****
Info: Internal Builder is used for build
g++ "-IC:\\MinGW\\freeglut\\include" "-IC:\\MinGW\\include" "-IC:\\MinGW\\lib\\gcc\\mingw32\\4.8.1" "-IC:\\MinGW\\mingw32\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\\main.cpp"
..\main.cpp: In function 'void spinCube()':
..\main.cpp:70:21: warning: left operand of comma operator has no effect [-Wunused-value]
if (theta[axis] > 360,0) theta[axis] -= 360;
^
g++ "-LC:\\MinGW\\freeglut\\lib\\x64\\Debug" -o spinningCube.exe main.o -lglu32 -lopengl32 -lfreeglut
main.o: In function `Z7displayv':
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:62: undefined reference to `_imp__glutSwapBuffers@0'
main.o: In function `Z8spinCubev':
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:71: undefined reference to `_imp__glutPostRedisplay@0'
main.o: In function `Z8keyboardhii':
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:86: undefined reference to `_imp__glutIdleFunc@4'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:88: undefined reference to `_imp__glutIdleFunc@4'
main.o: In function `main':
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:111: undefined reference to `_imp__glutInit@8'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:113: undefined reference to `_imp__glutInitDisplayMode@4'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:114: undefined reference to `_imp__glutInitWindowSize@8'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:115: undefined reference to `_imp__glutCreateWindow@4'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:116: undefined reference to `_imp__glutReshapeFunc@4'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:117: undefined reference to `_imp__glutDisplayFunc@4'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:118: undefined reference to `_imp__glutIdleFunc@4'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:119: undefined reference to `_imp__glutMouseFunc@4'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:120: undefined reference to `_imp__glutKeyboardFunc@4'
C:\Users\Josh Donckels\workspace\spinningCube\Debug/../main.cpp:122: undefined reference to `_imp__glutMainLoop@0'
collect2.exe: error: ld returned 1 exit status
16:19:36 Build Finished (took 1s.89ms)
答案 0 :(得分:0)
首先:你应该真正修复编译器警告。 ','不是c ++中的小数逗号。
对于链接器错误:您必须更改链接库的顺序。 freeglut必须在opengl32之前的列表中。
g++ "-LC:\\MinGW\\freeglut\\lib\\x64\\Debug" -o spinningCube.exe main.o -lfreeglut -lglu32 -lopengl32
应解决问题