现代OpenGL自学----纹理测试
来源:互联网 发布:淘宝平拍裤子褶皱手法 编辑:程序博客网 时间:2024/06/05 03:04
继续学习OpenGL。
#include <iostream>#include <glad\glad.h>#include <GLFW\glfw3.h>#define STB_IMAGE_IMPLEMENTATION#include "stb_image.h"#include <shader.h>using namespace std; //切记 如果2个纹理图片大小不一样 则会出现未知的错误!!!!!!!!!!void framebuffer_size_callback(GLFWwindow * window, int width, int height);void processInput(GLFWwindow * window);const unsigned int SCR_WIDTH = 800;const unsigned int SCR_HEIGHT = 600;int main(){ glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_PROFILE); GLFWwindow * window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "111", NULL, NULL); if (!window) { cout << "Error to create a window" << endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { cout << "Failed to initialize GLAD" << endl; return -1; } Shader ourshader("1.txt", "2.txt"); float vertices[] = { 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 2.0f, 2.0f, // top right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 2.0f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 2.0f // top left }; unsigned int indices[] = { 0, 1, 3, 1, 2, 3 }; unsigned int VAO, VBO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); //位置属性 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); //颜色属性 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); // 纹理属性 glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); unsigned int texture1, texture2; glGenTextures(1, &texture1); //生成一个纹理数量,存储在texture1中 glBindTexture(GL_TEXTURE_2D, texture1);//绑定纹理 //纹理环绕 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);//二维的目标纹理,纹理轴为x(s即为x)轴,环绕方式为重复 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);//二维的目标纹理,纹理轴为y(t即为y)轴,环绕方式为重复 //纹理过滤 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//二维的目标纹理,缩小纹理过滤选择线性过滤 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//二维的目标纹理,放大纹理过滤选择邻近过滤 int width, width1, height, height1, nrChannels, nrChannels1; stbi_set_flip_vertically_on_load(true); //翻转图像为正 unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);//载入jpg图像,宽长通道都为此jpg图像的 if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); //载入图片生成纹理 glGenerateMipmap(GL_TEXTURE_2D);//自动生成多级渐远纹理 } else { std::cout << "Failed to load texture" << std::endl; } stbi_image_free(data);//释放图像内存 glGenTextures(1, &texture2);//生成一个纹理数量,存储在texture2中 glBindTexture(GL_TEXTURE_2D, texture2);//绑定纹理 //设置纹理环绕方式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); //设置纹理过滤方式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); unsigned char *data1 = stbi_load("awesomeface.png", &width1, &height1, &nrChannels1, 0); if (data1) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data1); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout << "Failed to load texture" << std::endl; } stbi_image_free(data1); ourshader.use();//启用着色器 glUniform1i(glGetUniformLocation(ourshader.ID, "texture2"), 0);//使用glUniform之前要启用着色器 用glGetUniformLocation查询uniform texture1的位置值 // or set it via the texture class ourshader.setInt("texture2", 1); while (!glfwWindowShouldClose(window)) { // input // ----- processInput(window); // render // ------ glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // bind textures on corresponding texture units glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture2); // render container ourshader.use(); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // ------------------------------------------------------------------------------- glfwSwapBuffers(window); glfwPollEvents(); } // optional: de-allocate all resources once they've outlived their purpose: // ------------------------------------------------------------------------ glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &EBO); // 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);}
阅读全文
1 0
- 现代OpenGL自学----纹理测试
- 现代OpenGL自学----坐标变换
- 【LWJGL2 WIKI】【现代OpenGL篇】用纹理画方形
- openGL纹理
- OpenGL 纹理
- OpenGL纹理
- OpenGL 纹理
- OpenGL 纹理
- OPenGL纹理
- opengl纹理
- OpenGL: 纹理
- OpenGL 纹理
- opengl纹理
- OpenGL纹理
- OpenGL纹理
- OpenGL纹理
- OPENGL纹理
- OpenGL -- 纹理
- java变量与数据类型
- 作为一名技术主管,开始记录技术研究内容
- ajax获取后台json信息时跨域
- KMP算法
- 工作流引擎
- 现代OpenGL自学----纹理测试
- 纪念一位歌手
- PX4飞控之导航及任务架构
- Android Studio使用Git(OsChina)
- Copy相关
- oracle基础函数语法
- Dialog、Toast、SnackBar给用户一个舒服的"提醒"
- Django入门:template之过滤器
- Java中普通代码块,构造代码块,静态代码块区别及代码示例