CGPath invisible on CCLayer - cocos2d-iphone

I am working with Cocos2d. I am trying to make a "trail" using a CGPath. The CGPath is on screen I have NSLogged it. The "trail" or CGPath isn't visible.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 20.0);
CGContextMoveToPoint(context, p0.x, p0.y);
CGContextAddLineToPoint(context, p1.x, p1.y);
CGContextStrokePath(context);
}
The "trail" needs to be dynamically generated.
I have a few different errors that look like:
<Error>: CGContextDrawPath: invalid context 0x0
Any help would be nice

//try this
- (void)drawRect:(CGRect)rect
{
UIGraphicsBeginImageContext(self.boundingBox.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 20.0);
CGContextMoveToPoint(context, p0.x, p0.y);
CGContextAddLineToPoint(context, p1.x, p1.y);
CGContextStrokePath(context);
UIGraphicsEndImageContext();
}

Related

OpenGL object distortion in perspective view

Do you guys have any idea what is the cause of this problem? It looks fine in orthographic view.
Orthographic View:
Perspective View:
Code that might be related:
//Global variable
float tx = 0, tz = 0, tSpeed = 1.0;
bool isOrtho = true;
float ONear = -20.0, OFar = 20.0;
float PNear = 1.0, PFar = 41.0;
float ptx = 0, pty = 0, ptSpeed = 0.1;
float pry = 0, prSpeed = 1.0;
void projection() {
glMatrixMode(GL_PROJECTION); //refer to projection matrix
glLoadIdentity(); //reset projection matrix
glTranslatef(ptx, pty, 0.0); //translation for projection
glRotatef(pry, 0.0, 1.0, 0.0); //rotate for projection
if (isOrtho) {
//Ortho View
glOrtho(-20.0, 20.0, -20.0, 20.0, ONear, OFar); //Ortho view
}
else {
//Perspective view
gluPerspective(45, 1.0, -1.0, 1.0);
glFrustum(-20.0, 20.0, -20.0, 20.0, PNear, PFar);
}
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
projection();
glMatrixMode(GL_MODELVIEW);
lighting();
drawRobot();
}
The glu docs for gluPerspective state:
zNear
Specifies the distance from the viewer to the near clipping plane (always positive).
(emphasis mine).
Your code gluPerspective(45, 1.0, -1.0, 1.0); passes -1.0 for zNear, which is not in the allowed range of values.

Bounding box in opengl c programming

How can I create a bounding box around a 3d object that I've created in 3DS MAX, and then used it in an openGL application? I need the bounding box for collision detection, but I don't understand how to link a bounding box to an object. The function that creates the object is:
void drawR2D2(){
if (xrot <= 5.0) xrot = 5.0;
if (xrot >= 70.0) xrot = 70.0;
if (ztransKit2 >= 0.7) ztransKit2 = 0.7;
if (xtransS1 >= 0.2) xtransS1 = 0.2;
if (ztransKit2 <= 0.0) ztransKit2 = 0.0;
if (xtransS1 <= 0.0) xtransS1 = 0.0;
glPushMatrix();
drawBodyR2();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0, 1.2, 0.0);
glScalef(2.85, 2.85, 2.85);
glRotatef(yrotHead, 0.0, 1.0, 0.0);
drawHeadR2();
glPopMatrix();
glPushMatrix();
glScalef(3.5, 3.5, 3.5);
drawLegsR2();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.41, 0.45, 0.26);
glScalef(1.4, 1.4, 1.4);
glRotatef(xrotKit1, 1.0, 0.0, 0.0);
drawKit1R2();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0.5, 0.0 + ztransKit2);
glScalef(2.0, 2.0, 2.0);
glRotatef(zrotKit2, 0.0, 0.0, 1.0);
drawKit2R2();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.25 + xtransS1, 0.48, 0.45);
glScalef(1.55, 1.55, 1.55);
drawShield1R2();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.44 + xtransS2, 0.48, 0.37);
glScalef(1.12, 1.12, 1.12);
drawShield2R2();
glPopMatrix();
}
The other functions: drawBodyR2(),drawHeadR2, etc are the components of the entire object.
The code for the drawBodyR2, for example, is:
void drawBodyR2(){
InitModel("r2d2/body.obj", &body);//function to initialize the object
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, bodyId);
glmDraw(body, GLM_SMOOTH | GLM_TEXTURE);
glDisable(GL_TEXTURE_2D);
}
Can anyone show me how to create a bounding box around the object resulted from the drawR2D2 function?
The "best" box you can build for collision detection is an oriented bounding box (OBB), that is the smallest possible box that fits the object. In order to do that you need to learn some maths (what eigenvectors are mainly) and use them for the OBB (check this answer for a detailed explanation about OBB computation).
Note that:
OBB has nothing to do with OpenGL. You can compute the OBB/ABB having a model and never drawing it. Separate the tasks!
An OBB should never be computed twice for the same model, it is computational nonsensical. The ideal thing would be to precompute the coordinates of the 8 points forming the OBB of each of the objects you are going to draw and store them. Remember that this implies that whatever geometric transformation you apply to your object, you need to also apply it to the OBB.

