opengl安装教程win7_系统安装教程

(2) 2024-05-29 10:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说opengl安装教程win7_系统安装教程,希望能够帮助你!!!。

OpenGL安装教程

一、安装前准备
1:VS2017
2:GLFW下载链接
(建议下载32位)
3:GLAD下载链接
opengl安装教程win7_系统安装教程_https://bianchenghao6.com/blog__第1张
点击下面的generate,会看到一个glad.zip,直接下载即可。
二、安装步骤
1.首先VS2017创建一个CPP工程,和正常创建工程一样。
2.右键打开工程所在文件夹
opengl安装教程win7_系统安装教程_https://bianchenghao6.com/blog__第2张
3. 将glad文件夹下的include文件夹复制到刚才打开的文件夹下,并且也将glfw下include文件夹下的GLFW文件夹复制到刚刚的include文件夹下;
在刚刚打开的工程目录下新建lib文件夹,将glfw文件夹下的lib-vc2017下的两个静态库文件复制到新建的lib文件夹下;
将glad下的src文件夹里的glad.c也复制到工程目录下,右键源文件添加现有项,将glad.c文件添加进去
效果如下
opengl安装教程win7_系统安装教程_https://bianchenghao6.com/blog__第3张

4.右键打开工程属性
先在C/C++下附加包含目录按如下配置
opengl安装教程win7_系统安装教程_https://bianchenghao6.com/blog__第4张
然后在链接器配置附加库目录
opengl安装教程win7_系统安装教程_https://bianchenghao6.com/blog__第5张
再点击链接器下输入,进入附加依赖项
添加opengl32.lib,glfw3.lib,msvcrt.lib
opengl安装教程win7_系统安装教程_https://bianchenghao6.com/blog__第6张
在属性里点击右下角应用即可保存设置
5.测试配置是否成功
注意这样安装会使glad.c报错,只需将#include"glad/glad.h"替换glad.c的不能打开的那句,或者改变之前安装方式,include文件夹下直接放库文件

#include <iostream>
#include"glad/glad.h"//!!!注意这里,/前面的是h文件的上级目录,可能每个人安装不同,也有不同
#include"GLFW/glfw3.h"
using namespace std;

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{ 
   
	if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
	{ 
   
		glfwSetWindowShouldClose(window, GL_TRUE);
	}

}

//vertex shader
const GLchar* vertexShader_Code =
"#version 330 core\n"
"layout(location=0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"gl_Position=vec4(aPos.x,aPos.y,aPos.z,1.0);\n"
"}";


const GLchar* fragmentShader_Code =
"#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor=vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}";

int main()
{ 
   
	//三角顶点
	float vertices[] =
	{ 
   
		-0.5f,-0.5f,0.0f,//左下角
		0.5f,-0.5f,0.0f,//右下角
		0.0f, 0.5f,0.0f//顶端角
	};
	glfwInit();//初始化
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//配置GLFW
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//配置GLFW
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//
	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

	GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
	glfwSetKeyCallback(window, key_callback);


	if (window == nullptr)
	{ 
   
		cout << "Failed to create GLFW window" << endl;
		glfwTerminate();//terminate the glfw(end the glfw)
		return -1;
	}

	glfwMakeContextCurrent(window);//设置当前openGL上下

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{ 
   
		cout << "Failed to initialize GLAD" << endl;
		return -1;
	}
	//data
	int isSuccess;
	char infoLog[512];
	//vertexShader data
	unsigned int vertexShader;
	vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexShader, 1, &vertexShader_Code, NULL);
	glCompileShader(vertexShader);
	/*编译是否通过*/
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isSuccess);
	if (!isSuccess)
	{ 
   
		glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
		cout << "ERROR::VertexShader::Compile_failed" << infoLog << endl;
	}
	else
	{ 
   
		cout << "SUCCESS::VertexShader" << endl;
	}

	//fragmentShader Data
	unsigned int fragmentShader;
	fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader, 1, &fragmentShader_Code, NULL);
	glCompileShader(fragmentShader);
	/*编译是否通过*/
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isSuccess);
	if (!isSuccess)
	{ 
   
		glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
		cout << "ERROR::FragmentShader::Compile_failed" << infoLog << endl;
	}
	else
	{ 
   
		cout << "SUCCESS::FragmentShader" << endl;
	}

	//Shader Pragram
	unsigned int shaderProgram;
	shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	glLinkProgram(shaderProgram);

	glGetProgramiv(shaderProgram, GL_LINK_STATUS, &isSuccess);
	if (!isSuccess)
	{ 
   
		glGetShaderInfoLog(shaderProgram, 512, NULL, infoLog);
		cout << "ERROR::ShaderProgram::Link_failed" << infoLog << endl;
	}
	else
	{ 
   
		cout << "SUCCESS::ShaderProgram" << endl;
		glUseProgram(shaderProgram);
		glDeleteShader(vertexShader);
		glDeleteShader(fragmentShader);
	}

	//vectices data
	unsigned int VAO;
	unsigned int VBO;

	glGenVertexArrays(1, &VAO);//获得一个顶点数组对象
	glBindVertexArray(VAO);
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	while (!glfwWindowShouldClose(window))
	{ 
   
		glfwPollEvents();//检查有没有触发事件
		glClearColor(0.27f, 0.27f, 0.27f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		glUseProgram(shaderProgram);
		glBindVertexArray(VAO);
		glDrawArrays(GL_TRIANGLES, 0, 3);
		glfwSwapBuffers(window);//双缓冲(替换窗口的每一个像素值)
	}
	glfwTerminate();
	return 0;
}

若出现报错:默认库MSVCRTD与……冲突,属性->链接器->输入->忽略特定默认库->编辑,将MSVCRTD.lib添加进去即可
结果:
opengl安装教程win7_系统安装教程_https://bianchenghao6.com/blog__第7张

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复