For my project i needed to rotate a rectangle. I thought, that would be easy but i'm getting an unpredictable behavior when running it..
Here is the code:
glPushMatrix();
glRotatef(30.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(vec_vehicle_position_.x, vec_vehicle_position_.y, 0);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(width_sprite_, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(width_sprite_, height_sprite_);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0, height_sprite_);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
The problem with that, is that my rectangle is making a translation somewhere in the window while rotating. In other words, the rectangle doesn't keep the position : vec_vehicle_position_.x and vec_vehicle_position_.y.
What's the problem ?
Thanks
You need to flip the order of your transformations:
glRotatef(30.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(vec_vehicle_position_.x, vec_vehicle_position_.y, 0);
becomes
glTranslatef(vec_vehicle_position_.x, vec_vehicle_position_.y, 0);
glRotatef(30.0f, 0.0f, 0.0f, 1.0f);
To elaborate on the previous answers.
Transformations in OpenGL are performed via matrix multiplication. In your example you have:
M_r_ - the rotation transform
M_t_ - the translation transform
v - a vertex
and you had applied them in the order:
M_r_ * M_t_ * v
Using parentheses to clarify:
( M_r_ * ( M_t_ * v ) )
We see that the vertex is transformed by the closer matrix first, which is in this case the translation. It can be a bit counter intuitive because this requires you to specify the transformations in the order opposite of that which you want them applied in. But if you think of how the transforms are placed on the matrix stack it should hopefully make a bit more sense (the stack is pre-multiplied together).
Hence why in order to get your desired result you needed to specify the transforms in the opposite order.
Inertiatic provided a very good response. From a code perspective, your transformations will happen in the reverse order they appear. In other words, transforms closer to the actual drawing code will be applied first.
For example:
glRotate();
glTranslate();
glScale();
drawMyThing();
Will first scale your thing, then translate it, then rotate it. You effectively need to "read your code backwards" to figure out which transforms are being applied in which order. Also keep in mind what the state of these transforms is when you push and pop the model-view stack.
Make sure the rotation is applied before the translation.
Related
First off, I'm sorry if I confuse anyone because I don't know how to phrase this, exactly.
Anyway, What I want to do is rotate on 3 axis, but independent of eachother. If I have
glRotatef(getPitch(),1f,0,0);
glRotatef(getYaw(),0,1f,0);
glRotatef(getRoll(),0,0,1f);
Then, it rotates my object on the x axis just fine, but the other two axis rotate on the offset of the x rotation. How do I rotate these all independent of eachother? (On the same object)
Again, Sorry if I confused anyone.
You could push and pop the matrix onto and off the stack, so you could do:
glPushMatrix();
glRotatef( getPitch(), 1.0f, 0.0f ,0.0f );
glPopMatrix();
glPushMatrix();
glRotatef( getYaw(), 0.0f, 1.0f, 0.0f);
glPopMatrix();
glPushMatrix();
glRotatef( getRoll(), 0.0f, 0.0f, 1.0f);
glPopMatrix();
so basically, pushing the matrix, saves the transformation matrix in it's current state. You apply the transformations that you want on the object (in your case rotations around an axis), which updates the matrix. Popping it restores it to the original state before the rotation was applied. You can then apply each rotation independently of the other ones.
Hello guys I am beginner to opengl and trying to understand the concept gluPerspective() funtion. I went through this post gluPerspective parameters- what do they mean? post and I wrote this code
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0,0,500,500);
gluPerspective(45,16/9,1.0,3.0);
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex3f(100.0f,0.0f,2.0f);
glVertex3f(0.0f,0.0f,2.0f);
glVertex3f(0.0f,100.0f,2.0f);
glVertex3f(100.0f,100.0f,2.0f);
glEnd();
glFlush();
This is the dispay function but black screen is rendered.What is wrong in my code
Call glMatrixMode(GL_PROJECTION); before calling gluPerspective.
After that switch back with glMatrixMode(GL_MODELVIEW);.
gluPerspective() sets up a projection matrix under the assumption that your eye point is at the origin, and you're looking down the negative z-axis. The zNear and zFar parameters specify the range of distances along the negative z-axis that will be contained within the view volume.
Therefore, with zNear set to 1.0 and zFar to 3.0, z-values within a range of -1.0 to -3.0 will be within the view volume.
To make your quad visible, you will have to use coordinates within that range. Changing all your z-coordinates from 2.0 to -2.0 would work. The more typical approach is to apply a view transformation to place the geometry where it is needed for the projection transformation.
For example, in your case, you could place the quad around the origin, and then use either gluLookAt(), or a simple translation, to move the geometry down the negative z-axis.
You also need to watch out for the matrix modes. The projection matrix should normally be set in the corresponding matrix mode.
The whole thing will then look like this:
glMatrixMode(GL_PROJECTION);
gluPerspective(45,16/9,1.0,3.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0f, 0.0f, -2.0f);
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex3f(100.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 100.0f, 0.0f);
glVertex3f(100.0f, 100.0f, 0.0f);
glEnd();
Note that the range of your x and y coordinates is far beyond the default coordinate range of [-1.0, 1.0]. You will either want to use additional translation/scaling, or use much smaller values.
i want to code a little minecraft clone. Now i tried to insert some simple lighting but my results are very bad. I read much about it and i tried different solutions without any result.
Thats what i got.
Initializing:
GL11.glViewport(0, 0, Config.GAME_WIDTH, Config.GAME_HEIGHT);
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
GL11.glLoadIdentity(); // Reset The Modelview Matrix
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glFrontFace(GL11.GL_CCW);
GL11.glLightModeli(GL11.GL_LIGHT_MODEL_LOCAL_VIEWER, GL11.GL_TRUE);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT0);
FloatBuffer qaAmbientLight = floatBuffer(0.0f, 0.0f, 0.0f, 1.0f);
FloatBuffer qaDiffuseLight = floatBuffer(1.0f, 1.0f, 1.0f, 1.0f);
FloatBuffer qaSpecularLight = floatBuffer(1.0f, 1.0f, 1.0f, 1.0f);
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, qaAmbientLight);
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, qaDiffuseLight);
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, qaSpecularLight);
FloatBuffer qaLightPosition = floatBuffer(lightPosition.x, lightPosition.y, lightPosition.z, 1.0f);
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, qaLightPosition);
So now before each render i tried this:
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glClearColor(0.0f, 100.0f, 100.0f, 1.0f);
GL11.glShadeModel(GL11.GL_FLAT);
GL11.glLoadIdentity();
FloatBuffer qaLightPosition = floatBuffer(lightPosition.x, lightPosition.y, lightPosition.z, 1.0f);
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, qaLightPosition);
FloatBuffer ambientMaterial = floatBuffer(0.2f, 0.2f, 0.2f, 1.0f);
FloatBuffer diffuseMaterial = floatBuffer(0.8f, 0.8f, 0.8f, 1.0f);
FloatBuffer specularMaterial = floatBuffer(0.0f, 0.0f, 0.0f, 1.0f);
GL11.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT, ambientMaterial);
GL11.glMaterial(GL11.GL_FRONT, GL11.GL_DIFFUSE, diffuseMaterial);
GL11.glMaterial(GL11.GL_FRONT, GL11.GL_SPECULAR, specularMaterial);
GL11.glMaterialf(GL11.GL_FRONT, GL11.GL_SHININESS, 50.0f);
Of course this is not much code but this is all about lighting. Did i make a mistake? I read that OpenGL is not as good as DirectX for lighting and shadowing.
That's what it looks like:
http://img199.imageshack.us/img199/7014/testrender.png
Can someone give me tips to get it a better look?
I found one post with an awesome block landscape.
http://i.imgur.com/zIocp.jpg
That's how it should look like :)
Can someone give me tips to get it a better look?
Neither OpenGL nor DirectX have nothing to do with lighting and shadowing, if you use programmable pipeline. The normals become just another vertex attribute, which can be used for lighting computation. Fixed functionality is old and deprecated, and thus not recommended, if you aren't forced to use it.
Changing to shaders isn't really that hard, and you won't be limited by the fixed pipeline anymore; you have then complete control over how the lighting is computed, you can easily output more debug information (such as coloring surfaces based on their normals).
That's how it should look like :)
The screen you posted has also visible ambient occlusion. Achieving this effect without shaders would be extremely hard and simply not worth the effort.
I happen to be doing a similar project myself; I wouldn't mention it, if it wasn't OpenSource and publicly available. Here's the sample result:
You can find the lighting shader code here.
I'll post an excerpt to prevent links from rotting:
float CalcDirectionalLightFactor(vec3 lightDirection, vec3 normal) {
float DiffuseFactor = dot(normalize(normal), -lightDirection);
if (DiffuseFactor > 0) {
return DiffuseFactor;
}
else {
return 0.0;
}
}
vec3 DiffuseColor = Light0.Color * Light0.DiffuseIntensity * CalcDirectionalLightFactor(Light0.Direction, normal);
Bartek's answer is a good one. You will want to go down the path of writing your own shaders, understanding what OpenGl provides for shadowing and lighting and not relying on older, deprecated lighting models. It is a lot more complex the glEnable(LIGHTING_AND_SHADOWING).
But, if you just want to play with your code to see the colors change from binary black/white, one potential idea is turning off the qaSpecularLight (which creates "glossy" all-white highlights that don't help you get to a "matte" look) and adjusting the glShadeModel setting for SMOOTH shading.
That should help somewhat, but will not get you all the way to your goal. Follow Bartek's suggested path (or google for similar ideas).
I am creating a 3D game. I have objects in my game. When an enemy hits my position I want my screen to go red for a short time. I have chosen to do this by trying to render a full screen red square at my camera position. This is my attempt which is in my render method.
RenderQuadTerrain();
//Draw the skybox
CreateSkyBox(vNewPos.x, vNewPos.y, vNewPos.z,3500,3000,3500);
DrawCoins();
CollisionTest(g_Camera.Position().x, g_Camera.Position().y, g_Camera.Position().z);
DrawEnemy();
DrawEnemy1();
//Draw SecondaryObjects models
DrawSecondaryObjects();
//Apply lighting effects
LightingEffects();
escapeAttempt();
if(hitbyenemy==true){
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE); // additive blending
float blendFactor = 1.0;
glColor3f(blendFactor ,0,0); // when blendFactor = 0, the quad won't be visible. When blendFactor=1, the scene will be bathed in redness
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd();
}
All this does, however, is turn all of the objects in my game a transparent colour, and I can't see the square anywhere. I don't even know how to position the quad. I'm very new to openGL.
How my game looks without an attempt to render a quad:
How my game looks after my attempt:
With Kevin's code and glDisable(GL_DEPTH_TEST);
EDIT: I have changed the code to the below paste..still looks like image 1.
http://pastebin.com/eiVFcQqM
There are several possible contributions to the problem:
You probably want regular blending, not additive blending; additive blending will not turn white, yellow, or purple objects red. Change the blend func to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); and use a color of glColor4f(1, 0, 0, blendFactor);
You should glDisable(GL_DEPTH_TEST); while drawing the overlay, to prevent it from being hidden by other geometry, and reenable it afterward (or use glPush/PopAttrib(GL_ENABLE_BIT)).
The projection and modelview matrixes should be the identity, to ensure a quad with those coordinates covers the entire screen. (However, you may have that implicitly already, since you say it is affecting the full screen, just not in the right way.)
If these suggestions do not fix it, please edit your question showing screenshots of your game with and without the red flash so we can understand the problem better.
Say I use glRotate to translate the current view based on some arbitrary user input (i.e, if key left is pressed then rtri+=2.5f)
glRotatef(rtri,0.0f,1.0f,0.0f);
Then I draw the triangle in the rotated position:
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
How do I get the resulting translated vertexes for use in collision detection? Or will I have to manually apply the transform myself and thus doubling up the work?
The reason I ask is that I wouldn't mind implementing display lists.
The objects you use for collision detection are usually not the objects you use for display. They are usually simpler and faster.
So yes, the way to do it is to maintain the transformation you're using manually but you wouldn't be doubling up the work because the objects are different.
Your game loop should look like (with c++ syntax) :
void Scene::Draw()
{
this->setClearColor(0.0f, 0.0f, 0.0f);
for(std::vector<GameObject*>::iterator it = this->begin(); it != this->end(); ++it)
{
this->updateColliders(it);
glPushMatrix();
glRotatef(it->rotation.angle, it->rotation.x, it->rotation.y, it->rotation.z);
glTranslatef(it->position.x, it->position.y, it->position.z);
glScalef(it->scale.x, it->scale.y, it->scale.z);
it->Draw();
glPopMatrix();
}
this->runNextFrame(this->Draw, Scene::MAX_FPS);
}
So, for instance, if i use a basic box collider with a cube the draw method will :
Fill the screen with a black color (rgb : (0,0,0))
For each object
Compute the collisions with position and size informations
Save the actual ModelView matrix state
Transform the ModelView matrix (rotate, translate, scale)
Draw the cube
Restore the ModelView matrix state
Check the FPS and run the next frame at the right time
** The class Scene inherits from the vector class
I hope it will help ! :)