Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninjas! As you all know, designing something is a work that excites many people in today's world. We have made so many designs on paper, clothes, etc. But have you ever tried to make designs using a programing language? Isn't it exciting and challenging at the same time?
So, moving ahead with this excitement, we are here with a design tool named Cinder. Our today's topic is Basic Shaders in Cinder. Let's cover all the basic details one by one.
About Cinder
Cinder is an open-source and free library for skilled, quality creative coding in C++. The cinder library helps with Graphics, Geometry, Audio, Video, and Texture inclusion. It was released in the spring of 2010 as a public tool. It is generally used in a non-browser environment.
Cinder is an OpenStack Block Storage service. It virtualises the control of block storage devices. Also, it offers end users a self-service API to request and use those resources without requiring any knowledge.
Shader
The Shader is a program written in OpenGL Shading Language (GLSL) in the case of OpenGL, which runs on the GPU. This is as opposed to the C++ code, which runs on the CPU.
There are two types of Shaders:
Vertex shader: The Vertex shader works on each vertex of the geometry to which we are applying it.
Fragment shader: The fragment shader works on each pixel, which is interpolated across a triangle when the given geometry is rasterised.
Let's now look at some examples of Basic Shaders in Cinder.
Minimal Shader
We will now see a simple example code of Minimal Shader under the Basic Shaders in Cinder. Let's start.
In the above example, we have used the gl::GlslProg::create() method and passed it a gl::GlslProg::Format. This will construct the CI_GLSL macro for both the vertex() and fragment().
In the CI_GLSL, we have passed the number 15, which tells us about the targeting OpenGL version. We also used the gl::setMatrices() method to set or fix the camera position.
There are mainly two roles of the Vertex shader.
The Vertex shader is responsible for defining the features we want the fragment shader to have access to.
Vertex shaders take vertex positions shown in Object space and output them as positions in the Clip space.
The role of the Fragment shader is to set a final pixel colour. As you can see in the code, we encountered the out variable oColor, which records the results.
Interpolation
We will now look at how variables can be interpolated across a polygon shape using the vertex shader.
Let's first check the code example, and then we will discuss it in the Explanation part.
The above example will create a batch with the custom shader and a geom::Plane. The code will render 30 copies of the given plane, which are rotating around the Z axis and changing with a different colour. Here, we have created a new uniform uColor that is declared and used in the fragment shader.
Textures in Shader
The code to use the textures from shaders under the Basic Shaders in Cinder is given below.
We have modified the last example and added a solid colour with a texture. We have used a variable ciTexCoord0 in the Vertex shader. And the output is a matching TexCoord0. Coming to the fragment shader, we have used the uTex0 uniform of type sampler2D. We have called the bind() in the setup() on the texture and passed it 0.
In this example, we used multiple texture units, so we need to specify all these texture units when we call mGlsl->uniform( "uTex0", 0 ) later.
Manipulating the Vertex shader
Let us now try to perform some more interesting manipulations in the vertex shader under the Basic Shaders in Cinder.
We have used all the key features discussed earlier in this blog in this example. For example, using the CI_GLSL that tells about the version, vertex, fragments, and many more. We have used the setMatrices() method to set or fix the camera position.
Frequently Asked Questions
What is the difference between C++ - based openFrameworks and Cinder?
Cinder uses system-specific libraries for better performance, while openFrameworks affords better control over its underlying libraries.
What is the use of Cinder?
Cinder makes the library more appropriate for heavily abstracted projects, including art installations, commercial campaigns, and other advanced animation work.
What is the cinder framework?
One of the most intriguing frameworks for creative coding is Cinder. It is made in C++ for improved performance and enables rapid development of interactive applications with complex visuals.
What is OpenGL?
Open Graphics Library (OpenGL) is a cross-platform and cross-language API for rendering 2D and 3D vector graphics.
What is the difference between GPU and Raster in graphics in Cinder?
GPU is a specialised hardware responsible for handling OpenGL-related graphics. Whereas, Raster is the pixel-based graphics, slower than GPU equivalents.
Conclusion
This article discusses the topic of Basic Shaders in Cinder. In detail, we have seen the definition of a Shader, and then we have discussed the different types of shaders in detail, along with code and their explanations.
We hope this blog has helped you enhance your knowledge of Basic Shaders in Cinder. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!