iPhone OpenGL : Moving the view back from an object - iphone

A bit of a weird one here. I have a 3d cube that rotates on the screen but I had to change the view from glOrthof to glFrustumf. Now the cube is place on me and not a in front of me.
So I thought I use a glTranslate (or even scale) to move it back so I can properly so it but the object continues redraws from its old position and adds a bit to it so by moving, the rotation goes Pete Tong. I never used used glGetFloatv and glMultMatrixf before but I guessing they take the old rotation and add a bit to it to keep the object moving. Just wish I could step back from it
Cube code
GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glLoadIdentity();
glRotatef(self.angle, self.dy,self.dx,0);
/* reapply other rotations so far */
glMultMatrixf(matrix);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, texturedVertices);
glTexCoordPointer(2, GL_FLOAT, 0, texturedCubeCoord);
glBindTexture(GL_TEXTURE_2D, 1);
glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[0]);
glBindTexture(GL_TEXTURE_2D, 2);
glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[6]);
glBindTexture(GL_TEXTURE_2D, 1);
glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[12]);
glBindTexture(GL_TEXTURE_2D, 2);
glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[18]);
glBindTexture(GL_TEXTURE_2D, 1);
glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[24]);
glBindTexture(GL_TEXTURE_2D, 2);
glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[30]);

I fixed it by a 1 time only move of -5 then reset the matrix
/* save current rotation state */
GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
/* re-center cube, apply new rotation */
glLoadIdentity();
if (firstRunOnly == NO) {
firstRunOnly = YES;
glTranslatef(0, 0, -5);
} else {
glTranslatef(0, 0, 0);
}
/* reapply other rotations so far */
glMultMatrixf(matrix);
glRotatef(self.angle, self.dy,self.dx,0);

Related

Changing Alpha Value of OpenGL ES Object - GL_BLEND - iPhone

I have a textured object in my OpenGL ES scene (version 1.1) for which I want to change the alpha to 0.5.
I am trying the following code :
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0, 1.0, 1.0, 0.5);
glVertexPointer(3, GL_FLOAT, 0, vertexes);
glNormalPointer(GL_FLOAT, 0, normals);
glTexCoordPointer(2, GL_FLOAT, 0, textures);
GLfloat ambientAndDiffuse[] = kAmbience;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, ambientAndDiffuse);
GLfloat specular[] = kSpecular;
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, kShininess);
GLfloat emission[] = kEmission;
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission);
glDrawElements(GL_TRIANGLES, vertexCount, GL_UNSIGNED_SHORT, indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_BLEND);
The problem is that the transparency of the model is not changing. The model is displayed on the screen correctly, however the alpha is still 1.
EDIT
My textures are jpgs. Do I need to save these as PNGs ?
Can anyone spot how I can correct this ?
Thank you.
Ah think I've solved it - you need to turn off the lighting and then it seems to work.
glDisable(GL_LIGHTING);
Have you set the texture environment variable to modulate? Like this:
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
That tells OpenGL to multiply the object's color by the texture's color. Also, I don't remember if you need to use a GL_COLOR_ARRY with glDrawElements() instead of just setting the current color.

How to disable or configure clipping when rendering to texture using FBO in OpenGL?

