记一次VS2022的OpenGL的配置

开始补计算机图形学作业了

参考《计算机图形学编程》(使用OpenGL和C++)(第二版)

也可以看看这个网站,感觉还挺不错的LearnOpenGL-CN


使用VS2022,WIN11 64位

先放我编译好的,X64,Debug的OpenGLtemplate.7z

如果无法正常用的话建议自己去编译,用CMAKE和premake5

GLFW 需要CMake GLFW

GLEW GLEW

GLM  GLM

SOIL2 需要premake5 SOIL2

准备两个文件夹

include、lib分别存放头文件和静态链接库,下图来自书籍


可以把glew32.dll放到每次运行程序的同目录或者C:\Windows\System32下

然后在VS里自定义项目模板

自定义项目模板后就不用每次去配置了,效果如下

1.新建一个C++空项目,自己命名

2.点击“项目”,属性

3.VC++->常规->包含目录,把include文件夹添加进去,(包括后面)建议“所有配置”和“所有平台”,毕竟我们目前还不考虑单独为debug/release或者x86/x64单独准备

4.链接器->常规->附加库目录,把lib文件夹添加进去

5.链接器->输入->附加依赖项,填入以下内容

glfw3.lib
glew32.lib
soil2-debug.lib
opengl32.lib

6.然后往里面加点自己用的东西就行了

目前我也才看完第二章,贴一下现在的代码,一个三角形左右晃动,后面可能还会有改进,记得两个glsl文件用ANSI编码

utils.h
#ifndef UTILS_H
#define UTILS_H
#include<gl/glew.h>
#include<GLFW/glfw3.h>
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include <cwchar>
#include <cstring>
#include <locale>
#include <codecvt>
#define _CRT_SECURE_NO_WARNINGS
//头文件里一定不要再包含对应的cpp了
std::string readShaderSource(const char* filePath);
void printShaderLog(GLuint shader);
void printProgramLog(GLuint prog);
bool checkOpenGLError();
void getCompErr(GLuint shader);
void getLinkErr(GLuint program);
#endif
utils.cpp
 #include"utils.h"

std::string readShaderSource(const char* filePath)//从文件读入GLSL代码
{
	std::string context;
	std::ifstream fileStream(filePath, std::ios::in);
	std::string line = "";
	while (!fileStream.eof())
	{
		getline(fileStream, line);
		context.append(line + "\n");
	}
	fileStream.close();
	return context;
}


void printShaderLog(GLuint shader)//输出编译失败日志
{
	int len = 0;
	int chWrittn = 0;
	char* log;
	glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
	if (len > 0)
	{
		log = (char*)malloc(len);
		glGetShaderInfoLog(shader, len, &chWrittn, log);
		std::cout << "GLSL编译失败!\n";
		std::cout << "Shader 日志信息: " << log << "\n";
		free(log);
	}
}

void printProgramLog(GLuint prog)//输出链接失败日志
{
	int len = 0;
	int chWrittn = 0;
	char* log;
	glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
	if (len > 0)
	{
		log = (char*)malloc(len);
		glGetProgramInfoLog(prog, len, &chWrittn, log);
		std::cout << "GLSL链接失败!\n";
		std::cout << "Pragram 日志信息: " << log << "\n";
		free(log);
	}
}

bool checkOpenGLError()
{
	bool foundError = 0;
	int glErr = glGetError();
	while (glErr != GL_NO_ERROR)
	{
		std::cout << "glErr : " << glErr << '\n';
		foundError = 1;
		glErr = glGetError();
	}
	return foundError;
}
void getCompErr(GLuint Shader)//获取编译的错误,放在编译后运行
{
	checkOpenGLError();
	GLint vertCompiled;
	glGetShaderiv(Shader, GL_COMPILE_STATUS, &vertCompiled);
	if (vertCompiled != 1)
	{
		std::cout << "顶点编译失败\n";
		printShaderLog(Shader);
	}
}

void getLinkErr(GLuint Program)//获取链接的错误,放在链接后运行
{
	checkOpenGLError();
	GLint linked;
	glGetProgramiv(Program, GL_LINK_STATUS, &linked);
	if (linked != 1)
	{
		std::cout << "链接失败\n";
		printProgramLog(Program);
	}
}
main.cpp
 #include "utils.h"

//#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")//不显示控制台

#define numVAOs 11
using namespace std;

GLuint renderingProgram;
GLuint vao[numVAOs];

