Is there a way you can get the position of an object in the 3d world? I read I had to multiply my vertices position (what vertices? Isn't that what i'm looking for?) and the GL_MODELVIEW. How am I supose to do this? Can someone post some code?
For example I have:
glpushmatrix();
glrotatef(something, 1.0, 0.0, .0);
glrotatef(anotherting, .0, 1.0, .0);
glRotatef(more, .0, 1.0, .0);
glTranslatef(.0, .0, 100);
PaintObect();
glPopMatrix();
Is it possible to know the 3d world absolut coordinates?
Thanks guys.
Nevermind i just found out.
GLfloat matrix[16];
glGetFloatv (GL_MODELVIEW_MATRIX, matrix);
const float position_x = matrix[12];
const float position_y = matrix[13];
const float position_z = matrix[14];
Related
I'm trying to fill a curved rectangle, but it is overflowing from the curved part, have used paths to create this shape. Any suggestions for how to achieve this.
Or is there any way to create a rectangle with 4 offset values, whose one side is curved like this?
I have offset values of two straight line (x1,y1), (x2,y2), (x3,y3) and (x4,y4). Any references?
final path = Path();
path.moveTo(87.75, 10.06);
path.lineTo(263.25, 80.06);
path.lineTo(263.25, 359.0);
path.lineTo(87.75, 359.0);
canvasWrapper.drawPath(path, _barAreaPaint..color = Colors.red);
Try drawing an arc
Rect rect = Rect.fromLTWH(0.0, 0.0, size.width, size.height);
path.arcTo(
rect,
0.0,
pi / 2,//adjust radians accordingly
false);
}
Having never actually developed with iOS before, I am investigating the possibilies with standard iOS controls such as text fields, list etc. and would like to know which transforms can be applied to them.
Do these standard controls support 2D transforms such as scale, translate and rotate?
Do these standard controls also support 3D transforms such as scale, translate and rotate which include a z axis?
If the answer is yes to either questions, what "level" of support exists? For example with a text field, if I transform it in 3D coordinate space can I still enter text into it?
Yes, and a lot. UITextField for example inherits from UIControl with inherits from UIView. This means that you have access to the view's transform property directly via its transform property:
[myTextField setTransform:CGAffineTransformMake(a, b, c, d, x, y)];
Or for 3D support, you can access the transform property of the view's layer to apply a CATransform3D:
[myTextField.layer setTransform:CATransform3DRotate(trans, theta, x, y, z)];
CATransform3D is defined as follows, and as #Xman pointed out, you'll need to import the QuartzCore framework using #import <QuartzCore/QuartzCore.h>, as well as link against it in your build phases.
struct CATransform3D
{
CGFloat m11, m12, m13, m14;
CGFloat m21, m22, m23, m24;
CGFloat m31, m32, m33, m34;
CGFloat m41, m42, m43, m44;
};
typedef struct CATransform3D CATransform3D;
In both of these cases, you can still interact with the text field after a transform has been applied to it.
More information can be found in Apple's documentation.
Check CATransform3D also,
CATransform3D yourTransform = CATransform3DIdentity;
yourTransform.m34 = 1.0 / -500;
//You can rotate the component in any angle around x,y,z axis using following line.
//Below line will rotate the component in 60 degree around y-axis.
yourTransform = CATransform3DRotate(yourTransform, DEGREES_TO_RADIANS(60), 0.0f, 1.0f, 0.0f); //#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)
//You can even translate the component along x,y,z axis
//Below line will translate component by 50 on y-axis
yourTransform = CATransform3DTranslate(yourTransform, 0, 50, 0);
//apply transform to component
yourComponent.layer.transform = yourTransform;
Don't forget to import
#import <QuartzCore/QuartzCore.h>
Hope this will help you.
I have loaded an wavefront object in Iphone OpenGL.
It can be rotated around x/y axis, panned around, zoomed in/out.
My task is - when object is tapped, highlight it's 2d center coordinates on screen for example like this: (Imagine that + is in the center of visible object.)
When loading OpenGL object I store it's:
object center position in world,
x,y,z position offset,
x,y,z rotation,
zoom scale.
When user taps on the screen, I can distinguish which object was tapped. But - as user can tap anywhere on object - Tapped point is not center.
When user touches an object, I want to be able to find out corresponding object visible approximate center coordinates.
How can I do that?
Most code in google I could find is meant - to translate 3d coordinates to 2d but without rotation.
Some variables in code:
Vertex3D centerPosition;
Vertex3D currentPosition;
Rotation3D currentRotation;
//centerPosition.x, centerPosition.y, centerPosition.z
//currentPosition.x, currentPosition.y, currentPosition.z
//currentRotation.x, currentRotation.y, currentRotation.z
Thank You in advance.
(To find out which object I tapped - re-color each object in different color, thus I know what color user tapped.)
object drawSelf function:
// Save the current transformation by pushing it on the stack
glPushMatrix();
// Load the identity matrix to restore to origin
glLoadIdentity();
// Translate to the current position
glTranslatef(currentPosition.x, currentPosition.y, currentPosition.z);
// Rotate to the current rotation
glRotatef(currentRotation.x, 1.0, 0.0, 0.0);
glRotatef(currentRotation.y, 0.0, 1.0, 0.0);
glRotatef(currentRotation.z, 0.0, 0.0, 1.0);
// Enable and load the vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, vertexNormals);
// Loop through each group
if (textureCoords != NULL)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(valuesPerCoord, GL_FLOAT, 0, textureCoords);
}
for (OpenGLWaveFrontGroup *group in groups)
{
if (textureCoords != NULL && group.material.texture != nil)
[group.material.texture bind];
// Set color and materials based on group's material
Color3D ambient = group.material.ambient;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (const GLfloat *)&ambient);
Color3D diffuse = group.material.diffuse;
glColor4f(diffuse.red, diffuse.green, diffuse.blue, diffuse.alpha);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat *)&diffuse);
Color3D specular = group.material.specular;
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (const GLfloat *)&specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, group.material.shininess);
glDrawElements(GL_TRIANGLES, 3*group.numberOfFaces, GL_UNSIGNED_SHORT, &(group.faces[0]));
}
if (textureCoords != NULL)
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
// Restore the current transformation by popping it off
glPopMatrix();
ok, as I said, you'll need to apply the same transformations to the object center that are applied to the object's vertices by the graphics pipeline; only this time, the graphics pipeline won't help you - you'll have to do it yourself. And it involves some matrix calculations, so I'd suggest getting a good maths library like the OpenGL Maths library, which has the advatage that function names etc. are extremly similar to OpenGL.
step 1: transform the center form object coordinates to modelview coordinates
in your code, you set up your 4x4 modelview matrix like this:
// Load the identity matrix to restore to origin
glLoadIdentity();
// Translate to the current position
glTranslatef(currentPosition.x, currentPosition.y, currentPosition.z);
// Rotate to the current rotation
glRotatef(currentRotation.x, 1.0, 0.0, 0.0);
glRotatef(currentRotation.y, 0.0, 1.0, 0.0);
glRotatef(currentRotation.z, 0.0, 0.0, 1.0);
you need to multiply that matrix with the object center, and OpenGL does not help you with that, since it's not a maths library itself. If you use glm, there are functions like rotate(), translate() etc that function similiar to glRotatef() & glTranslatef(), and you can use them to build your modelview matrix. Also, since the matrix is 4x4, you'll have to append 1.f as 4th component to the object center ( called 'w-component' ), otherwise you can't multiply it with a 4x4 matrix.
Alternatively, you could query the current value of th modelview matrix directly from OpenGl:
GLfloat matrix[16];
glGetFloatv (GL_MODELVIEW_MATRIX, matrix);
but then you'll have to write your own code for the multiplication...
step 2: go from modelview coordinates to clip coordinates
from what you posted, I can't tell whether you ever change the projection matrix ( is there a glMatrixMode( GL_PROJECTION ) somewhere? ) - if you never touch the projection matrix, you can omit this step; otherwise you'll now need to multiply the transformed object center with the projection matrix as well.
step 3: perspective division
divide all 4 components of the object center by the 4th - then throw away the 4th component, keeping only xyz.
If you omitted step 2, you can also omit the division.
step 4: map the object center coordinates to window coordinates
the object center is now defined in normalized device coordinates, with x&y components in range [-1.f, 1.f]. the last step is mapping them to your viewport, i.e. to pixel positions. the z-component does not really matter to you anyway, so let's ignore z and call the x & y component obj_x and obj_y, respectively.
the viewport dimensions should be set somewhere in your code with glViewport( viewport_x, viewport_y, width, height ). from the function arguments, you can then calculate the pixel position for the center like this:
pixel_x = width/2 * obj_x + viewport_x + width/2;
pixel_y = height/2 * obj_y + viewport_y + height/2;
and that's basically it.
I have very simple OpenGL ES example similar to Hehe's example : http://nehe.gamedev.net/tutorial/ios_lesson_02__first_triangle/50001/
As shown above triangle filled with three colors - red, blue, green.
Instead in my app i always get triangle almost completely filled with black color, only small area around top vertex filled with green and small area around right bottom filled with red ... and there is no blue at all.
The first question is : why do the colors not interpolate in the middle of my triangle and why does blue color is not visible at all?
Any changes in my colors array affect nothing, e.g. when i try to make triangle white the colors do not change anyway ... in the meantime if i change Z coordinate in positions array then i can see the blue color.
The second question is : why any changes in colors do nothing and changes in positions change the color instead?
Seems like somewhere here i made one stupid mistake but i can't catch it.
This is Vertex / Color arrays :
const float colors[] = { // this does not work, triangle still black-green-red
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0
};
const float positions[] = { // if i change 3rd index to 1.0 then i will see blue color
-0.5, -0.5, 0.0, 1.0,
0.0, 0.5, 0.0, 1.0,
0.5, -0.5, 0.0, 1.0
};
This is VBO :
- (BOOL)setupVBO
{
BOOL success = YES;
glGenBuffers(1, &_positionBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _positionBuffer);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(positions) * sizeof(float),
&positions[0],
GL_STATIC_DRAW);
glGenBuffers(1, &_colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _colorBuffer);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(colors) * sizeof(float),
&colors[0],
GL_STATIC_DRAW);
return success;
}
Render :
- (void)render:(CADisplayLink*)displayLink
{
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, _positionBuffer);
glVertexAttribPointer(_positionSlot, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, _colorRenderBuffer);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glDrawArrays(GL_TRIANGLES, 0, 3);
[_glContext presentRenderbuffer:GL_RENDERBUFFER];
}
Thanks for any advice ...
Ok, i found where was the issue :)
As far as i am beginner in Open GL i just copypasted the code from example and renamed some variables ... and did not catch that i renamed ColorBuffer (VBO color, i.e. actual color data of drawing object) variable to ColorRenderBuffer variable (place in memory where GL processes actual color data)
Stupid mistake and i hope nobody will do the same :)
So I'm starting to work with openGL, been following the tutorials over at Jeff LaMarche's blog and I've run into a problem when trying to draw a square using the GL_TRIANGLE_STRIP mode with glDrawArrays.
It works, by that I mean that I can draw a square, but I get this weird drawing glitch. You can see it here:
Drawing glitch http://img10.imageshack.us/img10/1631/picture1io.png
I'm using the xcode template that's provided by Jeff, so I assume the setup is all proper. The code I'm using in the drawView function is as follows:
Square2D *square = malloc(sizeof(Square2D));
Square2DSet(square, -0.25, -0.25, -20.0, 0.5, 0.5);
glLoadIdentity();
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, square);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 12);
glDisableClientState(GL_VERTEX_ARRAY);
if(square != NULL)
free(square);
here's the code for Square2d and Square2DSet:
typedef struct {
Vertex3D tl;
Vertex3D bl;
Vertex3D tr;
Vertex3D br;
} Square2D;
static inline void Square2DSet(Square2D *sq, GLfloat x, GLfloat y, GLfloat z, GLfloat width, GLfloat height)
{
sq->tl = Vertex3DMake(x, y, z);
sq->bl = Vertex3DMake(x, y + height, z);
sq->tr = Vertex3DMake(x + width, y, z);
sq->br = Vertex3DMake(x + width, y + height, z);
}
I'm sure I'm doing something wrong, just not sure what. Eventually the glitch will go away after the program has run for a little bit.
Any ideas?
Cheers
glDrawArrays(GL_TRIANGLE_STRIP, 0, 12);
is drawing 12 vertices. You want 4. Its asking how many indices, not how many values to look at. It will multiply the index by the number of values at each index.
The reason why its going crazy is because its drawing junk, then it disappears because I assume that junk gets filled with 0s or something and it just goes away.