【Cherno的OpenGL视频】Using modern OpenGL in C++

本文详细介绍了如何从glew官网下载并集成到OpenGL项目中,包括解压、重命名、设置附加包含目录和添加GLEW_STATIC宏。通过实例展示了如何在Application.cpp中使用GLEW和GLFW进行OpenGL编程,确保读者理解关键步骤和常见问题解决。
摘要由CSDN通过智能技术生成

1、glew官网下载库。
UsingModernOpenGLInCpp
2、解压后同样放到解决方案OpenGL目录下的Dependencies目录里。
UsingModernOpenGLInCpp
3、glew-2.1.01改个名字为:GLEW。
UsingModernOpenGLInCpp
UsingModernOpenGLInCpp
4、【重要】查看使用说明!!!
在这里插入图片描述
在这里插入图片描述
5、右键OpenGL项目->属性,如图设置附加包含目录等等。
UsingModernOpenGLInCpp
UsingModernOpenGLInCpp
UsingModernOpenGLInCpp
6、【重要】添加GLEW_STATIC宏。
UsingModernOpenGLInCpp
UsingModernOpenGLInCpp
7、代码实现:
Application.cpp

#include <GL/glew.h>// 放在glfw3.h之前,任何OpenGL之前。
#include <GLFW/glfw3.h>
#include <iostream>

// glew: OpenGL extension wrangler, a bit simpler than glad.
// glad: a more specific with extensions to OpenGL.

int main(void)
{
	GLFWwindow* window;

	/* Initialize the library */
	if (!glfwInit())
		return -1;

	/* Create a windowed mode window and its OpenGL context */
	window = glfwCreateWindow(640, 480, "I am Groot", NULL, NULL);
	if (!window)
	{
		glfwTerminate();
		return -1;
	}

	/* Make the window's context current */
	glfwMakeContextCurrent(window);

	// after glfwMakeContextCurrent() -> Testing GLEW.
	if (glewInit() != GLEW_OK)
	{
		std::cout << "glewInit() error" << std::endl;
	}
	std::cout << glGetString(GL_VERSION) << std::endl;
	// Testing GLEW.

	/* Loop until the user closes the window */
	while (!glfwWindowShouldClose(window))
	{
		/* Render here */
		glClear(GL_COLOR_BUFFER_BIT);

		// Testing GLFW.
		glBegin(GL_TRIANGLES);	// legacy opengl.
		glVertex2f(-0.5f, -0.5f);
		glVertex2f(0.0f, 0.5f);
		glVertex2f(0.5f, -0.5f);
		glEnd();//legacy opengl
		// Testing GLFW.

		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}

8、运行效果:
UsingModernOpenGLInCpp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值