OpenGL是跨平台的图形库,这意味着不管是Windows、Macintosh还是Linux下,由它编写的代码都能正确执行,OpenGL提供绘制图形的API,是进阶学习虚幻引擎的重要组成部分。

VSCode配置OpenGL走过不少弯路,写成博客的方式记录一下配置步骤以免重新配置的时候又得到处找博客教程。

以前我用的编译器是gcc,而现在改用clang,编译起来感觉clang更快,出错提示也更友好,如果你不想改用clang编译,那么就需要另找教程配置了。

LLVM下载:https://releases.llvm.org/download.html

vscode tortoisesvn配置 vscode配置glut_opengl

freeglut下载:https://www.transmissionzero.co.uk/software/freeglut-devel/

vscode tortoisesvn配置 vscode配置glut_opengl_02

MinGW下载:

这里不推荐使用mingw-get-setup.exe,而是用MinGW-online installer,因为随着mingw-get-setup.exe官网版本的更新,以前的mingw-get-setup.exe版本获取不了mingw的下载资源


windows导向配置:

vscode tortoisesvn配置 vscode配置glut_json_03

抓取资源较慢,但胜在稳定,几乎不会掉档:

vscode tortoisesvn配置 vscode配置glut_opengl_04

在原来的freeglut下的include文件夹下找到glut.h放到LLVM的include文件夹下,或者在include目录下创建GL文件夹,放到GL文件夹里面,因为很多引用都是#include<GL\glut.h>,

所以我们这个时候以保万全,两个目录下都放一个吧。

另外编译时还需要引用到动态连接库和lib文件,我们将glut32.dll文件放到LLVM\bin目录下面,glut32.lib文件放到LLVM\lib目录下

vscode tortoisesvn配置 vscode配置glut_json_05

vscode tortoisesvn配置 vscode配置glut_c++_06

至此还需要配置环境变量,将LLVM\bin加入到系统的环境变量:

vscode tortoisesvn配置 vscode配置glut_opengl_07

打开cmd输入clang -v和gcc -v,出现这些就说明大功告成了:

vscode tortoisesvn配置 vscode配置glut_LLVM_08

接下来是.vscode的各项配置,把我能用的贴出来,具体路径根据本机环境修改:

c_cpp_properties.json:

{
    "configurations": [
      {
        "name": "MinGW",
        "intelliSenseMode": "clang-x64",
        "compilerPath": "D:/LLVM/bin/gcc.exe",
        "includePath": [
          "${workspaceFolder}"
        ],
        "defines": [],
        "browse": {
          "path": [
            "${workspaceFolder}"
          ],
          "limitSymbolsToIncludedHeaders": true,
          "databaseFilename": ""
        },
        "cStandard": "c11",
        "cppStandard": "c++17"
      }
    ],
    "version": 4
}

launch.json:

{
  "version": "0.2.0",
  "configurations": [
      {
          "name": "(Windows) Launch",
          "type": "cppvsdbg",
          "request": "launch",
          "program": "enter program name, for example ${workspaceFolder}/a.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": true
      },
      {
          "name": "(Windows) Attach",
          "type": "cppvsdbg",
          "request": "attach",
          "processId": "${command:pickProcess}"
      },
    {
      "name": "(gdb) Launch", 
      "type": "cppdbg", 
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}.exe", 
      "args": [], 
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true, 
      "internalConsoleOptions": "neverOpen",
      "MIMode": "gdb",
      "miDebuggerPath": "D:/LLVM/bin/gdb.exe", 
      "setupCommands": [ 
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": false
        }
      ],
      "preLaunchTask": "Compile" 
    }
  ]
}

settings.json:

{
  "editor.formatOnType": true, 
  "editor.snippetSuggestions": "top",
  "code-runner.runInTerminal": true,
  "code-runner.executorMap": {
    "c": "cd $dir && clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c11 && $dir$fileNameWithoutExt",
    "cpp": "cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -lglut32 -lglu32 -lopengl32 -std=c++17 && $dir$fileNameWithoutExt"
  }, 
  "code-runner.saveFileBeforeRun": true, 
  "code-runner.preserveFocus": true, 
  "code-runner.clearPreviousOutput": false, 
  "C_Cpp.clang_format_sortIncludes": true,
  "C_Cpp.intelliSenseEngine": "Default", 
  "C_Cpp.errorSquiggles": "Disabled", 
  "C_Cpp.autocomplete": "Disabled",
  
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "Compile", 
        "command": "clang++", 
        "args": [
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}.exe",
          "-g", 
          "-Wall",
          "-static-libgcc",
          "-fcolor-diagnostics",
          "--target=x86_64-w64-mingw", 
          "-std=c++17" ,
          "-lglut32",//使用glut
          "-lglu32",
          "-lopengl32",
        ], 
        "type": "shell", 
        "group": {
          "kind": "build",
          "isDefault": true 
        },
        "presentation": {
          "echo": true,
          "reveal": "always", 
          "focus": false, 
          "panel": "shared" 
        }
        // "problemMatcher":"$gcc" 
      }
    ]
  }

都配置好就可以调用OpenGL的API了:

#include <GL/glut.h>
void init();
void display();
int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(300, 300);
  glutCreateWindow("OpenGL 3D View");
  init();
  glutDisplayFunc(display);
  glutMainLoop();
  return 0;
}
void init()
{
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glOrtho(-5, 5, -5, 5, 5, 15);
  glMatrixMode(GL_MODELVIEW);
  gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}
void display()
{
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(0, 1.0, 0);
  glutWireTeapot(3);
  glFlush();
}

效果是这样的:

vscode tortoisesvn配置 vscode配置glut_opengl_09

如果你只出现控制台而没有出现OpenGL的窗口,那么可能是vscode配置文件还是有一点问题,执行途中出现了断点:

vscode tortoisesvn配置 vscode配置glut_LLVM_10

点那个进行按钮就可以继续了。