# OpenGL Code that should work.

## Recommended Posts

Dospro    257
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
kRogue    100
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
Dospro    257
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
TheUmpteenth    142

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
Thaumaturge    3826
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
Dospro    257
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
Thaumaturge    3826
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
Dospro    257
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
Dospro    257
Well, yep the problem was that, the orthographic projection. I tried moving to the sides and it worked.

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

## Create an account

Register a new account

• ### Similar Content

• Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).

• Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
Skype: Mangodoor408
• By tyhender
Hello, my name is Mark. I'm hobby programmer.
So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine).
And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
Sorry for late replies.
I mostly give more information when people PM me,but this post is REALLY short,even for me =D
So here's few more points:
Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me.

• By Zaphyk
I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?

• I'm trying to get some legacy OpenGL code to run with a shader pipeline,
The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
I've got a version 330 vertex shader to somewhat work:
#version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
Question:
What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?

Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.

• 10
• 11
• 24
• 26
• 17