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 (40 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 (40 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.

08 Dec 2014, 00:33
Confluence-profile_pragsmall

Mark Elston (4 posts)

Just to follow up on this I see the same thing when running under the emulator. It works fine when on a real device.

Just to make sure I changed the clear color to a dim magenta

glClearColor(0.3f, 0.1f, 0.2f, 0.0f);

and I see the magenta screen with a black rectangle in the center. Again, when I run on a device it works fine.

Not sure if this makes a difference or not but when running on the emulator the LogCat shows:

12-06 22:47:12.437: V/ShaderHelper(616): Results of compiling source:
12-06 22:47:12.437: V/ShaderHelper(616): attribute vec4 a_Position;
12-06 22:47:12.437: V/ShaderHelper(616): void main()
12-06 22:47:12.437: V/ShaderHelper(616): {
12-06 22:47:12.437: V/ShaderHelper(616):     gl_Position = a_Position;
12-06 22:47:12.437: V/ShaderHelper(616):     gl_PointSize = 10.0;
12-06 22:47:12.437: V/ShaderHelper(616): }
12-06 22:47:12.437: V/ShaderHelper(616):   -> No errors.
12-06 22:47:12.437: V/ShaderHelper(616): Results of compiling source:
12-06 22:47:12.437: V/ShaderHelper(616): precision mediump float;
12-06 22:47:12.437: V/ShaderHelper(616): uniform vec4 u_Color;
12-06 22:47:12.437: V/ShaderHelper(616): void main()
12-06 22:47:12.437: V/ShaderHelper(616): {
12-06 22:47:12.437: V/ShaderHelper(616):     gl_FragColor = u_Color;
12-06 22:47:12.437: V/ShaderHelper(616): }
12-06 22:47:12.437: V/ShaderHelper(616):   -> No errors.
12-06 22:47:12.445: E/(616): QemuPipeStream::readFully failed, buf=NULL, len 4
12-06 22:47:12.445: V/ShaderHelper(616): Results of linking program (1, 2): 
12-06 22:47:12.455: E/(616): QemuPipeStream::readFully failed, buf=NULL, len 4
12-06 22:47:12.455: V/ShaderHelper(616): Results of validating program: 667251
12-06 22:47:12.455: V/ShaderHelper(616): Log: 
12-06 22:47:17.845: E/EGL_emulation(616): rcMakeCurrent returned EGL_FALSE
12-06 22:47:17.845: E/EGL_emulation(616): tid 630: eglMakeCurrent(953): error 0x3006 (EGL_BAD_CONTEXT)
12-06 22:47:17.845: A/libc(616): Fatal signal 11 (SIGSEGV) at 0x00000020 (code=1), thread 630 (Thread-72)

The first two errors show up right away. The last three when exiting the app.

  You must be logged in to comment