[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [49959] branches/soc-2012-swiss_cheese/ source/blender/gpu: Extends gpu_object for glsl by adding color( pointer and single) and texture coord.

Alexander Kuznetsov kuzsasha at gmail.com
Fri Aug 17 19:36:38 CEST 2012


Revision: 49959
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=49959
Author:   alexk
Date:     2012-08-17 17:36:38 +0000 (Fri, 17 Aug 2012)
Log Message:
-----------
Extends gpu_object for glsl by adding color(pointer and single) and texture coord.
Also adding two shader for glsl:
shader_main - simple color
shader_alphatexture - alpha texture (for text).
Later we will have more orgonized shaders.

gpu_object_glsl is available on desktop also (but not used) for OpenGL 3.0+

Modified Paths:
--------------
    branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_object.h
    branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object.c
    branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object_gles.c
    branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object_gles.h

Modified: branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_object.h
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_object.h	2012-08-17 17:04:02 UTC (rev 49958)
+++ branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_object.h	2012-08-17 17:36:38 UTC (rev 49959)
@@ -4,7 +4,9 @@
 	void (*gpuNormalPointer)  (          int type, int stride, const void *pointer);
 	void (*gpuColorPointer )  (int size, int type, int stride, const void *pointer);
 	void (*gpuTexCoordPointer)(int size, int type, int stride, const void *pointer);
+	void (*gpuColorSet)  (const float *value);
 
+
 	void (*gpuClientActiveTexture)(int texture);
 	void (*gpuCleanupAfterDraw)(void);
 

Modified: branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object.c
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object.c	2012-08-17 17:04:02 UTC (rev 49958)
+++ branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object.c	2012-08-17 17:36:38 UTC (rev 49959)
@@ -1,27 +1,31 @@
 #include "GPU_object.h"
-#ifdef GLES
 #include "gpu_object_gles.h"
-#else
+#ifndef GLES
 #include "gpu_object_gl11.h"
 #endif
+#include "GPU_extensions.h"
 
 GPU_object_func gpugameobj = {0}; 
  
 
 void GPU_init_object_func(void)
 {
-#ifdef GLES
 
+if(!GPU_GLTYPE_FIXED_ENABLED)
+{
 gpugameobj.gpuVertexPointer = gpuVertexPointer_gles;
 gpugameobj.gpuNormalPointer = gpuNormalPointer_gles;
 gpugameobj.gpuColorPointer = gpuColorPointer_gles;
 gpugameobj.gpuTexCoordPointer = gpuTexCoordPointer_gles;
 gpugameobj.gpuClientActiveTexture = gpuClientActiveTexture_gles;
 
-gpugameobj.gpuCleanupAfterDraw = gpuCleanupAfterDraw_gles;
+gpugameobj.gpuColorSet = gpuColorSet_gles;
 
-#else
 
+gpugameobj.gpuCleanupAfterDraw = gpuCleanupAfterDraw_gles;
+}
+#ifndef GLES
+else {
 gpugameobj.gpuVertexPointer = gpuVertexPointer_gl11;
 gpugameobj.gpuNormalPointer = gpuNormalPointer_gl11;
 gpugameobj.gpuColorPointer = gpuColorPointer_gl11;
@@ -29,7 +33,7 @@
 gpugameobj.gpuClientActiveTexture = gpuClientActiveTexture_gl11;
 
 gpugameobj.gpuCleanupAfterDraw = gpuCleanupAfterDraw_gl11;
-
+}
 #endif
 
 

Modified: branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object_gles.c
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object_gles.c	2012-08-17 17:04:02 UTC (rev 49958)
+++ branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object_gles.c	2012-08-17 17:36:38 UTC (rev 49959)
@@ -1,10 +1,9 @@
- 
-#ifdef GLES
 
-#include <GLES2/gl2.h>
+#include "gpu_glew.h"
 #include "gpu_object_gles.h"
 
 #include "GPU_functions.h"
+#include "MEM_guardedalloc.h"
 
 #include REAL_GL_MODE
 struct GPUGLSL_ES_info *curglslesi = 0;
@@ -34,20 +33,42 @@
 {
 
 
+	if(curglslesi && (curglslesi->colorloc!=-1))
+	{
+		glEnableVertexAttribArray(curglslesi->colorloc);
+		glVertexAttribPointer(curglslesi->colorloc, size, type, GL_TRUE, stride, pointer);
+	}
 
 
 
-
 }
 
-void gpuTexCoordPointer_gles(int size, int type, int stride, const void *pointer)
+void gpuColorSet_gles(const float *value)
 {
 
 
+	if(curglslesi && (curglslesi->colorloc!=-1))
+	{
+		glDisableVertexAttribArray(curglslesi->colorloc);
+		glVertexAttrib4fv(curglslesi->colorloc, value);
+		
 
+	}
+
+
+
 }
 
+void gpuTexCoordPointer_gles(int size, int type, int stride, const void *pointer)
+{
+	if(curglslesi && (curglslesi->texturecoordloc!=-1))
+	{
+		glEnableVertexAttribArray(curglslesi->texturecoordloc);
+		glVertexAttribPointer(curglslesi->texturecoordloc, size, type, 0, stride, pointer);
+	}
+}
 
+
 void gpuClientActiveTexture_gles(int texture)
 {
 
@@ -72,11 +93,193 @@
 		glslesinfo->normalmatloc = gpu_glGetUniformLocation(program, "b_NormalMatrix");	
 		glslesinfo->viewmatloc = gpu_glGetUniformLocation(program, "b_ModelViewMatrix");	
 		glslesinfo->projectionmatloc = gpu_glGetUniformLocation(program, "b_ProjectionMatrix");
+		glslesinfo->texturecoordloc = gpu_glGetUniformLocation(program, "b_TextureMatrix");
 		
+		glslesinfo->texidloc = gpu_glGetUniformLocation(program, "v_texid");
+		
 		glslesinfo->vertexloc = gpu_glGetAttribLocation(program, "b_Vertex");
 		glslesinfo->normalloc = gpu_glGetAttribLocation(program, "b_Normal");
+		glslesinfo->colorloc = gpu_glGetAttribLocation(program, "b_Color");
 }
 
 
 
-#endif
+
+GLuint static compile_shader(GLenum type, const char** src, int count)
+{
+	GLint status;
+	GLuint shader = gpu_glCreateShader(type);
+	
+	
+	if(shader == 0)
+		return 0;
+		
+		
+	gpu_glShaderSource(shader, count, src, NULL);
+	gpu_glCompileShader(shader);
+	
+	
+	gpu_glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
+	
+	if(status == 0)
+	{
+		GLint len = 0;
+		gpu_glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
+		
+		if(len > 0)
+		{
+			char * log = MEM_mallocN(len, "GLSLErrLog");
+			
+			gpu_glGetShaderInfoLog(shader, len, NULL, log);
+			printf("Error in generating Objet GLSL: \n %s\n", log);
+			
+			MEM_freeN(log);
+			
+			gpu_glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &len);
+			
+			if(len > 0)
+			{
+				char* shadersrc = MEM_mallocN(len, "GLSLErrLog");
+				GLsizei *rlen;
+				
+				//glGetShaderSource(shader, len, rlen, log);
+				//printf("Objet GLSL source: \n %s", shadersrc);
+				
+				MEM_freeN(shadersrc);
+				
+				
+				
+					
+			}
+				
+		}
+		
+		gpu_glDeleteShader(shader);
+	
+		return 0;
+	}
+	
+	return shader;
+
+}
+
+
+GLuint static create_program(GLuint vertex, GLuint fragment)
+{
+	GLint status = 0;
+	GLuint program = gpu_glCreateProgram();
+	
+	if(program == 0)
+	{
+		printf("Cannot create GLSL program object\n");
+		return 0;	
+	}
+	
+	gpu_glAttachShader(program, vertex);
+	gpu_glAttachShader(program, fragment);
+	
+	/* b_Color cannot be 0 because b_Color can be a singular color and glVertexAttrib* won't work */
+	glBindAttribLocation(program, 1, "b_Color");
+	
+	gpu_glLinkProgram(program);
+	
+	gpu_glGetProgramiv(program, GL_LINK_STATUS, &status);
+	
+	if(status == 0) 
+	{
+		GLint len = 0;
+		
+		gpu_glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
+		
+		if(len > 0)
+		{
+			char * log = MEM_mallocN(len, "GLSLErrProgLog");
+			
+			gpu_glGetProgramInfoLog(program, len, NULL, log);
+			printf("Error in generating Program GLSL: \n %s\n", log);
+			
+			MEM_freeN(log);
+				
+		}
+		
+		gpu_glDeleteProgram(program);
+		return 0;	
+	}
+
+	return program;
+}
+
+char * object_shader_vector_basic = 
+	"uniform mat4 b_ProjectionMatrix ;	\n"
+	"uniform mat4 b_ModelViewMatrix ;	\n"
+"attribute vec4 b_Vertex;	\n"
+"attribute vec4 b_Color;	\n"
+"varying vec4 v_Color;	\n"
+"void main()	\n"
+"{	\n"
+"	gl_Position = b_ProjectionMatrix * b_ModelViewMatrix * b_Vertex;	\n"
+"	v_Color = b_Color;	\n"
+"}	\n"
+;
+
+char * object_shader_fragment_basic = 
+"precision mediump float;	\n"
+"varying vec4 v_Color;	\n"
+"void main()	\n"
+"{	\n"
+"	gl_FragColor = vec4(vec3(v_Color), v_Color.a * 1.0);	\n"
+"}	\n"
+;
+
+
+char * object_shader_vector_alphatexture = 
+	"uniform mat4 b_ProjectionMatrix ;	\n"
+	"uniform mat4 b_ModelViewMatrix ;	\n"
+	"uniform mat4 b_TextureMatrix ;	\n"
+"attribute vec4 b_Vertex;	\n"
+"attribute vec4 b_Color;	\n"
+"varying vec4 v_Color;	\n"
+"attribute vec2 b_Coord;	\n"
+"varying vec2 v_Coord;	\n"
+"void main()	\n"
+"{	\n"
+"	gl_Position = b_ProjectionMatrix * b_ModelViewMatrix * b_Vertex;	\n"
+"	v_Coord = mat2(b_TextureMatrix) * b_Coord;	\n"
+"	v_Color = b_Color;	\n"
+"}	\n"
+;
+
+char * object_shader_fragment_alphatexture = 
+"precision mediump float;	\n"
+"varying vec4 v_Color;	\n"
+"varying vec2 v_Coord;	\n"
+"uniform sampler2D v_texid;	\n"
+"void main()	\n"
+"{	\n"
+"	gl_FragColor = vec4(v_Color.rgb, texture2D(v_texid, v_Coord).a*v_Color.a);	\n"
+"}	\n"
+;
+
+
+GPUGLSL_ES_info shader_main_info;
+int shader_main;
+
+GPUGLSL_ES_info shader_alphatexture_info;
+int shader_alphatexture;
+
+
+void gpu_object_init_gles(void)
+{
+	GLuint vo = compile_shader(GL_VERTEX_SHADER, &object_shader_vector_basic, 1);
+	GLuint fo = compile_shader(GL_FRAGMENT_SHADER, &object_shader_fragment_basic, 1);	
+	shader_main = create_program(vo, fo);
+	gpu_assign_gles_loc(&shader_main_info, shader_main);
+
+	vo = compile_shader(GL_VERTEX_SHADER, &object_shader_vector_alphatexture, 1);
+	fo = compile_shader(GL_FRAGMENT_SHADER, &object_shader_fragment_alphatexture, 1);
+	shader_alphatexture = create_program(vo, fo);	
+	
+	gpu_assign_gles_loc(&shader_alphatexture_info, shader_alphatexture);
+}
+
+

Modified: branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object_gles.h
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object_gles.h	2012-08-17 17:04:02 UTC (rev 49958)
+++ branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_object_gles.h	2012-08-17 17:36:38 UTC (rev 49959)
@@ -1,5 +1,5 @@
-#ifdef GLES
 
+
 #ifdef __cplusplus 
 extern "C" {
 #endif
@@ -9,10 +9,13 @@
 		int viewmatloc;
 		int normalmatloc;
 		int projectionmatloc;
+		int texturecoordloc;
 	
+		int texidloc;
 
 		int vertexloc;
 		int normalloc;	
+		int colorloc;
 	
 
 } GPUGLSL_ES_info;
@@ -28,11 +31,20 @@
 void gpuColorPointer_gles (int size, int type, int stride, const void *pointer);
 void gpuTexCoordPointer_gles(int size, int type, int stride, const void *pointer);
 void gpuClientActiveTexture_gles(int texture);
+void gpuColorSet_gles(const float *value);
 
 void gpuCleanupAfterDraw_gles(void);
 
+extern GPUGLSL_ES_info shader_main_info;
+extern int shader_main;
 
+
+extern GPUGLSL_ES_info shader_alphatexture_info;
+extern int shader_alphatexture;
+
+
+void gpu_object_init_gles(void);
+
 #ifdef __cplusplus 
 }
 #endif
-#endif




More information about the Bf-blender-cvs mailing list