Object Disappearance with rotation


13 posts
by Hardcheese » Sun Dec 23, 2012 12:10 pm
Hi,

Very recently I posted a question on transformations this is pretty much the same but I think I might be quite close to success after hunting around on the net although something must be going wrong because the rendered object disappears when I run the program. Here is the draw function:
Code: Select all
void Draw(void)
{
GLint loc;
GLfloat viewMatrix[16] = {
1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0};

static GLfloat trans[4] = {
0.0,0.0,0.0,0.0
};

static GLfloat rot[4] = {
0.0, 0.0, 0.0, 0.0
};

static GLfloat shotTrans[4] = {
0.0,0.0,0.0,0.0
};

static GLfloat  move = 0.01;
static int first = 1;
float angle = 90.0f;

glViewport(0, 0, state->render_width, state->render_height);

glClear( GL_COLOR_BUFFER_BIT );

float c = cos(90*3.14159f/180.0f);
float s = sin(90*3.14159f/180.0f);

rot[0] = c;
rot[1] = s;
rot[2] = -s;
rot[3] = c;

fprintf(stderr, "%f %f\n", s, c);

if (upPressed == 1)
{
if (trans[1] > 1.0 - 0.3)
   trans[1] = trans[1];
else
   trans[1] += 0.01;
}
if (rightPressed == 1)
{
if (trans[0] > 1.0 - 0.06)
   trans[0] = trans[0];
else
   trans[0] += 0.01;
}
if (leftPressed == 1)
{
if (trans[0] < -1.0 + 0.06)
   trans[0] = trans[0];
else
   trans[0] -= 0.01;
}
if (downPressed == 1)
{
if (trans[1] < -1.0 - 0.16)
   trans[1] = trans[1];
else
   trans[1] -= 0.01;
}

//trans[0] += move;
//if( (trans[0] > 1.0) || (trans[0] < -1.0)) move *= -1.0;
shader_select(shader);

glLineWidth(3.0f);


if(first)
{
    first = 0;
    loc =  glGetUniformLocation( shader->program, "u_mvpMatrix" );
    printf("locmvp=%d\n",loc);
    loc =  glGetUniformLocation( shader->program, "u_Translate" );
    printf("loctrans=%d\n",loc);
    loc =  glGetUniformLocation( shader->program, "u_Rotate" );
    printf("locrot=%d\n", loc);
    loc = glGetAttribLocation( shader->program, "a_position" );
    printf("locposn=%d\n",loc);
}

glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, ship );
glEnableVertexAttribArray( 0 );

glUniformMatrix4fv(2 , 1, GL_FALSE, (GLfloat*) &viewMatrix[0] );

glUniform4fv(1 , 1,  (GLfloat *) &trans[0]);

glUniform4fv(0, 1, (GLfloat *) &rot[0]);


glDrawArrays ( GL_LINES, 0, 4 );
}


And the shader code....

Code: Select all
GLchar vShaderStr[] =
"uniform mat4 u_mvpMatrix;    \n"
"uniform vec4 u_Translate;    \n"
"uniform mat2 u_Rotate;       \n"
"attribute vec4 a_position;   \n"
"void main()                  \n"
"{                            \n"
"   vec4 posn;            \n"
"   posn = a_position + u_Translate; \n"
"   posn.xy = a_position.xy * u_Rotate; \n"
"   gl_Position = u_mvpMatrix * posn;  \n"
"}                            \n";


I have also attached the full source along with this message if you need anything else. Any help is appreciated.
Attachments
code.tgz
Code
(11.72 KiB) Downloaded 69 times
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm
by PeterO » Sun Dec 23, 2012 12:40 pm
$ make
gcc -DRPI_NO_X *.c -o asteroids -I./Common -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -lGLESv2 -lEGL -lm -lbcm_host -L/opt/vc/lib `pkg-config --cflags --libs sdl`

$ ./asteroids
Unable to init SDL!

I've not used SDL so I'm not sure why SDL_Init(SDL_INIT_VIDEO) is failing.

