Reputation: 51
I've already searched for the solution, but none worked. I have already inited the Lighting and all but the objects stay unlit.
When i download examples they work but when i try to enable lighting on mine it doesnt work.
Here is the code:
#include <GLFW/glfw3.h>
#include <glm\gtx\transform.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <GL\glut.h>
#include "Camera.h"
#define windowWidth 1024
#define windowHeight 620
#define CAMERA_SPEED 0.1
GLFWwindow* Window;
CCamera Camera;
bool aPressed = false;
bool dPressed = false;
bool wPressed = false;
bool sPressed = false;
bool ePressed = false;
bool qPressed = false;
bool spacePressed = false;
static void keyEvent(GLFWwindow* window_, int key, int scancode, int action, int mods)
{
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(Window, GL_TRUE);
if(key == GLFW_KEY_A && action == GLFW_PRESS)
aPressed=true;
if(key == GLFW_KEY_D && action == GLFW_PRESS)
dPressed=true;
if(key == GLFW_KEY_W && action == GLFW_PRESS)
wPressed=true;
if(key == GLFW_KEY_S && action == GLFW_PRESS)
sPressed=true;
if(key == GLFW_KEY_E && action == GLFW_PRESS)
ePressed=true;
if(key == GLFW_KEY_Q && action == GLFW_PRESS)
qPressed=true;
if(key == GLFW_KEY_SPACE && action == GLFW_PRESS)
spacePressed=true;
if(key == GLFW_KEY_A && action == GLFW_RELEASE)
aPressed=false;
if(key == GLFW_KEY_D && action == GLFW_RELEASE)
dPressed=false;
if(key == GLFW_KEY_W && action == GLFW_RELEASE)
wPressed=false;
if(key == GLFW_KEY_S && action == GLFW_RELEASE)
sPressed=false;
if(key == GLFW_KEY_E && action == GLFW_RELEASE)
ePressed=false;
if(key == GLFW_KEY_Q && action == GLFW_RELEASE)
qPressed=false;
if(key == GLFW_KEY_SPACE && action == GLFW_RELEASE)
spacePressed=false;
}
int main()
{
if(!glfwInit())
{
exit(EXIT_FAILURE);
}
Window = glfwCreateWindow(windowWidth, windowHeight, "3D 2.0", NULL, NULL);
if(!Window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glClearColor (0.0, 0.0, 0.0, 0.0);
GLfloat light_ambient[] =
{0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[] =
{1.0, 0.0, 0.0, 1.0};
GLfloat light_specular[] =
{1.0, 1.0, 1.0, 1.0};
/* light_position is NOT default value */
GLfloat light_position[] =
{1.0, 1.0, 1.0, 0.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glfwMakeContextCurrent(Window);
glfwSwapInterval(1);
glfwSetKeyCallback(Window, keyEvent);
while(!glfwWindowShouldClose(Window))
{
glViewport(0, 0, (double)windowWidth, (double)windowHeight);
//glClearColor(0.0,0.5,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective( 90, (double)windowWidth / (double)windowHeight, 0.1, 100 );
gluLookAt(0,0,5,0,0,-0.1,0.0,1.0,0.0);
glMatrixMode(GL_MODELVIEW_MATRIX);
if(aPressed)
Camera.StrafeRight(-CAMERA_SPEED);
if(dPressed)
Camera.StrafeRight(CAMERA_SPEED);
if(wPressed)
Camera.MoveForward(-CAMERA_SPEED);
if(sPressed)
Camera.MoveForward(CAMERA_SPEED);
if(ePressed)
Camera.MoveUpward(CAMERA_SPEED);
if(qPressed)
Camera.MoveUpward(-CAMERA_SPEED);
/* Draw Here */
Camera.Render();
/* Draw Cube */
glPushMatrix();
glTranslated(0.0,0.0,0.0);
glColor3f(0.0,0.0,0.0);
glLineWidth(4);
//glutWireCube(1);
glColor3f(0.0,0.3,1.0);
glNormal3f(0.0,0.0,1.0);
glutSolidCube(1);
glPopMatrix();
/* Draw Teapot */
glPushMatrix();
glTranslated(3.0,0.0,0.0);
glColor3f(0.0,0.0,0.0);
glLineWidth(4);
glutWireTeapot(1);
glColor3f(0.6,0.3,0.0);
glutSolidTeapot(1);
glPopMatrix();
/* Draw Torus */
glPushMatrix();
glTranslated(-3.0,0.0,0.0);
glColor3f(0.0,0.0,0.0);
glLineWidth(4);
glutWireTorus(0.25, 1, 16, 16);
glColor3f(0.6,0.3,0.3);
glutSolidTorus(0.25, 1, 16, 16);
glPopMatrix();
glfwSwapBuffers(Window);
glfwPollEvents();
}
glfwDestroyWindow(Window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
Upvotes: 0
Views: 303
Reputation: 54562
gluLookAt()
is used to build a view matrix. It therefore must be specified while you're in GL_MODELVIEW
matrix mode. The sequence of calls for the initial transformation setup should be:
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective(90.0, (double)windowWidth / (double)windowHeight, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW_MATRIX);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, -0.1, 0.0, 1.0, 0.0);
You can actually move this call sequence outside the event loop. There's no need to repeat it for each frame. Well, the gluPerspective()
call depends on the window size, so you should repeat it when the window size changes. But it looks like you have the window size hardwired in your code.
You may also want to look into setting material properties with glMaterialfv()
. The glColor3f()
calls you're using for setting the color won't have the desired effect once you enable lighting.
And of course, as already pointed out in another answer, you need to call glfwMakeContextCurrent()
before making any OpenGL calls.
I hope you're aware that most of the OpenGL features you're using are deprecated, and not available anymore in more recent versions of OpenGL (Core Profile, as well as OpenGL ES 2.0 and later).
Upvotes: 1
Reputation: 1632
You are using glut
functions without first calling glutInit
.
main(int argc, char *argv[])
{
glutInit(&argc, argv);
...
And also glfwMakeContextCurrent(Window);
have to be called before any OpenGL functions.
For correct lighting, consider adjusting also your light direction (-1,-1,-1,0) would be better with your actual view matrix.
Upvotes: 1