从头学openGL4.5 第一篇:openGL环境配置

TOC

前言

学习openGL也有一年的时间了,感觉也才刚刚入门,先后看过这样几本书

  1. 《计算机图形学》《3D.Computer.Graphics.-.A.Mathematical.Introduction.with.OpenGL》(看完)
  2. 《OpenGL超级宝典》《OpenGL SUPERBIBLE Fifth Edition》(未完)
  3. 《LearnOpenGL CN》中文版 《LearnOpenGL》英文版(未完)
  4. 《OpenGL着色语言》(未完)
  5. 《OpenGL 4.0 Shading Language Cookbook》没有中文版(未完)
    只有计算机图形学是看完了,因为是纯理论,不用去实际敲代码,其他基本基本都只看了一半,因为这些书看到接近有一半的时候,感觉就看不懂了,即使都是跟着书上的代码敲了一遍,也还是不懂意思。发现里面的c++代码基本能看懂,但是里面的shader程序确实看不懂。只好又来补shader的内容,在看到shader的时候,又发现shader里面的函数和方程不懂,只好又又又来看理论。
    每个人的学习基础不同,所以我的感觉是:如果完全没有写过openGL程序的小伙伴,就从《LearnOpenGL CN》中文版 《LearnOpenGL》英文版 看起,中文版英文版都有,如果英文好的话就看英文版,中文版虽然也很好,也更新到2018年,但有几处明显的翻译错误。

openGL环境配置

  1. GLEW
    配置和下载

  2. GLFW
    配置和下载

  3. freeglut(可选)
    创建窗口代码

程序运行结果
在这里插入图片描述

在这里插入图片描述
源码下载

#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);
}


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210602161835506.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FveHVlc3R1ZHk=,size_16,color_FFFFFF,t_70)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值