Hey Guys!
I still have the lighting Problem… I have watched some tutorials during the week, but none of them helped me to solve it.
Do you have an idea or some direction I should follow to solve the problem?
For some models, the lighting is moving when I rotate it. And for others is completly wrong. This is not what I want to… I want light just to make it look more real. I tried already to do something to “compensate” when it moves, but I made it wrong.
#define GLFW_INCLUDE_GLU
#include <glew.h>
#include <glfw3.h>
#include <cstdio>
int rotacionarx = 0;
int rotacionary = 0;
int rotacionarz = 0;
float lightAmbient[] = { 0.2f, 0.3f, 0.6f, 1.0f };
float lightDiffuse[] = { 0.2f, 0.3f, 0.6f, 1.0f };
float lightPosition[] = { -2, 2, -3, 1 };//100, 0, -100, 1 };//-2, 2, -3, 1 };// 0, 0, 3, 1};//10, 10, -100, 1};
float u = 0;
float v = 0;
float w = 0;
float xs = 0;
float ys = 0;
float zs = 0;
float qs = 0;
float sk = 0;
float minx = 100000;
float miny = 100000;
float minz = 100000;
float maxx = 0;
float maxy = 0;
float maxz = 0;
void calculamaxmin(){
for (int i = 0; i < n_triangles; i++){
if (tripontos[i].pointss[0].x > maxx) maxx = tripontos[i].pointss[0].x;
if (tripontos[i].pointss[1].x > maxx) maxx = tripontos[i].pointss[1].x;
if (tripontos[i].pointss[2].x > maxx) maxx = tripontos[i].pointss[2].x;
if (tripontos[i].pointss[0].y > maxy) maxy = tripontos[i].pointss[0].y;
if (tripontos[i].pointss[1].y > maxy) maxy = tripontos[i].pointss[1].y;
if (tripontos[i].pointss[2].y > maxy) maxy = tripontos[i].pointss[2].y;
if (tripontos[i].pointss[0].z > maxz) maxz = tripontos[i].pointss[0].z;
if (tripontos[i].pointss[1].z > maxz) maxz = tripontos[i].pointss[1].z;
if (tripontos[i].pointss[2].z > maxz) maxz = tripontos[i].pointss[2].z;
if (tripontos[i].pointss[0].x < minx) minx = tripontos[i].pointss[0].x;
if (tripontos[i].pointss[1].x < minx) minx = tripontos[i].pointss[1].x;
if (tripontos[i].pointss[2].x < minx) minx = tripontos[i].pointss[2].x;
if (tripontos[i].pointss[0].y < miny) miny = tripontos[i].pointss[0].y;
if (tripontos[i].pointss[1].y < miny) miny = tripontos[i].pointss[1].y;
if (tripontos[i].pointss[2].y < miny) miny = tripontos[i].pointss[2].y;
if (tripontos[i].pointss[0].z < minz) minz = tripontos[i].pointss[0].z;
if (tripontos[i].pointss[1].z < minz) minz = tripontos[i].pointss[1].z;
if (tripontos[i].pointss[2].z < minz) minz = tripontos[i].pointss[2].z;
}
}
void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS)
if (key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, GL_TRUE);
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS){
rotacionary = rotacionary - 5;
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS){
rotacionary = rotacionary + 5;
}
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS){
rotacionarx = rotacionarx - 5;
}
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS){
rotacionarx = rotacionarx + 5;
}
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS){
rotacionarz = rotacionarz - 5;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS){
rotacionarz = rotacionarz + 5;
}
}
GLFWwindow* initWindow(const int resX, const int resY)
{
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW
");
return NULL;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
// Open a window and create its OpenGL context
GLFWwindow* window = glfwCreateWindow(resX, resY, "TEST", NULL, NULL);
if (window == NULL)
{
fprintf(stderr, "Failed to open GLFW window.
");
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, controls);
// Get info of GPU and supported OpenGL version
printf("Renderer: %s
", glGetString(GL_RENDERER));
printf("OpenGL version supported %s
", glGetString(GL_VERSION));
glEnable(GL_DEPTH_TEST); // Depth Testing
glDepthFunc(GL_LEQUAL);
glDepthFunc(GL_LESS);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDisable(GL_CULL_FACE);
glCullFace(GL_BACK);
return window;
}
void drawCube()
{
GLfloat vertices[] =
{
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat colors[] =
{
0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1
};
static float alpha = 0;
//attempt to rotate cube
//glTranslatef(-1, 1, 0);
glRotatef(rotacionarx, 0, 1, 0);
glRotatef(rotacionary, 1, 0, 0);
//glTranslatef(1, -1, 0);
///* We have a color array and a vertex array */
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
/* Send data : 24 vertices */
glDrawArrays(GL_QUADS, 0, 24);
///* Cleanup states */
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void drawSTL(){
glPushMatrix();
glBegin(GL_TRIANGLES);
for (int i = 0; i < n_triangles; i++){
glNormal3f(trinormals[i].x, trinormals[i].y, trinormals[i].z);
glVertex3f(tripontos[i].pointss[0].x, tripontos[i].pointss[0].y, tripontos[i].pointss[0].z);
glVertex3f(tripontos[i].pointss[1].x, tripontos[i].pointss[1].y, tripontos[i].pointss[1].z);
glVertex3f(tripontos[i].pointss[2].x, tripontos[i].pointss[2].y, tripontos[i].pointss[2].z);
}
glEnd();
glPopMatrix();
}
void display(GLFWwindow* window)
{
static int first = 1;
while (!glfwWindowShouldClose(window))
{
// Scale to window size
GLint windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
// Draw stuff
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective(60, (double)windowWidth / (double)windowHeight, 1, 10000);
glMatrixMode(GL_MODELVIEW_MATRIX);
glTranslatef(0, 0, -3);
/* lightPosition[0] = lightPosition[0] - rotacionarx;
lightPosition[1] = lightPosition[1] - rotacionary;
*///lightPosition[2] = lightPosition[2] - rotacionarz;
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); //Setup The Ambient Light
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); //Setup The Diffuse Light
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); //Position The Light
glRotatef(rotacionarx, 0.0f, 1.0f, 0.0f);
glRotatef(rotacionary, 1.0f, 0.0f, 0.0f);
glRotatef(rotacionarz, 0.0f, 0.0f, 1.0f);
if (first){
first = 0;
calculamaxmin();
// to scale model
xs = (maxx - minx) / 2.0;
ys = (maxy - miny) / 2.0;
zs = (maxz - minz) / 2.0;
if (xs > ys&&xs > zs) qs = xs;
else if (ys > zs) qs = ys;
else qs = zs;
sk = 1.0 / qs;
// to fit inside cube
u = (minx + maxx) / 2.0;
v = (miny + maxy) / 2.0;
w = (minz + maxz) / 2.0;
}
glScalef(sk, sk, sk);
glTranslatef(-u, -v, -w);
// drawCube();
drawSTL();
// Update Screen
glfwSwapBuffers(window);
// Check for any input, or window movement
glfwPollEvents();
}
}
int meumain()
{
GLFWwindow* window = initWindow(1024, 620);
if (NULL != window)
{
display(window);
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
PS: I tried to upload some prints but it didnt work out… That is why I created this link Dropbox - File Deleted
The spider I find already very good, but the others are wrong