Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualSollum

Posted 17 June 2013 - 03:35 PM

Ahhh, yes, sorry, forgot to post shader code, my bad. I'll post entire shader code with initialization, some assigned handles are not working, because they do not exist, but they aint that critical. I'll admit that shader initialization code is glued from many sources, so i might not be entirely sure whats going on in there. In any case, ill try to fix it by myself tomorrow after work, because i am dead tired already >.>

	final String vertexShader =
		"uniform mat4 u_MVPMatrix;\n"
		+ "uniform mat4 u_MVMatrix;\n"
		+ "uniform vec4 u_Color;\n"
				 
		+ "attribute vec4 a_Position;\n"
		+ "attribute vec4 a_Color;\n"
		+ "attribute vec3 a_Normal;\n"
		+ "attribute vec2 a_TexCoordinate;\n"
				 
		+ "varying vec3 v_Position;\n"
		+ "varying vec4 v_Color;\n"
		+ "varying vec3 v_Normal;\n"
		+ "varying vec2 v_TexCoordinate;\n"
				 
		+ "void main()\n"
		+ "{\n"
		+ "    v_Position = vec3(u_MVMatrix * a_Position);\n"
		+ "    v_Color = a_Color;\n"
		+ "    v_TexCoordinate = a_TexCoordinate;\n"
		+ "    gl_Position = u_MVPMatrix * a_Position;\n"
		+ "}\n";
			
	final String fragmentShader =
		"precision mediump float;\n"
				
		+ "uniform vec3 u_LightPos;\n"
		+ "uniform sampler2D u_Texture;\n"
		//+ "uniform vec4 u_Color;\n"
				
		//+ "varying vec3 v_Position;\n"
		+ "varying vec4 v_Color;\n"
		//+ "varying vec3 v_Normal;\n"
		+ "varying vec2 v_TexCoordinate;\n"
				
		+ "void main()\n"
		+ "{\n"
		+ "    gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));\n"
		+ "}\n";
		
	vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
		
	if (vertexShaderHandle != 0) 
	{
		// Pass in the shader source.
		GLES20.glShaderSource(vertexShaderHandle, vertexShader);

		// Compile the shader.
		GLES20.glCompileShader(vertexShaderHandle);

		// Get the compilation status.
		final int[] compileStatus = new int[1];
		GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

		// If the compilation failed, delete the shader.
		if (compileStatus[0] == 0) 
		{				
			GLES20.glDeleteShader(vertexShaderHandle);
			vertexShaderHandle = 0;
		}
	}

	if (vertexShaderHandle == 0)
	{
		throw new RuntimeException("Error creating vertex shader.");
	}
		
	// Load in the fragment shader shader.
	fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

	if (fragmentShaderHandle != 0) 
	{
		// Pass in the shader source.
		GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);

		// Compile the shader.
		GLES20.glCompileShader(fragmentShaderHandle);

		// Get the compilation status.
		final int[] compileStatus = new int[1];
		GLES20.glGetShaderiv(fragmentShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
			
		// If the compilation failed, delete the shader.
		if (compileStatus[0] == 0) 
		{				
			GLES20.glDeleteShader(fragmentShaderHandle);
			fragmentShaderHandle = 0;
		}
	}

	if (fragmentShaderHandle == 0)
	{
		throw new RuntimeException("Error creating fragment shader.");
	}
		
	// Create a program object and store the handle to it.
	programHandle = GLES20.glCreateProgram();
		
	if (programHandle != 0) 
	{
		// Bind the vertex shader to the program.
		GLES20.glAttachShader(programHandle, vertexShaderHandle);			

		// Bind the fragment shader to the program.
		GLES20.glAttachShader(programHandle, fragmentShaderHandle);
			
		// Bind attributes
		GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
		GLES20.glBindAttribLocation(programHandle, 1, "a_Color");
		GLES20.glBindAttribLocation(programHandle, 2, "a_TexCoordinate");
		GLES20.glBindAttribLocation(programHandle, 3, "a_Normal");
			
		// Link the two shaders together into a program.
		GLES20.glLinkProgram(programHandle);

		// Get the link status.
		final int[] linkStatus = new int[1];
		GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

		// If the link failed, delete the program.
		if (linkStatus[0] == 0) 
		{				
			GLES20.glDeleteProgram(programHandle);
			programHandle = 0;
		}
	}
		
	if (programHandle == 0)
	{
		throw new RuntimeException("Error creating program.");
	}
		
	GLES20.glUseProgram(programHandle);
		
	mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
	mMVMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVMatrix"); 
	mLightPosHandle = GLES20.glGetUniformLocation(programHandle, "u_LightPos");
	mTextureUniformHandle = GLES20.glGetUniformLocation(programHandle, "u_Texture");
	mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
	mColorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");
	mNormalHandle = GLES20.glGetAttribLocation(programHandle, "a_Normal"); 
	mTextureCoordinateHandle = GLES20.glGetAttribLocation(programHandle, "a_TexCoordinate");      
        
	GLES20.glUseProgram(programHandle);