I have a question about the way the FBO clips the visible scene.
All this happens in iPhone's OpenGL ES.
I'm rendering in 2D and using render-to-texture with FBO.
I find that when any part of the scene passes any side of the rectangle of screen size (480*320) that it appears clipped from the FBO generated texture.
Why does it do that? How do I disable or configure it?
I've tried disabling scissor test by
lDisable(GL_SCISSOR_TEST);
I've tried to set up needed section by
`glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, 100, 100);`
I've tried to adjust Viewport by
glViewport(0,0, 512,512);
it doesn't work as needed. Instead of extending the render area to needed 512*512 it looks like it still clips the area to 480*320 and stretches it afterwards to 512*512
All these didn't bring needed result.
In my code:
I create the texture
GLuint textureID;
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);`
Create FBO and attach texture to it
GLuint textureFrameBuffer;
GLint prevFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &prevFBO);
// create framebuffer
glGenFramebuffersOES(1, &textureFrameBuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);
// attach renderbuffer
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, textureID, 0);
if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
NSLog(#"ERROR - WHILE CREATING FBO");
// unbind frame buffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, prevFBO);
And then, when I draw some stuff to FBO and render the square with this texture,
static const float textCoords[] =
{
0, 1,
1, 1,
0, 0,
1, 0,
};
static const float quadVerts[] =
{
0, 320,
320, 320,
0, 0,
320, 0,
};
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);
glVertexPointer(2, GL_FLOAT, 0, quadVerts);
glTexCoordPointer(2, GL_FLOAT, 0, textCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
To my opinion I should see the square filled with all the stuff I had drawn to my FBO. Whereas, I only see a part of it – clipped rectangle of size 480x320 at lower left corner.
So, the question is steel actual
You need to change your viewport doing:
glViewport(0,0,textureWidth,textureHeight);
EDIT:
And don't forget to switch it back to its normal value after rendering onto your FBO.
/// save viewport
GLint vp[4];
glGetIntegerv(GL_VIEWPORT, vp);
/// restore viewport
glViewport(vp[0], vp[1], vp[2], vp[3]);

Separately rotating 2 texture coordinates sets for the same object

I've given this a go for a while, but I haven't been able to get favorable results. Basically, I have a sphere with 2 textures. One is blended on top with other and the idea is to have it be a face environment mapping. I want one texture to rotate with the sphere and the other to stay fixed to the initial texture coordinates.
I have tried rotating the GL_TEXTURE matrix of one texture and resetting the other, but this doesn't really do the trick because the texture mapping isn't right when it's rotated certain ways.
Anyone know how i can rotate only one set of texture coordinates and keep the other fixed?
Here's my render code at the moment:
glColor4f(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glClientActiveTexture( GL_TEXTURE0);
glActiveTexture( GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, tex[0]);
glEnableClientState( GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(TexturedVertexData3D), &SphereVertexData[0].texCoord);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glTranslatef(0, 0, -4.5f);
glRotatef(.00001, .00001, .0, .0);
glClientActiveTexture( GL_TEXTURE1);
glActiveTexture( GL_TEXTURE1);
glEnable( GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, tex[1]);
glEnableClientState( GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(TexturedVertexData3D), &SphereVertexData[0].texCoord);
glPopMatrix();
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0, 0, -4.5f);
glVertexPointer(3, GL_FLOAT, sizeof(TexturedVertexData3D), &SphereVertexData[0].vertex);
glNormalPointer(GL_FLOAT, sizeof(TexturedVertexData3D), &SphereVertexData[0].normal);
glDrawArrays(GL_TRIANGLES, 0, kSphereNumberOfVertices);
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
rotation += .0001;
If anyone needs to see more of what is going on just let me know.
You could use two spheres and only issue the glRotatef() to the sphere with the texture that you want rotating. The other sphere would just receive the corresponding glTranslatef() commands. If you are using the alpha channel to blend the textures it should work the same.

OpenGL-ES variable texture alpha (2D)?

I have a texture with transparency (the white triangle with that lighting information), and just can't make it's alpha variable.
alt text http://gotoandplay.freeblog.hu/files/alpha_help.png
The drawing code, with the missing piece:
//Place polygon vertices to the bottom left within the view.
glLoadIdentity();
glTranslatef(centerX, centerY, 0);
//Draw "diffuse" layer.
glBindTexture(GL_TEXTURE_2D, spriteTexture[0]); //Bind.
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//Offset during development only.
glLoadIdentity();
glTranslatef(centerX-10, centerY+10, 0);
//Draw "specular" layer.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, spriteTexture[1]); //Bind.
//Some smart alpha scaling code needs here...
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Could somebody please help me out with the appropriate lines of code?
Some glBlendFunc, or maybe glTextEnvi stuff I suppose.
Ok, I got it even if I don't understand what I did exactly.
//Place polygon vertices to the bottom left within the view.
glLoadIdentity();
glTranslatef(centerX, centerY, 0);
//--1
//Draw "diffuse" layer.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, spriteTexture[0]); //Bind.
//Blending.
glBlendFunc(GL_ONE, GL_ONE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//--2
//Draw "specular" layer.
glBindTexture(GL_TEXTURE_2D, spriteTexture[1]); //Bind.
//Blending.
glColor4f(opacity, opacity, opacity, opacity);
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
I tried another way before...
glColor4f(1.0f, 1.0f, 1.0f, opacity);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
...but the second map was somehow "weaker".

Animating a texture across a surface in OpenGL

I'm working with the iPhone OpenGLES implementation and I wish to endlessly scroll a texture across a simple surface (two triangles making up a rectangle). This should be straightforward, but it's not something I've done before and I must be missing something. I can rotate the texture fine, but translate does not work at all. Do I have a minor implementation issue or am I doing something fundamentally wrong?
// move texture
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
// increment offset - no reset for demo purposes
wallOffset += 1.0;
// move the texture - this does not work
glTranslatef(wallOffset,wallOffset,0.0);
// rotate the texture - this does work
//glRotatef(wallOffset, 1.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glBindTexture(GL_TEXTURE_2D, WallTexture.name);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
// simple drawing code
glNormalPointer(GL_FLOAT, 0, normals);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// push matrix back
glMatrixMode(GL_TEXTURE);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
You're incrementing your texture offset by 1.0f; but textures coordinates are considered in the range [0, 1], so you're not actually changing the texture coordinates (assuming you've enabled some sort of wrapping).
Try changing that increment (try .01f, or maybe something depending on the framerate) and see if it works. If not, then it may have something to do with the texture parameters you've got enabled.