✠OpenGL-Windows Visual Studio环境配置

Posted itzyjr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了✠OpenGL-Windows Visual Studio环境配置相关的知识,希望对你有一定的参考价值。

《计算机图形学编程(使用OpenGL和C++)》环境配置文件-C++文档类资源-CSDN下载《计算机图形学编程(使用OpenGL和C++)》环境配置文件更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/itzyjr/85031937下载文件,解压到目录,如:

 打开VS2019,新建工程,Project -> Properties:

VC++ Directories -> Include Directories,添加解压文件中的“include”目录:

 Linker -> General -> Additional Library Directories,添加解压文件中的“lib”目录:

 Linker -> Input -> Additional Dependencies,输入这4个.lib文件名:glfw3.lib,glew32.lib,soil2-debug.lib 和 opengl32.lib(最后一个已作为标准 WindowsSDK 的一部分提供)。


 


对“调试”和“发布”模式的项目属性都进行上述更改后,就可以开始创建模板了。创建模板通过进入“项目”菜单并选择“导出模板”来完成。选择“项目”模板,并为模板提供有意义的名称,例如“ OpenGL project”。

 安装库并设置好自定义模板后,创建一个新的 OpenGL C++项目就很简单了。

下拉菜单允许你指定“ x86”或“ x64”——选择“ x86”。

将解压文件的根目录中的“glew32.dll”文件复制到“Release”或“Debug”文件夹中,具体取决于所需的解决方案配置。


在开发、测试和调试应用程序之后,程序可以作为一个单独的可执行文件进行部署。部署时需要在“发布”模式下构建项目,然后将以下文件放在同一个文件夹中

  • 应用程序使用的所有着色器文件。
  • 应用程序使用的所有纹理图像和模型文件。

环境配置好了,运行一个完整的示例,来检测环境是否真的配置OK:

// EXE.cpp
#include <GL\\glew.h>
#include <GLFW\\glfw3.h>
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <glm\\glm.hpp>
#include <glm\\gtc\\type_ptr.hpp>
#include <glm\\gtc\\matrix_transform.hpp>
using namespace std;
#define numVAOs 1
#define numVBOs 2
float cameraX, cameraY, cameraZ;
float cubeLocX, cubeLocY, cubeLocZ;
GLuint renderingProgram;
GLuint vao[numVAOs];
GLuint vbo[numVBOs];
GLuint mvLoc, projLoc;
int width, height;
float aspect;
glm::mat4 pMat, vMat, mMat, mvMat;

string readShaderFile(const char* filePath) 
	string content;
	ifstream fileStream(filePath, ios::in);
	string line = "";
	while (!fileStream.eof()) 
		getline(fileStream, line);
		content.append(line + "\\n");
	
	fileStream.close();
	return content;


GLuint prepareShader(int shaderTYPE, const char* shaderPath) 
	GLint shaderCompiled;
	string shaderStr = readShaderFile(shaderPath);
	const char* shaderSrc = shaderStr.c_str();
	GLuint shaderRef = glCreateShader(shaderTYPE);
	glShaderSource(shaderRef, 1, &shaderSrc, NULL);
	glCompileShader(shaderRef);
	return shaderRef;


int finalizeShaderProgram(GLuint sprogram) 
	glLinkProgram(sprogram);
	return sprogram;


GLuint createShaderProgram(const char* vp, const char* fp) 
	GLuint vShader = prepareShader(GL_VERTEX_SHADER, vp);
	GLuint fShader = prepareShader(GL_FRAGMENT_SHADER, fp);
	GLuint vfprogram = glCreateProgram();
	glAttachShader(vfprogram, vShader);
	glAttachShader(vfprogram, fShader);
	finalizeShaderProgram(vfprogram);
	return vfprogram;


