完成章节后练习

练习

1. Make sure only the happy face looks in the other/reverse direction by changing the fragment shader.

#version 330 core
out vec4 FragColor;
in vec4 Color;
in vec2 texCoord;
uniform sampler2D texSampler1;
uniform sampler2D texSampler2;
void main()
{FragColor = mix(texture(texSampler1, texCoord)*Color, texture(texSampler2, vec2(-texCoord.x, texCoord.y)), 0.2);
}

2. Experiment with the different texture wrapping methods by specifying texture coordinates in the range 0.0f to 2.0f. See if you can display 4 smiley faces on a single container image clamped at its edge.

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

3. Try to display only the center pixels of the texture image on the rectangle in such a way that the individual pixels are getting visible by changing the texture coordinates. Try to set the texture filtering method to GL_NEAREST to see the pixels more clearly.

float vertices[] = {0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.6f, 0.6f,-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.4f, 0.6f,0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.6f, 0.4f,-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.4f, 0.4f};
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_GL_LINEAR); //纹理被缩小时的过滤方式对该题没有影响
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

4. Use a uniform variable as the mix function's third parameter to vary the amount the two textures are visible. Use the up and down arrow keys to change how much the container or the smiley face is visible.

#version 330 core
out vec4 FragColor;
in vec4 Color;
in vec2 texCoord;
uniform sampler2D texSampler1;
uniform sampler2D texSampler2;
uniform float ratio;
void main()
{FragColor = mix(texture(texSampler1, texCoord), texture(texSampler2, vec2(-texCoord.x, texCoord.y)), ratio);
}
ourShader.use();
flat fragmentRatio = 0.0f;
glUniform1f(glGetUniformLocation(ourShader.ID, "ratio"), fragmentRatio);
//render loop
while(!glfwWindowShouldClose(window)){processInput(window);glClearColor(0.2f, 0.3f, 0.3f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);ourShader.use();if(glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS){fragmentRatio += 0.01f;glUniform1f(glGetUniformLocation(ourShader.ID, "ratio"), fragmentRatio);}if(glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS){fragmentRatio -= 0.01f;glUniform1f(glGetUniformLocation(ourShader.ID, "ratio"), fragmentRatio);}glBindVertexArray(VAO);glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, texture[0]);glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D, texture[1]);glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);glfwSwapBuffers(window);glfwPollEvents();
}

转载于:https://www.cnblogs.com/yiqian/p/10872525.html

OpenGL学习(4)——纹理(补)相关推荐

  1. OpenGL学习笔记——纹理贴图

    简单地说,纹理就是矩形的数据数组.例如,颜色数据.亮度数据.颜色和alpha数据.纹理数组中的单个值常常称为纹理单元(texel).纹理贴图之所以复杂,是因为矩形的纹理可以映射到非矩形的区域,并且必须 ...

  2. OpenGL学习笔记(十三):将纹理贴图应用到四边形上,对VAO/VBO/EBO/纹理/着色器的使用方式进行总结

    原博主博客地址:http://blog.csdn.net/qq21497936 本文章博客地址:http://blog.csdn.net/qq21497936/article/details/7919 ...

  3. OpenGL学习(九)阴影映射(shadowMapping)

    目录 写在前面 阴影映射原理简介 封装 Camera 类 帧缓冲 阴影映射 准备工作 创建帧缓冲与深度纹理附件 从光源方向进行渲染 正常地渲染场景 如何查找bug(⚠重要) 多纹理传送 查看深度纹理数 ...

  4. OpenGL学习之着色器详解

    OpenGL着色器语言(GLSL)看上去很像C语言,它由OpenGL实现进行编译和连接,并且(经常是)完全在图形硬件中运行. 我们有顶点着色器.片段着色器和几何着色器,前两种是必需的,后一种是可选的. ...

  5. OpenGL学习(1)

    免费课程:计算机图形学OpenGL:5.2.缩放,旋转,位移_哔哩哔哩_bilibili 付费课程:计算机图形学OpenGL[合集]大礼包_腾讯课堂 我看的是免费版的 1.1,状态机-上下文-对象 G ...

  6. OpenGL学习之路17---- 镜面反射光

    代码放在github上 根据教程:ogldev一步步开始,记录学习历程 之前完成环境光和漫射光的学习.环境光的计算只由光强来决定,场景中所有位置是同一亮度:漫射光的计算由光强和光的方向一同决定,相关博 ...

  7. 【我的OpenGL学习进阶之旅】C++如何加载TGA文件?

    一.TGA文件相关介绍 通过前面的博客 [我的OpenGL学习进阶之旅]什么是TGA文件以及如何打开TGA文件? 地址:https://ouyangpeng.blog.csdn.net/article ...

  8. OpenGL学习(十)天空盒

    目录 写在前面 天空盒简介 创建立方体贴图 渲染一个立方体 立方体贴图着色器 开始绘制天空盒 完整代码 着色器 c++ 写在前面 上一篇博客回顾:OpenGL学习(九)阴影映射(shadowMappi ...

  9. 【我的OpenGL学习进阶之旅】【持续更新】关于学习OpenGL的一些资料

    目录 一.相关书籍 OpenGL 方面 C方面 NDK 线性代数 二.相关博客 2.0 一些比较官方的链接 2.1 OpenGL着色器语言相关 2.2 [[yfan]](https://segment ...

  10. 8.OpenGL学习之颜色混合

    颜色混合 通常情况下OpenGL渲染时会把颜色值放在颜色缓冲区中.每个片段的深度值也是放在深度缓冲区中的.当深度测试被关闭(禁用)时,新的颜色值简单得覆盖颜色缓冲区中已经存在的其他值.当深度测试被打开 ...

最新文章

  1. iOS 的本地化使用和创建过程
  2. 使用Spring Cloud Config作为外部化配置
  3. HTML+CSS+JS实现 ❤️圆圈波纹动画特效❤️
  4. 【中生代技术】1024程序员节快跑,提需求的来了
  5. Java设计模式1:设计模式概论
  6. Web安全之权限攻击
  7. 基于Android Classic Bluetooth的蓝牙聊天软件
  8. 数据预处理之将类别数据数字化的方法 —— LabelEncoder VS OneHotEncoder
  9. java 定义接口school_Java接口介绍
  10. 卓有成效的程序员读书笔记
  11. MFC CListCtrl 将一个列表的选中项添加到另一个列表
  12. 用在线RaxML构建系统发育树
  13. java pdf 水印 加密_Java生成PDF 加密 水印
  14. 从平面坐标转球面坐标加旋转
  15. ios 清理缓存功能实现
  16. Activity初窥门径
  17. 钉钉开放平台查询宜搭表单实例数据
  18. Python实用功能之pdf文件转png图片数据
  19. 四川大学计算机考研专业参考书目,四川大学计算机技术(专业学位)研究生考试科目和考研参考书目...
  20. PFC的数据类型及命名规则

热门文章

  1. 使用嵌入式关系型SQLite数据库存储数据
  2. 用“ul+li”及css制作韩国风格菜单
  3. ec20 复位命令_《EC20 — AT指令》
  4. react-native 查看对象属性
  5. 汇编指令mrs_汇编指令 - Mrs.kang - 博客园
  6. dart参数传方法_为 JavaScript 开发人员准备的 Dart 参考教程
  7. CAB归档文件提取工具cabextract
  8. PlayMaker的Transition和Global Transition
  9. 加强版dd工具dc3dd
  10. centos7升级python到3_CentOS7 升级Python2.x到3.x