How to scale an image in only one direction in opengl? - opengl

I understand that glScalef(x,0,0) scales on X axis in both directions(+ve and -ve). But how to do the scaling in only one direction?(either +ve or -ve). And what coordinates should y and z have?(0 or 1) Throw some light on this topic.

glTranslatef(x*0.5f, 0.0f, 0.0f);
glScalef(x, 1.0f, 1.0f);
drawObject();
treat this code like this: scale my object (x, 1, 1) then translate (x*0.5, 0, 0)... from bottom to top.
do not scale by 0! glScalef(x, 0, 0) will make your object dissapear!
look here and here
note that you are using old opengl and try to look for "modern" opengl tutorials.

Related

From gluOrtho2D to 3D

I followed a guide to draw a Lorenz system in 2D.
I want now to extend my project and switch from 2D to 3D. As far as I know I have to substitute the gluOrtho2D call with either gluPerspective or glFrustum. Unfortunately whatever I try is useless.
This is my initialization code:
// set the background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
/// set the foreground (pen) color
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);*/
// set the foreground (pen) color
glColor4f(1.0f, 1.0f, 1.0f, 0.02f);
// enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// enable point smoothing
glEnable(GL_POINT_SMOOTH);
glPointSize(1.0f);
// set up the viewport
glViewport(0, 0, 400, 400);
// set up the projection matrix (the camera)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(-2.0f, 2.0f, -2.0f, 2.0f);
gluPerspective(45.0f, 1.0f, 0.1f, 100.0f); //Sets the frustum to perspective mode
// set up the modelview matrix (the objects)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while to draw I do this:
glClear(GL_COLOR_BUFFER_BIT);
// draw some points
glBegin(GL_POINTS);
// go through the equations many times, drawing a point for each iteration
for (int i = 0; i < iterations; i++) {
// compute a new point using the strange attractor equations
float xnew=z*sin(a*x)+cos(b*y);
float ynew=x*sin(c*y)+cos(d*z);
float znew=y*sin(e*z)+cos(f*x);
// save the new point
x = xnew;
y = ynew;
z = znew;
// draw the new point
glVertex3f(x, y, z);
}
glEnd();
// swap the buffers
glutSwapBuffers();
the problem is that I don't visualize anything in my window. It's all black. What am I doing wrong?
The name "gluOrtho2D" is a bit misleading. In fact gluOrtho2D is probably the most useless function ever. The definition of gluOrtho2D is
void gluOrtho2D(
GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top )
{
glOrtho(left, right, bottom, top, -1, 1);
}
i.e. the only thing it does it calling glOrtho with default values for near and far. Wow, how complicated and ingenious </sarcasm>.
Anyway, even if it's called ...2D, there's nothing 2-dimensional about it. The projection volume still has a depth range of [-1 ; 1] which is perfectly 3-dimensional.
Most likely the points generated lie outside the projection volume, which has a Z value range of [0.1 ; 100] in your case, but your points are confined to the range [-1 ; 1] in either axis (and IIRC the Z range of the strange attractor is entirely positive). So you have to apply some translation to see something. I suggest you choose
near = 1
far = 10
and apply a translation of Z: -5.5 to move things into the center of the viewing volume.

How to use glOrtho2D with moving positions?

I'm trying to move the camera based on a player (simple square)'s position (x, y). The scale is relatively small and the character is 0.5f by 0.5f.
How can I focus the camera on the player's x, and y coordinates using glOrtho2D?
I am really confused by how you use left, right, down, and up. It makes absolutely no sense, it apparently defines the screen ratio as well as the position in which it draws?
Any help is EAGERLY appreciated.
I switched from the 3d version (gluLookAt) which was the following:
gluLookAt(jake.px, 0.0f, jake.pz + 20, jake.px, 7.0f, jake.pz, 0.0f, 1.0f, 0.0f );
and on Resize
gluPerspective(45.0f, ratio, 0.1f, 100.0f);

How do I specify texture coordinates for a GL_QUAD_STRIP?

