VSCode配置OpenGL freeglut

在VSCode中配置OpenGL环境, 就是让VSCode帮忙给编译, 和平时编译的区别就是调用了OpenGL的动态链接库, 所以找到对应的文件 ( *.h, *.lib, *.dll ( *.o ) ), 然后在编译时指明位置, 传送参数给g++, 就可以,

《计算机图形学及其实践教程》

VSCode安装, MinGW64安装

CSDN上有

安装 C/C++ Project Generator 插件

这样就可以在VSCode中方便的管理工程, 有工程单独的 .vscode, include, lib, src, output 目录, 以及Makefile文件, 可以使用 make 命令来通过Makefile 进行编译, 不用每次添加一个头文件就加在 g++ 命令里, 它可以查找对应目录下的文件并组成命令, 最后删除编译过程的中间文件.

安装完成后的目录结构如下图: 其实有用的只有freeglut, glut, 这些部分, 其他的glfw是OpenGL的其他扩展函数库, 也就是说功能上有重复,不用下载.
OpenGL之gult/freeglut/glew/glfw/glad的联系与区别 CSDN可以查到

原实用工具库glut是32位, 如果要用会出现 ld: i386 architecture of input file is incompatible with i386:x86-64 output, 给自己的g++命令加上 -m32 可以, 但其他库还有代码也要改成32位, 所以用freeglut

下面是这个插件的用法,先要选定文件夹然后建立C++的一个工程,就可以利用工程里的Makefile以及include lib等文件结构,比较方便
VSCode配置OpenGL freeglut_第1张图片

VSCode配置OpenGL freeglut_第2张图片
Makefile:

#
# 'make'        build executable file 'main'
# 'make clean'  removes all .o and executable files
#

# define the Cpp compiler to use
CXX = g++

# define any compile-time flags
CXXFLAGS	:= -std=c++17 -Wall -Wextra -g

# define library paths in addition to /usr/lib
#   if I wanted to include libraries not in /usr/lib I'd specify
#   their path using -Lpath, something like:
LFLAGS =

# define output directory
OUTPUT	:= output

# define source directory
SRC		:= src

# define include directory
INCLUDE	:= include

# define lib directory
LIB		:= lib
LIBRARIES	:= -lfreeglut -lopengl32


ifeq ($(OS),Windows_NT)
MAIN	:= main.exe
SOURCEDIRS	:= $(SRC)
INCLUDEDIRS	:= $(INCLUDE)
LIBDIRS		:= $(LIB)
FIXPATH = $(subst /,\,$1)
RM			:= del /q /f
MD	:= mkdir
else
MAIN	:= main
SOURCEDIRS	:= $(shell find $(SRC) -type d)
INCLUDEDIRS	:= $(shell find $(INCLUDE) -type d)
LIBDIRS		:= $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -f
MD	:= mkdir -p
endif

# define any directories containing header files other than /usr/include
INCLUDES	:= $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))

# define the C libs
LIBS		:= $(patsubst %,-L%, $(LIBDIRS:%/=%))

# define the C source files
SOURCES		:= $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS)))

# define the C object files 
OBJECTS		:= $(SOURCES:.cpp=.o)

#
# The following part of the makefile is generic; it can be used to 
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#

OUTPUTMAIN	:= $(call FIXPATH,$(OUTPUT)/$(MAIN))

all: $(OUTPUT) $(MAIN)
	@echo Executing 'all' complete!

$(OUTPUT):
	$(MD) $(OUTPUT)

# add $(LIBRARIES)
$(MAIN): $(OBJECTS) 
	$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBRARIES)

# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file) 
# (see the gnu make manual section about automatic variables)
.cpp.o:
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $<  -o $@

.PHONY: clean
clean:
	$(RM) $(OUTPUTMAIN)
	$(RM) $(call FIXPATH,$(OBJECTS))
	@echo Cleanup complete!

run: all
	./$(OUTPUTMAIN)
	@echo Executing 'run: all' complete!

下载freeglut

CSDN可以查到, 下载完移动到对应的目录下, dll文件放在exe目录下, 在连接的时候方便找, 我没有放在SysWOW64这儿
甚至我把opengl32.dll winmm.dll 也复制到了这里, 原本在C:\Windows\SysWOW64, 可能是在Makefile里没有指明, 出现了: undefined reference to `__imp_glClearColor’ 报错

改写Makefile

添加LIBRARIES
LIBRARIES := -lfreeglut -lopengl32
下面这里添加 $(LIBRARIES)
$(MAIN): $(OBJECTS)
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBRARIES)

运行

代码:

#include 
void display(void) {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);//设置清屏颜色
    glClear(GL_COLOR_BUFFER_BIT);//刷新颜色缓存区
    glRectf(-0.5, -0.5, 0.5, 0.5);
    glFlush();//用于刷新命令队列和缓存区,使所有尚未被执行的OpenGL命令得到执行
}

int main(int argc, char* argv[]) {
    char s[2][30] = {"我的第一个OpenGL 程序", ""};
    if (argc == 1){
        argv[0] = s[0];
        argv[1] = s[1];
        argc = 2;
    }
    glutInit(&argc, argv);//初始化GLUT库
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//设置显示模式:(缓存,颜色类型)
    glutInitWindowSize(500, 500);//绘图窗口大小
    glutInitWindowPosition(1920 / 2 - 250, 1080 / 2 - 250);//窗口左上角在屏幕的位置
    glutCreateWindow(argv[0]);//创建窗口,标题为
    glutDisplayFunc(display);// 用于绘制当前窗口
    glutMainLoop();//表示开始运行程序,用于程序的结尾
    return 0;
}

运行: 终端输入 make run, 如果没有这个命令, 在MinGW目录下查找, mingw64\bin下的mingw32-make, 使用mingw32-make run也是一样, 不过为了方便直接复制一份重命名为make就行了

输出结果:
VSCode配置OpenGL freeglut_第3张图片
g++ -std=c++17 -Wall -Wextra -g -Iinclude -o output\main.exe src/main.o -Llib -lfreeglut -lopengl32

你可能感兴趣的:(其他,vscode,编辑器)