PeterO
User avatar
Posts: 864
Joined: Sun Jul 22, 2012 4:14 pm
by Hardcheese » Sun Dec 23, 2012 12:41 pm
You can remove all the SDL code and it would still work that is just for keyboard input.
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm
by Hardcheese » Sun Dec 23, 2012 12:49 pm
The SDL code works perfectly on my pi so maybe you could just do an apt-get install libsdl-dev or something to make it work.
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm
by PeterO » Sun Dec 23, 2012 1:13 pm
Hardcheese wrote:The SDL code works perfectly on my pi so maybe you could just do an apt-get install libsdl-dev or something to make it work.

That's what I did to get it to compile......
PeterO
User avatar
Posts: 864
Joined: Sun Jul 22, 2012 4:14 pm
by Hardcheese » Sun Dec 23, 2012 1:21 pm
Are you saying that it runs without the SDL errors now?
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm
by PeterO » Sun Dec 23, 2012 1:34 pm
Hardcheese wrote:Are you saying that it runs without the SDL errors now?

No, I'm saying I've not looked into the problem with SDL in your code because that's not the original problem you wanted help with (and because I'm busy putting bugs into my own code ;) )

PeterO
User avatar
Posts: 864
Joined: Sun Jul 22, 2012 4:14 pm
by pibow » Sun Dec 23, 2012 1:44 pm
Can you post a screenshot of the transition (where the object starts to disappear?).

Sounds like perhaps you have back-face culling enabled and maybe the winding order of your polygons is wrong?
Posts: 17
Joined: Wed Jul 18, 2012 11:48 am
by PeterO » Sun Dec 23, 2012 2:22 pm
The SDL problem was because I was trying to run it via SSH ....

Now I have a blank screen with a few white pixels in the middle.

Some tips:
1) Learn how to lay out your C code properly.
2) Don't ask people to debug your code until you've at least got something that doesn't require a reboot after every run to get the console to work again :evil:

PeterO
User avatar
Posts: 864
Joined: Sun Jul 22, 2012 4:14 pm
by Hardcheese » Sun Dec 23, 2012 5:19 pm
Ahh yeh sorry about that I experienced the whole reboot thing a few times it is dependent on the SDL code working properly so you can press the escape button to exit and you can't press ctrl - C to exit.

I haven't even got white pixels on mine it's just blank and I don't know why.
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm
by fxmaker » Mon Dec 24, 2012 4:42 pm
Hi,

I have not looked at your code (and won't have time) but I can offer you advice based on debugging hundreds of student projects.

Things disappear for common reasons:
1) OpenGL fails silently - once it fails it just stops drawing until some future time. Prove that you have not caused a GL error by calling glGetError in a loop - read up of glGetError you'll see what I mean.
2) Things disappear when they are inadvertently told to - such as not being certain of the contents of a matrix before starting to use it. I saw from your code snippet you've got a very naive approach to transformations. Look up glm and use that.
3) There is a notion of near and far plane - anything rendered too close or too far will get clipped.
4) Remember winding - disable face culling for test purposes.
5) You might be getting caught by unexpected contents of a depth buffer - disable depth testing for test purposes.
6) Black backgrounds can hide objects being drawn erroneously in black. Change the background color to a fixed known color and see if there are any holes in the background color - these would be your objects.

General guidelines:
1) Simplify Simplify Simplify - create the simplest instance possible exhibiting the same bug.
2) Use the scientific method - debugging is supposed to eliminate unknowns, not introduce more of them.
3) Holmes' / Doyle's law: When you have eliminated the impossible whatever remains, however improbable, must be the solution - in other words: Test every assumption you've made and prove them to be true.

Good luck
Posts: 43
Joined: Fri Dec 07, 2012 6:37 pm
by Hardcheese » Tue Dec 25, 2012 6:53 pm
Hi fxmaker,

Thanks for those tips although I can't use GLM because I am using C as most of the examples are written in C. I don't think there is a plain C interface for GLM but I might be wrong.
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm
by PeterO » Tue Dec 25, 2012 7:53 pm
fxmaker did say he hadn't looked at your code so its not surprising some his advice doesn't apply.
PeterO
User avatar
Posts: 864
Joined: Sun Jul 22, 2012 4:14 pm