Jump to content

  • Log In with Google      Sign In   
  • Create Account


Ashaira

Member Since 07 Feb 2010
Offline Last Active Apr 17 2012 06:14 AM
-----

Topics I've Started

Understanding assembler

17 April 2012 - 05:17 AM

Well it seems i am lacking some very basic knowledge regarding c++ and a friend of mine told me i seem to not understand the hardware part of how programming and memory works and that understanding how the assembler works would help me cement and connect what i already know.
(consider that when i saw square(3++); i thought 4 would be sent as a parameter i tend to agree with him)

so does anyone know where i could learn about how assembler works? i dont want to learn to code in it just understand the principles of how it does things.

Rendering to FBO and Post Processing Quad

17 April 2012 - 03:00 AM

so i thought i understood how FBO work but it seems not. ive been trying to render to a FBO and then using the texture to render a quad that i will use for post processing effects. I went through several tutorials and everything "seems" right but then again it always does.

This is how i implemented my FBO in the scene manager constructor

glGenRenderbuffers(1, &RBO); // Generate one render buffer and store the ID in fbo_depth
glBindRenderbuffer(GL_RENDERBUFFER, RBO); // Bind the fbo_depth render buffer
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 1024, 780); // Set the render buffer storage to be a depth component, with a width and height of the window
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, RBO); // Set the render buffer of this buffer to the depth buffer
glBindRenderbuffer(GL_RENDERBUFFER, 0); // Unbind the render buffer
glGenTextures(1, &FBO_Texture); // Generate one texture
glBindTexture(GL_TEXTURE_2D, FBO_Texture); // Bind the texture fbo_texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 780, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // Create a standard texture with the width and height of our window
// Setup the basic texture parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);  
glGenFramebuffers(1, &FBO); // Generate one frame buffer and store the ID in fbo
glBindFramebuffer(GL_FRAMEBUFFER, FBO); // Bind our frame buffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FBO_Texture, 0); // Attach the texture fbo_texture to the color buffer in our frame buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, RBO); // Attach the depth buffer fbo_depth to our frame buffer
/*switch (glCheckFramebufferStatus(GL_FRAMEBUFFER)){
case GL_FRAMEBUFFER_COMPLETE:
  std::cout << "Complete." << std::endl;
  break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  std::cout << "Incomplete Attachment" << std::endl;
  break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  std::cout << "Incomplete Missing Attachment" << std::endl;
  break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
  std::cout << "Not all attached images have the same width and height." << std::endl;
  break;
case GL_FRAMEBUFFER_UNSUPPORTED:
  std::cout << "Framebuffer Unsupported" << std::endl;
  break;
}*/
glBindFramebuffer(GL_FRAMEBUFFER, 0);

this should be ok as i have used glCheckFramebufferStatus and it didnt return any errors.

now for the rendering. This is the draw call in my scene manager (which works fine if i remove the FBO so the error cant be in a diff place)

glViewport ( 0, 0, esContext->width, esContext->height );
// Clear the color buffer
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
for(int j=0; j<Resources->ShaderNum ; j++)
{
  userData->programObject = Resources->Programs[j];
  glUseProgram ( Resources->Programs[j] );
  for(int i=0; i<ObjectsLoaded; i++)
  {
   if(GameObjects[i]->shader == j+1)
   {
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
  
	if(Resources->Shaders[j].Culling && Culling == false)
	{
	 glEnable(GL_CULL_FACE);
	 glCullFace(GL_BACK);
	 Culling = true;
	}
	if(Resources->Shaders[j].Blending && Blending == false)
	{
	 glEnable(GL_BLEND);
	 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	 Blending = true;
	}
	glBindFramebuffer(GL_FRAMEBUFFER, FBO);
	GameObjects[i]->Draw(esContext);
	if(Culling == true)
	{
	 glDisable(GL_CULL_FACE);
	 Culling = false;
	}
	if(Blending == true)
	{
	 glDisable(GL_BLEND);
	 Blending = false;
	}
   }
  }
}
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind
// Clear the color buffer
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
userData->programObject = Resources->PostProcessingPrograms[0];
Quad->Draw(esContext);

and this is what the post processing quad draw call looks like

GLfloat vVertices[] = { 512.0f,  390.0f, 0.0f,
	   -512.0f, 390.0f, 0.0f,
	   -512.0f, -390.0f, 0.0f,
	   512.0f, -390.0f, 0.0f};
