12 Jun 2014, 02:35
Generic-user-small

Michael Wood (1 post)

After finishing chapter 3 I ran the app on my Samsung S4 with KitKat. Seems that only the glClearColor is working. Had an all red screen when I tested it halfway through and now an all black screen (except for app title bar) at the end. Here’s the main part of the code (please help me find the bug): onDrawFrame()


// Clear the rendering surface.
		glClear(GL_COLOR_BUFFER_BIT);
		
		// draw the table
		glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f); 	// set color to WHITE *must specify 4 component color RGBA
																//(uniforms don't have default values, unlike attributes)
		glDrawArrays(GL_TRIANGLES, 	// draw triangles
				0, 					// start reading array at starting position 0
				6);					// read in 6 vertices = 2 triangles = 1 rectangle for hockey tabletop
		
		// draw the diving line
		glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);	// set color to RED *must specify 4 component color RGBA
		glDrawArrays(GL_LINES, 		// draw lines
				6, 					// start at vertex #7 (zero based count / first vertex was 0)
				2);					// read in 2 vertices (2 points to draw 1 line)
		
	} // END METHOD onDrawFrame()


onSurfaceCreated()


glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		
		// reading in the shader code
		String vertexShaderSource = TextResourceReader
				.readTextFileFromResource(context, R.raw.simple_vertex_shader);
		String fragmentShaderSource = TextResourceReader
				.readTextFileFromResource(context, R.raw.simple_fragment_shader);
		
		int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
		int fragmentShader = ShaderHelper.compileFragmentShader(fragmentShaderSource);
		
		program = ShaderHelper.linkProgram(vertexShader, fragmentShader);
		
		// validate program ONLY during development and testing
		if (LoggerConfig.ON){
			ShaderHelper.validateProgram(program);
		}
		
		// enable the OpenGL program just created
		glUseProgram(program);
		
		uColorLocation = glGetUniformLocation(program, U_COLOR);
		aPositionLocation = glGetAttribLocation(program, A_POSITION);
		
		// Tell OpenGL where to find the data for the attribute
		vertexData.position(0); // native memory buffer - tell OpenGL to read buffer from beginning/0
		// glVertexAttribPointer(int index, int size, int type, boolean normalized, int stride, Buffer ptr)
		glVertexAttribPointer(aPositionLocation, // index / attribute location
				POSITION_COMPONENT_COUNT, 		 // size / component count for each vertex: 2 floats x and y
				GL_FLOAT, 						 // type / list of floating point values
				false, 							 // normalized / only applies with integer data not floats
				0, 								 // stride / used only when storing more than one attribute in a single array
				vertexData);					 // buffer pointer / tells OpenGL where to start reading data from the buffer
	
		glEnableVertexAttribArray(aPositionLocation);
		
	} // END METHOD onSurfaceCreated()

Constructor for tableverticeswithtriangles


float[] tableVerticesWithTriangles = {
				// triangle 1
				-0.5f,  -0.5f,		// vertex #0
				 0.5f,   0.5f,		// #1
				-0.5f,   0.5f,		// #2
				
				// triangle 2
				-0.5f,  -0.5f,		// #3
				 0.5f,  -0.5f,		// #4
				 0.5f,   0.5f,		// #5
				
				// line 1
				-0.5f,     0f,		// #6
				 0.5f,     0f,		// #7
				
				// mallets (2 points)
				   0f, -0.25f,		// #8
				   0f,  0.25f		// #9
		};
		
		vertexData = ByteBuffer
				.allocateDirect(tableVerticesWithTriangles.length * BYTES_PER_FLOAT)
				.order(ByteOrder.nativeOrder())
				.asFloatBuffer();
		
		vertexData.put(tableVerticesWithTriangles);
		
	} // END CONSTRUCTOR

12 Jun 2014, 13:22
Generic-user-small

Kevin Brothaler (36 posts)

Hi Michael,

I can’t tell just by looking at the code above, but you might find it if you compare the code to the source code here: here: http://pragprog.com/titles/kbogla/source_code. Do you see any errors / messages in the logcat view in Eclipse? What happens if you run the examples from the source code?

12 Jun 2014, 13:31
Generic-user-small

Kevin Brothaler (36 posts)

I would start by comparing onSurfaceCreated / onSurfaceChanged (an error with the projection matrix can cause nothing to show up), and then checking the shaders to ensure they’re compiling without errors.

  You must be logged in to comment