Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: strange problm (accessing an ivec4 in vertex shdr)

  1. #1
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    strange problm (accessing an ivec4 in vertex shdr)

    Hi all,
    I am trying to do a mass spring vertex shader using VBO and TBO. The mass connection information is stored as GLint as follows,
    Code :
    GLint connections[MAX_MASSES][4] =	{
    {-1,1,5,0},{1,2,6,0},{2,3,7,1},{3,4,8,2},...,};
    In the vertex shader, the connection is attached to attribute location 2 in an ivec4 as follows,
    Code :
    layout(location=2) in ivec4 connection;
    when i access the attribute connection like this or using .x to fetch the top neighbor from the TBO using
    Code :
    texelFetchBuffer(tex_position_mass,  connection[0]);
    I get wrong result. However, if i change GLint to GLfloat when attaching the attributelocation (glVertexAttribPointer), change my connection var to float (vec4) and then cast it to int in the vertex shader, the output is correct.
    Note that I have already checked the TBO and it contains the correct information.
    Why doesn't it work with ivec4?

    Regards,
    Mobeen
    Regards,
    Mobeen

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: strange problm (accessing an ivec4 in vertex shdr)

    Just to append another question to this thread, if i have a single VBO and my data is just plain positions which i render as GL_POINTS, do i need VAO? I cant really seem to understand when I should use a VAO along with my VBO currently i m not using it but I need to understand when VAO should be used. The information about using VAO is scarce (I found some information on the opengl Wiki but thats about it). Where can i get more info. on this topic?
    Regards,
    Mobeen

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,649

    Re: strange problm (accessing an ivec4 in vertex shdr)

    Quote Originally Posted by mobeen
    do i need VAO?
    If you're using compatibility profile, no. You can ignore them.

    If you're using core profile, then I seem to recall at some point they disallowed the implicit VAO 0. If you still don't want to use VAOs, IIRC the work-around is to just create a VAO, bind it, and then just forget about VAOs.

    But don't forget about them. When you're optimizing, you may want to come back and make better use of them. They can sometimes give you a speed-up, though not as much as with display lists or NVidia bindless.


  4. #4
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896

    Re: strange problm (accessing an ivec4 in vertex shdr)

    I get wrong result. However, if i change GLint to GLfloat when attaching the attributelocation (glVertexAttribPointer)
    Have you tried to use glVertexAttribIPointer for integer attributes? IIUC it should not be necessary, but you never know...

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: strange problm (accessing an ivec4 in vertex shdr)

    HI carsten neumann,
    Thanks for ur reply. Yes I have tried using vertexattributepointer for integer attributes but i dont know why but it doesnot work.

    Regards,
    Mobeen
    Regards,
    Mobeen

  6. #6
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: strange problm (accessing an ivec4 in vertex shdr)

    OK I am trying to follow the discussion of mass spring simulation on page 417 OpenGL superbible. The book says I need 2 TBOs, 2 VBOs and 2VAOs. So I do my setup as follows,
    Code :
    void createVBO()
    {
    	//fill the vertices
    	int count = 0;
    	for(int i=0;i<MAX_MASSES;i++) {
    		vertices[i].position_mass[0]=vSpringMassVerts[i][0];
    		vertices[i].position_mass[1]=vSpringMassVerts[i][1];
    		vertices[i].position_mass[2]=vSpringMassVerts[i][2];
    		vertices[i].position_mass[3]=vSpringMassVerts[i][3];
     
    		vertices[i].velocity[0]=0;
    		vertices[i].velocity[1]=0;
    		vertices[i].velocity[2]=0;
    		vertices[i].velocity[3]=0;
     
    		vertices[i].connection[0]=connections[i][0];
    		vertices[i].connection[1]=connections[i][1];
    		vertices[i].connection[2]=connections[i][2];
    		vertices[i].connection[3]=connections[i][3];	 
    	}
     
     
        // create buffer object
    	glGenVertexArrays(2, vaoID);
    	glGenBuffers( 2, vboID);
     
    	glBindVertexArray(vaoID[0]);
        glBindBuffer( GL_ARRAY_BUFFER, vboID[0]);
    	    glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), &amp;(vertices[0]), GL_STATIC_DRAW);
    		glEnableVertexAttribArray(0);
    		glEnableVertexAttribArray(1);
    		glEnableVertexAttribArray(2);
    			glVertexAttribPointer(0,  4, GL_FLOAT, GL_FALSE, sizeof(MyVertex), (const GLvoid*) offsetof(MyVertex, position_mass));
    			glVertexAttribPointer(1,  4, GL_FLOAT, GL_FALSE, sizeof(MyVertex), (const GLvoid*) offsetof(MyVertex, velocity));
    			glVertexAttribPointer(2,  4, GL_FLOAT, GL_FALSE, sizeof(MyVertex), (const GLvoid*) offsetof(MyVertex, connection));
     
    	glBindVertexArray(vaoID[1]);
    	glBindBuffer( GL_ARRAY_BUFFER, vboID[1]);
    	glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), &amp;(vertices[0]), GL_STATIC_DRAW);
    			glVertexAttribPointer(0,  4, GL_FLOAT, GL_FALSE, sizeof(MyVertex), (const GLvoid*) offsetof(MyVertex, position_mass));
    			glVertexAttribPointer(1,  4, GL_FLOAT, GL_FALSE, sizeof(MyVertex), (const GLvoid*) offsetof(MyVertex, velocity));
    			glVertexAttribPointer(2,  4, GL_FLOAT, GL_FALSE, sizeof(MyVertex), (const GLvoid*) offsetof(MyVertex, connection));
     
    		glDisableVertexAttribArray(0);
    		glDisableVertexAttribArray(1);
    		glDisableVertexAttribArray(2);
        glBindBuffer( GL_ARRAY_BUFFER, 0);
    	glBindVertexArray(0);
     
        gltCheckErrors();
    }
     
    void createTBO(size_t size) {
    	glGenTextures( 2, texID);
        glGenBuffers( 2, tboID); 
    	for(int i=0;i<2;i++) {
    		glBindVertexArray(vaoID[i]);
    		glBindBuffer( GL_TEXTURE_BUFFER, tboID[i]);
    		glBufferData( GL_TEXTURE_BUFFER, size, vSpringMassVerts, GL_STATIC_DRAW);
    		glActiveTexture(GL_TEXTURE0+i);
    		glBindTexture( GL_TEXTURE_BUFFER, texID[i]);
    		glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F_ARB, tboID[i]);	
    	}
    }

    And as given in the sample codes of the book, I render my mass spring system using,
    Code :
    glUseProgram(massSpringShader);
    				glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix());
    				glUniform4fv(colorLocation,1, vRed); 
     
    				glUniform1f(tLocation,0.01);
    				glUniform1f(kLocation, k); 
    				glUniform1f(cLocation, c); 
    				glUniform1f(rLengthLocation, l);
     
     
    				gltCheckErrors(); 
     
    					glBindVertexArray( vaoID[readID]);
    					glActiveTexture( GL_TEXTURE0+readID);					
    					glBindTexture( GL_TEXTURE_BUFFER, texID[readID]);
    					glUniform1i( glGetUniformLocation( massSpringShader, "tex_position_mass"), readID);   				 
    					glBindBuffer( GL_ARRAY_BUFFER, vboID[readID]);
    						glDrawArrays(GL_POINTS, 0, MAX_MASSES);
     
     
    					int tmp = readID;
    					readID=writeID;
    					writeID = tmp;
     
    					glBindBuffer( GL_ARRAY_BUFFER, 0);
    					glBindVertexArray( 0);
     
    			glUseProgram(0);
    However, the output just gives me flickering nodes which donot move. Am I doing this correctly? As said in the book, the output would be written to the other TBO/VBO pair. One thing I am not sure is how do i assign the output of the vertex shader to a specific TBO/VBO? And here is the vertex shader (copied from the book.
    Code :
    #version 330
    precision highp float;
     
    #extension EXT_gpu_shader4 : require
    in vec4 position_mass;
    in vec4 velocity;
    in vec4 connection;
     
    uniform mat4 MVP;
    uniform samplerBuffer tex_position_mass;
    uniform float t, k, c, rest_length;
     
    const vec3 gravity = vec3(0,0,0);
     
     
    out Vertex {
    	vec4 position_mass;
    	vec4 velocity;
    }vertex;	
     
     
    void main(void) 
    {  
    	vec3 p = position_mass.xyz;
     
    	float m = position_mass.w;
    	vec3 u = velocity.xyz;
    	vec3 F;
    	vec3 v = u;
    	vec3 s = vec3(0);
     	if(connection[0] != -1) {
    		F = gravity + c*u;
    		for(int i=0;i<4;i++) {
    			if(connection[i]!=gl_VertexID) {
    				vec3 q = texelFetchBuffer(tex_position_mass, int(connection[i])).xyz;
    				vec3 d = q-p;
    				float x = length(d);
    				F += -k*(rest_length-x)*normalize(d);
    			}
    		}
    		vec3 a = F/m;
    		s = u*t +0.5*a*t*t;
    		v = u+a*t;
    	}
    	vertex.position_mass = vec4(p+s, m);	
    	vertex.velocity = vec4(v,0);
    	gl_Position = MVP*vec4(p+s, 1.0);
    Regards,
    Mobeen
    Regards,
    Mobeen

  7. #7
    Senior Member Frequent Contributor
    Join Date
    Oct 2009
    Posts
    592

    Re: strange problm (accessing an ivec4 in vertex shdr)

    How can VAO give a speed-up if you use the VBO caching scheme you so nicely explained? Is there a choice then between using multiple VBOs and VAOs and one VBO and no VAOs?

    Which choice is better?

  8. #8
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: strange problm (accessing an ivec4 in vertex shdr)

    Hi ugluk,
    Currently, I am just trying to implement the mass spring example missing from the opengl superbible examples. I m not looking on optimization yet. Its just to make it work first which is not working at the moment).

    Thanks,
    Mobeen
    Regards,
    Mobeen

  9. #9
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: strange problm (accessing an ivec4 in vertex shdr)

    I know the reason of flickering. Its due to the vao[1] which is empty. I dont understand why it is empty when I am doing the same thing as I did for vao[0]. Any insights would be appreciated. I am also checking the error bit and it is clear no GL errors.

    Thanks,
    Mobeen
    Regards,
    Mobeen

  10. #10
    Senior Member Frequent Contributor
    Join Date
    Nov 2009
    Posts
    614

    Re: strange problm (accessing an ivec4 in vertex shdr)

    Quote Originally Posted by mobeen
    I know the reason of flickering. Its due to the vao[1] which is empty. I dont understand why it is empty when I am doing the same thing as I did for vao[0]. Any insights would be appreciated. I am also checking the error bit and it is clear no GL errors.

    You have:

    Code :
    for(int i=0;i<2;i++) {
    		glBindVertexArray(vaoID[i]);
    		glBindBuffer( GL_TEXTURE_BUFFER, tboID[i]);
    		glBufferData( GL_TEXTURE_BUFFER, size, vSpringMassVerts, GL_STATIC_DRAW);
    		glActiveTexture(GL_TEXTURE0+i);
    		glBindTexture( GL_TEXTURE_BUFFER, texID[i]);
    		glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F_ARB, tboID[i]);	
    	}

    which makes texture unit $i source from texID[$i]... VAO's only "store" what to use for attributes and index buffers, not any texture jazz. When you switch between the VAO's, you also need to:
    • once bind texID[i] to GL_TEXTURE0+i AND
    • during rendering set the uniform: glUniform1i(location_of_tex_position_mass, current_i)

    Alternatively, one can also:

    • once set lUniform1i(location_of_tex_position_mass, 0)
    • during rendering change what is bound to GL_TEXTURE0


    Also, if you use ivecN in a shader, you MUST use glVertexAttribIPointer.



Page 1 of 3 123 LastLast

Similar Threads

  1. Trouble accessing texture data from the vertex shader
    By Nick's a Novice in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 08-17-2012, 10:09 AM
  2. Declaring vertex attribute as ivec4
    By Vexator in forum OpenGL: GLSL
    Replies: 2
    Last Post: 06-23-2011, 05:59 PM
  3. accessing texture data in a vertex shader
    By g0l3m in forum OpenGL: GLSL
    Replies: 5
    Last Post: 12-19-2006, 09:03 AM
  4. accessing texture in vertex shader
    By pegasy in forum OpenGL: GLSL
    Replies: 4
    Last Post: 04-28-2005, 08:18 AM
  5. accessing vertex data after model transformations
    By jwlee in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-05-2000, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean