完成章节后练习

练习

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)——纹理(补)的更多相关文章

  1. OpenGL学习笔记(四)纹理

    目录 要完成的纹理效果 纹理环绕方式 纹理过滤 多级渐远纹理 加载与创建纹理 stb_image库的使用方法 生成纹理对象 应用纹理 纹理单元 参考资料:OpenGL中文翻译 要完成的纹理效果 纹理是 ...

  2. OpenGL学习进程(12)第九课:矩阵乘法实现3D变换

    本节是OpenGL学习的第九个课时,下面将详细介绍OpenGL的多种3D变换和如何操作矩阵堆栈.     (1)3D变换: OpenGL中绘制3D世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...

  3. OpenGL学习之路(一)

    1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...

  4. OpenGL学习之路(三)

    1 引子 这些天公司一次次的软件发布节点忙的博主不可开交,另外还有其它的一些事也占用了很多时间.现在坐在电脑前,在很安静的环境下,与大家分享自己的OpenGL学习笔记和理解心得,感到格外舒服.这让我回 ...

  5. opengl学习笔记

    准备: 1.准备资源:从GLEW1.13.0下载GLEW,并且解压出glew-1.13.0目录.从FreeGLUT官网下载3.0.0版本.直接从这里下的编译后的FreeGLUT,选for MSVC,下 ...

  6. OpenGL学习进程(7)第五课:点、边和图形(二)边

    本节是OpenGL学习的第五个课时,下面介绍OpenGL边的相关知识: (1)边的概念: 数学上的直线没有宽度,但OpenGL的直线则是有宽度的.同时,OpenGL的直线必须是有限长度,而不是像数学概 ...

  7. OpenGL学习进程(5)第三课:视口与裁剪区域

    本节是OpenGL学习的第三个课时,下面介绍如何运用显示窗体的视口和裁剪区域:     (1)知识点引入:     1)问题现象: 当在窗体中绘制图形后,拉伸窗体图形形状会发生变化: #include ...

  8. OpenGL学习进程(4)第二课:绘制图形

    本节是OpenGL学习的第二个课时,下面介绍如何用点和线来绘制图形:     (1)用点的坐标来绘制矩形: #include <GL/glut.h> void display(void) ...

  9. OpenGL学习笔记3——缓冲区对象

    在GL中特别提出了缓冲区对象这一概念,是针对提高绘图效率的一个手段.由于GL的架构是基于客户——服务器模型建立的,因此默认所有的绘图数据均是存储在本地客户端,通过GL内核渲染处理以后再将数据发往GPU ...

  10. OpenGL学习进程(11)第八课:颜色绘制的详解

        本节是OpenGL学习的第八个课时,下面将详细介绍OpenGL的颜色模式,颜色混合以及抗锯齿.     (1)颜色模式: OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. R ...

随机推荐

  1. 一篇文章教你如何部署.NET Core WPF应用,你还在等什么?

    DevExpress广泛应用于ECM企业内容管理. 成本管控.进程监督.生产调度,在企业/政务信息化管理中占据一席重要之地.通过DevExpress WPF Controls,您能创建有着强大互动功能 ...

  2. linux centos ftp服务器搭建

    原文参考 步骤一:构建vsftpd服务器 1)使用yum安装vsftpd软件包yum -y install vsftpdrpm -q vsftpd #确认安装成功,显示vsftpd对应版本 vsftp ...

  3. JSONP劫持

    发出空refer的POC <!DOCTYPE html> <html> <head> <meta name="referrer" cont ...

  4. python3.6中 字典类型和字符串类型互相转换的方法

    mydic = {"俄罗斯": {"1":"圣彼得堡", "2":"叶卡捷琳堡", "3& ...

  5. 题解 [JOI 2019 Final] 硬币收藏

    题面 解析 首先题目可以理解为把一些点放进一个框里,每个格子只能放一个. 那么显然你可以先把这个点移到框里离它最近的格子里, (这个时候格子里可以放很多个) 然后再在框里乱跑移动. 那么我们先考虑只有 ...

  6. jvm参考(生产使用)

    #4g JAVA_OPTS=-Xms3g -Xmx3g -XX:+PrintFlagsFinal -XX:+UnlockDiagnosticVMOptions -XX:NewRatio=2 -XX:P ...

  7. BZOJ 1420 Discrete Root

    思路:数学大汇总 提交:\(3\)次 错因:有一个\(j\)写成\(i\) 题解: 求:\(x^k \equiv a \mod p\) 我们先转化一下:求出\(p\)的原根\(g\) 然后我们用\(B ...

  8. 使用 ALinq 实现 Linq to MySQL【转】

    http://www.cnblogs.com/huangcong/archive/2011/05/24/2055204.html

  9. @Async 异步注释 @EnableAsync

    @SpringBootApplication @ComponentScan(basePackages = "com.fddsfsg") //@EnableSwagger2 @Ena ...

  10. bzoj2725

    * 给出一张图 * 每次删掉一条边后求 the shortest path from S to T * 线段树维护最短路径树 * 具体维护从某点开始偏离最短路而到达 T 的最小距离 * 首先记录下最短 ...