GLuint createShaderProgma()
{
	string vshaderSource = readShaderSource("vertShader.glsl");//顶点着色器
	string fshaderSource = readShaderSource("fragShader.glsl");//片段着色器
	const char* vertShaderSrc = vshaderSource.c_str();
	const char* fargShaderSrc = fshaderSource.c_str();
	cout << "顶点着色器代码:\n" << vshaderSource << "\n片段着色器代码:\n" << fshaderSource << "\n";
	GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
	GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
	//编译
	glShaderSource(vShader, 1, &vertShaderSrc, NULL);//顶点着色器
	//着色器字符串/着色器字符串数目/着色器指针/着色器字符串长度NULL为自动计算
	glShaderSource(fShader, 1, &fargShaderSrc, NULL);//片段着色器
	glCompileShader(vShader);
	checkOpenGLError();
	getCompErr(vShader);
	cout << "v ok\n";
	glCompileShader(fShader);


	checkOpenGLError();
	getCompErr(fShader);
	cout << "f ok\n";

	//链接
	GLuint vfProgram = glCreateProgram();
	glAttachShader(vfProgram, vShader);
	glAttachShader(vfProgram, fShader);
	glLinkProgram(vfProgram);
	checkOpenGLError();
	getLinkErr(vfProgram);

	return vfProgram;
}

void init(GLFWwindow* window)
{
	renderingProgram = createShaderProgma();
	glGenVertexArrays(numVAOs, vao);
	glBindVertexArray(vao[0]);

}
float x = 0.0f;
float inc = 0.01f;
void display(GLFWwindow* window, double currentTime)
{
	glClear(GL_DEPTH_BUFFER_BIT);//清除深度缓冲区
	glClearColor(0.0, 0.0, 0.0, 1.0);//设置清除颜色
	glClear(GL_COLOR_BUFFER_BIT);//清除颜色缓冲区

	glUseProgram(renderingProgram);
	x += inc;
	if (x > 1.0f)inc = -0.01f;
	if (x < -1.0f)inc = 0.01f;

	GLuint offsetLoc = glGetUniformLocation(renderingProgram, "offset");//获取指向offset变量的指针
	glProgramUniform1f(renderingProgram, offsetLoc, x);//将x的值传给offset
	glPointSize(60.0f);
	glDrawArrays(GL_TRIANGLES, 0, 3);
}

void windowSizeCallback(GLFWwindow* window, int width, int height) {
	// 设置OpenGL视口,以匹配新的窗口大小
	glViewport(0, 0, width, height);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除颜色缓冲区和深度缓冲区

	display(window, glfwGetTime());
	glfwSwapBuffers(window);    // 交换缓冲区
	glfwPollEvents();            // 处理事件
}


int main()
{
	if (!glfwInit())
	{
		exit(EXIT_FAILURE);
	}
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	//设置与OPENGL4.3兼容

	GLFWwindow* window = glfwCreateWindow(800, 600, "opengl", NULL, NULL);//创建窗口
	if (!window) {
		// 窗口创建失败的处理
		glfwTerminate();
	}
	//glfwSetWindowAttrib(window, GLFW_RESIZABLE, GLFW_FALSE);//设置不能改变大小
	glfwMakeContextCurrent(window);//绑定配置
	glfwSetWindowSizeCallback(window, windowSizeCallback);// 注册窗口大小改变回调函数

	if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); }

	glfwSwapInterval(1);//开启垂直同步
	init(window);
	cout << "init is ok!\n";
	while (!glfwWindowShouldClose(window))
	{
		display(window, glfwGetTime());
		glfwSwapBuffers(window);//交换缓冲区
		glfwPollEvents();//处理窗口事件
	}

	glfwDestroyWindow(window);
	glfwTerminate();
	exit(EXIT_SUCCESS);
}
fragShader.glsl
 #version 430
out vec4 color; // 声明color变量
void main(void)
{ 
  color=vec4(0.0,0.0,1.0,1.0);
}
vertShader.glsl
 //顶点着色器
#version 430
uniform float offset;//可以与CPP代码交互的变量
void main(void)
{ if(gl_VertexID==0)gl_Position = vec4(0.25+offset,-0.25,0.0,1.0);
  else if(gl_VertexID==1)gl_Position = vec4(-0.25+offset,-0.25,0.0,1.0);
  else gl_Position = vec4(0.25+offset,0.25,0.0,1.0);
}

7.项目->导出模板,然后一步步导出即可。完成后在新建项目的时候搜索框里可以搜到了

posted @ 2023-09-24 20:13  qbning  阅读(731)  评论(0编辑  收藏  举报
描述