[Bf-blender-cvs] [e1b8a5d] master: OpenGL: set OpenGL lights through simple shader API.

Brecht Van Lommel noreply at git.blender.org
Sat Nov 28 15:36:36 CET 2015


Commit: e1b8a5dc6fc8a3f7a5100a9c66b95d07c39b1cb3
Author: Brecht Van Lommel
Date:   Fri Nov 27 21:08:48 2015 +0100
Branches: master
https://developer.blender.org/rBe1b8a5dc6fc8a3f7a5100a9c66b95d07c39b1cb3

OpenGL: set OpenGL lights through simple shader API.

===================================================================

M	source/blender/editors/interface/interface_draw.c
M	source/blender/gpu/intern/gpu_draw.c

===================================================================

diff --git a/source/blender/editors/interface/interface_draw.c b/source/blender/editors/interface/interface_draw.c
index 84767ea..97af45f 100644
--- a/source/blender/editors/interface/interface_draw.c
+++ b/source/blender/editors/interface/interface_draw.c
@@ -55,6 +55,9 @@
 
 #include "BLF_api.h"
 
+#include "GPU_draw.h"
+#include "GPU_simple_shader.h"
+
 #include "UI_interface.h"
 
 /* own include */
@@ -1216,14 +1219,12 @@ void ui_draw_but_COLORBAND(uiBut *but, uiWidgetColors *UNUSED(wcol), const rcti
 void ui_draw_but_UNITVEC(uiBut *but, uiWidgetColors *wcol, const rcti *rect)
 {
 	static GLuint displist = 0;
-	int a, old[8];
-	GLfloat diff[4], diffn[4] = {1.0f, 1.0f, 1.0f, 1.0f};
-	float vec0[4] = {0.0f, 0.0f, 0.0f, 0.0f};
-	float dir[4], size;
+	float diff[4], diffn[4] = {1.0f, 1.0f, 1.0f, 1.0f};
+	float size;
 	
 	/* store stuff */
 	glGetMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
-		
+
 	/* backdrop */
 	glColor3ubv((unsigned char *)wcol->inner);
 	UI_draw_roundbox_corner_set(UI_CNR_ALL);
@@ -1234,24 +1235,18 @@ void ui_draw_but_UNITVEC(uiBut *but, uiWidgetColors *wcol, const rcti *rect)
 	glCullFace(GL_BACK);
 	glEnable(GL_CULL_FACE);
 	
-	/* disable blender light */
-	for (a = 0; a < 8; a++) {
-		old[a] = glIsEnabled(GL_LIGHT0 + a);
-		glDisable(GL_LIGHT0 + a);
-	}
-	
-	/* own light */
-	glEnable(GL_LIGHT7);
-	glEnable(GL_LIGHTING);
-	
-	ui_but_v3_get(but, dir);
+	/* setup lights */
+	GPULightData light = {0};
+	light.type = GPU_LIGHT_SUN;
+	copy_v3_v3(light.diffuse, diffn);
+	zero_v3(light.specular);
+	ui_but_v3_get(but, light.direction);
+
+	GPU_simple_shader_light_set(0, &light);
+	for (int a = 1; a < 8; a++)
+		GPU_simple_shader_light_set(a, NULL);
 
-	dir[3] = 0.0f;   /* glLightfv needs 4 args, 0.0 is sun */
-	glLightfv(GL_LIGHT7, GL_POSITION, dir); 
-	glLightfv(GL_LIGHT7, GL_DIFFUSE, diffn); 
-	glLightfv(GL_LIGHT7, GL_SPECULAR, vec0); 
-	glLightf(GL_LIGHT7, GL_CONSTANT_ATTENUATION, 1.0f);
-	glLightf(GL_LIGHT7, GL_LINEAR_ATTENUATION, 0.0f);
+	glEnable(GL_LIGHTING);
 	
 	/* transform to button */
 	glPushMatrix();
@@ -1283,10 +1278,10 @@ void ui_draw_but_UNITVEC(uiBut *but, uiWidgetColors *wcol, const rcti *rect)
 	glCallList(displist);
 
 	/* restore */
+	GPU_default_lights();
 	glDisable(GL_LIGHTING);
 	glDisable(GL_CULL_FACE);
 	glMaterialfv(GL_FRONT, GL_DIFFUSE, diff); 
-	glDisable(GL_LIGHT7);
 	
 	/* AA circle */
 	glEnable(GL_BLEND);
@@ -1298,12 +1293,6 @@ void ui_draw_but_UNITVEC(uiBut *but, uiWidgetColors *wcol, const rcti *rect)
 
 	/* matrix after circle */
 	glPopMatrix();
-
-	/* enable blender light */
-	for (a = 0; a < 8; a++) {
-		if (old[a])
-			glEnable(GL_LIGHT0 + a);
-	}
 }
 
 static void ui_draw_but_curve_grid(const rcti *rect, float zoomx, float zoomy, float offsx, float offsy, float step)
diff --git a/source/blender/gpu/intern/gpu_draw.c b/source/blender/gpu/intern/gpu_draw.c
index 46924a3..193230d 100644
--- a/source/blender/gpu/intern/gpu_draw.c
+++ b/source/blender/gpu/intern/gpu_draw.c
@@ -78,6 +78,7 @@
 #include "GPU_draw.h"
 #include "GPU_extensions.h"
 #include "GPU_material.h"
+#include "GPU_simple_shader.h"
 
 #include "PIL_time.h"
 
@@ -1894,7 +1895,6 @@ void GPU_end_object_materials(void)
 
 int GPU_default_lights(void)
 {
-	float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f}, position[4];
 	int a, count = 0;
 	
 	/* initialize */
@@ -1918,41 +1918,27 @@ int GPU_default_lights(void)
 		U.light[2].spec[3] = 1.0;
 	}
 