#2Sollum

Posted 17 June 2013 - 03:34 PM

Ahhh, yes, sorry, forgot to post shader code, my bad. I'll post entire shader code with initialization, some non-existing handles are assigned as they are not critical. I'll admit that shader initialization code is glued from many sources, so i might not be entirely sure whats going on in there. In any case, ill try to fix it by myself tomorrow after work, because i am dead tired already >.>

	final String vertexShader =
		"uniform mat4 u_MVPMatrix;\n"
		+ "uniform mat4 u_MVMatrix;\n"
		+ "uniform vec4 u_Color;\n"
				 
		+ "attribute vec4 a_Position;\n"
		+ "attribute vec4 a_Color;\n"
		+ "attribute vec3 a_Normal;\n"
		+ "attribute vec2 a_TexCoordinate;\n"
				 
		+ "varying vec3 v_Position;\n"
		+ "varying vec4 v_Color;\n"
		+ "varying vec3 v_Normal;\n"
		+ "varying vec2 v_TexCoordinate;\n"
				 
		+ "void main()\n"
		+ "{\n"
		+ "    v_Position = vec3(u_MVMatrix * a_Position);\n"
		+ "    v_Color = a_Color;\n"
		+ "    v_TexCoordinate = a_TexCoordinate;\n"
		+ "    gl_Position = u_MVPMatrix * a_Position;\n"
		+ "}\n";
			
	final String fragmentShader =
		"precision mediump float;\n"
				
		+ "uniform vec3 u_LightPos;\n"
		+ "uniform sampler2D u_Texture;\n"
		//+ "uniform vec4 u_Color;\n"
				
		//+ "varying vec3 v_Position;\n"
		+ "varying vec4 v_Color;\n"
		//+ "varying vec3 v_Normal;\n"
		+ "varying vec2 v_TexCoordinate;\n"
				
		+ "void main()\n"
		+ "{\n"
		+ "    gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));\n"
		+ "}\n";
		
	vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
		
	if (vertexShaderHandle != 0) 
	{
		// Pass in the shader source.
		GLES20.glShaderSource(vertexShaderHandle, vertexShader);

		// Compile the shader.
		GLES20.glCompileShader(vertexShaderHandle);

		// Get the compilation status.
		final int[] compileStatus = new int[1];
		GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

		// If the compilation failed, delete the shader.
		if (compileStatus[0] == 0) 
		{				
			GLES20.glDeleteShader(vertexShaderHandle);
			vertexShaderHandle = 0;
		}
	}

	if (vertexShaderHandle == 0)
	{
		throw new RuntimeException("Error creating vertex shader.");
	}
		
	// Load in the fragment shader shader.
	fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

	if (fragmentShaderHandle != 0) 
	{
		// Pass in the shader source.
		GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);

		// Compile the shader.
		GLES20.glCompileShader(fragmentShaderHandle);

		// Get the compilation status.
		final int[] compileStatus = new int[1];
		GLES20.glGetShaderiv(fragmentShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
			
		// If the compilation failed, delete the shader.
		if (compileStatus[0] == 0) 
		{				
			GLES20.glDeleteShader(fragmentShaderHandle);
			fragmentShaderHandle = 0;
		}
	}

	if (fragmentShaderHandle == 0)
	{
		throw new RuntimeException("Error creating fragment shader.");
	}
		
	// Create a program object and store the handle to it.
	programHandle = GLES20.glCreateProgram();
		
	if (programHandle != 0) 
	{
		// Bind the vertex shader to the program.
		GLES20.glAttachShader(programHandle, vertexShaderHandle);			

		// Bind the fragment shader to the program.
		GLES20.glAttachShader(programHandle, fragmentShaderHandle);
			
		// Bind attributes
		GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
		GLES20.glBindAttribLocation(programHandle, 1, "a_Color");
		GLES20.glBindAttribLocation(programHandle, 2, "a_TexCoordinate");
		GLES20.glBindAttribLocation(programHandle, 3, "a_Normal");
			
		// Link the two shaders together into a program.
		GLES20.glLinkProgram(programHandle);

		// Get the link status.
		final int[] linkStatus = new int[1];
		GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

		// If the link failed, delete the program.
		if (linkStatus[0] == 0) 
		{				
			GLES20.glDeleteProgram(programHandle);
			programHandle = 0;
		}
	}
		
	if (programHandle == 0)
	{
		throw new RuntimeException("Error creating program.");
	}
		
	GLES20.glUseProgram(programHandle);
		
	mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
	mMVMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVMatrix"); 
	mLightPosHandle = GLES20.glGetUniformLocation(programHandle, "u_LightPos");
	mTextureUniformHandle = GLES20.glGetUniformLocation(programHandle, "u_Texture");
	mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
	mColorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");
	mNormalHandle = GLES20.glGetAttribLocation(programHandle, "a_Normal"); 
	mTextureCoordinateHandle = GLES20.glGetAttribLocation(programHandle, "a_TexCoordinate");      
        
	GLES20.glUseProgram(programHandle);

