0

I want to draw object within ar but got unexpected result - gl mashine think that i see object from another side (or from inside).

Here image what i want to draw (taken from separate project)

enter image description here

And here - what i got when try to draw this object in my ar (inside of the sphere)

enter image description here

So I guess that problem is that because I put object inside sphere and adjust position of obj using base mat from sphere obj.

Camera positioned in the center of the sphere - so for this obj I use same mat - just scale/rotate/translate it.

This is how I calculate projection mat

CGRect viewFrame = self.frame;
if (!CGSizeEqualToSize(self.newSize, CGSizeZero){
    size = self.newSize;
}
CGFloat aspect = viewFrame.size.width / viewFrame.size.height;
CGFloat scale = self.interractor.scale;
CGFloat FOVY = DEGREES_TO_RADIANS(self.viewScale) / scale;

CGFloat cameraDistanse = -(1.0 / [Utilities FarZ]);
GLKMatrix4 cameraTranslation = GLKMatrix4MakeTranslation(0, 0, cameraDistanse);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(FOVY, aspect, NearZ, [Utilities FarZ]);
projectionMatrix = GLKMatrix4Multiply(projectionMatrix, cameraTranslation);
//and also here added some code for modifying, but I skip it here

For this obj I just calculate new scale and position of obj - looks like it's correct because I able to see obj and change his position etc, so skip this part.

In the second project where I got correct result of displaying obj I calculate projection mat in similar way, but with a little bit less calculation:

float aspect = self.glView.frame.size.width / self.glView.frame.size.height;
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.01f, 100);
//scale
//rotate
//translate
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1.5f);
modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, projectionMatrix);

GLfloat scale = 0.5 *_scale;
GLKMatrix4 scaleMatrix = GLKMatrix4MakeScale(scale, scale, scale);

modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, _positionX, _positionY, -5);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotationX, 0.0f, 1.0f, 0.0f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotationY, 1.0f, 0.0f, 0.0f);

modelViewMatrix = GLKMatrix4Multiply(scaleMatrix, modelViewMatrix);

In first project (correct one) I also use

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDisable(GL_CULL_FACE);

In second with few obj - depend from obj that I want to draw:

glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);

glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

//call sphere draw

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

//call obj draw

glEnable(GL_SCISSOR_TEST);

So as I sad before, I guess that the problem is that openGL "think" that we are looking to obj from another side, but I'm not sure. And if i'm right how can i fix this? Or whats done incorrect?


Update

@codetiger I check ur suggestions:

1) Wrong face winding order - recheck it again and try to inverse order, also try to build same model in another project (all works perfect) - result i guess that order is ok;

2) Wrong Culling - check all combinations of

glDisable / glEnable with argument GL_CULL_FACE
glCullFace  with argument GL_FRONT, GL_BACK or GL_FRONT_AND_BACK 
glFrontFace with argument GL_CW or GL_CCW

What i see - a little bit change but i see still incorrect obj (or wrong side or partial obj etc)

3) vertices are flipped - try to flip them, as result - even worse than before

4) try to combine this 3 suggestion one with another - result not acceptable

hbk
  • 10,908
  • 11
  • 91
  • 124
  • Use glFrontFace( GL_CW ) before drawing the object. If that fixes your problem either the winding order of your faces is wrong, which can be fixed in your model exporter or one of your matrices is "flipped". Are your sure _scale is positive? – Andreas Sep 01 '16 at 10:47
  • yes scale is grater 0, will test and let u know result – hbk Sep 01 '16 at 10:50
  • Like @Andreas has mentioned, your triangles are facing in the wrong directions. Reasons might be, 1) Wrong face winding order, 2) Wrong Culling, 3) vertices are flipped. – codetiger Sep 01 '16 at 12:15
  • just try glFrontFace( GL_CW ) - this not fix my problem, also recheck `_scale` - positive, vertices not flipped - redraw same obj in different project without my AR sphere - looks like ok, so i guess problem due to some behaviour between ar sphere projection and my obj; also try to draw with GL_Point mode to make sure that all points are positioned correctly - and result - yes all points are correctly positioned... (here is link https://drive.google.com/file/d/0B1GU18BxUf8heG83QXNvWVRkN0k/view?usp=sharing for reference video to make it more clear) – hbk Sep 01 '16 at 12:28
  • @codetiger - please see my update – hbk Sep 02 '16 at 06:24
  • Plz check if depth mask is enabled and you are properly setting up your depth buffer. May be OpenGL is not able to differentiate near and far triangles as there is some depth buffer issue. – codetiger Sep 02 '16 at 07:25
  • @gbk either, you are not writing to the depth buffer, or your depth buffer is not set properly. – codetiger Sep 02 '16 at 07:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122481/discussion-between-gbk-and-codetiger). – hbk Sep 02 '16 at 07:27
  • I use text coord, vertexBuffer, indises and normals also this `glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE);` – hbk Sep 02 '16 at 07:31
  • @codetiger one more interesting point - when i use glEnable(Gl_SCISSORS_TEST) - i not able to see object at all - this mean i guess that openGL engine think that we dont see object - but why? – hbk Sep 05 '16 at 19:55

0 Answers0