Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 07 May 2013
Offline Last Active Sep 26 2013 10:10 PM

Topics I've Started

OpenGL ES 2.0: Point light optimisation

06 June 2013 - 08:08 AM

Hey devs!


I've been working on a OpenGL ES 2.0 android engine and I have begun implementing some simple (point) lighting. I had something fairly simple working, so I tried to get fancy and added color-tinting light. And it works great... with only one or two lights. Any more than that, the application drops about 15 frames per light added (my ideal is at least 4 or 5). I know implementing lighting is expensive, I just didn't think it was that expensive. I'm fairly new to the world of OpenGL and GLSL, so there is a good chance I've written some crappy shader code. If anyone had any feedback or tips on how I can optimize this code, please let me know.


Vertex Shader

    uniform mat4 u_MVPMatrix;		
    uniform mat4 u_MVMatrix;			  
    attribute vec4 a_Position;		
    attribute vec3 a_Normal;	
    attribute vec2 a_TexCoordinate;
    varying vec3 v_Position;	
    varying vec3 v_Normal;		          			
    varying vec2 v_TexCoordinate;	
    void main()	{
        v_Position = vec3(u_MVMatrix * a_Position);		     	
        v_TexCoordinate = a_TexCoordinate;		
        v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
        gl_Position = u_MVPMatrix * a_Position;	

Fragment Shader

precision mediump float;
uniform vec4 u_LightPos["+numLights+"];
uniform vec4 u_LightColours["+numLights+"];
uniform float u_LightPower["+numLights+"];
uniform sampler2D u_Texture;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec2 v_TexCoordinate;

void main()
   gl_FragColor = (texture2D(u_Texture, v_TexCoordinate));
   float diffuse = 0.0;
   vec4 colourSum = vec4(1.0);
   for (int i = 0; i < "+numLights+"; i++) {
   	vec3 toPointLight = vec3(u_LightPos[i]);
   	float distance = length(toPointLight - v_Position);
   	vec3 lightVector = normalize(toPointLight - v_Position);
   	float diffuseDiff = 0.0; // The diffuse difference contributed from current light
   	diffuseDiff = max(dot(v_Normal, lightVector), 0.0);
   	diffuseDiff = diffuseDiff * (1.0 / (1.0 + ((1.0-u_LightPower[i])* distance * distance))); //Determine attenuatio
   	diffuse += diffuseDiff;
   	gl_FragColor.rgb *= vec3(1.0) / ((vec3(1.0) + ((vec3(1.0) - vec3(u_LightColours[i]))*diffuseDiff))); //The expensive part
  diffuse += 0.1; //Add ambient light
   gl_FragColor.rgb *= diffuse;

Am I making any rookie mistakes? Or am I just being unrealistic about what I can do? Thanks in advance

Game engine: Batch rendering advice?

19 May 2013 - 01:26 AM

Hey guys, I looking for some advice/insights into render batching. I've been building my own game engine for Android (OpenGL ES 2.0) and at the moment, I feel like I am handling my game object rendering very inefficiently.


Currently, I have GameObjects that have are stored in a list in a GameWorld. GameObjects have a reference to a texture and a mesh. At the moment, all I do is loop through the GameObjects in my renderer, binding textures and unbinding them object by object. My understanding is that binding is quite an expensive process. So, I guess my question is... does anyone have an elegant way to handle this?


Some of the ideas I had were:


- Sort the GameObjects list on the texture id (possibly also expensive to do) so that objects with the same texture render together

- Instead of an array with everything in it,  GameObjects of the same type are kept in their own arrays, and each one of those is looped through with only one 'bind' call required per group.


I hope I was clear enough to create some idea of where I'm at and what I'm trying to do, I can't wait to hear your feedback.

OpenGL ES 2.0: Weird artifact renderering plane

11 May 2013 - 10:45 PM

I'm trying to render a basic plane and I'm having a pretty bad time. I render the square ant it draws, but it also seems to draw a giant half-cube on top of it.





Is there any obvious mistake I'm making that might be responsible for this?

OpenGL ES 2.0: My parsed .obj is all wrong

08 May 2013 - 05:16 AM

So, I've recently begun exploring OpenGL ES 2.0 and I've been trying to render and .obj model that I've loaded with a parser that I worked with using OpenGL 1.x (its from Mario Zechner's "Beginning Android Games"). The parsed data is stored in an IntBuffer called 'vertices' in my renderer. 


When I try to draw the mesh, however, my "spaceship" ends up looking like this:




Here's how I'm drawing it:


//Set up program, handles, etc etc

GLES20.glVertexAttribPointer(mPositionsHandle, 3, GLES20.GL_FLOAT, false, 0, vertices);        
	// Pass in the color information
	GLES20.glVertexAttribPointer(mColoursHandle,  4, GLES20.GL_FLOAT, false, 0,  vertices);        
	 // Pass in the texture coordinate information
        vertices.position(hasColor ? 7 : 3);
	GLES20.glVertexAttribPointer(mTextureCoordinatesHandle, 2, GLES20.GL_FLOAT, false, 0,  vertices);
       // Pass in the normal information
    	int offset = 3;
        if (hasColor)
                offset += 4;
         if (hasTexCoords)
                offset += 2;
        GLES20.glVertexAttribPointer(mNormalsHandle,  3, GLES20.GL_FLOAT, false, 0, vertices);

GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, getNumVertices());


Is there anything I'm forgetting to do that I'm meant to? Or am I doing something wrong stupid?  I appreciate any help - I'm an OpenGL beginner and I'm a bit lost.