I am trying to understand how to specify texture coordinates for a GL_QUAD_STRIP.
I have managed to get things working for one quad:
float vertices[] = { 0.0f, 0.0f, 1.0f, +1.0f, 0.0f, 0.0f, // bottom line
0.0f, 1.0f, 1.0f, +1.0f, 1.0f, 0.0f}; // top line
unsigned int indices[] = {2, 0, // x = 0
3, 1}; // x = +1
float textureCoordinates[] = { 1.0f, 0.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f};
...
glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinds any buffer object previously bound
glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibufferid);
glDrawElements(GL_QUAD_STRIP, 4, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
And here is how the result looks (white rectangle with image, rest is drawn on to help explain):
However I do not understand the logic behind the choice of textureCoordinates[] :-(.
The first texture coordinate is (1,0); I would assume that this corresponds to lower right corner?
Also I would assume that when OpenGL reads the first index: 2, it uses this to look up the vertex: (0,1,1): upper left corner. Next it reads the first texture coordinate: (1,0).
But as mentioned above I would assume this to be the lower right corner of the texture !?
However the texture is shown unrotated so this can not be the case!?
Just like the vertices, the texture coordinates are also selected based on the indices used by glDrawElements(). So the first texture coordinate is not (1,0), but (1,1) because the first index is 2. Vertices and coordinates would be according to the following table, where i = index, v = vertex and t = texture coordinate. (I'll only take the x and y coordinates into consideration for the vertices, as the z coordinate doesn't really matter in this case.)
i v t
2 (0,1) (1,1)
0 (0,0) (1,0)
3 (1,1) (0,1)
1 (1,0) (0,0)
If we draw this on a piece of paper, we can see that this means the coordinates make more sense, since the indices matter. (I recommend that you do this! I had to do that to understand what was going on.) Notice in the table how the y coordinates match perfectly between the vertex and texture coordinate for a given index. But the x coordinates don't match: when the vertex has x = 0, the texture coordinate has x = 1 and vice versa. I assume this would make the image appear mirrored around the y axis instead of rotated in any way. What does the original image look like? Is it mirrored compared to what we see in the image you posted so that the building is on the left? If so, the texture coordinates would be the explanation. In that case, texture coordinate 2 and 3 should switch places.
In case you are curious, you could take a look at the OpenGL 2.1 specification on page 18, Figure 2.5(a), to see why the vertex indices were selected as they were. It would create a quad with vertices specified in a counterclockwise direction when projected on the screen. This is good because the initial value for glFrontFace() is GL_CCW, which means we see the front face of the polygons in the rendered image and the polygons would not have been culled if culling was enabled (see glCullFace()). (Culling is not enabled by default though, so it may or may not have mattered in your case.)
I hope this helped. Do comment if something is unclear!

Push/Pop matrix and individual object rotation around its own axis in OpenGL

This has been asked many, many times before and I've read loads of posts and forums on the internet about it, but I just can't get one object to rotate around it's own axis.
I have several objects drawn like this:
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
.....
gl.glPushMatrix();
gl.glRotatef(angle, 0.0f, 1.0f, 0.0f);
gl.glTranslatef(0.0f, 0.0f, 0.0f);
texture.bind(gl);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, textureRLt);
gl.glNormalPointer(GL2.GL_FLOAT, 0, normalRLt);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexRLt);
gl.glDrawElements(GL2.GL_TRIANGLES, countI, GL2.GL_UNSIGNED_SHORT, indexRLt);
gl.glPopMatrix();
And this is drawn correctly, with all textures and normals applied..
I know that OpenGL executes commands in reverse, so that's why glRotatef is first. Also I know that all rotations are around the origin, so I need to translate the object to that origin (not that I think I have to, because "the pen" is already at the origin because I save the matrix before drawing every object and pop it afterwards). Is it something with glDrawElements? Something doesn't seem right.
Any help will be greatly appreciated. :)
Edit: I can see how the objects rotate around the main x axis, but I want them to rotate around their local x-axis.
"OpenGL executes commands reversely", means it multiplies the transformation matrix from right rather than left. What does this mean?
Imagine transformation A and B:
y = Ax
transforms x by A and yields y
This is equivalent to:
// sudo code:
glA()
glDraw(x)
Now, usually, in programming you think you get the transformations in order that you write them. So, you think that
glA()
glB()
glDraw(x)
would give you
y = BAx
but that is wrong. You actually get:
y = ABx
This means that, first B is applied to x and then A to the result.
Put in english, take a look at this example:
glScalef(...) // third, scale the whole thing
glTranslatef(...) // second, translate it somewhere
glRotatef(...) // first, rotate the object (or course,
// around its own axes, because the object is at origin)
glDrawElements(...) // draw everything at origin
So, what you need to do is to write:
// When everything is drawn, move them to destination:
gl.glTranslatef(destination[0], destination[1], destination[2]);
// For every object, rotate it the way it should, then restore transformation matrix
// object: RLt
gl.glPushMatrix();
gl.glRotatef(angle, 0.0f, 1.0f, 0.0f);
texture.bind(gl);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, textureRLt);
gl.glNormalPointer(GL2.GL_FLOAT, 0, normalRLt);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexRLt);
gl.glDrawElements(GL2.GL_TRIANGLES, countI, GL2.GL_UNSIGNED_SHORT, indexRLt);
gl.glPopMatrix();
// object: RLt2
gl.glPushMatrix();
gl.glRotatef(angle2, 0.0f, 1.0f, 0.0f);
texture.bind(gl);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, textureRLt2);
gl.glNormalPointer(GL2.GL_FLOAT, 0, normalRLt2);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexRLt2);
gl.glDrawElements(GL2.GL_TRIANGLES, countI2, GL2.GL_UNSIGNED_SHORT, indexRLt2);
gl.glPopMatrix();
I am not sure, whether you are updating 'angle' variable periodically. If you are not, then there won't be any rotation. The pseudo code is below. You can use the gluttimerfunc for periodic update of the variable.
for (every opengl loop)
angle+=5.0f
Satish

