• Advertisement

krienie

Member
  • Content count

    7
  • Joined

  • Last visited

Community Reputation

102 Neutral

About krienie

  • Rank
    Newbie
  1. Hey guys, I have a problem with rendering out my vertex normals in GLSL. I have a simple scene with one point light, a cube and a low-poly sphere. Now I'm trying to render out the vertex normals of the objects, but somehow they render not as expected. Here is the normal data I pass to the shader for the cube: [code]GLfloat tmpNorm[24] = { -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f };[/code] For the ball I used the Icosahedron sample from the OpenGL red book 7th edition, so the normal data should be right, but here is what I get: [attachment=5736:renderComposition.jpg] Not quite what you'd expect. The normals for the ball are all facing one direction and the normals of the cube are facing inwards. Just to make sure it wasn't my awefull GLSL skills, I used [url="http://stackoverflow.com/questions/4703432/why-does-my-opengl-phong-shader-behave-like-a-flat-shader"]this[/url] shader to render out the normals, as shown below (the shader from answer 2, with the bunny ). Vertex shader: [code]#version 150 core in vec3 vPos; in vec3 vNorm; uniform mat4 mvMatrix; uniform mat4 mvpMatrix; uniform mat4 normMatrix; out Data { vec4 vPos; vec4 vNorm; mat4 mvp; } vdata; void main() { vdata.mvp = mvpMatrix; vdata.vPos = vec4(vPos, 1.0f); vdata.vNorm = normalize(normMatrix * vec4(vNorm, 1.0f));}[/code] Geometry shader: [code]#version 150 core layout(triangles) in; layout(line_strip, max_vertices = 6) out; in Data { vec4 vPos; vec4 vNorm; mat4 mvp; } vdata[3]; out gData { vec4 color; } gdata; void main() { const vec4 green = vec4(0.0f, 1.0f, 0.0f, 1.0f); const vec4 blue = vec4(0.0f, 0.0f, 1.0f, 1.0f); for (int i = 0; i < 3; i++) { gl_Position = vdata[i].mvp * vdata[i].vPos; gdata.color = green; EmitVertex(); gl_Position = vdata[i].mvp * (vdata[i].vPos + vdata[i].vNorm); gdata.color = blue; EmitVertex(); EndPrimitive(); } }[/code] Fragment shader: [code]#version 150 core in Data { vec4 color; } gdata; out vec4 outputColor; void main() { outputColor = gdata.color; }[/code] So I then figured it had to be my normalmatrix. To calculate the normalmatrix for each object, I use the inverted and then transposed upper-left 3x3 matrix of the object's modelmatrix. Because my rendering engine can only work with 4x4 matrices, I put that normal matrix in a 4x4 matrix like so: [code][ n1, n2, n3, 0, n4, n5, n6, 0, n7, n8, n9, 0, 0, 0, 0, 1][/code] According to my research that should be correct, so now I'm really lost. I really don't know what I"m doing wrong here. The normal matrix seems right and so does the shader (although I'm not sure why the normals are rendered in black, instead of green and blue....), so I was wondering if you guys could help me figure out what I'm doing wrong here. I hope I've given enough info for you guys to help me out. Many thanks in advance!
  2. maybe you should post some code. Like how you create your perspective matrix and the face drawing code.
  3. OpenGL Translation Question.

    I don't recommend using glTranslate at all, because it has been deprecated since version 3.0 and removed since 3.1. What you can do is use buffers like Vertex Buffer Objects for storing your vertex information and then use functions like glDrawElements to draw everything in the buffer at once. It is much more modern and faster. If you are really into doing modern openGL stuff, you should also take a look at GLSL shaders. Hope this helps you out ;)
  4. OpenGL proper glUniform use

    [quote name='V-man' timestamp='1314288666' post='4853695'] [quote name='phantom' timestamp='1314227924' post='4853441'] [quote name='Murdocki' timestamp='1314209993' post='4853319'] [font="Arial"]You probably misinterpreted the "not changing frequently" part, uniforms are described as not changing frequently because they're the same for the whole draw call unlike vertex attributes which change every vertex. [/font][/quote] The term 'not changing frequently' applies just as well to something you set once during all your draw calls in a scene as it would to per-object. In fact if your MVP matrix is only set once and never update during a scene/frame render setting it multiple times per scene/frame is wasteful and something which should be avoided (CPU time and GPU time overhead). Now, I'm not saying there IS a way to use GLSL like that, although I hope there is because if not then that's a *facepalm* right there, but there really should be a way of doing it. My GLSL is just too rusty right now to point out that method if it exists [/quote] Of course there is. UBO = Uniform buffer object Which is basically a VBO for uniform values. You need to make a GL 3.1 context to use it. Just shuv a GL_UNIFORM_BUFFER into your VBO function calls and get the location from your shader object. [url="http://www.opengl.org/documentation/specs/"]http://www.opengl.or...entation/specs/[/url] and you update the whole fucking uniform block inluding your MVP matrix in one go. [/quote] Now this is interesting! I'm totally going to check this out. Thank you very much!
  5. OpenGL proper glUniform use

    [quote name='karwosts' timestamp='1314158282' post='4853070'] ... This sounds like a reasonable thing to do to me. Obviously you don't want to send data that you don't have to, but I can't imagine many cases where a single object gets a shader all to itself. I think just updating the matrices before drawing is not a big deal, unless you have unusual use case where it becomes impractical. [/quote] That's what I thought. It seems a bit stupid to have two objects have it's own shader, each doing the exactly the same thing. I'll leave it the way it is then. Thanks for the replies everyone ;)
  6. I've worked with Autodesk Maya for a couple of years and it has NURBS which are quite the same as the vectors you describe. You should look into it.
  7. Hey all, I'm kind of new to openGL and GLSL, so I"m not that familiar with all the proper uses yet. Like glUniform. So here's my sitiuation: I'm currently working on a game engine that uses openGL. I've made a DisplayObject that can render out 3D object from data stored in VBO's and a GLSL shader. It also uses a home-made matrix class for storing model-view, normal and modelview-projection matrices which the GLSL shader can use. I"m currently reading through the OpenGL Shading Language book (orange book) and in there the writers state that uniform attributes should only be used with data that doesn't change frequently. So what I did is declare the matrices in the shader as uniforms and made a neat updateMatrices() function for my DisplayObject class that is called everytime a transformation is done to the object (for example when it translates or rotates). This function updates the matrices and then sends the new information to the GLSL shader using glUniform. Now this all works just fine when using one object, but when I introduce a second object or more, all the objects are snapped to the coordinates and orientation of the object that did the last transformation. After some time of debugging I was able to fix this by calling chop the glUniform calls out of updateMatrices and call just before every object is drawn instead. Even though this works, I'm really doubting if I did the right thing here, because what I'm now doing is sending matrix data to the shader even though the values didn't change. [code]void DisplayObject::render() { if ( readyToRender ) { glBindVertexArray(vaoName); /* send new matrices to the shaderprogram */ glUniformMatrix4fv(glGetUniformLocation(program, "mvMatrix"), 1, GL_FALSE, modelmatrix.data); glUniformMatrix4fv(glGetUniformLocation(program, "normMatrix"), 1, GL_FALSE, normalMatrix.data); /* send new modelview-projection matrix to the shaderprogram */ modelmatrix = modelmatrix * *projectionmatrix; glUniformMatrix4fv(glGetUniformLocation(program, "mvpMatrix"), 1, GL_FALSE, modelmatrix.data); glDrawElements(GL_TRIANGLES, bufferSize[index], GL_UNSIGNED_INT, 0); } }[/code] So my question now is: am I doing the right thing and if not: how do you guys suggest I should send the matrix data to the shader? Many thanks in advance ;)
  • Advertisement