前言
学习openGL也有一年的时间了,感觉也才刚刚入门,先后看过这样几本书
- 《计算机图形学》《3D.Computer.Graphics.-.A.Mathematical.Introduction.with.OpenGL》(看完)
- 《OpenGL超级宝典》《OpenGL SUPERBIBLE Fifth Edition》(未完)
- 《LearnOpenGL CN》中文版 《LearnOpenGL》英文版(未完)
- 《OpenGL着色语言》(未完)
- 《OpenGL 4.0 Shading Language Cookbook》没有中文版(未完)
只有计算机图形学是看完了,因为是纯理论,不用去实际敲代码,其他基本基本都只看了一半,因为这些书看到接近有一半的时候,感觉就看不懂了,即使都是跟着书上的代码敲了一遍,也还是不懂意思。发现里面的c++代码基本能看懂,但是里面的shader程序确实看不懂。只好又来补shader的内容,在看到shader的时候,又发现shader里面的函数和方程不懂,只好又又又来看理论。
每个人的学习基础不同,所以我的感觉是:如果完全没有写过openGL程序的小伙伴,就从《LearnOpenGL CN》中文版 《LearnOpenGL》英文版 看起,中文版英文版都有,如果英文好的话就看英文版,中文版虽然也很好,也更新到2018年,但有几处明显的翻译错误。
openGL环境配置
程序运行结果
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
//if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
//{
// std::cout << "Failed to initialize GLAD" << std::endl;
// return -1;
//}
//这里我把glad换成了glew,目前流行的库是glew+glfw,glad比较老了,已经放弃更新了
GLenum glewErr = glewInit();
if (GLEW_OK != glewErr)
{
cout << "GLEW initialization failed!" << endl;
return -1;
}
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