3D object in front of 2D Sprite (background) , how?

I'm new to Direct3D and I was on a project taking pictures from a webcam and draw some 3D objects in front of it.
I was able to render webcam images as background using Orthogonal Projection.
//init matrix
D3DXMatrixOrthoLH(&Ortho, frameWidth, frameHeight, 0.0f, 100.0f);
//some code
D3DXVECTOR3 position = D3DXVECTOR3(0.0f, 0.0f, 100.0f);
g_pSprite->Begin(D3DXSPRITE_OBJECTSPACE);
g_pSprite->Draw(g_pTexture,NULL,&center,&position,0xFFFFFFFF);
g_pSprite->End();
Then I tried to insert a simple triangle in front of it. The Matrices are setup as follow
D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation( &matWorld, 0.0f,0.0f,5.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
5.0 should be < 100.0 and the triangle is supposed to be appear in front of the images. However it does not appear unless set the z position to 0. At position 0, i can see the triangle but background is blank.
Do you guys have any suggestions?
I would not draw the webcam image in the object space (D3DXSPRITE_OBJECTSPACE) if you intend to use your image solely for background purpose; something like
D3DXVECTOR3 backPos (0.f, 0.f, 0.f);
pBackgroundSprite->Begin(D3DXSPRITE_ALPHABLEND);
pBackgroundSprite->Draw (pBackgroundTexture,
0,
0,
&backPos,
0xFFFFFFFF);
pBackgroundSprite->End();
should hopefully do what you're looking for.
As a quick fix you could disable depth testing as follows;
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
This way the z-index of the primitives being drawn should reflect the order in which they are drawn.
Also, try using the PIX debugging tool (this is bundled with the DirectX SDK). This is always my first port of call for drawing discrepancies as it allows you to debug each Draw call separately with access to the depth buffer and transformed vertices.