@[TOC](一步步学OpenGL(2) -《你好,顶点》–实战2)
参考资料:
准备库文件
GLEW官网:The OpenGL Extension Wrangler Library
最新版本在github,下载还更快。
Windows binaries for 32-bit and 64-bit.
解压后,可以执行 “D:\xxx\c\glew-2.2.0\bin\Release\Win32\glewinfo.exe”
生成一个glewinfo.txt,里面包含GLEW版本,像素格式,GL版本及各特性的支持情况
---------------------------
GLEW Extension Info
---------------------------
GLEW version 2.2.0
Reporting capabilities of pixelformat 7
Running on a AMD Radeon(TM) Graphics from ATI Technologies Inc.
OpenGL version 4.6.14761 Compatibility Profile Context 21.30.24.04 30.0.13024.4001 is supported
GL_VERSION_1_1: OK
---------------
GL_VERSION_1_2: OK
---------------
glCopyTexSubImage3D: OK
......
拷贝头文件 D:\xxx\c\glew-2.2.0\include\GL目录
到 D:\tool\strawberry-perl-5.32.1.1-64bit-portable\c\include
D:\XXX\C\GLEW-2.2.0\INCLUDE\GL
eglew.h
glew.h
glxew.h
wglew.h
拷贝库文件 D:\xxx\c\glew-2.2.0\lib\Release\Win32下的文件(命令行/dos窗口应该都是32位的,但是后面报错了,正确的是拷贝x64目录下的):glew32.lib和glew32s.lib
到 D:\tool\strawberry-perl-5.32.1.1-64bit-portable\c\lib
动态链接文件 D:\xxx\c\glew-2.2.0\bin\Release\Win32\glew32.dll (正确的是拷贝x64目录)
到C:\Windows\System32。
#include <GLUT/freeglut.h> // freeGLUT图形库
需要修改为
#include <GL/freeglut.h> // freeGLUT图形库
缺少库文件,先注释掉
//#include "ogldev_math_3d.h"
//Vector3f Vertices[1];
//Vertices[0] = Vector3f(0.0f, 0.0f, 0.0f);
//glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
编译报错 undefined reference to __imp_glewInit
g++.exe -c glut.cpp -o glut.o -I"D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/include" -I"D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/x86_64-w64-mingw32/include" -I"D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/lib/gcc/x86_64-w64-mingw32/8.3.0/include" -I"D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/lib/gcc/x86_64-w64-mingw32/8.3.0/include/c++"
g++.exe glut.o -o OpenGL.exe -L"D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/lib" -L"D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/x86_64-w64-mingw32/lib" -static-libgcc -lfreeglut -mwindows -lopengl32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: glut.o:glut.cpp:(.text+0x1b6): undefined reference to `__imp_glewInit'
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: glut.o:glut.cpp:(.text+0x1cd): undefined reference to `__imp_glewGetErrorString'
collect2.exe: error: ld returned 1 exit status
D:\xxx\c\devcppEnv\Makefile.win:25: recipe for target 'OpenGL.exe' failed
mingw32-make.exe: *** [OpenGL.exe] Error 1
看起来像32位的。
d:\xxx>mingw32-make.exe -v
GNU Make 3.82.90
Built for i686-pc-mingw32
Copyright (C) 1988-2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
参考案例:Undefined reference to `_imp__glewInit@0’
增加编译选项 -lglew32
新的报错:
g++.exe glut.o -o OpenGL.exe -L"D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/lib" -L"D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/x86_64-w64-mingw32/lib" -static-libgcc -lfreeglut -lglew32 -mwindows -lopengl32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/lib/glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../lib/glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/lib/glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/lib\glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../lib/glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../lib\glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../..\glew32.lib when searching for -lglew32
D:/tool/strawberry-perl-5.32.1.1-64bit-portable/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lglew32
collect2.exe: error: ld returned 1 exit status
D:\xxx\c\devcppEnv\Makefile.win:25: recipe for target 'OpenGL.exe' failed
mingw32-make.exe: *** [OpenGL.exe] Error 1
发现之前拷贝的glew32.lib文件所在目录,D:\tool\strawberry-perl-5.32.1.1-64bit-portable\c\lib
在上述ld的目录中。
注意提示的是 incompatible,不兼容,那拷贝64位的试试,编译通过;
但运行该文件提示不能启动,拷贝x64目录下的到C:\Windows\System32替换源文件,运行正常。
删除编译选项 -lglew32,则不能正常编译。
在https://ogldev.org/网站中点击 Get the source https://github.com/emeiri/ogldev
可以下载源码,包括ogldev_math_3d.h
不能直接点右键另存为,左键点击,然后拷贝保存。
该头文件还包含其他头文件,打包下载165M,把include拉出来。
折腾半天,把"ogldev_util.h"和"ogldev_math_3d.h",还有ogldev_types.h,放到和cpp同目录;把assimp目录拷贝到编译器的include目录下。
总算成功画了一个点。
总结:
拷贝x64目录下的文件,增加编译选项 -lglew32