Jump to content

Talk:OpenGL Programming/Scientific OpenGL Tutorial 02

Page contents not supported in other languages.
Add topic
From Wikibooks, open books for an open world
Latest comment: 6 years ago by Jwaffe

Could somebody add more descriptions for the magic numbers / scale factors in the code?

[edit source]

I had a hard time following all of the scale / offset factors going on in the sample code, I'm going through it and trying to figure out what all these constants do. If anyone knows what these do and why they are needed, could somebody add more information to the article? Thanks!

I did my best below but it's nowhere near comprehensive, I'll edit this if I can figure out more of these,

Point array generation:

for(intpointIndex=0;pointIndex<DATA_POINTS;pointIndex++){constintcHalfway=DATA_POINTS/2;// Note that x is not the x coordinate of the point in the OpenGL window.// These are texture coordinates, I think.//// x goes from -10.24 to +10.24floatx=(pointIndex-cHalfway)/100.0;// Why x * 10.0?floaty=sin(x*10.0)/(1.0+x*x);// I think y is in the range [-1, 1], I think this is scaling it to be between 0 and 255.graph[pointIndex]=roundf(y*128+128);}

Vertex shader code

attributefloatcoord1d;// this sample's amplitudevaryingvec4f_color;uniformfloatoffset_x;uniformfloatscale_x;uniformsampler2Dmytexture;constfloatcXAxisScale=10.24;voidmain(void){// ?????//float y = (texture2D(mytexture, vec2(x / 10.24 / 2.0 + 0.5, 0)).r - 0.5) * 2.0;// x coordinates go from -10.24 to +10.24// Why cXAxisScale / 2.0?floatmysteriousXScaleFactor=cXAxisScale/2.0;// Why subtract 0.5?floatmysteriousXOffset=0.5;floatx=(coord1d/scale_x)-offset_x;floattextureXPos=x/mysteriousXScaleFactor+mysteriousXOffset;vec2textureCoord=vec2(textureXPos,0);vec4textureValue=texture2D(mytexture,textureCoord);// Subtract 0.5 to shift it to the middle I think?// I'm pretty sure this is vector subtraction...floatmysteriousTextureShiftValue=0.5;floatmysteriousYScaleFactor=2.0;// I think textureValue.x makes more sense than textureValue.r in the source; it's not a color.floaty=(textureValue.x-mysteriousTextureShiftValue)*mysteriousYScaleFactor;gl_Position=vec4(coord1d,y,0.0,1.0);// ????f_color=vec4(x/2.0+0.5,y/2.0+0.5,1.0,1.0);gl_PointSize=5.0;}

--Jwaffe (discusscontribs) 21:08, 10 March 2019 (UTC)Reply

My comments

[edit source]
  • Try to find out which texture formats your card supports. -> no idea :)
  • Should we use "x / 2 + 0.5" instead of "x * 2 + 0.5" in the vertex shader?
float y = (texture2D(mytexture, vec2(x / 2.0 + 0.5, 0)).r - 0.5) * 2.0; 

Similarly, to do a full [-1,+1]->[0,1] conversion, and a smoother gradient, the color could be /2:

f_color = vec4(x/2 + 0.5, y/2 + 0.5, 1.0, 1.0); 
  • Maybe add a few explanations on the scaling/offset algorithm. Between tut01 and tut02, not only the formula is reversed for x, but also the offset has not the same meaning (scaling-independent in tut01 and scaling-dependent in tut02). I hit my head on the wall a couple times before I understood everything ;)
close