OpenGL学习(4)——纹理(补)
完成章节后练习
练习
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();
}
OpenGL学习(4)——纹理(补)的更多相关文章
- OpenGL学习笔记(四)纹理
目录 要完成的纹理效果 纹理环绕方式 纹理过滤 多级渐远纹理 加载与创建纹理 stb_image库的使用方法 生成纹理对象 应用纹理 纹理单元 参考资料:OpenGL中文翻译 要完成的纹理效果 纹理是 ...
- OpenGL学习进程(12)第九课:矩阵乘法实现3D变换
本节是OpenGL学习的第九个课时,下面将详细介绍OpenGL的多种3D变换和如何操作矩阵堆栈. (1)3D变换: OpenGL中绘制3D世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...
- OpenGL学习之路(一)
1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...
- OpenGL学习之路(三)
1 引子 这些天公司一次次的软件发布节点忙的博主不可开交,另外还有其它的一些事也占用了很多时间.现在坐在电脑前,在很安静的环境下,与大家分享自己的OpenGL学习笔记和理解心得,感到格外舒服.这让我回 ...
- opengl学习笔记
准备: 1.准备资源:从GLEW1.13.0下载GLEW,并且解压出glew-1.13.0目录.从FreeGLUT官网下载3.0.0版本.直接从这里下的编译后的FreeGLUT,选for MSVC,下 ...
- OpenGL学习进程(7)第五课:点、边和图形(二)边
本节是OpenGL学习的第五个课时,下面介绍OpenGL边的相关知识: (1)边的概念: 数学上的直线没有宽度,但OpenGL的直线则是有宽度的.同时,OpenGL的直线必须是有限长度,而不是像数学概 ...
- OpenGL学习进程(5)第三课:视口与裁剪区域
本节是OpenGL学习的第三个课时,下面介绍如何运用显示窗体的视口和裁剪区域: (1)知识点引入: 1)问题现象: 当在窗体中绘制图形后,拉伸窗体图形形状会发生变化: #include ...
- OpenGL学习进程(4)第二课:绘制图形
本节是OpenGL学习的第二个课时,下面介绍如何用点和线来绘制图形: (1)用点的坐标来绘制矩形: #include <GL/glut.h> void display(void) ...
- OpenGL学习笔记3——缓冲区对象
在GL中特别提出了缓冲区对象这一概念,是针对提高绘图效率的一个手段.由于GL的架构是基于客户——服务器模型建立的,因此默认所有的绘图数据均是存储在本地客户端,通过GL内核渲染处理以后再将数据发往GPU ...
- OpenGL学习进程(11)第八课:颜色绘制的详解
本节是OpenGL学习的第八个课时,下面将详细介绍OpenGL的颜色模式,颜色混合以及抗锯齿. (1)颜色模式: OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. R ...
随机推荐
- 关于button的onclientclick事件和onclick事件
利用onclientclick事件在onclick事件之前执行,对用户输入文本进行检查,如果不符合规定则retrun false JS代码 function check() { var aLength ...
- .Net优秀应用界面大PK!DevExpress年度大赛,群雄逐鹿花落谁家
DevExpress 优秀界面图片火热征集中! 只要您晒出来,慧都就为您颁奖! 角逐前三,百度AI音箱.小米行李箱等惊喜大礼等您Pick! 活动时间:12月1日-12月31日 立即参与 活动详情 活动 ...
- python-windows安装相关问题
1.python的环境配置,有些时候是没有配置的,需要在[系统环境]-[path]里添加. 2.安装pip:从官网下载pip包,然后到包目录==>python setup.py install ...
- Python 10.1
- 变形课 HDU - 1181 【floyd传递闭包水题】
呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个 ...
- 001_C#我的第一个串口上位机软件
(一)首先感谢杜洋工作室 < 入门 C#设计 >带来的视频教学 (二)本次设计从会单片机但是又不会上位机又想搞简单上位机的人群角度提供教程 (三)本次教程的目的是制作普通的串口软件,从而实 ...
- Oracle 11.2 静默安装脚本
Oracle 11.2 静默安装脚本 cat db_init.sh.20190401 #!/bin/bash####安装Oracle所需依赖包function install_yum(){ yum ...
- https 非对称加密
- python3.5+win7 安装 numpy 和scipy的总结
1.安装numpy. 官网下载numpy.下载地址为https://pypi.Python.org/pypi/numpy.选择与Python版本相对应的numpy版本.之后在python35 ...
- error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const
类中包含信号槽在在类的声明中一定得使用Q_OBJECT.当编译出现问题上述问题时. 解决方法: 1.删除项目中的头文件以及源文件,再添加. 2.在头文件中对该类进行声明,不是使用class mycla ...