0

Opengl Shader

Hi guys, I'm following tutorial from learnopengl.com site and i am a beginner my question is does every mesh in the scene need its own vertex and fragment shader? and also the camera to!! so for every mesh i need to render it is require to calculate the projection Metrix is that mean i need to pass camera object for every single mesh in the scene? if that so i will have very performance issue

19th Jun 2021, 10:26 AM
Eyob
Eyob - avatar
4 Answers
+ 2
As I already wrote in my first answer the camera is represented by the view matrix which is passed to the vertex shader as uniform. What that means is that you recalculate the view matrix each time the camera changes its position or orientation which is usually at most one time per frame. Then you load it into the OpenGL shader program (with glUniform). This way the vertex shader is able to use it from there on. It uses this view matrix until you load in another one. So not every mesh needs access to the camera object which contains the view matrix because once you have given it to OpenGL it will be used for all further rendering anyway. The only reason a mesh would need a reference to the camera object would be that there is some kind of interaction between the mesh and the camera. For just rendering the mesh it does not need to know anything about a camera.
23rd Jun 2021, 6:46 PM
Hape
Hape - avatar
+ 1
Eyob No not every mesh needs its own vertex and fragment shader. Multiple meshes can use the same shaders. You don't need a shader for the camera. The "camera" is usually represented by a view matrix which is passed to the vertex shader as a uniform. You usually don't need to recalculate the projection matrix very often. You only need to calculate it when you want the projection to change for example when the window size changes or you want to switch between orthographic and perspective projection etc. If you are a beginner don't worry to much about performance for now. Unless you have thousands of draw calls per frame you should be fine.
19th Jun 2021, 2:53 PM
Hape
Hape - avatar
0
Hi Hape, First Thankyou very much for answering, I think i made a mistake when i try to describe my question, I meant the View Matrix (not Projection Matrix) my bad! I'm sorry!! When i say "the camera too" i meant does every mash need to access the camera in order to get the lookAt() Matrix, so when i create new mesh do i need to pass the camera object? my English is really bad i know!! Thankyou in advance..
23rd Jun 2021, 6:03 PM
Eyob
Eyob - avatar
0
Thankyou for the lesson bro!! keep up a good work!!
25th Jun 2021, 1:38 PM
Eyob
Eyob - avatar