How avoid light rotating in OpenGL? - c++

In OpenGL I created a simple cube that I rotate. I am rotating the cube, not the camera!
Then I added a light source. The light also rotates with the cube. How can I avoid this?
Can somebody tell me when to activate which matrix mode and when to push and pop the current matrix. I have tried many, many combinations and sequences, but the damn light always (!) follows the rotation.
I am not expecting a general answer that just says that has something to do with the matrix stack. A concrete short example of a rotating object with a non-rotating light would be great!

A probable reason why the light follows a rotating object is that there was defined no (suitable) normal vector for the object. Although the light and the movements of the objects are programmed correctly the wrong or no normal vectors can make the whole scene look like the light follows a rotating object.
Defining normal vectors for the objects, that determine how the light is reflected, can solve the problem.

Related

matrix non uniform scale rotation issue

I just ran into this while doing some 2d graphics work with opengl.
if I do a non uniform scale then rotation things gets really wonky.
This is because the upper 3x3 matrix holds both rotation and scale.
what are some ways to deal with this non uniform scaling and rotations? I could provide a video but I am sure most people here have seen this problem although I could provide a video.
It depends upon what type of output you want, you have to see when you are rotating the object , around what axis you want to rotate the object.
If you want to rotate the object around its own axis, better translate back to origin and then do your transformations and then again translate back to original position. if you can post video, it will be better to visualize what you exactly want. Also order of transformation can make things reaaly wonky :)

Keeping polygon perpendicular to camera

Currently I'm using WebGL for a school project, and am stuck on trying to keep an object perpendicular to the camera while allowing rotation of the rest of the scene.
I currently have a cube and other object on the canvas, and they're being successfully rotated using a quaternion and converting that to a rotation matrix. I would now like to add a simple square within the scene, but have it consistently perpendicular to the camera.
In thinking about this, I've considered an approach using multiple vertex shader programs for the different objects in the scene: 1 for handling the positioning of all objects in the scene that can be rotated, and another vertex shader corresponding to the square that I don't want rotated.
Though, I really don't know if this approach will work as expected, as I am still a novice at WebGL.
Would there be a better approach to this? I greatly apologize if there's a lack of info given, and can give any more info if needed. I've searched quite a bit for something involving this, but have hit a dead end.
Thank you!
If you want a more step by step tutorial: http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/billboards/.

handling lighting for objects within objects

Basically I'm writing a C++ program to draw objects in a world and I'm having some difficulty with lighting when I'm rotating/translating an object that consists of multiple objects (For example a tree object consists of a cylinder object for the trunk and pyramid objects for the leaves).
I have a working light source right now but I run into some issues when rotating a subcomponent of an object (like the pyramid object inside its parent, the tree).
All lighting/shading works when I apply a rotation to the tree object, but lighting gets wonky and random if I attempt to rotate the pyramid object inside the tree. I'm really hoping someone may have some tips or hints as to what I could be running into?
Edit: There are a lot of files with this project and it would be difficult to throw enough up here to give the entire picture. Essentially my steps are:
Set up a world with a functioning opengl light source.
Create a shape object which has a transformation matrix associated with it.
Using the shape and its matrix and calculate its normals to use for shading.
Now create a new shape object which consists of the other shape objects, and throw it in the world for the lighting to take effect.
Now the problem happens here. If I rotate the parent object, lighting is fine, but if I rotate the child object, lighting becomes random.
I found the problem in my code. I was updating the normals too frequently so opengl lighting had bad normals to work with.
Thanks for the help everyone!

OpenGL 2d drawing facing the user

I have an OpenGL scene in which the user can rotate the camera. I have some two dimensional shapes that I would like to always face the user. I do have the forward facing vector, and I do have the screen point at which the component should be drawn. I'm not sure the best way to approach this problem - should I be rotating the shape to the forward vector (which I'm not entirely sure how to do correctly)? Or is there another way I can just draw in two dimensions and ignore the rotation of the camera (maybe by using an orthographic projection)? Any sample code for helping with this would be appreciated.
PS - I'm doing this in Java, but the language is irrelevant here (it is just OpenGL specific).
I already answered it in Inverting rotation in 3D, to make an object always face the camera?
My first though is to use the "gluLookAt" matrix.
http://www.opengl.org/resources/faq/technical/viewing.htm
I would say, that you keep the position of the 2d objects, and then take the "eye" or camera position and set that as the target value for the 2d objects. It should keep them facing the camera.

Rotating light source in openGL

I've implemented an arcball interface for a 3d objects so i can rotate the object with the mouse. I have a fixed light source. I want the light source to move (rotate) with the object as one unit - meaning that if the light was above the object, after i rotate the object the light rotates with it and remains above it. I tried to get the MODEL_VIEW_MATRIX (glGetDoublev();) and multiply the light original coordinates by the model view matrix, but it doesn't work well. Any other way to do it? Thanks.
Actualy the MODEL_VIEW_MATRIX effects also the light position, so i only have to define it in the right reference frame.