当前位置: 首页 > news >正文

OpenGL学习(3)——Shader(补)

完成章节后练习。

练习

1. Adjust the vertex shader so that the triangle is upside down.

#version 330 core
layout (location = 0) in vec3 Pos;
layout (location = 1) in vec3 Col;
out vec4 Color;
void main()
{
    gl_Position = vec4(Pos.x, -Pos.y, Pos.z, 1.0f);
    Color = vec4(Col, 1.0f);
}

2. Specify a horizontal offset via a uniform and move the triangle to the right side of the screen in the vertex shader using this offset value.

#version 330 core
layout (location = 0) in vec3 Pos;
layout (location = 1) in vec3 Col;
uniform float offset;
out vec4 Color;
void main()
{
    gl_Position = vec4(Pos.x + offset, Pos.y, Pos.z, 1.0f);
    Color = vec4(Col, 1.0f);
}
ourShader.use();
float offset = 0.5;
int uniformlocation = glGetUniformLocation(ourShader.ID, "offset");
glUniform1f(uniformlocation, offset);

3. Output the vertex position to the fragment shader using the out keyword and set the fragment's color equal to this vertex position (see how even the vertex position values are interpolated across the triangle). Once you managed to do this; try to answer the following question: why is the bottom-left side of our triangle black?

#version 330 core
layout (location = 0) in vec3 Pos;
layout (location = 1) in vec3 Col;
out vec3 fragPos;
void main()
{
    gl_Position = vec4(Pos.x, Pos.y, Pos.z, 1.0f);
    fragPos = Pos;
}
#version 330 core
out vec4 FragColor;
in vec3 fragPos;
void main()
{
    FragColor = vec4(fragPos, 1.0f);
}

1373119-20190502012317448-483271941.png
左上角顶点坐标(-0.5, 0.5, 0),右下角顶点坐标(0.5, -0.5, 0),在顶点间二分之一处插值得到的结果是(0, 0, 0),因此渲染成黑色。

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

相关文章:

  • WebService到底是什么?
  • Java基础教程(24)--集合
  • 网页Gzip
  • 阿里云服务器防火墙相关命令
  • struts_22_xwork校验器列表使用说明
  • 洛谷 P1126 机器人搬重物
  • 自定义starter
  • 【转】js onclick用法:跳转到指定URL
  • 从0到1实现一个模块间通信的服务组件
  • Redis执行Lua脚本的情况
  • Java Static解析
  • 结构型模式:享元模式
  • Qtum研究院:近400万枚比特币不翼而飞?
  • 最大开源代码sourceforge 简介 及视音频方面常用的开源代码
  • SpringBoot 教程之 profile
  • Android Volley源码解析
  • Angular Elements 及其运作原理
  • es6--symbol
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • JavaScript对象详解
  • vue--为什么data属性必须是一个函数
  • Wamp集成环境 添加PHP的新版本
  • 给初学者:JavaScript 中数组操作注意点
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 如何实现 font-size 的响应式
  • 世界上最简单的无等待算法(getAndIncrement)
  • 微服务核心架构梳理
  •  一套莫尔斯电报听写、翻译系统
  • C# - 为值类型重定义相等性
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • ​如何防止网络攻击?
  • #NOIP 2014# day.1 T2 联合权值
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (待修改)PyG安装步骤
  • (定时器/计数器)中断系统(详解与使用)
  • (附源码)计算机毕业设计大学生兼职系统
  • (理论篇)httpmoudle和httphandler一览
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (三)Hyperledger Fabric 1.1安装部署-chaincode测试
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (算法)前K大的和
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (一)VirtualBox安装增强功能
  • (转)树状数组
  • (转)项目管理杂谈-我所期望的新人
  • (转载)(官方)UE4--图像编程----着色器开发
  • (转载)虚函数剖析
  • ******之网络***——物理***
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .a文件和.so文件
  • .Net IOC框架入门之一 Unity
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .NET 中的轻量级线程安全