stuck on black screen :(

Started by xman84, September 24, 2010, 17:43:31

Previous topic - Next topic

xman84

Hello, I'm trying to draw a cube.  I have a vertex and fragment shader which I compile successfully according to link status.  For the opengl buffers I used bindbuffer and bufferdata calls.  I use drawelements to draw the cube.  I have no errors when I run but I just get a black screen.  I've tried changing the rotation about the y axis to make sure I'm not staring the wrong direction and I just keep getting the black screen.  The code I have is large so I'm not sure its of any use to post it.  If someone has done something similar I would like to see it very much.  Any ideas as to what I could be doing wrong are greatly appreciated.


xman84

Ok I've narrowed it down! :D  It has something to do with texturing the cube.  I changed the code to use colors instead and it worked.  I'll play with it and if I can't figure it out I'll update this topic with my texture code for help.

xman84

Below are the code snippets related to texturing in my program.  When I use this instead of just simple colors I get a completely black screen.  Any ideas are appreciated!
private static void initTextures() {
		glContext.enable(LWJGLAdapter.TEXTURE_2D);
		cubeTexture = glContext.createTexture();

		handleTextureLoaded(loadImage("grass.png"), cubeTexture);
		
	}
	
	private static void handleTextureLoaded(java.nio.ByteBuffer image, int texture) {
		glContext.bindTexture(LWJGLAdapter.TEXTURE_2D, texture);
		//glContext.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, image.getElement(), true);
		glContext.texImage2D(LWJGLAdapter.TEXTURE_2D, 0, LWJGLAdapter.RGBA, 0,0,0, LWJGLAdapter.RGBA, LWJGLAdapter.UNSIGNED_BYTE, image);
		glContext.texParameteri(LWJGLAdapter.TEXTURE_2D, LWJGLAdapter.TEXTURE_MAG_FILTER, LWJGLAdapter.NEAREST); // LINEAR
		glContext.texParameteri(LWJGLAdapter.TEXTURE_2D, LWJGLAdapter.TEXTURE_MIN_FILTER, LWJGLAdapter.NEAREST); // LINEAR MIPMAP NEAREST
		//glContext.generateMipmap(LWJGLAdapter.TEXTURE_2D);
		//glContext.bindTexture(LWJGLAdapter.TEXTURE_2D, null);
		
	}
	
	private static ByteBuffer loadImage(String textureName) {
		BufferedImage image;
		ByteBuffer data = null;
		try {
			image = ImageIO.read(new File(textureName));
			//int width = image.getWidth();
			//int height = image.getHeight();
			DataBufferByte rawData =  (DataBufferByte) image.getData().getDataBuffer();

			data = BufferUtils.createByteBuffer(rawData.getSize());
			data.put(rawData.getData());
			data.rewind();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;

	private static void drawCube() {
        ....
        
        glContext.bindBuffer(LWJGLAdapter.ARRAY_BUFFER, cubeVerticesTextureCoordBuffer);
        glContext.vertexAttribPointer(vertexTextureCoordAttribute, 2, LWJGLAdapter.FLOAT, false, 0, 0);
        glContext.activeTexture(LWJGLAdapter.TEXTURE0);
        glContext.bindTexture(LWJGLAdapter.TEXTURE_2D, cubeTexture);
        glContext.uniform1i(samplerUniform, 0);
        ...
	}

  private static final String fragmentShaderTextureText = 
"#ifdef GL_ES\n" +
"precision highp float;\n" +
"#endif\n" +
"varying vec2 vTextureCoord;\n" +
"uniform sampler2D uSampler;\n" +
"void main(void)\n" +
"{\n" +
"  gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));\n" +
"}\n";
  
  private static final String vertexShaderTextureText =
"attribute vec3 vertexPosition;\n" +
"attribute vec2 aTextureCoord;\n" +
"uniform mat4 modelViewProjectionMatrix;\n" + 
"varying vec2 vTextureCoord;\n" + 
"void main(void)\n" +
"{\n" +
"  gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);\n" +
"  vTextureCoord = aTextureCoord;\n" +
"}\n";

private static void initBuffers() {
        ...
        cubeVerticesTextureCoordBuffer = glContext.createBuffer();
        glContext.bindBuffer(LWJGLAdapter.ARRAY_BUFFER, cubeVerticesTextureCoordBuffer);
        float[] textureCoordinates = new float[] {
        	    // Front  
        	    0.0f,  0.0f,  
        	    1.0f,  0.0f,  
        	    1.0f,  1.0f,  
        	    0.0f,  1.0f,  
        	    // Back  
        	    0.0f,  0.0f,  
        	    1.0f,  0.0f,  
        	    1.0f,  1.0f,  
        	    0.0f,  1.0f,  
        	    // Top  
        	    0.0f,  0.0f,  
        	    1.0f,  0.0f,  
        	    1.0f,  1.0f,  
        	    0.0f,  1.0f,  
        	    // Bottom  
        	    0.0f,  0.0f,  
        	    1.0f,  0.0f,  
        	    1.0f,  1.0f,  
        	    0.0f,  1.0f,  
        	    // Right  
        	    0.0f,  0.0f,  
        	    1.0f,  0.0f,  
        	    1.0f,  1.0f,  
        	    0.0f,  1.0f,  
        	    // Left  
        	    0.0f,  0.0f,  
        	    1.0f,  0.0f,  
        	    1.0f,  1.0f,  
        	    0.0f,  1.0f  	
        };
        textureCoordinatesDirectBuffer = BufferUtils.createFloatBuffer(textureCoordinates.length).put(textureCoordinates);
        textureCoordinatesDirectBuffer.flip();
        glContext.bufferData(LWJGLAdapter.ARRAY_BUFFER, textureCoordinatesDirectBuffer, LWJGLAdapter.STATIC_DRAW);
...
}

private static void drawCube() {
...
        glContext.bindBuffer(LWJGLAdapter.ARRAY_BUFFER, cubeVerticesTextureCoordBuffer);
        glContext.vertexAttribPointer(vertexTextureCoordAttribute, 2, LWJGLAdapter.FLOAT, false, 0, 0);
        glContext.activeTexture(LWJGLAdapter.TEXTURE0);
        glContext.bindTexture(LWJGLAdapter.TEXTURE_2D, cubeTexture);
        glContext.uniform1i(samplerUniform, 0);
...
}


added code tag - mod

xman84

Can anyone help me with this problem?  I had changed direction and started doing WebGL but recently discovered I can't use WebGL because the frame rate drops too much.  I'm now getting back into LWJGL and am stuck on this problem again.  Any clues as to how I could solve this.  Or how to debug a black screen situation?

xman84

Well I'm dumb.  In the shader I had named an attribute "vertexPosition" but when I went to get the attribute location I used "aVertexPosition".  Would've been nice if I had gotten an error somewhere.  Anyway glad it's resolved.