[Bf-blender-cvs] [be2bc7e] blender2.8: OpenGL: draw color picker wheel with new immediate mode

Mike Erwin noreply at git.blender.org
Sun Aug 21 04:25:30 CEST 2016


Commit: be2bc7e0f66b9ed1c863790b3555aa3e7db4cb76
Author: Mike Erwin
Date:   Sat Aug 20 15:40:08 2016 -0400
Branches: blender2.8
https://developer.blender.org/rBbe2bc7e0f66b9ed1c863790b3555aa3e7db4cb76

OpenGL: draw color picker wheel with new immediate mode

Includes new imm_draw_lined_circle function that can be used for other
widgets.

Part of T49043

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

M	source/blender/editors/include/BIF_glutil.h
M	source/blender/editors/interface/interface_widgets.c
M	source/blender/editors/screen/glutil.c

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

diff --git a/source/blender/editors/include/BIF_glutil.h b/source/blender/editors/include/BIF_glutil.h
index dfb22ad..40d4639 100644
--- a/source/blender/editors/include/BIF_glutil.h
+++ b/source/blender/editors/include/BIF_glutil.h
@@ -81,6 +81,20 @@ void glutil_draw_lined_arc(float start, float angle, float radius, int nsegments
 void glutil_draw_filled_arc(float start, float angle, float radius, int nsegments);
 
 /**
+ * Draw a circle outline with the given \a radius,
+ * starting at angle \a start and arcing through
+ * \a angle. The circle is centered at \a x, \a y
+ * and drawn in the XY plane.
+ *
+ * \param pos The vertex attribute number for position.
+ * \param x Horizontal center.
+ * \param y Vertical center.
+ * \param radius The circle's radius.
+ * \param nsegments The number of segments to use in drawing (more = smoother).
+ */
+void imm_draw_lined_circle(unsigned pos, float x, float y, float radius, int nsegments);
+
+/**
  * Returns a float value as obtained by glGetFloatv.
  * The param must cause only one value to be gotten from GL.
  */
diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c
index 02981b5..c22a310 100644
--- a/source/blender/editors/interface/interface_widgets.c
+++ b/source/blender/editors/interface/interface_widgets.c
@@ -58,6 +58,8 @@
 #include "interface_intern.h"
 
 #include "GPU_basic_shader.h"
+#include "GPU_shader.h"
+#include "GPU_immediate.h"
 
 #ifdef WITH_INPUT_IME
 #  include "WM_types.h"
@@ -2266,18 +2268,17 @@ void ui_hsvcircle_pos_from_vals(uiBut *but, const rcti *rect, float *hsv, float
 
 static void ui_draw_but_HSVCIRCLE(uiBut *but, uiWidgetColors *wcol, const rcti *rect)
 {
+	/* TODO(merwin): reimplement as shader for pixel-perfect colors */
+
 	const int tot = 64;
 	const float radstep = 2.0f * (float)M_PI / (float)tot;
 	const float centx = BLI_rcti_cent_x_fl(rect);
 	const float centy = BLI_rcti_cent_y_fl(rect);
 	float radius = (float)min_ii(BLI_rcti_size_x(rect), BLI_rcti_size_y(rect)) / 2.0f;
 
-	/* gouraud triangle fan */
 	ColorPicker *cpicker = but->custom_data;
 	const float *hsv_ptr = cpicker->color_data;
-	float xpos, ypos, ang = 0.0f;
 	float rgb[3], hsvo[3], hsv[3], col[3], colcent[3];
-	int a;
 	bool color_profile = ui_but_is_colorpicker_display_space(but);
 		
 	/* color */
@@ -2308,11 +2309,18 @@ static void ui_draw_but_HSVCIRCLE(uiBut *but, uiWidgetColors *wcol, const rcti *
 	
 	ui_color_picker_to_rgb(0.0f, 0.0f, hsv[2], colcent, colcent + 1, colcent + 2);
 
-	glBegin(GL_TRIANGLE_FAN);
-	glColor3fv(colcent);
-	glVertex2f(centx, centy);
+	VertexFormat* format = immVertexFormat();
+	unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
+	unsigned color = add_attrib(format, "color", GL_FLOAT, 3, KEEP_FLOAT);
+
+	immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR);
+
+	immBegin(GL_TRIANGLE_FAN, tot + 2);
+	immAttrib3fv(color, colcent);
+	immVertex2f(pos, centx, centy);
 	
-	for (a = 0; a <= tot; a++, ang += radstep) {
+	float ang = 0.0f;
+	for (int a = 0; a <= tot; a++, ang += radstep) {
 		float si = sinf(ang);
 		float co = cosf(ang);
 		
@@ -2320,25 +2328,37 @@ static void ui_draw_but_HSVCIRCLE(uiBut *but, uiWidgetColors *wcol, const rcti *
 
 		ui_color_picker_to_rgb_v(hsv, col);
 
-		glColor3fv(col);
-		glVertex2f(centx + co * radius, centy + si * radius);
+		immAttrib3fv(color, col);
+		immVertex2f(pos, centx + co * radius, centy + si * radius);
 	}
-	glEnd();
-	
+	immEnd();
+	immUnbindProgram();
+
 	/* fully rounded outline */
-	glPushMatrix();
-	glTranslatef(centx, centy, 0.0f);
+	format = immVertexFormat();
+	pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
+
+	immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
+
 	glEnable(GL_BLEND);
 	glEnable(GL_LINE_SMOOTH);
-	glColor3ubv((unsigned char *)wcol->outline);
-	glutil_draw_lined_arc(0.0f, M_PI * 2.0, radius, tot + 1);
+
+	const float scale = 1.0f / 255.0f; /* TODO: treat as sRGB? */
+	const float outline_r = scale * (unsigned char)wcol->outline[0];
+	const float outline_g = scale * (unsigned char)wcol->outline[1];
+	const float outline_b = scale * (unsigned char)wcol->outline[2];
+
+	immUniform4f("color", outline_r, outline_g, outline_b, 1.0f);
+	imm_draw_lined_circle(pos, centx, centy, radius, tot);
+
 	glDisable(GL_BLEND);
 	glDisable(GL_LINE_SMOOTH);
-	glPopMatrix();
+
+	immUnbindProgram();
 
 	/* cursor */
+	float xpos, ypos;
 	ui_hsvcircle_pos_from_vals(but, rect, hsvo, &xpos, &ypos);
-
 	ui_hsv_cursor(xpos, ypos);
 }
 
diff --git a/source/blender/editors/screen/glutil.c b/source/blender/editors/screen/glutil.c
index b99927c..3ed89ed 100644
--- a/source/blender/editors/screen/glutil.c
+++ b/source/blender/editors/screen/glutil.c
@@ -49,6 +49,7 @@
 #include "IMB_imbuf_types.h"
 
 #include "GPU_basic_shader.h"
+#include "GPU_immediate.h"
 
 #include "UI_interface.h"
 
@@ -181,6 +182,17 @@ void glutil_draw_lined_arc(float start, float angle, float radius, int nsegments
 	glEnd();
 }
 
+void imm_draw_lined_circle(unsigned pos, float x, float y, float rad, int nsegments)
+{
+	immBegin(GL_LINE_LOOP, nsegments);
+	for (int i = 0; i < nsegments; ++i) {
+		float angle = 2 * M_PI * ((float)i / (float)nsegments);
+		immVertex2f(pos, x + rad * cosf(angle),
+		                 y + rad * sinf(angle));
+	}
+	immEnd();
+}
+
 float glaGetOneFloat(int param)
 {
 	GLfloat v;




More information about the Bf-blender-cvs mailing list