# OpenGL Lighting and rotations

## Recommended Posts

Mmmkay, this is probably a total schoolboy error on my part. I'm looking for ideas about what I might have done wrong. I've got what basically amounts to a spinning cuboid, drawn in immediate mode, in front of a fixed camera. I'm using glRotatef to spin the object, and I'm using common or garden OpenGL lighting (one light source which for the purposes of my problem is in a fixed spot somewhere off to one side of the camera). I calculate the normals for the corners of the front face of the cube by various cunning means and I'm satisfied it works fine (at least until the front face spins out of view, after which I've not been able to check because the camera is in a fixed spot). The back face is parallel to the front face, so I just take each of the 4 normal vectors and negate them, applying them to the corresponding points on the back face. Hopefully that makes sense, if not I'll try again with some ASCII art. My problem is that when the cube rotates towards 180 degrees, the lighting on the back face seems the wrong way round, as if the light source is coming from (say) the top left as opposed to the bottom right. Does anyone know what could cause this? I've seen lights which appear to move with the camera before, and lights which appear to rotate with the model, but this doesn't seem to be either of those. Here's hoping I explained that well enough... :o)

##### Share on other sites
*youre only calculating the normals once in the model creation phase + not each frame
*your repositioning the lights every frame after u position the camera

try just one side of the vube
glNormal3f(0,0,1)
g;Vertex3f(-1,-1,1)
g;Vertex3f( 1,-1,1)
g;Vertex3f( 1, 1,1)
g;Vertex3f(-1, 1,1)

does it light cottrvtly when u spin it aroun (might need to turn on twosided lighting)

##### Share on other sites
I think I've got it fixed. Negating the normal vectors for the front face didn't quite work (my cube isn't exactly a cube: the normals for each vertex of the front face aren't parallel). I had to rotate each front face normal 180 degrees through the axis of rotation to get the back vectors, and do some weird compensating stuff depending on which axis I'm rotating around (the cube's local x or local y)

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627671
• Total Posts
2978552
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 11
• 10
• 10
• 12
• 22