# arthurw

Member

6

102 Neutral

• Rank
Newbie
1. ## Calculating height at an arbitrary position on a quad

In short: calculate the plane of the quad. Solve the plane equation for Y. An attempt at a few more details... Solving for y: a plane equation is in the form Ax + By + Cz + D = 0, so y = -(Ax + Cz + D) / B. Plane for the quad: the cross product of two adjacent edges in the quad gives the normal of the plane (A, B, C) and then any point in the quad can be used to solve for D. [indent=1]N = (Q2 - Q1) x (Q1 - Q0) [indent=1]A = Nx [indent=1]B = Ny [indent=1]C = Nz [indent=1]D = -(N dot Q0) (where Q0 through Q3 are the points of the quad. D could be rewritten as D = -(A*x + B*y + C*z) where x,y,z are the components of Q0, if that makes more sense to you.) so expanding it out a bit: [indent=1]Py = -(Nx * Px + Nz * Pz - (N dot Q0)) / Ny I'm sure a google search or two could explain the details better than I have, but hopefully this is enough info to get you going.

3. ## vector-math library compatible with OpenGL

I've been using GLM for a while and have been very happy with it. It's not perfect, but it certainly beats writing the world's billionth version of matrix multiplication functions. (And as for getting a float pointer to a vec3, you can use simple C++: "vec3 v; float* p = &v.x;" ). I agree that it's sometimes hard to know if GLM supports what you need or not. A GLM quick reference / cheat sheet would be very helpful for the library. On the other hand, I've found the author of the library be responsive in maintaining a library: I logged two defects and they were both fixed rather quickly. For what it's worth, I've found GLM to be a pretty solid library once you spend enough time to know your way around it.
4. ## Best openGL resource

Nothing like suggesting something you yourself aren't willing to do, but I'll go ahead and do it anyway... It'd be great if someone went and rewrote the NeHe tutorials using only the OpenGL 3+ core profile only. I'm sure it'd immediately become a very popular site. (By the way, I completely agree with the last couple posts. It understand why it happens, but it's frustrating when tutorials, books, lessions, etc. develop a framework or toolkit when they claim to be about learning the core API. Sure, to get to some advanced features you'll need to reuse some code, but limit it to functions, not frameworks - and write out the API calls full in when possible!)
5. ## Per-face data in a GLSL geometry shader (GLSL 1.5)

Following up on my own post... V-man, thanks - I appreciate the reply. However, I was looking to do something a bit different. I didn’t want to control the interpolation level (the smooth/flat shading example for a poor one on my part, sorry); I wanted to figure out how to pass down arbitrary data on a per-face basis to the geometry shader. Some effects could be done via per-vertex attributes and the “flat” keyword, but would require figuring out a unique provoking vertex for each face to know where to assign those face attributes - a complication which I wanted to avoid. The solution for what I wanted I realized was quite easy: Create a 1D texture of width = number of facesFill the texture so that each texel represents the data for one faceIndex the texture in the geometry shader by (gl_PrimitiveIDIn + .5) / textureSize(sampler, 0) That got me exactly what I was looking for. Again, thanks for the initial reply - just wanted to follow up in case others were interested.
6. ## Per-face data in a GLSL geometry shader (GLSL 1.5)

GLSL newbie question: how do pass per-face data to a GLSL geometry shader? I'm targeting GLSL 1.5. What I'm trying to do specifically is decide whether to use the vertex normals or generate a face normal (via the cross product) for each face in the object. I have this working across the entire object via a uniform variable (i.e. toggle between smooth and flat shading); however, I'd like to pass a per-face flag to allow finer grain control. Is there some way I should be using gl_PrimitiveIDIn to accomplish this? I'm fairly confident in how to pass down per-vertex arrays of data and using uniforms, but I'm not sure of the middle ground of per-face data works. I realize I could duplicate vertices between faces and simply pass per-face data as redundant per-vertex data, but I'd prefer not use that approach if there are other options. For reference, below is a simplified version of the geometry shader I currently have that uses a uniform for the whole object rather than per-face basis. In other words, what I'm trying to do if replace that "unifFlatNormals" variable with a per-face variable. [source lang="cpp] uniform int unifFlatNormals; in vec3 geomVertexEc[]; // Vertex position in eye coordinates from the vertex shader in vec3 geomNormalEc[]; // Vertex normal in eye coordinates from the vertex shader varying out vec3 fragNormalEc; vec3 computeNormal (vec3 v0, vec3 v1, vec3 v2) { vec3 a = v2 - v1; vec3 b = v1 - v0; return normalize( cross (b, a) ); } void main() { vec3 n = computeNormal(geomVertexEc[0], geomVertexEc[1], geomVertexEc[2]); for (int i = 0; i < gl_VerticesIn; i++) { gl_Position = gl_PositionIn; if (unifFlatNormals == 1) fragNormalEc = n; // Replace the per-vertex data with a uniform normal across the entire primitive else fragNormalEc = normalize(geomNormalEc); // Pass the per-vertex data directly EmitVertex(); } EndPrimitive(); } [/source]