1. Bart van Strien
  2. love-experiments

Commits

Alex Szpakowski  committed 148f75c

Attempted to conditionally integrate non-deprecated vertex attributes into shaders, realized it would probably work better if non-deprecated attributes were always used when shaders are supported, instead of only in ES2

  • Participants
  • Parent commits b5f4ed0
  • Branches GLES2-compatibility

Comments (0)

Files changed (4)

File src/modules/graphics/opengl/Context.cpp

View file
  • Ignore whitespace
 
 	// set vertex attribute index map
 	state.vertexAttribMap.clear();
-	state.vertexAttribMap[ATTRIB_VERTEX] = GL_VERTEX_ARRAY;
-	state.vertexAttribMap[ATTRIB_TEXCOORD] = GL_TEXTURE_COORD_ARRAY;
-	state.vertexAttribMap[ATTRIB_COLOR] = GL_COLOR_ARRAY;
+	if (GLEW_ES_VERSION_2_0)
+	{
+		// use explicit generic attribute locations in OpenGL ES 2
+		state.vertexAttribMap[ATTRIB_VERTEX] = 0;
+		state.vertexAttribMap[ATTRIB_TEXCOORD] = 1;
+		state.vertexAttribMap[ATTRIB_COLOR] = 2;
+	}
+	else
+	{
+		// and use the old glEnableClientState etc. in regular GL
+		state.vertexAttribMap[ATTRIB_VERTEX] = GL_VERTEX_ARRAY;
+		state.vertexAttribMap[ATTRIB_TEXCOORD] = GL_TEXTURE_COORD_ARRAY;
+		state.vertexAttribMap[ATTRIB_COLOR] = GL_COLOR_ARRAY;
+	}
 
 	// making sure all vertex attributes are disabled is much easier than querying them...
 	state.enabledVertexAttribArrays.clear();
 		// modelview/projection/normal matrices, point size, etc.
 	}
 
-	// TODO: load current matrices into OpenGL with glLoadMatrix if applicable
+	// TODO: otherwise, load current matrices into OpenGL with glLoadMatrix
 }
 
 void Context::setCapability(GLenum capability, bool enable)
 	return GLEW_ES_VERSION_2_0 || attrib > ATTRIB_COLOR;
 }
 
+GLint Context::getVertexAttribID(unsigned int attrib) const
+{
+	std::map<unsigned int, GLenum>::const_iterator glattrib = state.vertexAttribMap.find(attrib);
+	if (glattrib == state.vertexAttribMap.end())
+		return -1;
+
+	return glattrib->second;
+}
+
 void Context::useVertexAttribArray(unsigned int attrib, bool use)
 {
 	if (attrib == ATTRIB_NONE)
 		return;
 
-	// find the internal OpenGL representation for this attribute, if it exists
-	std::map<unsigned int, GLenum>::const_iterator glattrib = state.vertexAttribMap.find(attrib);
-	if (glattrib == state.vertexAttribMap.end())
+	// get the internal OpenGL representation for this attribute, if it exists
+	GLint glattrib = getVertexAttribID(attrib);
+	if (glattrib < 0)
 		return;
 
 	// is this attribute already set to the state we want?
 	if (isGenericVertexAttrib(attrib))
 	{
 		if (use)
-			glEnableVertexAttribArray(glattrib->second);
+			glEnableVertexAttribArray(glattrib);
 		else
-			glDisableVertexAttribArray(glattrib->second);
+			glDisableVertexAttribArray(glattrib);
 	}
 	else
 	{
 		if (use)
-			glEnableClientState(glattrib->second);
+			glEnableClientState(glattrib);
 		else
-			glDisableClientState(glattrib->second);
+			glDisableClientState(glattrib);
 	}
 
 	// save the attribute's state
 {
 	if (isGenericVertexAttrib(attrib))
 	{
-		// find the internal OpenGL representation for this attribute, if it exists
-		std::map<unsigned int, GLenum>::const_iterator it = state.vertexAttribMap.find(attrib);
-		if (it == state.vertexAttribMap.end())
+		// get the internal OpenGL representation for this attribute, if it exists
+		GLint glattrib = getVertexAttribID(attrib);
+		if (glattrib < 0)
 			return;
 
 		// TODO: better check for normalization?
 		GLboolean normalize = (type == GL_UNSIGNED_BYTE) ? GL_TRUE : GL_FALSE;
-		glVertexAttribPointer(it->second, size, type, normalize, stride, pointer);
+		glVertexAttribPointer(glattrib, size, type, normalize, stride, pointer);
 	}
 	else
 	{

File src/modules/graphics/opengl/Context.h

View file
  • Ignore whitespace
 	 **/
 	bool getCapability(GLenum capability);
 
+	bool isGenericVertexAttrib(unsigned int attrib) const;
+
+	GLint getVertexAttribID(unsigned int attrib) const;
+
 	/**
 	 * Sets whether a vertex attribute is active when rendering a vertex array.
 	 *
 	void initTextureState();
 	void createDefaultTexture();
 
-	bool isGenericVertexAttrib(unsigned int attrib) const;
-
 	// Currently tracked OpenGL state.
 	struct
 	{

File src/modules/graphics/opengl/Shader.cpp

View file
  • Ignore whitespace
 	if (textureCounters.size() < (size_t) maxTextureUnits)
 		textureCounters.resize(maxTextureUnits, 0);
 
+	// set names of generic vertex attributes in the shader code
+	// only relevant when the deprecated built-in ones aren't used
+	vertexAttribNames[Context::ATTRIB_VERTEX] = "VertexPosition";
+	vertexAttribNames[Context::ATTRIB_TEXCOORD] = "VertexTexCoord";
+	vertexAttribNames[Context::ATTRIB_COLOR] = "VertexColor";
+
 	// load shader source and create program object
 	loadVolatile();
 }
 	if (program == 0)
 		return;
 
-	// TODO: glBindAttribLocation
+	// bind generic vertex attribute locations to names in the shader
+	// does nothing if we only have built-in (deprecated) attributes
+
+	Context *ctx = getContext();
+
+	std::map<Context::VertexAttribType, std::string>::const_iterator it;
+	for (it = vertexAttribNames.begin(); it != vertexAttribNames.end(); ++it)
+	{
+		GLint glattrib = ctx->getVertexAttribID(it->first);
+		if (glattrib < 0 || !ctx->isGenericVertexAttrib(it->first))
+			continue;
+
+		glBindAttribLocation(program, glattrib, it->second.c_str());
+	}
 }
 
 void Shader::createProgram(const std::vector<GLuint> &shaderids)
 	for (it = shaderids.begin(); it != shaderids.end(); ++it)
 		glAttachShader(program, *it);
 
+	// bind custom vertex attributes to predefined locations
 	bindVertexAttribs();
 
 	glLinkProgram(program);

File src/modules/graphics/opengl/Shader.h

View file
  • Ignore whitespace
 
 	GLuint program; // volatile
 
+	// map of generic vertex attributes to names in the shader code
+	std::map<Context::VertexAttribType, std::string> vertexAttribNames;
+
 	// Uniform location buffer map
 	std::map<std::string, GLint> uniforms;