From patchwork Thu Jul 21 12:36:31 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandros Frantzis X-Patchwork-Id: 2948 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id E9E2523F52 for ; Thu, 21 Jul 2011 12:42:35 +0000 (UTC) Received: from mail-qw0-f52.google.com (mail-qw0-f52.google.com [209.85.216.52]) by fiordland.canonical.com (Postfix) with ESMTP id B895DA18287 for ; Thu, 21 Jul 2011 12:42:35 +0000 (UTC) Received: by mail-qw0-f52.google.com with SMTP id 8so853237qwb.11 for ; Thu, 21 Jul 2011 05:42:35 -0700 (PDT) Received: by 10.229.25.212 with SMTP id a20mr174113qcc.148.1311252155512; Thu, 21 Jul 2011 05:42:35 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.229.217.78 with SMTP id hl14cs139504qcb; Thu, 21 Jul 2011 05:42:35 -0700 (PDT) Received: by 10.227.172.199 with SMTP id m7mr163325wbz.85.1311251794578; Thu, 21 Jul 2011 05:36:34 -0700 (PDT) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id fp4si2401217wbb.17.2011.07.21.05.36.34; Thu, 21 Jul 2011 05:36:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) client-ip=91.189.90.139; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) smtp.mail=bounces@canonical.com Received: from loganberry.canonical.com ([91.189.90.37]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1QjsUT-0000Cp-4u for ; Thu, 21 Jul 2011 12:36:33 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id 976972EA03A for ; Thu, 21 Jul 2011 12:36:31 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: glmark2 X-Launchpad-Branch: ~glmark2-dev/glmark2/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 23 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~glmark2-dev/glmark2/trunk] Rev 23: Simplified and improved basic shader efficiency. Message-Id: <20110721123631.17019.24693.launchpad@loganberry.canonical.com> Date: Thu, 21 Jul 2011 12:36:31 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13475"; Instance="initZopeless config overlay" X-Launchpad-Hash: 3ece5682e7bfd9ff1332087b8bfbb49bbd05179c ------------------------------------------------------------ revno: 23 committer: Alexandros Frantzis timestamp: Thu 2010-07-08 18:48:22 +0300 message: Simplified and improved basic shader efficiency. modified: data/shaders/light-basic.frag data/shaders/light-basic.vert --- lp:glmark2 https://code.launchpad.net/~glmark2-dev/glmark2/trunk You are subscribed to branch lp:glmark2. To unsubscribe from this branch go to https://code.launchpad.net/~glmark2-dev/glmark2/trunk/+edit-subscription === modified file 'data/shaders/light-basic.frag' --- data/shaders/light-basic.frag 2010-07-08 15:17:11 +0000 +++ data/shaders/light-basic.frag 2010-07-08 15:48:22 +0000 @@ -1,24 +1,6 @@ -// Because we are using the varying float Diffuse in -// our vertex shader, if we wish to read it in here, we -// need to set the same variable, so I have done this -// first. - -// After that, I am simply changing the line: -// gl_FragColor = vec4(0,0,1,1) -// to -// gl_FragColor = Diffuse * vec4(0,0,1,1); -// This multiplies the intensity of the light by the -// color we originally set the cube to. - -// This can or cannot eliminate the need for materials, -// depending on your needs, but just for the record -// (and future tutorials), materials are worth while -// and can be read from inside GLSL :) - -varying float Diffuse; +varying vec4 Color; void main(void) { - // Multiply the light Diffuse intensity by the color of the - gl_FragColor = Diffuse * vec4(0.0, 0.0, 1.0, 1.0); + gl_FragColor = Color; } === modified file 'data/shaders/light-basic.vert' --- data/shaders/light-basic.vert 2010-07-08 15:17:11 +0000 +++ data/shaders/light-basic.vert 2010-07-08 15:48:22 +0000 @@ -1,63 +1,3 @@ -// For this tutorial, I am going show you how to perform per -// vertex lighting using shaders. Yes, shaders support per pixel -// but this is just a starting point. - -// First I am going to teach you about varying variables. These -// are variables that can be shared between the vertex and fragment -// shaders. These variables are set inside the vertex shader, and -// then read from the fragment shader. In this case we are going -// to share a float type variable from the Vertex shader to the -// Fragment shader. This variable is going to be called Diffuse -// and will hold a number that will tell us how lit our vertex -// should be. - -// Because we are using per vertex lighting, the GLSL shader will -// automatically interpolate the color between the vertices. Just -// like OpenGL does. - -// Now first off we need to know the surface normal of the current -// vertex. Because I am using a GLUT cube, these are already -// calculated and sent off with the glNormal command. Any numbers -// sent from OpenGL through glNormal(1,2,3); can be read with -// the variable gl_Normal. In later tutorials, I will be using -// self calculated normals, using my own normal generation code. - -// Now as for our normal, I am storing it in the variable vec3 Normal. -// But first, I have to multiply the surface normal (gl_Normal) by -// the gl_NormalMatrix. This places the normal in coordinates that -// we can use. (Later on I will show you how to use tangent space -// normals). We also then have to normalize this multiplication so -// that all of the normal vectors are between (and including) -1 and 1. -// This makes sure that we have no scaling errors. - -// Next on, we are going to work with our light. I am storing this -// in a variable called vec3 Light. In this, I am calling for the -// position of glLight0. This is gathered by the call: -// gl_LightSource[0].position.xyz -// This gives us the position of glLight0, and to get the position of -// any other light, we just change the number 0 to the light number we -// are after. We do not need to multiply this expression by the -// gl_NormalMatrix, but we do have to normalize it still. - -// Now that we have our surface Normal and the position of our Light. -// We can now calculate the Diffuse value of our lighting at the -// given vertex. To do this we need to take the dot product of both -// the Normal and the Light together. If you have any idea on -// calculating Normals, then you should have a fair idea of what the -// dot product does. It works as such: -// dot(a,b) = (x1 * x2) + (y1 * y2) + (z1 * z3) -// If this happens to be equal to 0, then the vectors a and b are -// perpendicular (the light meets the surface at a 90 degree angle). -// Because this point is when the light is at its brightest, we need -// to set the maximum value for our diffuse value to 0.0. -// So our Diffuse equation becomes: -// Diffuse = max(dot(Normal, Light),0.0); - -// And from that, we have now calculated the intensity of the light -// at the given vertex. That started to turn into a bit of a maths -// lesson, but it even cleared up some stuff for me without even -// thinking about it. GO MATHS :) - attribute vec3 position; attribute vec3 normal; attribute vec2 texture; @@ -68,15 +8,21 @@ uniform vec3 LightSourceDiffuse; uniform vec3 MaterialDiffuse; -varying float Diffuse; +varying vec4 Color; void main(void) { + // Transform the normal to eye coordinates vec3 N = normalize(vec3(NormalMatrix * vec4(normal, 1.0))); + // The LightSourcePosition is actually its direction for directional light vec3 L = normalize(LightSourcePosition.xyz); - Diffuse = max(dot(N, L), 0.0); + // Multiply the diffuse value by the vertex color (which is fixed in this case) + // to get the actual color that we will use to draw this vertex with + float diffuse = max(dot(N, L), 0.0); + Color = diffuse * vec4(0.0, 0.0, 1.0, 1.0); + // Transform the position to clip coordinates gl_Position = ModelViewProjectionMatrix * vec4(position, 1.0); }