void setupVertices(void) 
	float vertexPositions[108] = 
		-1.0f,  1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
		1.0f, -1.0f, -1.0f, 1.0f,  1.0f, -1.0f, -1.0f,  1.0f, -1.0f,
		1.0f, -1.0f, -1.0f, 1.0f, -1.0f,  1.0f, 1.0f,  1.0f, -1.0f,
		1.0f, -1.0f,  1.0f, 1.0f,  1.0f,  1.0f, 1.0f,  1.0f, -1.0f,
		1.0f, -1.0f,  1.0f, -1.0f, -1.0f,  1.0f, 1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f,  1.0f, -1.0f,  1.0f,  1.0f, 1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f,  1.0f, -1.0f, -1.0f, -1.0f, -1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f, -1.0f, -1.0f,  1.0f, -1.0f, -1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f,  1.0f,  1.0f, -1.0f,  1.0f,  1.0f, -1.0f, -1.0f,
		1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,  1.0f,
		-1.0f,  1.0f, -1.0f, 1.0f,  1.0f, -1.0f, 1.0f,  1.0f,  1.0f,
		1.0f,  1.0f,  1.0f, -1.0f,  1.0f,  1.0f, -1.0f,  1.0f, -1.0f
	;
	glGenVertexArrays(1, vao);
	glBindVertexArray(vao[0]);
	glGenBuffers(numVBOs, vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);


void init(GLFWwindow* window) 
	renderingProgram = createShaderProgram("vertShader.glsl", "fragShader.glsl");
	cameraX = 0.0f; cameraY = 0.0f; cameraZ = 8.0f;
	cubeLocX = 0.0f; cubeLocY = -2.0f; cubeLocZ = 0.0f;
	setupVertices();


void display(GLFWwindow* window, double currentTime) 
	glClear(GL_DEPTH_BUFFER_BIT);
	glUseProgram(renderingProgram);
	mvLoc = glGetUniformLocation(renderingProgram, "mv_matrix");
	projLoc = glGetUniformLocation(renderingProgram, "proj_matrix");
	glfwGetFramebufferSize(window, &width, &height);
	aspect = (float)width / (float)height;
	pMat = glm::perspective(1.0472f, aspect, 0.1f, 1000.0f);
	vMat = glm::translate(glm::mat4(1.0f), glm::vec3(-cameraX, -cameraY, -cameraZ));
	mMat = glm::translate(glm::mat4(1.0f), glm::vec3(cubeLocX, cubeLocY, cubeLocZ));
	mvMat = vMat * mMat;
	glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mvMat));
	glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(pMat));
	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
	glEnableVertexAttribArray(0);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glDrawArrays(GL_TRIANGLES, 0, 36);


int main(void) 
	if (!glfwInit())  exit(EXIT_FAILURE); 
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	GLFWwindow* window = glfwCreateWindow(600, 600, "Chapter 4 - program 1", NULL, NULL);
	glfwMakeContextCurrent(window);
	if (glewInit() != GLEW_OK)  exit(EXIT_FAILURE); 
	glfwSwapInterval(1);
	init(window);
	while (!glfwWindowShouldClose(window)) 
		display(window, glfwGetTime());
		glfwSwapBuffers(window);
		glfwPollEvents();
	
	glfwDestroyWindow(window);
	glfwTerminate();
	exit(EXIT_SUCCESS);
// vertShader.glsl
#version 430
layout (location=0) in vec3 position;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void) 
	gl_Position = proj_matrix * mv_matrix * vec4(position,1.0);


// fragShader.glsl
#version 430
out vec4 color;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void) 
    color = vec4(1.0, 0.0, 0.0, 1.0);

       

运行程序,结果如下图(一个有透视效果的红色立方体):

 至此,说明环境搭配完美成功!

以上是关于✠OpenGL-Windows Visual Studio环境配置的主要内容,如果未能解决你的问题,请参考以下文章

ST Visual Programmer批量烧写教程

Microsoft Visual Basic - 运行时错误“3075:查询表达式“1st”、“A”、1-Jan-15'、“1”中的语法错误(缺少运算符))

STM8S903K3T6C基于ST Visual Develop开发输入输出按键检测示例

STM8S903K3基于ST Visual Develop开发串口接收中断示例

STM8S903K3T6C基于ST Visual Develop开发串口数据收发示例

STM8S903K3基于ST Visual Develop开发定时器5中断示例