webGL,SLGL? where should I start with this code - webgl

I am familiar with C or C++
However, I am a quite newbie for WEBGL and GLSL.
It might be too basic or silly question though... I appreciate you give me some hints.
I found this source code which makes the great CG graphics.
I want to compile this by myself, However honestly saying I am not sure what it is , and what with compiler I can compile and make this work .
#define R rotate2D
for(float i,e,g,s;i++<1e2;g+=e*.2)
{
vec3 n,p=vec3((FC.xy-.5*r)/r.y*g,g-4.);
p.zy*=R(.4);
p.z+=t;
e=p.y;
for(s=1.;s<6e3;s+=s)p.xz*=R(s),n.xz*=R(s),n+=cos(p*s),e+=dot(sin(p.xz*s),r/r/s/3.);
n.y=1.;
n=normalize(n);
e-=snoise2D(p.xz*40.)*n.y*n.y*.4;
o-=exp(-e*9.-5.);
}
o++;
So at first, I started to learn about webGL.
And finished learning basic webGL
However I cant make it work with webGL.
Next, I started to learn about SLGL on unity,,, however it's not hit the spot.
What should I learn to compile this???

This looks a piece of a GLSL shader, and you would compile that with gl.compileShader()
But it's incomplete. First of all to be completely compilable it needs a main function that sets gl_FragColor. And I see a reference to snoise2D and rotate2D which should be functions, but are missing. And a least a few local undefined variables like FC and r. So without those pieces, this won't compile.
If you can solve those issues, you should be able to plug into something like ShaderToy without have to invent a whole WebGL application from scratch. But if this shader is meant to interact with specific geometry that might not work at all.
It's hard to advise more without more info on what this is, how its intended and what all that single letter variable names are supposed to mean.

Related

Is reusing a WebGLProgram a good idea?

Instead of creating new WebGlProgram's using gl.createProgram() is it a good idea to keep reusing one?
I am listing the steps that I should be doing if I am to reuse one:
AttachShader(s): in my case I need to attach a new Fragment shader only. (Question: Can I hang on to a compiled shader?)
linkProgram
useProgram
getAttribLocation
getUniformLocation
What are you trying to do? 99.9% of all GPU apps make shader programs and are done. They might make 1, they might make 5000, but they aren't ever in a position where they would even need to consider your question. So what are you really trying to do?
Those few apps that do allow you to edit shaders (shadertoy, glslsandbox, vertexshaderart, ...) either make new ones and delete old or reuse. There's no benefit to one or the other it's just a matter of style.
Yes you can hold on to shaders. You can use shaders with multiple programs. It's common to do so.
If you change a shader it won't affect a program unless/until you relink that program with gl.linkProgram. Anytime you call gl.linkProgram and it's successful all your previous uniform locations for that program are obsolete and you have to query new ones.

How to use User-Input to address specific variables in Pascal (Eval/Exec?)

I'm trying to do something very specific in the fractal program Apophysis 7X, the scripting language in use is Pascal (the project is written in Delphi).
What I want to do:
Write a script that can dynamically address certain variables. In the program I have so called transforms, and each transform has multiple variations, new variations can be added by plugins, hence I do not know all names there could be.
The variables are addressed like this:
Transform.Linear:=Sin(Pi*(FrameCount / FrameQuantity));
The Variation that is to be changed might not be Linear though, but a dozen other words, like Spherical or Zcone.
If eval would work I'd assume the solution to be something like this:
VariationName:=User-Input;
eval('Transform.' + VariationName + ':=Sin(Pi*(FrameCount / FrameQuantity));')
As far as I know though, there is no such thing like eval or exec in Pascal (Tried: Eval/eval/Exec/exec). Searching other sites and the internet didn't turn up any ideas either.
So the question is how can I use the User-Input to address those variables? Obviously:
Transform.'User-Input':=Sin(Pi*(FrameCount / FrameQuantity));
will not work. Since I don't know all names up front I can't just use an array or anything static either. Any ideas would be greatly welcome.
What you want to do is called Reflection. Delphi does have support for it (they call it Extended RTTI). Give a look at the functions IsPublishedProp and SetPropValue.

Loading a texture from file post DX11.1?