GLfloat vTexCoord[] = { 1.0f, 1.0f, 0.0f,
	   0.0f, 1.0f, 0.0f,
	   0.0f, 0.0f, 0.0f,
	   1.0f, 0.0f, 0.0f};
// Use the program object
glUseProgram ( userData->programObject );
glBindAttribLocation ( userData->programObject, 0, "vPosition" );
glBindAttribLocation ( userData->programObject, 1, "a_texCoord" );
glActiveTexture(GL_TEXTURE0 + 1);
Texture_Uniform_Loc = glGetUniformLocation ( userData->programObject, "texture1" );
glBindTexture(GL_TEXTURE_2D, Scene->FBO_Texture);
glUniform1i(Texture_Uniform_Loc, 1);
// Load the vertex data
glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
glEnableVertexAttribArray ( 0 );
glVertexAttribPointer ( 1, 3, GL_FLOAT, GL_FALSE, 0, vTexCoord );
glEnableVertexAttribArray ( 1 );
glDrawArrays ( GL_TRIANGLES, 0, 3 );
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );

there are no errors but everything on the screen is just black.

i tried making the quad smaller reversing the vertex order (Clockwise and CounterClockwise) i tried using the perspective matrix and rotated around to try and find the quad in case i misplaced it but nothing works the screen just stays black. im pretty sure im simply not rendering to the FBO right or im not applying the texture to the quad right but im not sure what i did wrong.

Any help would be greatly appreciated.

FBO and RBO and how they are used

06 April 2012 - 03:54 AM

I need to add frame buffer objects and render buffer objects to my engine but i am having trouble understanding how they work. Ive done some image processing at university which was basically applying algorithms on the image loaded. I thought at first that FBO basically meant rendering to it first applying said post processing algorithm then send the result to the screen.

I think i am missunderstanding how post processing effects work. i looked at a few FBO tutorials and basically only the textures are sent to it not the actual "screen" as an image. Does that mean that the effects are applied to the textures and then the textures are sent to the objects from the FBO? and what are the render buffer objects for then if the texture is sent to the FBO?

Shader ';' : syntax error; parse error

05 April 2012 - 09:48 AM

I'm working on adding normal map lighting to my terrain shader and for some reason i get a syntax error when it compiles but i cant find whats wrong with it. i added a total of nor more than 15 lines from the previous ver which compiled and worked fine.

this is what it looks like with the modifications for the normal map.

# Vertex Shader Start
uniform mat4 u_MVP;
				  
attribute vec3 aPosition;
varying vec3 vPosition;
	
attribute vec2 a_texCoord;  
varying vec2 v_texCoord;
attribute vec3 a_Tangents;  
varying vec3 v_Tangents;
attribute vec3 a_Bitangents;  
varying vec3 v_Bitangents;  
uniform mat4 matWorld;
uniform vec3 CameraPos;
varying float v_eyeDist;
uniform sampler2D texture5;
attribute vec3 a_Normals;  
varying vec3 v_Normals;
varying vec3 v_viewDirection;
varying vec3 v_lightDirection;