#1Sollum

Posted 17 June 2013 - 03:33 PM

Ahhh, yes, sorry, forgot to post shader code, my bad. I'll post entire shader code with initialization, some handles are assigned as they are not critical. I'll admit that shader initialization code is glued from many sources, so i might not be entirely sure whats going on in there. In any case, ill try to fix it by myself tomorrow after work, because i am dead tired already >.>

	final String vertexShader =
		"uniform mat4 u_MVPMatrix;\n"
		+ "uniform mat4 u_MVMatrix;\n"
		+ "uniform vec4 u_Color;\n"
				 
		+ "attribute vec4 a_Position;\n"
		+ "attribute vec4 a_Color;\n"
		+ "attribute vec3 a_Normal;\n"
		+ "attribute vec2 a_TexCoordinate;\n"
				 
		+ "varying vec3 v_Position;\n"
		+ "varying vec4 v_Color;\n"
		+ "varying vec3 v_Normal;\n"
		+ "varying vec2 v_TexCoordinate;\n"
				 
		+ "void main()\n"
		+ "{\n"
		+ "    v_Position = vec3(u_MVMatrix * a_Position);\n"
		+ "    v_Color = a_Color;\n"
		+ "    v_TexCoordinate = a_TexCoordinate;\n"
		+ "    gl_Position = u_MVPMatrix * a_Position;\n"
		+ "}\n";
			
	final String fragmentShader =
		"precision mediump float;\n"
				
		+ "uniform vec3 u_LightPos;\n"
		+ "uniform sampler2D u_Texture;\n"
		//+ "uniform vec4 u_Color;\n"
				
		//+ "varying vec3 v_Position;\n"
		+ "varying vec4 v_Color;\n"
		//+ "varying vec3 v_Normal;\n"
		+ "varying vec2 v_TexCoordinate;\n"
				
		+ "void main()\n"
		+ "{\n"
		+ "    gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));\n"
		+ "}\n";
		
	vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
		
	if (vertexShaderHandle != 0) 
	{
		// Pass in the shader source.
		GLES20.glShaderSource(vertexShaderHandle, vertexShader);

		// Compile the shader.
		GLES20.glCompileShader(vertexShaderHandle);

		// Get the compilation status.
		final int[] compileStatus = new int[1];
		GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

		// If the compilation failed, delete the shader.
		if (compileStatus[0] == 0) 
		{				
			GLES20.glDeleteShader(vertexShaderHandle);
			vertexShaderHandle = 0;
		}
	}

	if (vertexShaderHandle == 0)
	{
		throw new RuntimeException("Error creating vertex shader.");
	}
		
	// Load in the fragment shader shader.
	fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

	if (fragmentShaderHandle != 0) 
	{
		// Pass in the shader source.
		GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);

		// Compile the shader.
		GLES20.glCompileShader(fragmentShaderHandle);

		// Get the compilation status.
		final int[] compileStatus = new int[1];
		GLES20.glGetShaderiv(fragmentShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
			
		// If the compilation failed, delete the shader.
		if (compileStatus[0] == 0) 
		{				
			GLES20.glDeleteShader(fragmentShaderHandle);
			fragmentShaderHandle = 0;
		}
	}

	if (fragmentShaderHandle == 0)
	{
		throw new RuntimeException("Error creating fragment shader.");
	}
		
	// Create a program object and store the handle to it.
	programHandle = GLES20.glCreateProgram();
		
	if (programHandle != 0) 
	{
		// Bind the vertex shader to the program.
		GLES20.glAttachShader(programHandle, vertexShaderHandle);			

		// Bind the fragment shader to the program.
		GLES20.glAttachShader(programHandle, fragmentShaderHandle);
			
		// Bind attributes
		GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
		GLES20.glBindAttribLocation(programHandle, 1, "a_Color");
		GLES20.glBindAttribLocation(programHandle, 2, "a_TexCoordinate");
		GLES20.glBindAttribLocation(programHandle, 3, "a_Normal");
			
		// Link the two shaders together into a program.
		GLES20.glLinkProgram(programHandle);

		// Get the link status.
		final int[] linkStatus = new int[1];
		GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

		// If the link failed, delete the program.
		if (linkStatus[0] == 0) 
		{				
			GLES20.glDeleteProgram(programHandle);
			programHandle = 0;
		}
	}
		
	if (programHandle == 0)
	{
		throw new RuntimeException("Error creating program.");
	}
		
	GLES20.glUseProgram(programHandle);
		
	mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
	mMVMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVMatrix"); 
	mLightPosHandle = GLES20.glGetUniformLocation(programHandle, "u_LightPos");
	mTextureUniformHandle = GLES20.glGetUniformLocation(programHandle, "u_Texture");
	mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
	mColorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");
	mNormalHandle = GLES20.glGetAttribLocation(programHandle, "a_Normal"); 
	mTextureCoordinateHandle = GLES20.glGetAttribLocation(programHandle, "a_TexCoordinate");      
        
	GLES20.glUseProgram(programHandle);

PARTNERS