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.
Related
I'm using OpenGL to draw an object in my scene, but I can't seem to get it to rotate around the camera rather than just rotating in place.
What can I do to have it rotate around the camera instead? I know that I can draw it in camera/view space, but I need to get the object's position and rotation from after the transformation (so that I can give that object a rigid body with Bullet). If the position and rotation can still be retrieved from an object rendered in the view space, then please disregard the first part of the post.
In other words, how can I move and rotate the object around the camera and get it's coordinates and rotation from the ModelMatrix so that I can fill these:
glm::vec3 objectPosition;
glm::quat objectRotation;
I've seen similar posts online that use older OpenGL/not the MVP framework, so I'm not sure how to do this. Cheers.
Have you tried using MVP2 without the projection matrix? The projection matrix splats it on to the screen. So, taking that out might help.
Hi im trying to create a shader for my 3D models with materials and fog. Everything works fine but the light direction. I'm not sure what to set it to so I used a fixed value, but when I rotate my 3D model (which is a simple textured sphere) the light rotates with it. I want to change my code so that my light stays in one place according to the camera and not the object itself. I have tried multiplying the view matrix by the input normals but the same result occurs.
Also, should I be setting the light direction according to the camera instead?
EDIT: removed pastebin link since that is against the rules...
Use camera depended values just for transforming vertex pos to view and projected position (needed in shaders for clipping and rasterizer stage). The video cards needs to know, where to draw your pixel.
For lighting you normally pass additional to the camera transformed value the world position of the vertex and the normal in world position to the needed shader stages (i.e pixel shader stage for phong lighting).
So you can set your light position, or better light direction in world space coordinate system as global variable to your shaders. With that the lighting is independent of the camera view position.
If you want to have a effect like using a flashlight. You can set the lightposition to camera position, and light direction to your look direction. So the bright parts are always in the center of your viewing frustum.
good luck
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.
I am drawing 3d shapes in OpenGL. I am trying to add specular lighting to an object, but it doesn't seem to work. I have done ray tracing, so I know that specular lighting depends on the camera's position. In OpenGL, however, I have a static light spot.
Example:
The bright spot on the left of the arrow shaped object doesn't move, no matter where I view the object from. My light source is in line with the sphere and the arrow. It is a directional light. The shiny spot, however, doesn't move with the camera, it's completely static. It also doesn't appear on the other side of the arrow, where the light hits it from the same angle. Is this how it's supposed to work? How can I get it to move?
I have trouble in opengl. I want to rotate my vehicle while moving forward/backward. Here's a picture which shows exactly my problem. Effects of current code are in blue - after moving the car rotates over the starting location and not the current one. I want to have situation in red - in which my vehicle will rotate over current position and later move forward/backward correctly.
My current code:
lxr=sin(angle);
lzr=cos(angle);
xr+=speed*lxr;
zr+=speed*lzr;
totalangle+=angle
glRotatef(totalangle,0.0,1.0,0.0);
glTranslatef(0.0,0.0,xr);
drawVehicle();
You can try to call translate before rotate. glRotatef rotate view matrix and it affects on current view and also matrix glTranslatef.
From the image, I thought you are translating and then rotating, but looking at the code, I see it is not true.
So, it is obvious that you are in the drawVehicle(); function not rendering your object in the center (0,0). You need to render it in the center, rotate and then translate.
Also, your translation is bogus. You are just translating in z direction, not in y :
glTranslatef(0.0,0.0,xr);
You need to do something like this :
glRotatef(totalangle,0.0,1.0,0.0);
glTranslatef(0.0,yOffset,0.0);
drawVehicle(); // render around [0,0]
you have to move the origin of the coordinate system too, in order to rotate your car as you wish.