1.opengl入门

image.png

到这一讲稍微复杂点了,做个阶段性的总结,加深记忆

  1. 参考:learnOpenG-纹理

  2. opengl工作流理解:

    opengl实现渲染的套路有一定范式,把握两条主线:

    opengl 工作流

  3. 项目目录:

    项目目录

注意:1. 图片资源要放到代码同目录里加载才能成功 2.texture.vs和texture.fs源码在学习资料里通过点击跳转来获取

  1. 核心代码实现:

    #include <glad/glad.h> #include <GLFW/glfw3.h>

    #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h"

    #include "Shader.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); #endif

    1// glfw window creation 2// -------------------- 3GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); 4if (window == NULL) 5{ 6 std::cout << "Failed to create GLFW window" << std::endl; 7 glfwTerminate(); 8 return -1; 9} 10glfwMakeContextCurrent(window); 11glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); 12 13// glad: load all OpenGL function pointers 14// --------------------------------------- 15if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) 16{ 17 std::cout << "Failed to initialize GLAD" << std::endl; 18 return -1; 19} 20 21// build and compile our shader zprogram 22// ------------------------------------ 23Shader ourShader("4.1.texture.vs", "4.1.texture.fs"); 24 25// set up vertex data (and buffer(s)) and configure vertex attributes 26// ------------------------------------------------------------------ 27float vertices[] = { 28 // positions // colors // texture coords 29 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right 30 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right 31 -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left 32 -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left 33}; 34unsigned int indices[] = { 35 0, 1, 3, // first triangle 36 1, 2, 3 // second triangle 37}; 38unsigned int VBO, VAO, EBO; 39glGenVertexArrays(1, &VAO); 40glGenBuffers(1, &VBO); 41glGenBuffers(1, &EBO); 42 43glBindVertexArray(VAO); 44 45glBindBuffer(GL_ARRAY_BUFFER, VBO); 46glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 47 48glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); 49glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 50 51// position attribute 52glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); 53glEnableVertexAttribArray(0); 54// color attribute 55glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); 56glEnableVertexAttribArray(1); 57// texture coord attribute 58glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); 59glEnableVertexAttribArray(2); 60 61 62// load and create a texture 63// ------------------------- 64unsigned int texture1, texture2; 65glGenTextures(1, &texture1); 66glBindTexture(GL_TEXTURE_2D, texture1); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object 67// set the texture wrapping parameters 68glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method) 69glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 70// set texture filtering parameters 71glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 72glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 73// load image, create texture and generate mipmaps 74int width, height, nrChannels; 75// The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.

    // 测试代码,判断文件是否存在,这里一开始写绝对路径,找不到文件,把文件拷贝到项目目录下才可以 // std::fstream _file; // _file.open("container.jpg", std::ios::in); // if (_file.is_open()) { // std::cout<<"打开成功"<<std::endl; // } else { // std::cout <<"打开失败"<< std::endl; // } // // return 0;

    1stbi_set_flip_vertically_on_load(true); 2unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);

    // unsigned char *data = stbi_load("/Users/baidu/Downloads/container.jpg", &width, &height, &nrChannels, 0);

    1if (data) 2{ 3 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); 4 glGenerateMipmap(GL_TEXTURE_2D); 5} 6else 7{ 8 std::cout << "Failed to load texture" << std::endl; 9} 10stbi_image_free(data); 11 12 13// texture2 14glGenTextures(1, &texture2); 15glBindTexture(GL_TEXTURE_2D, texture2); 16// set the texture wrapping parameters 17glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 18glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 19// set texture filtering parameters 20glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 21glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 22// load image, create texture and generate mipmaps 23data = stbi_load("awesomeface.png", &width, &height, &nrChannels, 0); 24if (data) 25{ 26 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 27 glGenerateMipmap(GL_TEXTURE_2D); 28} 29else 30{ 31 std::cout << "Failed to load texture" << std::endl; 32} 33stbi_image_free(data); 34 35// tell opengl for each sampler to which texture unit it belongs to (only has to be done once) 36ourShader.use(); 37// either set it manually like so: 38glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0); 39// or set it via the texture class 40ourShader.setInt("texture2", 1); 41 42 43// render loop 44// ----------- 45while (!glfwWindowShouldClose(window)) 46{ 47 // input 48 // ----- 49 processInput(window); 50 51 // render 52 // ------ 53 glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 54 glClear(GL_COLOR_BUFFER_BIT); 55 56 // bind Texture 57 // bind textures on corresponding texture units 58 glActiveTexture(GL_TEXTURE0); 59 glBindTexture(GL_TEXTURE_2D, texture1); 60 glActiveTexture(GL_TEXTURE1); // GL_TEXTURE2就不行 61 glBindTexture(GL_TEXTURE_2D, texture2); 62 63 // render container 64 ourShader.use(); 65 glBindVertexArray(VAO); 66 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 67 68 // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) 69 // ------------------------------------------------------------------------------- 70 glfwSwapBuffers(window); 71 glfwPollEvents(); 72} 73 74// optional: de-allocate all resources once they've outlived their purpose: 75// ------------------------------------------------------------------------ 76glDeleteVertexArrays(1, &VAO); 77glDeleteBuffers(1, &VBO); 78glDeleteBuffers(1, &EBO); 79 80// glfw: terminate, clearing all previously allocated GLFW resources. 81// ------------------------------------------------------------------ 82glfwTerminate(); 83return 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); }

本文同步分享在 博客“天叔”(JianShu)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

1.opengl入门 - HelloWorld