提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
使用《计算机图形学编程(使用OpenGL和C++)》这本书时,其中GLFW、GLEW等相关库的配置着实令人头疼不已,特别是配置SOIL2的过程,花了不少时间。现在使用premake来配置SOIL2
一、premake是什么?
Premake 是一种命令工具,通过读取项目脚本,来生成各种开发环境的项目文件。主要用于:
生成开发人员喜欢的平台,工具集(协同开发的人员,可以使用不同的平台和开发工具) 通过脚本保持不同平台,工具集下的项目配置同步(比如新建文件夹,引入新的库文件) 通过脚本来快速更新许多不同的大型代码库,并重新生成项目(比如对依赖的多种著名库的版本更新) 快速升级工具集的版本(比如无缝地从VisualStudio 2010升级到VisualStudio 2019) 目前支持:
Microsoft Visual Studio 2005-2019 GNU Make,包括 Cygwin 和 MinGW XCode Codelite Premake 5.0 目前支持:
32 和 64 位平台 Xbox 360(仅支持Visual Studio) 插件模块可以支持其他语言,框架,和工具集 Premake 是存粹的旧版C应用程序,发布为一个单个的,非常小的exe文件。支持完整的Lua脚本环境 开源地址:https://github.com/premake/premake-core 下载地址:https://premake.github.io/ 实例地址:https://github.com/wuguyannian/tutorial_premake
二、下载SOIL2
SOIL2源码下载地址
1.引入库
下载完SOIL2和premake后,将它们两个解压,然后把premake解压得到的premake5.exe放在SOIL2的文件夹里
使用premake5配置SOIL2
在下图圈起来的地址栏位置,输入cmd,进入命令界面 然后在命令行界面输入,.\premake5.exe vs2019 注意前面有个 点和右斜杠 否则报错!!!! PS:因为我是在vs2019上进行编写,如果读者用的是vs2017,应该输入.\premake5.exe vs2017 Success! 此时SOIL2的文件夹多出个新文件夹make,make下有个文件夹windows,windows文件夹里面又有一个SOIL2.sln文件,打开它 进入解决方案资源管理器,对soil2-static-lib右键,点击生成 这里生成32位和64位的lib都可以 分别生成debug和release版本的lib和dll库 release版本: soil2.lib soil2.dll
debug版本 soil2-debug.lib soil2-debug.dll
使用SOIL2创建工程
- vs2019创建空工程
- 把SOIL2头文件,soil2-debug.lib、soil2-debug.dll文件拷贝到工程文件中,目录结构如下
SOIL2头文件 SOIL2静态库 soil2-debug.lib 是debug版本 soil2.lib 是release版本 SOIL2动态库
3.配置SOIL2.lib 4.代码
#include "glew/glew.h"
#include "glfw/glfw3.h"
#include "Utils.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#pragma comment(lib, "soil2-debug.lib")
static const int Screen_Width = 800;
static const int Screen_Height = 600;
static const GLuint numVAOs = 1;
GLuint vao[numVAOs];
GLuint renderintprogram = 0;
void printShaderLog(GLuint shaderId)
{
int nLen = 0;
int nWrittn = 0;
char* chLogMsg = nullptr;
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &nLen);
if (nLen > 0)
{
chLogMsg = (char*)malloc(nLen);
if (!chLogMsg)
{
cout << "Allocate memory failed. Invoke malloc()." << "Error file:" << __FILE__ << "...Error line:" << __LINE__ << std::endl;
return;
}
glGetShaderInfoLog(shaderId, nLen, &nWrittn, chLogMsg);
cout << "Shader error infomation:" << chLogMsg << std::endl;
free(chLogMsg);
}
}
void printProgramInfo(GLuint programId)
{
int nLen = 0;
int nWrittn = 0;
char* chLogMsg = nullptr;
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &nLen);
if (nLen > 0)
{
chLogMsg = new char[nLen];
if (!chLogMsg)
{
cout << "Allocate memory failed. Invoke new operator." << "Error file:" << __FILE__ << "...Error line:" << __LINE__ << std::endl;
return;
}
glGetProgramInfoLog(programId, nLen, &nWrittn, chLogMsg);
cout << "Program error log:" << chLogMsg << "Error file:" << __FILE__ << ".....Error line:" << __LINE__ << std::endl;
delete chLogMsg;
}
}
bool checkOpenglError()
{
bool foundError = false;
int nErrorCode = glGetError();
while (GL_NO_ERROR != nErrorCode)
{
cout << "OpenGL error, error number=" << nErrorCode << "error file:" << __FILE__ << ".......Error line:" << __LINE__ << std::endl;
foundError = true;
nErrorCode = glGetError();
}
return foundError;
}
void init(GLFWwindow* window)
{
renderintprogram = Utils::createShaderProgram("vertShader.glsl", "fragShader.glsl");
glGenVertexArrays(numVAOs, vao);
glBindVertexArray(vao[0]);
}
void display(GLFWwindow* window, double currentTiem)
{
glClearColor(1.f, 0.f, 1.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(renderintprogram);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
int main(int argc, char** argv)
{
int glfwStatus = glfwInit();
if (GLFW_FALSE == glfwStatus)
{
cout << "GLFW initialize failed!, Invoke glfwInit()." << "Error file:" << __FILE__ << ".....Error line:" << __LINE__ << std::endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_PROFILE);
GLFWwindow* window = glfwCreateWindow(Screen_Width, Screen_Height, "Draw trangle opengl", nullptr, nullptr);
if (nullptr == window)
{
cout << "GLFW create window failed, Invoke glfwCreateWindow()." << "Error file:" << __FILE__ << "Error line:" << __LINE__ << std::endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
int glewStatue = glewInit();
if (GLEW_OK != glewStatue)
{
cout << "GLEW initialize failed, Invoke glewInit(). Error file:" << __FILE__ << "Error line:" << __LINE__ << std::endl;
exit(EXIT_FAILURE);
}
glfwSwapInterval(1);
init(window);
while (!glfwWindowShouldClose(window))
{
display(window, glfwGetTime());
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_FAILURE);
return 0;
}
程序运行
三、工程下载
[工程源码下载](https://download.csdn.net/download/aoxuestudy/64632489)
|