sending texture into shader

Started by odrya12, May 14, 2011, 13:04:20

Previous topic - Next topic

odrya12

I tried to use this code and I do not understand what does not work ....
I created some functions will be easier for me to work:
public void draw_fast(Texture texture, int x, int y)
    {
    	Color.white.bind();
		texture.bind(); // or GL11.glBind(texture.getTextureID());
		
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0,0); GL11.glVertex2f(x, y);
			GL11.glTexCoord2f(1,0); GL11.glVertex2f(x+texture.getTextureWidth(), y);
			GL11.glTexCoord2f(1,1); GL11.glVertex2f(x+texture.getTextureWidth(), y+texture.getTextureHeight());
			GL11.glTexCoord2f(0,1); GL11.glVertex2f(x, y+texture.getTextureHeight());
		GL11.glEnd();
    }
	private int createShader()
	{
		return ARBShaderObjects.glCreateProgramObjectARB();
	}
	private int createVertShader(String filename)
	{
	    //vertShader will be non zero if succefully created

	    int vertShader = ARBShaderObjects.glCreateShaderObjectARB(ARBVertexShader.GL_VERTEX_SHADER_ARB);
	    //if created, convert the vertex shader code to a String
	    if(vertShader == 0)
	    {
	    	return 0;
	    }
	    String vertexCode = "";
	    String line;
	    try
	    {
	        BufferedReader reader = new BufferedReader(new FileReader(filename));
	        while((line=reader.readLine()) != null)
	        {
	        	vertexCode += line + "\n";
	        }
	    }
	    catch(Exception e)
	    {
	    	System.out.println("Fail reading vertex shading code");
	        return 0;
	    }
	    ARBShaderObjects.glShaderSourceARB(vertShader, vertexCode);
	    ARBShaderObjects.glCompileShaderARB(vertShader);
	        //if zero we won't be using the shader
	    return vertShader;
	}
	private int createFragShader(String filename)
	{
		int fragShader = ARBShaderObjects.glCreateShaderObjectARB(ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
	    if(fragShader == 0)
	    {
	        return 0;
	    }
	    String fragCode = "";
	    String line;
	    try
	    {
	        BufferedReader reader = new BufferedReader(new FileReader(filename));
	        while((line=reader.readLine())!= null)
	        {
	            fragCode += line + "\n";
	        }
	    }
	    catch(Exception e)
	    {
	        System.out.println("Fail reading fragment shading code");
	        return 0;
	    }
	    ARBShaderObjects.glShaderSourceARB(fragShader, fragCode);
	    ARBShaderObjects.glCompileShaderARB(fragShader);

	    return fragShader;
	}

	public void installShader(int shader, int vertShader, int fragShader)
	{
		if(vertShader !=0 && fragShader !=0)
		{
            ARBShaderObjects.glAttachObjectARB(shader, vertShader);
            ARBShaderObjects.glAttachObjectARB(shader, fragShader);
            ARBShaderObjects.glLinkProgramARB(shader);
            ARBShaderObjects.glValidateProgramARB(shader);
            ARBShaderObjects.glUseProgramObjectARB(shader);
		}
            
	}

	public void ShaderAddTexture(int shader, Texture tt, String name, int id)
	{
		int loc = ARBShaderObjects.glGetUniformLocationARB(shader, name);
		tt.bind();
		ARBShaderObjects.glUniform1iARB(loc, id);
    }
	
	public void useShader(int ss)
	{
		ARBShaderObjects.glUseProgramObjectARB(ss);
	}
	public void clearShader()
	{
		ARBShaderObjects.glUseProgramObjectARB(0);
	}

Then I used them like this:
try 
    	{
			dd = TextureLoader.getTexture("PNG", new FileInputStream("res/soldier.png"));
			//spr_soldier = TextureLoader.getTexture("PNG", new FileInputStream("res/soldier.png"));
		} 
    	catch (IOException e) 
    	{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
my_shader = createShader();
        my_vert_shader = createVertShader("shaders/vertex_shader.vert");
        my_frag_shader = createFragShader("shaders/pixel_shaders.frag");
        installShader(my_shader, my_vert_shader, my_frag_shader);
        ShaderAddTexture(my_shader, dd, "tex0", 0);

I tried to draw it like this:
useShader(my_shader);
draw_fast(dd, 200, 200);
clearShader();

I do not understand what's wrong with that ...
I use C + + everything works for me with those files
Help me please

CodeBunny

Does it crash? Do you get some sort of stack trace? Or does it just render a black screen?

odrya12

it render a black rectangle where the sprite should be.