I'm learning OpenGL using GLUT window. So far i have made a colorful cube rotating on y and x asis and now i have added lighting but it doesnt work. It only makes it all in shadow but it should make nice perspectively shaded cube.
There is the code and i hope i'll have help :
#include <GL/glew.h>
#include <GL/glut.h>
GLfloat rotquad;
GLfloat LightPosition[] = { 0, 0, 0, 1 };
GLfloat LightAmbient[] = { 1, 1, 1, 1 };
GLfloat LightDiffuse[] = { 1, 1, 1, 1 };
bool light = false;
bool diffuse = false;
bool emissive = false;
bool specular = false;
void Keyboard (unsigned char key, int x, int y)
{
if (key=='l')
{
light=!light;
if (!light)
{
glDisable(GL_LIGHTING);
} else
{
glEnable(GL_LIGHTING);
}
}
}
void KeyboardSpec (int key, int x, int y)
{
}
void Init()
{
glEnable (GL_BLEND);
glEnable (GL_DEPTH_TEST);
glEnable (GL_COLOR_MATERIAL);
glEnable (GL_LIGHT1);
}
void SetUpLight ()
{
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
}
void Display()
{
glClearColor(0.2, 0.2, 0.2, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
glTranslatef(0, 0, -6);
glRotatef(rotquad, 1, 0, 0);
glRotatef(rotquad, 0, 1, 0);
glBegin(GL_QUADS); // Start Drawing The Cube
glColor4f(1, 0, 0, 1);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor4f(0, 1, 1, 1);
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
glColor4f(1, 0, 1, 1);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor4f(1, 1, 0, 1);
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)
glColor4f(0, 0, 1, 1);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor4f(0, 1, 0, 1);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd();
rotquad -= 1.5f;
glutSwapBuffers();
}
void OnReshape(int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGBA); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize (500, 500); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
glutCreateWindow ("First GLUT window"); // Set the title for the window
Init();
glutDisplayFunc (Display);
glutIdleFunc (Display);
glutKeyboardFunc (Keyboard);
glutSpecialFunc (KeyboardSpec);
glutReshapeFunc (OnReshape);
glutMainLoop(); // Enter GLUT's main loop
return 0;
}
few functions may be quite strange, thats because i have combined header and files asociated to it (im not sure that im saing it right because not english )