OpenGL Code that should work.

Recommended Posts

Can someone tell me why this code doesn't do what i must do? Supposdly, when i press "a" the rectangle should go further, but it just desappears.

typedef float dpi_vertex[3];
typedef float dpi_matrix44[16];

void cDPI_Frame::getMatrix44(dpi_matrix44 m)
{
dpi_matrix44 x;
dpi_crossProduct(x, front, up);

m[0]=x[0];
m[1]=x[1];
m[2]=x[2];
m[3]=0;

m[4]=up[0];
m[5]=up[1];
m[6]=up[2];
m[7]=0;

m[8]=front[0];
m[9]=front[1];
m[10]=front[2];
m[11]=0;

m[12]=origin[0];
m[13]=origin[1];
m[14]=origin[2];
m[15]=1;

}

void cDPI_Frame::getOrigin(dpi_vertex v)
{
v[0]=origin[0];
v[1]=origin[1];
v[2]=origin[2];
}

void cDPI_Frame::MoveFoward(float p)
{
origin[0]+=front[0]*p;
origin[1]+=front[1]*p;
origin[2]+=front[2]*p;
}

void dpi_crossProduct(dpi_vertex result, dpi_vertex vectora, dpi_vertex vectorb)
{
result[0]=(vectora[1]*vectorb[2])-(vectorb[1]*vectora[2]);
result[1]=-(vectora[0]*vectorb[2])+(vectorb[0]*vectora[2]);
result[2]=(vectora[0]*vectorb[1])-(vectorb[0]*vectora[1]);
}

cDPI_Frame camera;
dpi_matrix44 temp;
dpi_vertex v;

int result=0;
result=SDL_Init(SDL_INIT_VIDEO);
if(result!=0)
return false;
result=(int)SDL_SetVideoMode(width, height, 32, SDL_OPENGL);
if(result!=0)
return false;

glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);

glOrtho(-100, 100, -100, 100, -100, 100);

glMatrixMode(GL_MODELVIEW);

for(;;)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
input->update();

if(input->isKeyPressed(SDLK_ESCAPE))
break;

if(input->isKeyPressed(SDLK_a))
{
printf("origin=(%f, %f, %f)\n", v[0], v[1], v[2]);
camera.MoveFoward(0.01);
}

camera.getMatrix44(temp);
camera.getOrigin(v);
glPushMatrix();
glMultMatrixf(temp);
glTranslatef(-v[0], -v[1], -v[2]);

glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glEnd();

glPopMatrix();
render->update();
}


There are only relevant pieces of code. Any doubt about it tell me. I have used gluLookat too and some other tricks but all of them do the same stuff, make the cube desappear instead of move foward.

Share on other sites
I think that your issue is that you do not take into account the framerate your box is running at, the code

camera.MoveFoward(0.01);

moves your cube forward by 0.01 units per _frame_, but you do see the cube before you hit 'a' right? use the SDL call SDL_GetTicks() {it returns how much time in thousandth's of a second has passed since SDL was started up} to see how much time has passed between frames and use that value to scale your 0.01f.

Best Regards

Share on other sites
Well, the problem is that i have used:

camera.MoveFoward(0.001);
and
camera.MoveFoward(0.0001);

both with the saame effect.
At that rate there should be something visible.

Also, after debugging, i saw that the square desappear when the counter reachers -1.00
Before that it stays.

Share on other sites

You're moving your camera, not the box.

You're using glOrtho(), which I've only ever used for 2d and masking. I'd use gluPerspective(). Make sure your box isn't outside of the clipping planes.

If I'm thinking straight, the glOrtho() thing doesn't show perspective (objects don't get smaller the further they are from the camera). Think of it as a cuboid as opposed to a pyramid with the top cut off.

so that's possibly it.

Share on other sites
Hmm... if I may ask, what are you initialising "front"'s values to?

Other than that, I would tend to agree with kRogue - it may simply be that your code is executing so fast that even using a step of 0.1*front or 0.0001*front is moving the object out of your viewing space too quickly.

I would second his suggestion to base the degree of forward movement on the time between frames - even if this is not your current problem, it should produce movement that better matches what I imagine you expect.

Share on other sites
Sorry, i forgot the constructor:

cDPI_Frame::cDPI_Frame(){	origin[0]=0.0;	origin[1]=0.0;	origin[2]=0.0;		front[0]=0.0;	front[1]=0.0;	front[2]=-1.0;		up[0]=0.0;	up[1]=1.0;	up[2]=0.0;		}

By the way, i want to say that the FPS i not an issue. As you can see there is a printf just above the MoveFoward member. And believe me, it doen't go THAT fast.

I can agree with the theory of the ortho-problem.
I will try moving to the sides instead and after i have what i want i may change it to perspective.

Share on other sites
Hmm... You do make a good point about the frame rate.

However, I do notice something else:

You appear to be moving the object along the z-axis, if I'm not much mistaken, in which case, since you are viewing the scene orthographically, it seems to me now that TheUmpteenth makes a good point. I would expect, given this, that you should see the object sit still for a moment, and then disappear as it exits the viewing volume. Note that, as TheUmpteenth says, an orthographic projection should not show any perspective.

If it disappears immediately, try changing the third value of "front" to "1" - if it remains on-screen for a longer period and then disappears, then you would seem to be moving the object behind the camera on the first step (since it would seem that you have the camera and object placed at the same point in space).

If you want the object to move across the screen, then I believe that you should be moving along x or y (or both, of course), instead of z, as you appear to be doing now.

If you want the object to become smaller as it moves "into" the screen, then I would recommend either a perspective projection or simply scaling the object.

Share on other sites
Well, actually what i wanted to make is to move with the arrows foward and backward, right and left, so i can "explore" the polygons.
Of course, this are just some tests before the real thing.
I'll try moving to other sides and changing to perspective.
thanks

Share on other sites
Well, yep the problem was that, the orthographic projection. I tried moving to the sides and it worked.

Share on other sites
np :) glad you got it fixed

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627656
• Total Posts
2978464
• Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33