void main()				 
{						 
   vPosition = aPosition;
   v_texCoord = a_texCoord; 
   v_Normals = normalize(vec3(matWorld * vec4(a_Normals, 0.0)));
   vec4 tmp5 = texture2D(texture5,v_texCoord);
   vPosition.y = vPosition.y + ((tmp5.r + tmp5.g*2.0 + tmp5.b*3.0)/10.0) * 20.0;
   vec4 vWorldPos = matWorld * vec4(vPosition,1.0);
   v_eyeDist = sqrt( (vWorldPos.x - CameraPos.x) * (vWorldPos.x - CameraPos.x) + (vWorldPos.y - CameraPos.y) * (vWorldPos.y - CameraPos.y) + (vWorldPos.z - CameraPos.z) * (vWorldPos.z - CameraPos.z) );  
  
   vec3 viewDirectionWorld = vWorldPos.xyz - vPosition;
   vec3 lightPos = (matWorld * vec4(0.0,1000.0,1000.0,1.0).xyz;
   vec3 lightDirection = lightPos - vPosition;
   mat3 tangentMat = mat3(a_Tangents,a_Bitangents,a_Normals);
   v_viewDirection = viewDirectionWorld * tangentMat;
   v_lightDirection = lightDirection * tangentMat;  
   gl_Position = u_MVP * vec4(vPosition,1.0);  
}   
# Vertex Shader End
# Fragment Shader Start
precision highp float;		 
varying vec2 v_texCoord;								  
uniform sampler2D texture1;
uniform sampler2D texture2; 
uniform sampler2D texture3;
uniform sampler2D texture4;
uniform sampler2D texture6;
varying vec3 v_Normals;
varying vec3 v_viewDirection;
varying vec3 v_lightDirection; 
varying float v_eyeDist;
		
float computeLinearFogFactor()
{
   float u_fogMaxDist = 10.0;
   float u_fogMinDist = 1.0;
   float factor;
  
   factor = (v_eyeDist - u_fogMinDist) /(u_fogMaxDist - u_fogMinDist );
		   
   factor = clamp(factor, 0.0, 1.0);
   return factor;		   
}
	 
void main()								 
{
   vec4 u_fogColor = vec4(0.7,0.7,0.7,1.0); 
   float fogFactor = computeLinearFogFactor();
   vec4 tmp1 = texture2D(texture1,v_texCoord*32.0);
   vec4 tmp2 = texture2D(texture2,v_texCoord*32.0);
   vec4 tmp3 = texture2D(texture3,v_texCoord*32.0);
   vec4 tmp4 = texture2D(texture4,v_texCoord);
   float factor = 1.0 / ( tmp4.x + tmp4.y + tmp4.z );
   vec4 tmp = vec4( (tmp1.rgb * tmp4.x + tmp2.rgb * tmp4.y + tmp3.rgb * tmp4.z) * factor, 1.0 );
   vec4 lightColor = vec4(1.0,1.0,1.0,1.0);
   vec4 normal = texture2D(texture6, v_texCoord);
   normal = normalize(normal * 2.0 - 1.0);
   vec3 lightDirection = normalize(v_lightDirection);
   vec3 viewDirection = normalize(v_viewDirection);
   float nDotL = dot(normal.xyz, lightDirection);
   vec3 reflection = (2.0 * normal.xyz * nDotL) - lightDirection;
   float rDotV = max(0.0, dot(reflection, viewDirection));
   vec4 ambient = lightColor * tmp;
   vec4 diffuse = lightColor * nDotL * tmp;
   vec4 specular = lightColor * pow(rDotV, 2.0);
   vec4 FinalColor = diffuse + specular + ambient;
   gl_FragColor = fogFactor * u_fogColor + (1.0 - fogFactor) * FinalColor;
}
# Fragment Shader End

and these are the lines that where added and are probably causing the error.

vec3 viewDirectionWorld = vWorldPos.xyz - vPosition;
   vec3 lightPos = (matWorld * vec4(0.0,1000.0,1000.0,1.0).xyz;
   vec3 lightDirection = lightPos - vPosition;
   mat3 tangentMat = mat3(a_Tangents,a_Bitangents,a_Normals);
   v_viewDirection = viewDirectionWorld * tangentMat;
   v_lightDirection = lightDirection * tangentMat;

vec4 lightColor = vec4(1.0,1.0,1.0,1.0);
   vec4 normal = texture2D(texture6, v_texCoord);
   normal = normalize(normal * 2.0 - 1.0);
   vec3 lightDirection = normalize(v_lightDirection);
   vec3 viewDirection = normalize(v_viewDirection);
   float nDotL = dot(normal.xyz, lightDirection);
   vec3 reflection = (2.0 * normal.xyz * nDotL) - lightDirection;
   float rDotV = max(0.0, dot(reflection, viewDirection));
   vec4 ambient = lightColor * tmp;
   vec4 diffuse = lightColor * nDotL * tmp;
   vec4 specular = lightColor * pow(rDotV, 2.0);
   vec4 FinalColor = diffuse + specular + ambient;

can anyone see where the problem might be? or at least hint me as to what exactly the error is about cause i sure as hell checked all the ;

Tangent Bitangent(binormal)

05 April 2012 - 03:27 AM

Well i hope this is the right section.

I need to calculate the tangent and bitangent for my models and i have found an algorithm to do it but i havent been able to fully understand on what to apply the algorithm. in some places it says to do it for each face others to go through all the verts of the model and do it.

so which is the right way? do it for the 3 verts of each face? and if that is the case what happens when one vert is in more than one face?

PARTNERS