-	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
+	GPU_simple_shader_light_set_viewer(false);
 
 	for (a = 0; a < 8; a++) {
-		if (a < 3) {
-			if (U.light[a].flag) {
-				glEnable(GL_LIGHT0 + a);
+		if (a < 3 && U.light[a].flag) {
+			GPULightData light = {0};
 
-				normalize_v3_v3(position, U.light[a].vec);
-				position[3] = 0.0f;
-				
-				glLightfv(GL_LIGHT0 + a, GL_POSITION, position); 
-				glLightfv(GL_LIGHT0 + a, GL_DIFFUSE, U.light[a].col); 
-				glLightfv(GL_LIGHT0 + a, GL_SPECULAR, U.light[a].spec); 
+			light.type = GPU_LIGHT_SUN;
 
-				count++;
-			}
-			else {
-				glDisable(GL_LIGHT0 + a);
+			normalize_v3_v3(light.direction, U.light[a].vec);
+			copy_v3_v3(light.diffuse, U.light[a].col);
+			copy_v3_v3(light.specular, U.light[a].spec);
 
-				glLightfv(GL_LIGHT0 + a, GL_POSITION, zero); 
-				glLightfv(GL_LIGHT0 + a, GL_DIFFUSE, zero); 
-				glLightfv(GL_LIGHT0 + a, GL_SPECULAR, zero);
-			}
+			GPU_simple_shader_light_set(a, &light);
 
-			/* clear stuff from other opengl lamp usage */
-			glLightf(GL_LIGHT0 + a, GL_SPOT_CUTOFF, 180.0);
-			glLightf(GL_LIGHT0 + a, GL_CONSTANT_ATTENUATION, 1.0);
-			glLightf(GL_LIGHT0 + a, GL_LINEAR_ATTENUATION, 0.0);
+			count++;
 		}
 		else
-			glDisable(GL_LIGHT0 + a);
+			GPU_simple_shader_light_set(a, NULL);
 	}
-	
-	glDisable(GL_LIGHTING);
 
+	glDisable(GL_LIGHTING);
 	glDisable(GL_COLOR_MATERIAL);
 
 	return count;
@@ -1963,15 +1949,14 @@ int GPU_scene_object_lights(Scene *scene, Object *ob, int lay, float viewmat[4][
 	Base *base;
 	Lamp *la;
 	int count;
-	float position[4], direction[4], energy[4];
 	
 	/* disable all lights */
 	for (count = 0; count < 8; count++)
-		glDisable(GL_LIGHT0 + count);
+		GPU_simple_shader_light_set(count, NULL);
 	
 	/* view direction for specular is not computed correct by default in
 	 * opengl, so we set the settings ourselfs */
-	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, ortho ? GL_FALSE : GL_TRUE);
+	GPU_simple_shader_light_set_viewer(!ortho);
 
 	count = 0;
 
@@ -1988,41 +1973,37 @@ int GPU_scene_object_lights(Scene *scene, Object *ob, int lay, float viewmat[4][
 		glPushMatrix();
 		glLoadMatrixf((float *)viewmat);
 		
-		if (la->type == LA_SUN) {
-			/* sun lamp */
-			copy_v3_v3(direction, base->object->obmat[2]);
-			direction[3] = 0.0;
+		/* setup light */
+		GPULightData light = {0};
+
+		mul_v3_v3fl(light.diffuse, &la->r, la->energy);
+		mul_v3_v3fl(light.specular, &la->r, la->energy);
 
-			glLightfv(GL_LIGHT0+count, GL_POSITION, direction); 
+		if (la->type == LA_SUN) {
+			/* directional sun light */
+			light.type = GPU_LIGHT_SUN;
+			normalize_v3_v3(light.direction, base->object->obmat[2]);
 		}
 		else {
-			/* other lamps with attenuation */
-			copy_v3_v3(position, base->object->obmat[3]);
-			position[3] = 1.0f;
-
-			glLightfv(GL_LIGHT0+count, GL_POSITION, position); 
-			glLightf(GL_LIGHT0+count, GL_CONSTANT_ATTENUATION, 1.0);
-			glLightf(GL_LIGHT0+count, GL_LINEAR_ATTENUATION, la->att1 / la->dist);
-			glLightf(GL_LIGHT0+count, GL_QUADRATIC_ATTENUATION, la->att2 / (la->dist * la->dist));
+			/* other lamps with position attenuation */
+			copy_v3_v3(light.position, base->object->obmat[3]);
+
+			light.constant_attenuation = 1.0f;
+			light.linear_attenuation = la->att1 / la->dist;
+			light.quadratic_attenuation = la->att2 / (la->dist * la->dist);
 			
 			if (la->type == LA_SPOT) {
-				/* spot lamp */
-				negate_v3_v3(direction, base->object->obmat[2]);
-				glLightfv(GL_LIGHT0 + count, GL_SPOT_DIRECTION, direction);
-				glLightf(GL_LIGHT0 + count, GL_SPOT_CUTOFF, RAD2DEGF(la->spotsize * 0.5f));
-				glLightf(GL_LIGHT0 + count, GL_SPOT_EXPONENT, 128.0f * la->spotblend);
+				light.type = GPU_LIGHT_SPOT;
+				negate_v3_v3(light.direction, base->object->obmat[2]);
+				normalize_v3(light.direction);
+				light.spot_cutoff = RAD2DEGF(la->spotsize * 0.5f);
+				light.spot_exponent = 128.0f * la->spotblend;
 			}
 			else
-				glLightf(GL_LIGHT0 + count, GL_SPOT_CUTOFF, 180.0);
+				light.type = GPU_LIGHT_POINT;
 		}
 		
-		/* setup energy */
-		mul_v3_v3fl(energy, &la->r, la->energy);
-		energy[3] = 1.0;
-
-		glLightfv(GL_LIGHT0 + count, GL_DIFFUSE, energy); 
-		glLightfv(GL_LIGHT0 + count, GL_SPECULAR, energy);
-		glEnable(GL_LIGHT0 + count);
+		GPU_simple_shader_light_set(count, &light);
 		
 		glPopMatrix();
 		
@@ -2092,6 +2073,7 @@ void GPU_state_init(void)
 	glDisable(GL_DEPTH_TEST);
 	glDisable(GL_FOG);
 	glDisable(GL_LIGHTING);
+	glDisable(GL_COLOR_MATERIAL);
 	glDisable(GL_LOGIC_OP);
 	glDisable(GL_STENCIL_TEST);
 	glDisable(GL_TEXTURE_1D);




More information about the Bf-blender-cvs mailing list