I'm trying to learn OpenGL with "the redbook", and I'm now at the point were I'm testing glMultiDrawElements. I already got my code to work with glDrawElements, and other "simpler" methods, but it stops working here, and now I'm completly stuck. My code is supposed to draw a cube, and the acctuall drawing code is posted under:
GLfloat verticies[] = {-0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5};
glVertexPointer(3, GL_FLOAT, 0, verticies);
GLubyte front[] = {4, 5, 6, 7};
GLubyte left[] = {0, 3, 7, 4};
GLubyte right[] = {5, 1, 2, 6};
GLubyte back[] = {0, 1, 2, 3};
GLubyte bottom[] = {0, 1, 5, 4};
GLubyte topp[] = {2, 3, 7, 6};
GLvoid *faces[6] = {front, left, right, back, bottom, topp};
glMultiDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, faces, 6);
glFlush();
The program is cocoa based, and compile fine, but stops under running due to "EXC_BAD_ACCESS". There is a yellow warning triangle next to glMultiDrawElements(..) saying "incompatible integer to pointer conversion", but i tought i was using pointers in the "faces" array (at least that exactly how it looks in "the red book"). Can somone help me?
The second parameter to glMultiDrawElements() is a pointer to an array of sizes, not a numerical value for a size. This is why the compiler is complaining about an integer to pointer conversion (which, by the way, is another reason why you should enable the compiler flag for turning warnings into errors when doing Cocoa development).
I believe changing the above to read
GLsizei count[] = {4, 4, 4, 4, 4, 4};
glMultiDrawElements(GL_QUADS, count, GL_UNSIGNED_BYTE, faces, 6);
should work, but I haven't tested it here.
Related
I'm trying to figure out how to use eigenVV, but no luck so far. The following is my code and could anyone of you point out what I'm doing wrong? Thanks.
CvMat* A2;
A2 = cvCreateMat(3, 3, CV_32FC1);
cvmSet(A2, 0, 0, 1);
cvmSet(A2, 0, 1, 3);
cvmSet(A2, 0, 2, -3);
cvmSet(A2, 1, 0, -3);
cvmSet(A2, 1, 1, 7);
cvmSet(A2, 1, 2, -3);
cvmSet(A2, 2, 0, -6);
cvmSet(A2, 2, 1, 6);
cvmSet(A2, 2, 1, -2);
CvMat* transp_A2;
CvMat* ATA2;
CvMat* eigenVal2;
CvMat* eigenVec2;
transp_A2 = cvCreateMat(3, 3, CV_32FC1);
ATA2 = cvCreateMat(3, 3, CV_32FC1);
eigenVal2 = cvCreateMat(3, 1, CV_32FC1);
eigenVec2 = cvCreateMat(3, 3, CV_32FC1);
cvTranspose(A2, transp_A2);
cvMatMul(transp_A2, A2, ATA2);
cvEigenVV(ATA2, eigenVec2, eigenVal2);
Edit :
This is output of eigenVec2 variable
-0.236854,0.862897,-0.446442,
0.969565,0.239264,-0.0519332,
-0.0620046,0.445154,0.893304,
I wanted to use that EigenVector EigenValue to create projection matrix and comparing it against with projection matrix I have. I thought my answer was wrong when it doesn't match with the one I have. But I figured out the resulting projection matrix is not exactly the same. It is differed by scale factor. So the way I use eigenvv is correct.
I am creating a web page to illustrate the 3D transformations and I am using Three.js. I have detected a problem when I try to do a negative scale in Y axis. In this case, the object is not affected (a face inversion should be done but it doesn't). However, for negative scales in axis X or Z it works well. Any help? This is my code:
var m = new THREE.Matrix4(
scaleX, 0, 0, 0,
0, scaleY, 0, 0,
0, 0, scaleZ, 0,
0, 0, 0, 1
);
cube.applyMatrix(m);
If I use cube.scale.set(scaleX,scaleY,scaleZ) the first transformation is performed rightly, but I can't link with other transformations. I need for my application that the user can do several transformations in the same scene.
Thanks in advance
Your matrix is not correct.
Try with :
var m = new THREE.Matrix4(
1, 0, 0, scaleX,
0, 1, 0, scaleY,
0, 0, 1, scaleZ,
0, 0, 0, 1
);
cube.applyMatrix(m);
I'm mystified. In this code:
SynthDef(\acid,
{
|out, gate = 1, freq, myParam, amp, cutoff, resonance, filtEnvAmt|
var env, audio, filtEnv;
if (myParam == \something, { freq = 200; });
env = Linen.kr(gate, 0, 1, 0, doneAction: 2);
audio = LFSaw.ar(freq, mul: amp);
filtEnv = Line.kr(midicps(cutoff + filtEnvAmt), midicps(cutoff), 0.2);
audio = RLPFD.ar(audio, ffreq: filtEnv + MouseX.kr(0, 5000), res: MouseY.kr(0, 1), dist: 0);
Out.ar(out, audio * env);
}
).add;
b = Pbind(*[
out: 0,
instrument: \acid,
stepsPerOctave: 19,
scale: [0, 3, 5, 8, 11, 14, 17],
octave: 3,
degree: Pseq([0, \, 3, 3, 4, 4, 9, 4, 4]),
myParam: \something,
//prevFreq: Pseq([0, 0, 0, 0, 9, 0, 0, 0, 0]),
dur: Pseq([0.4, 0.4, 0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.1]),
cutoff: Pseq([60, \, 50, 60, 80, 60, 90, 80, 50]),
filtEnvAmt: Pseq([20, \, 20, 20, 20, 20, -10, 20, 20]),
resonance: Pseq([0.5, \, 0.5, 0.5, 0.5, 0.5, 0.3, 0.5, 0.5])
]);
b.play;
..the equality test myParam == \something never returns true, despite the fact that the Pbind is clearly sending \something each time. No other value will work either: 0, nil etc.
The equality tests myParam == myParam and \something == \something do work however, so in these cases I get a monotone melody.
I can only guess that a Pbind sends each value in some kind of wrapper, but I've no idea how to then check them from inside the synth. Can anyone help?
First: you can't send symbols to a synth control. You can only send numbers.
Second: your example doesn't say what freq should be if the test is false. In fact, you should write it in more of a dataflow style such as:
freq = if(testcondition, 200, 300);
That's the kind of thing that will work in a synthdef.
Third is a slightly frustrating thing in sc language, which is that the == message is always evaluated at synthdef compile time - the equality is checked once, and then never again. In order to have "live" equality checking, you can use this slightly clunky expression:
BinaryOpUGen("==", thinga, thingb)
So in summary you might write
freq = if(BinaryOpUGen("==", myParam, 1), 200, 300);
I have built the following simple synth structure which creates a synth and routes its output through an effects unit:
b = Bus.audio(numChannels: 2);
SynthDef(
"mySynth",
{
|freq, amp, gate = 1|
var vol = 0.5;
var audio = Pulse.ar(freq, 0.5);
var env = EnvGen.kr(Env.perc, doneAction:2);
audio = Pan2.ar(audio, MouseX.kr(-1, 1));
Out.ar(b, audio * env);
}
).add;
SynthDef(
"effects",
{
var audio = In.ar(b, 2);
audio = LPF.ar(audio, MouseY.kr(200, 1000));
//TODO: Implement some crazy, revolutionary effects
Out.ar(0, audio);
}
).add;
// **** Dividing line for executing the code ****
e = Synth(\effects);
p = Pbind(*[
instrument: \mySynth,
scale: #[0, 2, 4, 5, 7, 9, 11],
degree: Pseq([3, 3, 9, 9, 2, 9, 9, 3, 5, 7], inf),
dur: Pseq([0.2, 0.2, 0.2, 0.1, 0.1, 0.2, 0.2, 0.2, 0.1, 0.1], inf),
amp: Pseq([1, 0.6, 0.9, 0.3, 0.4, 0.9, 0.6, 0.85, 0.3, 0.4], inf),
]);
p.play;
This only produces audible output when I execute the code in a particular way:
I can execute each block individually, in order, and I get audible output.
I can execute the first blocks up to the 'dividing line' comment, then the following blocks, and I get audible output.
If I execute all the code together, I don't get audible output.
I'm guessing there has to be some delay between declaring a SynthDef and then instantiate it using Synth(), while the server does set setup stuff. Can anyone shed any light?
I usually get around this with the Server.sync() method. It pauses execution of the enclosing thread (e.g. a Routine) until all asynchronous server commands have been completed. This includes sending SynthDefs and allocating Buffers. You can pass a Condition argument to Server.sync() for more explicit control.
so for example, you can execute this block in one go:
s = Server.local;
s.boot;
s.doWhenBooted({
Routine {
SynthDef.new(\sine, {
arg out=0, hz=220, dur=4.0;
var snd, amp;
snd = SinOsc.ar(hz);
amp = EnvGen.ar(Env.linen(0.1, dur, 0.1), doneAction:2);
Out.ar(out, (amp*snd).dup);
}).send(s);
s.sync; // waits here
x = Synth.new(\sine);
}.play;
});
It is because you can't just "add" SynthDefs to the server and create an instance of said synth in the same execution. If you "play" the synths as they are executed then an instance of them gets added to the server so that when you call the Synth up for execution it will already be loaded. Working code is included below.
(
b = Bus.audio(numChannels: 2);
SynthDef(
"mySynth",
{
|freq, amp, gate = 1|
var vol = 0.5;
var audio = Pulse.ar(freq, 0.5);
var env = EnvGen.kr(Env.perc, doneAction:2);
audio = Pan2.ar(audio, MouseX.kr(-1, 1));
Out.ar(b, audio * env);
}
).play;
SynthDef(
"effects",
{
var audio = In.ar(b, 2);
audio = LPF.ar(audio, MouseY.kr(200, 1000));
//TODO: Implement some crazy, revolutionary effects
Out.ar(0, audio);
}
).play;
// **** Dividing line for executing the code ****
e = Synth(\effects);
p = Pbind(*[
instrument: \mySynth,
scale: #[0, 2, 4, 5, 7, 9, 11],
degree: Pseq([3, 3, 9, 9, 2, 9, 9, 3, 5, 7], inf),
dur: Pseq([0.2, 0.2, 0.2, 0.1, 0.1, 0.2, 0.2, 0.2, 0.1, 0.1], inf),
amp: Pseq([1, 0.6, 0.9, 0.3, 0.4, 0.9, 0.6, 0.85, 0.3, 0.4], inf),
]);
p.play;
)
I'm sure you're right that it's to do with the delay between declaring the synthdef and it being ready.
I'm not really experienced enough with sclang to immediately tell you exactly how you should change your code (I generally use scsynth via OSC, only using sclang to write SynthDefs), but you should be able to do something with the optional completionMsg argument to SynthDef.add.
I'm working on getting a simple lighting right on my OpenGL ES iPhone scene. I'm displaying a simple object centered on the origin, and using an arcball to rotate it by touching the screen. All this works nicely, except I try to add one fixed light (fixed w.r.t. eye position) and it is badly screwed: the whole object (an icosahedron in this example) is lit uniformly, i.e. it all appears in the same color.
I have simplified my code as much as possible so it's standalone and still reproduces what I experience:
glClearColor (0.25, 0.25, 0.25, 1.);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrthof(-1, 1, -(float)backingWidth/backingHeight, (float)backingWidth/backingHeight, -10, 10);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = { -1.5f, 1.0f, -400.0f, 0.0f };
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
float currRot[4];
[arcball getCurrentRotation:currRot];
glRotatef (currRot[0], currRot[1], currRot[2], currRot[3]);
float f[4];
f[0] = 0.5; f[1] = 0; f[2] = 0; f[3] = 1;
glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, f);
glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, f);
f[0] = 0.2; f[1] = 0.2; f[2] = 0.2; f[3] = 1;
glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, f);
glEnableClientState (GL_VERTEX_ARRAY);
drawSphere(0, 0, 0, 1);
where the drawSphere function actually draws an icosahedron:
static void drawSphere (float x, float y, float z, float rad)
{
glPushMatrix ();
glTranslatef (x, y, z);
glScalef (rad, rad, rad);
// Icosahedron
const float vertices[] =
{ 0., 0., -1., 0., 0., 1., -0.894427, 0., -0.447214, 0.894427, 0.,
0.447214, 0.723607, -0.525731, -0.447214, 0.723607, 0.525731,
-0.447214, -0.723607, -0.525731, 0.447214, -0.723607, 0.525731,
0.447214, -0.276393, -0.850651, -0.447214, -0.276393, 0.850651,
-0.447214, 0.276393, -0.850651, 0.447214, 0.276393, 0.850651,
0.447214 };
const GLubyte indices[] =
{ 1, 11, 7, 1, 7, 6, 1, 6, 10, 1, 10, 3, 1, 3, 11, 4, 8, 0, 5, 4, 0,
9, 5, 0, 2, 9, 0, 8, 2, 0, 11, 9, 7, 7, 2, 6, 6, 8, 10, 10, 4, 3,
3, 5, 11, 4, 10, 8, 5, 3, 4, 9, 11, 5, 2, 7, 9, 8, 6, 2 };
glVertexPointer (3, GL_FLOAT, 0, vertices);
glDrawElements (GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_BYTE, indices);
glPopMatrix ();
}
A movie of what I see as the result is here. Thanks to anyone who can shed some light into this (no kidding!). I'm sure it will look embarassingly trivial to someone, but I swear I have looked at many lighting tutorials before this and am stuck.
Try adding some vertex normals using glNormalPointer(). It looks like OpenGL ES is just using the default normal for everything.