Qt mouse event left button not translating qt object

I have the following mouse event on QGLWidget class ...
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
QPointF mousePosition = event->pos();
float dx = event->x() - lastPos.x();
float dy = event->y() - lastPos.y();
if (event->buttons() & Qt::LeftButton) {
setXRotation(xRot + 8.0 * dy);
setYRotation(yRot + 8.0 * dx);
} else if (event->buttons() & Qt::RightButton) {
xTrans += dx;
yTrans -= dy;
}
lastPos = event->pos();
}
Rotation in paint GL is working but not translation.. What am I doing wrong... ?
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
glTranslatef(xTrans, yTrans, 0.0f);
glScalef(scale, scale, scale);
//draw function here
glPopMatrix();
}
You have to pop the matrix for it to get it applied.
http://www.swiftless.com/tutorials/opengl/pop_and_push_matrices.html
That allows for the pushed matrix to actually finish calculating and get used.
glPopMatrix(); //end the current object transformations
Hope that helps.

How to get transformed coordinate in OpenGL?

I draw a solid sphere like the followings:
glPushMatrix();
glScalef(0.015, 0.015, 0.015);
glRotatef(90, 1.0, 0.0, 0.0);
glTranslatef(0.0, 200, 0.0);
glRotatef(-20, 0.0, 0.0, 1.0);
glRotatef(-20, 1.0, 0.0, 0.0);
glTranslatef(78.75, -110.74, -13.53);
glutSolidSphere(4.0f,15,15);
glPopMatrix();
How can I get the transformed coordinates of this solid sphere?
You can get the state variables GL_MODELVIEW_MATRIX by the function glget.
It returns the current matrix from ModelView stack. I think that is what you need.
Put the translated coordinates in variables, then you won't have to retrieve the transformed coordinates of the shape.
float solidSphereX = whatever;
float solidSphereY = whatever;
float solidSphereZ = whatever;
float solidSphereRotationX = whatever in radians;
float solidSphereRotationY = whatever in radians;
float solidSphereRotationZ = whatever in radians;
...
glPushMatrix();
glRotatef(solidSphereRotationX, solidSphereRotationY, solidSphereRotationZ);
glTranslatef(solidSphereX, solidSphereY, solidSphereZ);
glPopMatrix();

What is wrong with this text color?

I'm using the openFrameworks ofxPango addon to render text with following code:
ofxPango* pango;
ofxPCContext* context;
ofxPCPangoLayout* layout;
ofImage text_image;
pango = new ofxPango();
context = pango->createContextWithSurface(width, height);
context->color4f(1,1,1, 0.0f);
context->paint();
layout = context->createPangoLayout();
layout->setText(text);
layout->setTextColor(186,34,29, 1.0f);
layout->setWidth(width);
layout->setJustify(true);
//context->paint();
ofxPCPangoFontDescription* fd = new ofxPCPangoFontDescription();
fd->createFromString(font);
layout->setFontDescription(*fd);
layout->show();
text_image.allocate(context->getSurface()->getWidth(), context->getSurface()->getHeight(), OF_IMAGE_COLOR_ALPHA);
text_image.setFromPixels(context->getSurface()->getPixels(), text_image.width, text_image.height, OF_IMAGE_COLOR_ALPHA, true);
I'm having trouble understanding how layout->setTextColor(r,g,b,a) works.
If I run:
0,0,0,1 - text is black as it should be
255,0,0 - text is red as it should be
186,34,29,1 - text appears very light gray (maybe white) when it should be red
186,34,0,1 - text is yellow, although it should be red
Why are these colors coming out wrong?
I think color values are supposed to be within the range 0.0f and 1.0f, where:
0.0f = no color
0.5f = half color
1.0f = full color
Here are some abridged examples from the Cairo library, which ofxPango calls out to:
color_white
1.0, 1.0, 1.0, 1.0
color_black
0.0, 0.0, 0.0, 1.0
color_transparent
0.0, 0.0, 0.0, 0.0
color_magenta
1.0, 0.0, 1.0, 1.0