In 11.1 and later Microsoft removed a lot of helpers for loading textures (fromfile, fromstream etc).
I'm trying to port my code over to 11.2 and everything works fine except for this line :
var texture = Texture2D.FromFile<Texture2D>(device, #"C:\Texture.png");
Now all i could find was guidance telling me to use WIC instead but i can't seem to find anything providing me with a Texture2D that is nearly as versatile (all the samples i found require passing in the pixel format among other things).
I'm looking for a solution that would let me load files (without knowing their format or layout before hand) and get a Texture2D out of it just like FromFile allowed. Is there anything like that or even close? I'm assuming there has to be "something" as they wouldn't just deprecate such a feature if it wasn't superfluous.
Any help is most appreciated.
Edit : I'd like to avoid using the SharpDX.Toolkit too, i'm looking for a raw DirectX / WIC etc solution as i don't want to add a dependency on the Toolkit. I'm however perfectly fine on adding any .net Framework 4.0 or 4.5 assembly as a dependency.
There is no easy solution apart from writing the whole WIC interop yourself. Even if you don't want to use the Toolkit, the source code is available and the class WicHelper.cs responsible for decoding images is fairly easy to adapt.
If you want a very simple solution that doesn't handle all WIC cases (format mappings...etc.), you can have a look at TextureLoader.cs.
Provided you have both a Texture2D and a WPF BitmapImage lying around, something like this will help:
var map = device.ImmediateContext.MapSubresource(texture, 0, MapMode.WriteDiscard, MapFlags.None);
bitmap.CopyPixels(Int32Rect.Empty, map.DataPointer, bitmap.PixelWidth * bitmap.PixelHeight * 4, bitmap.PixelWidth * 4);
device.ImmediateContext.UnmapSubresource(source, 0);
The function you mention was probably something fairly similar under the hood.
If you're not sure about the bitmap being Bgra, you have to convert it first.

How to make a change in Qualcomm's Vuforia Sample App

I have been looking through the threads at the Qualcomm Forums but no luck since I don't know exactly how to look for what I want.
I'm working with the ImageTargets Sample for iOS and I want to change the teapot to another image (a text rather) I had.
I already have the render and I got the .h using opengl library but I can't figure out what do I need to change to make this work and since this is the very basic and I haven't been able to make it work I really haven't ventured to try anything else.
Could anyone please help me out?
I would paste code here but it's a whole project so I don't know exactly what to put if needed please let me know.
If the case is still valid, here's what you have to do:
get header file for 3D object
get texture image for this object
in EAGLView.mm make this changes:
import "yourobject3d.h"
add your texture to textureFilenames array(this should be at the begining of EAGLView
eventually take care about kObjectScale (by deafult it was about 3.0f, for one object I did have to change it even up to 120.0f)
in setup3dObjects method assign proper arrays of vertices/normals/texture coords (check in "yourobject3d.h" file for proper arrays and naming) to Object3D *object
make this change in renderFrameQCAR
//glDrawElements(GL_TRIANGLES, obj3D.numIndices, GL_UNSIGNED_SHORT, (const GLvoid*)obj3D.indices);
glDrawArrays(GL_TRIANGLES, 0, obj3D.numVertices);
I believe that is all... if something take a look at Vuforia's forum, i.e. here: https://developer.vuforia.com/node/2047669
NOTE: default teapot.h does (!) have indices, which are not present in banana.h (from comment below) so take care about that too
Have a look at the EAGLView.mm file. There you'll have to load the textures (images) and 3d objects (you'll need to import your .h instead of teapot.h and modify setup3dObjects accordingly).
They are finally rendered by calling the renderFrameQCAR function.
Actually, teapot is not an image. It's a 3D model stored in .h format which includes Vertices, Normals, and Texture coordinates. You should have a good knowledge of OpenGL ES to understand those codes in sample app.
An easier way to change the 3D model to whatever you want is to use a rendering engine which facilitates the drawing and rendering stuffs and you don't need to bother OpenGL APIs. I've done it with jPCT-AE for Android platform but for iOS there is a counterpart called OpenFrameworks engine. It has some plugins to load 3Ds or MD2 files and since it's written in C++ you can easily integrate it with QCAR.
This is a short video of my result with jPCT and QCAR:
Qualcomm Vuforia + jPCT-AE test video

Using 3D Studio Max DirectX shader in XNA problem

UPDATE 2: It now appears this is more of a modelling issue than a programming one. Whoops.
I'm new to XNA development, and despite my C# experience, I've been stuck at one spot for going on two days now.
The situation: I've created a model in 3D Studio Max 2010 which uses two materials, both are of type DirectX Shader. The model exports to FBX without error and Visual Studio compiles it properly. When I ran the Draw() method initially, it threw an exception on the 'BasicEffect' portion of one of my loops, demonstrating (at least to me) that it was loading the .fx file correctly, which must be embedded in the FBX file or something.
The problem:
When using the following code
foreach (ModelMesh mesh in map.Meshes)
{
foreach (Effect effect in mesh.Effects)
{
effect.CurrentTechnique = effect.Techniques["DefaultTechnique"];
effect.Begin();
effect.Parameters["World"].SetValue(Matrix.CreateTranslation(Vector3.Zero));
effect.Parameters["View"].SetValue(ActiveCamera.ViewMatrix);
effect.Parameters["Projection"].SetValue(ActiveCamera.ProjectionMatrix);
effect.Parameters["WorldViewProj"].SetValue(Matrix.Identity * ActiveCamera.ProjectionMatrix);
effect.Parameters["WorldView"].SetValue(Matrix.Identity * ActiveCamera.ViewMatrix);
foreach (EffectPass ep in effect.CurrentTechnique.Passes)
{
ep.Begin();
// something goes here?
ep.End();
}
effect.End();
}
mesh.Draw();
}
The only thing that happens is a white box appears covering the bottom half of the screen, regardless of camera position or angle. I got the name of the effect parameters of the default.fx file specified in Max (it's at [program files]\autodesk\3ds Max 2010\maps\fx).
I get the feeling I'm setting one or all of these incorrectly. I've tried to look up tutorials and follow their code, however none of it seems to work for my model.
Any help or ideas?
UPDATE:
By making these changes:
effect.Parameters["WorldViewProj"].SetValue(Matrix.CreateTranslation(Vector3.Zero) * ActiveCamera.ViewMatrix * Conductor.ActiveCamera.ProjectionMatrix);
effect.Parameters["WorldView"].SetValue(Matrix.CreateTranslation(Vector3.Zero) * ActiveCamera.ViewMatrix);
The model was able to draw. However, everything is completely white :(
Unfortunately, especially without seeing your shader and/or knowing what error it is that you're getting, it's going to be pretty difficult to figure out what's wrong here. There are a number of things that could be going wrong.
My suggestion is to start with a simpler test. Make a box, apply a very simple shader ... and make that render. Then, add some parameter that (for example) multiplies the red component of the pixel shader by the amount passed in. And make that render successfully.
By simplifying the problem set, you are figuring out the nuances of the shaders that max exports and how you set the properties. At some point, you'll realize what you're doing wrong and will be able to apply that to your more complex shader.
I'm quite interested in hearing how this goes ... make sure you comment on this once you've fixed it so I see the outcome. Good luck! :-)

Resources