• Content count

  • Joined

  • Last visited

Community Reputation

121 Neutral

About Hintze

  • Rank
  1. Hey! i am having some issues with my framebuffers, i have successfully implemented deferred rendering and now instead of drawing the result of the light calculations on to screen i created a second frame buffer with a single texture that holds the results of the calculations, this works fine as i can see the texture getting the correct data in the OpenGL debugger. However when i try to draw to a quad on the screen with the result from the second frame buffer all i get is black, even when i try to draw without the texture just using the uv coordinates as the colors, this works fine if i skip the entire deferred rendering pass and just draw the screen quad colorized so the quad itself works fine. { // Create the FBO glGenFramebuffers(1, &m_fbo); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo); // Create the gbuffer textures glGenTextures(ARRAY_SIZE_IN_ELEMENTS(m_textures), m_textures); glGenTextures(1, &m_depthTexture); for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_textures) ; i++) { glBindTexture(GL_TEXTURE_2D, m_textures[i]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, m_textures[i], 0); } // depth glBindTexture(GL_TEXTURE_2D, m_depthTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WindowWidth, WindowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; glDrawBuffers(ARRAY_SIZE_IN_ELEMENTS(DrawBuffers), DrawBuffers); GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (Status != GL_FRAMEBUFFER_COMPLETE) { printf("FB error, status: 0x%x\n", Status); return false; } glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); // restore default FBO glGenFramebuffers(1, &m_fbopost); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbopost); glGenTextures(1, &postTexture); glBindTexture(GL_TEXTURE_2D, postTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_TEXTURE_2D, postTexture, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); return true; } void GBuffer::BindForWritingPost() { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbopost); } void GBuffer::BindForWriting() { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo); } void GBuffer::BindFortReadingPost() { glBindFramebuffer(GL_READ_FRAMEBUFFER,m_fbopost); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, postTexture); } void GBuffer::BindForReading() { //glBindFramebuffer(GL_READ_FRAMEBUFFER,0); glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo); for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_textures); i++) { glActiveTexture(GL_TEXTURE0 + i); glBindTexture(GL_TEXTURE_2D, m_textures[GBUFFER_TEXTURE_TYPE_POSITION + i]); } } while (!window.ShouldClose()) { _update_fps_counter(window.GetWindow()); DSGeometryPass(); BeginLightPasses(); DSPointLightsPass(); PostEffects(); glfwSwapInterval(0); glfwSwapBuffers(window.GetWindow()); glfwPollEvents (); glClearColor(0.f, 0.f, 0.f, 0.f); } void DSGeometryPass() { //retina // glViewport(0, 0, window.GetFrameBufferWidth()/2, window.GetFrameBufferHeight()/2); gBuffer.BindForWriting(); glDepthMask(GL_TRUE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); camera.ComputeMatricesFromInputs(window.GetWindow()); ViewMatrix = camera.GetViewMatrix(); ProjectionMatrix = camera.GetProjectionMatrix(); heightMap.Update(ViewMatrix, ProjectionMatrix); heightMap.Draw(); // for (int i = 0; i < spheres.size(); i++) { // spheres[i].Update(&window, ViewMatrix, ProjectionMatrix); // spheres[i].Draw(&camera, shadow.GetShadowMatrix()); // } glDepthMask(GL_FALSE); glDisable(GL_DEPTH_TEST); } void BeginLightPasses() { glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); glBlendFunc(GL_ONE, GL_ONE); gBuffer.BindForReading(); gBuffer.BindForWritingPost(); glClear(GL_COLOR_BUFFER_BIT); } void DSPointLightsPass() { // glViewport(0, 0, window.GetFrameBufferWidth(), window.GetFrameBufferHeight()); angle +=0.005; angle2 -= 0.03f; // pLight.setPosition(pLight.GetPosition().x+(glm::vec3(((float)cos(angle))*100,0.f,pLight.GetPosition().z+((float)sin(angle)*100)))); pLight2.setPosition((glm::vec3(((float)cos(angle2))*100,0.f,((float)sin(angle2)*100)))); sphere2.SetPosition(pLight2.GetPosition()); sphere2.Update(&window, camera.GetViewMatrix(), camera.GetProjectionMatrix()); float BSphereScale2 = CalcPointLightBSphere(pLight2); sphere2.Scale(glm::vec3(BSphereScale2,BSphereScale2,BSphereScale2)); sphere2.Draw(&camera, shadow.GetShadowMatrix()); sphere.SetPosition(pLight.GetPosition()); sphere.Update(&window, camera.GetViewMatrix(), camera.GetProjectionMatrix()); float BSphereScale = CalcPointLightBSphere(pLight); sphere.Scale(glm::vec3(BSphereScale,BSphereScale,BSphereScale)); sphere.Draw(&camera, shadow.GetShadowMatrix()); } void PostEffects() { if(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER)==0) { int i = 0; } // gBuffer.BindFortReadingPost(); screenQuad.Draw(); } // // ScreenQuad.cpp // 3D2 projekt scratch // #include "GBuffer.h" #include "ScreenQuad.h" static const GLfloat g_quad_vertex_buffer_data[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, }; void _print_shader_info_log (unsigned int shader_index) { int max_length = 2048; int actual_length = 0; char log[2048]; glGetShaderInfoLog (shader_index, max_length, &actual_length, log); printf ("shader info log for GL index %i:\n%s\n", shader_index, log); } void ScreenQuad::Init() { glGenVertexArrays(1,&VAO); glBindVertexArray(VAO); glGenBuffers(1,&Buffer); glBindBuffer(GL_ARRAY_BUFFER,Buffer); glBufferData(GL_ARRAY_BUFFER,sizeof( g_quad_vertex_buffer_data), g_quad_vertex_buffer_data,GL_STATIC_DRAW); ShaderInfo shaders[] = { {GL_VERTEX_SHADER,"resources/PostProcess.vs"}, {GL_FRAGMENT_SHADER,"resources/PostProcess.fs"}, {GL_NONE,NULL} }; glVertexAttribPointer( 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); program = LoadShaders(shaders); glUseProgram(program); glEnableVertexAttribArray(0); } void ScreenQuad::Draw() { glUseProgram(program); int test = 6; glActiveTexture(GL_TEXTURE0+test); glBindTexture(GL_TEXTURE_2D,test); glUniform1i(glGetUniformLocation(program, "screen"), 6); // glUniform1i(glGetUniformLocation(program, "screen"),test); // ensure the proper arrays are enabled glBindBuffer(GL_ARRAY_BUFFER, Buffer); glEnableVertexAttribArray(0); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 6); glDisableVertexAttribArray(0); } #version 410 uniform sampler2D screen; in vec2 UV; out vec4 fragColor; void main() { fragColor = vec4(UV.x,UV.y,1,1);//texture(screen,UV);// } thanks for your time
  2. I figured out one way to solve it and thought i share it if someone ever gets the same issue. I simply made a viewport with the desired texture resolution and then rendered into the texture and then before drawing the texture to the screen resizing the viewport to the actual window size and then scaling the texture up in the shader.   //Hintze
  3. Hey guys! I got a little problem that hopefully is an easy to solve one. I am using a mbp with a retina screen so when i in glfw call to create a window it creates one with twice the size of the parameters i send in.  I have implemented deffered rendering so i tried creating the textures with my desired resolution(half the size of the actual framebuffer). If I create my viewport with my desired resolution I only get a quarter of the screen when i draw out. But if I create a viewport with the actual frame buffer size I can scale up the texture but the center of the screen is somehow in the top corner, as if i try to move towards an object i move diagonally towards the top right corner.   Hope someone can shed some light on this one :)   thanks for your time! //Hintze
  4. thanks a lot guys!! :D it works perfectly now :)   //Hintze
  5. thanks for your reply! But I am afraid i cannot se any real difference in result, i tried to move the light to the center of the map and i noticed when i rotate the camera that the lighting stops clearly almost always at the same place, in the cameras perspective, i upload some pictures to illustrate what i mean.
  6. hey guys! I am having some problems with my light which i can't solve, and i have tried everything i can think of to try and solve it but to no avail.   I hope you can see what my problem is in the video, The quicktime screen recording crashed every time i started my program.   VertexShader #version 410 layout(location = 0) in vec4 vPosition; layout(location = 1) in vec4 vColor; layout(location = 2) in vec2 vUV; layout(location = 3) in vec3 vNormal; uniform mat4 MVP; uniform mat4 M; uniform mat4 V; uniform mat4 P; out vec4 color; out vec3 normal; out vec4 position; out vec3 eyeDirection; void main() { color = vColor; normal = vNormal; position = V*M* vPosition; eyeDirection = normalize(-(V * M * vPosition).xyz); normalize(eyeDirection); gl_Position = MVP * vPosition; } FragmentShader #version 410 in vec4 color; in vec3 normal; in vec4 position; in vec3 eyeDirection; uniform vec3 Ambient; uniform vec3 LightColor; uniform vec3 LightPosition; uniform float Shininess; uniform float Strength; uniform mat4 V; uniform mat4 M; uniform float ConstantAttenuation; uniform float LinearAttenuation; uniform float QuadricAttenuation; out vec4 fColor; void main() { vec3 lightDirection = vec4(M *V * vec4(LightPosition,1)-position).xyz; float lightDistance = length(lightDirection); //normalize the lightdirection vector, so that a dot product gives cosines lightDirection = lightDirection/lightDistance; //Model how much light is available for this fragment float attenuation = 1.0/ (ConstantAttenuation + LinearAttenuation * lightDistance + QuadricAttenuation * lightDistance * lightDistance); //the direction of maximum highlight also changes per fragment vec3 halfVector = normalize(lightDirection + eyeDirection); float diffuse = max(0.0,dot(normal,lightDirection)); float specular = max(0.0,dot(normal,halfVector)); if (diffuse == 0.0) specular = 0.0; else specular = pow(specular,Shininess)*Strength; vec3 scatteredLight = Ambient + LightColor * diffuse * attenuation; vec3 reflectedLight = LightColor * specular * attenuation; vec3 rgb = min(color.rgb * scatteredLight + reflectedLight,vec3(1.0)); fColor = vec4(rgb,color.a); // fColor = color; } thanks for your time! //Hintze    
  7. thanks for your response, When loading in my textures I save the data of the texture away so I don't need to read from the texture directly, this should be more efficient, right? the data of the texture is loaded into  unsigned char * data; and is deleted after loading them into OpenGL except the texture data i need later. if(strcmp (imagepath,"blend.bmp") == 0) { colorMapData = data; deletedata = false; }  I am pretty sure I get the data right this way because i successfully loaded in another texture in and drew it on top of the terrain just as i wanted, but when I try to read from the saved away texture data I get what you can see in the picture, I am happy to show you more code but I am not sure what would be useful. thanks for your time:) //Hintze
  8. Hi guys! I have a texture i wish to take a part of the data from and set to another texture, for example I have my color map covering my entire terrain and I wish to take  the data from a square 10x10 and set to another texture which is then to be used as a sub texture on my terrain. This i do as a test to see i am doing everything right, I will not take all the data from the color map when i get this to work. I can't get this to work anything like I want and all I get are stripes of RGB. for (int y = 0; y < 10; y++) { for (int x = 0; x<30; x++) { imageData[x+y*30]=colorMapData[x+512*3*y]; } } this code i thought would give a small 10x10 chunk of the color map to my imageData but instead I get what you can see in the picture. I hope you guys can see what I am doing wrong :)   Thanks for your time! //Hintze  
  9. thank you! when i loaded it in GL_BGR everything works just fine the only problem is the one i had before with the underlaying color of the colormap still showing in sharp angles, see the grass picture.   never mind what i said I don't seem to have the problem when using other textures so that is not an issue:) thanks everyone for your help! :D //Hintze
  10. thank you! :D when i loaded it in GL_BGR everything works just fine :) the only problem is the one i had before with the underlaying color of the colormap still showing in sharp angles, see the grass picture.
  11. It seems it all had to do with my new bmp i tried to load in, I  tried it again with a texture I know work and it´s all fine, except the color is wrong and the color of the color map is seen in sharp angles.  
  12. glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data); that is how i load in my color map, so i tried using GL_RGB but with the same result.   not quite sure how my data could be formatted wrong, all i do is instead of binding it to a texture in my textureLoader i save it instead as unsigned char * imageData; if(strcmp (imagepath,"test.bmp")) { imageData = data; } edit:might have found an error on my part... which is what I use later in my other function. thanks for your answer, and just ask if you need more info. //Hintze
  13. Hi guys! I have a little problem with my program, when i try to set a sub texture to my color map on my terrain it works just as i want it to except it draws the sub texture all wrong. So I am not quite sure how to proceed at the moment so any help is greatly appreciated :) void UpdateTexture::CreateSubTexture(glm::vec2 position, unsigned char * imageData) { glBindTexture(GL_TEXTURE_2D, 3); glTexSubImage2D(GL_TEXTURE_2D, 0, position.x, position.y, 128, 128 , GL_RGBA, GL_UNSIGNED_BYTE, imageData); } the sub texture is an all purple square at 128x128 for testing purposes, but as you can see in the picture that is not what i am getting at all.   thanks for your time! //Hintze