Ok, to be fully honest check out my mess.
I have a huge problem that i have been battling with, thats why i am asking about the normals in the first place, then i thought it was the fog that caused the problem and so on and so on… This is my first OpenGL implementation, i am a super and total n00b at this, and i have been trying to learn this these past 3-4 days. My problem is that i have defined 6 quads but only 2 of them are showing, every time i try to set the lighting model (ambient, diffuse, specular)… and i also have a spotlight in there somewhere…
I know this a long block of code, but i would really appreciate if you could help me out, cause i cant figure whats wrong with this…
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
GLfloat rotVar = 0.0f;
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
GLfloat fLightPos[4] = { -100.0f, 100.0f, 50.0f, 1.0f }; // Point source
GLfloat fNoLight = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat fLowLight = { 0.45f, 0.45f, 0.45f, 1.0f };
GLfloat fBrightLight = { 1.0f, 1.0f, 1.0f, 1.0f };
void RenderScene(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLoadIdentity(); //
glTranslated(0.0f, 0.0f, -10.0f); // selve kuglen
//keyboard variables
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glRotated(rotVar, 0.0, 5.0, 0.0);
//RED WALL - left side
glColor3f(1.f, 0.23f, 0.25f);
glBegin(GL_QUADS);
glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-200.0f, 125.3f, -100.0f);
glVertex3f(-100.0f, 100.3f, 100.0f);
glVertex3f(-100.0f, -25.3f, 100.0f);
glVertex3f(-200.0f, -25.3f, -100.0f);
glEnd();
//GREY WALL - rightside
glColor3f(0.62f, 0.62f, 0.62f);
glBegin(GL_QUADS);
glNormal3f(0.0f, -1.0, 0.0f);
glVertex3f(200.0f, 125.3f, -100.0f);
glVertex3f(100.0f, 100.3f, 100.0f);
glVertex3f(100.0f, -25.3f, 100.0f);
glVertex3f(200.0f, -25.3f, -100.0f);
glEnd();
//Ceiling
glColor3f(0.50f, 0.50f, 0.50f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(-200.0f, 110.3f, -100.0f);
glVertex3f(-100.0f, 110.3f, 100.0f);
glVertex3f(100.0f, 110.3f, 100.0f);
glVertex3f(200.0f, 110.3f, -100.0f);
glEnd();
//FLOOR
glColor3f(0.35f, 0.35f, 0.35f);
glBegin(GL_QUADS);
glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-2000.0f, -250.3f, -1000.0f);
glVertex3f(-1000.0f, -250.3f, 100.0f);
glVertex3f(1000.0f, -250.3f, 100.0f);
glVertex3f(2000.0f, -250.3f, -100.0f);
glEnd();
//END WALL
glColor3f(0.61f, 0.60f, 0.60f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(-800.0f, -25.3f, -800.0f);
glVertex3f(-800.0f, 125.3f, -800.0f);
glVertex3f(800.0f, 125.3f, -800.0f);
glVertex3f(800.0f, -25.3f, -800.0f);
glEnd();
rotVar +=0.1f;
//glRotated(rotVar, 0.0, 0.0, 0.0);
glColor3ub(100,23,25);
glutSolidSphere(1.0f, 30, 17);
glTranslated(3.0f, 1.4f, -1.0f);
//CUBE AND COLORS
GLfloat black[3] = {0,0,0};
GLfloat red[3] = {1,0,0};
GLfloat blue[3] = {0,0,1};
GLfloat green[3] = {0,1,0};
GLfloat magenta[3] = {1,0,1};
GLfloat yellow[3] = {1,1,0};
GLfloat cyan[3] = {0,1,1};
GLfloat white[3] = {1,1,1};
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glColor3fv(black);
glVertex3fv(black);
glColor3fv(red);
glVertex3fv(red);
glColor3fv(magenta);
glVertex3fv(magenta);
glColor3fv(blue);
glVertex3fv(blue);
glNormal3f(0,-1,0);
glColor3fv(yellow);
glVertex3fv(yellow);
glColor3fv(red);
glVertex3fv(red);
glColor3fv(black);
glVertex3fv(black);
glColor3fv(green);
glVertex3fv(green);
glNormal3f(0,0,-1);
glColor3fv(cyan);
glVertex3fv(cyan);
glColor3fv(white);
glVertex3fv(white);
glColor3fv(yellow);
glVertex3fv(yellow);
glColor3fv(green);
glVertex3fv(green);
glNormal3f(-1,0,0);
glColor3fv(green);
glVertex3fv(green);
glColor3fv(black);
glVertex3fv(black);
glColor3fv(blue);
glVertex3fv(blue);
glColor3fv(cyan);
glVertex3fv(cyan);
glNormal3f(1,0,0);
glColor3fv(white);
glVertex3fv(white);
glColor3fv(magenta);
glVertex3fv(magenta);
glColor3fv(red);
glVertex3fv(red);
glColor3fv(yellow);
glVertex3fv(yellow);
glNormal3f(0,1,0);
glColor3fv(cyan);
glVertex3fv(cyan);
glColor3fv(blue);
glVertex3fv(blue);
glColor3fv(magenta);
glVertex3fv(magenta);
glColor3fv(white);
glVertex3fv(white);
glEnd();
// Restore the matrix state
glPopMatrix();
// Refresh the Window
glutPostRedisplay();
glutSwapBuffers();
}
void SetupRC() {
///////////////////////////////////////////////////////////
//ALL LIGHT: SPECULAR, AMBIENT, DIFFUSE + SPOTLIGHTs
GLfloat lightPos = { 10.0f, 10.0f, 10.0f, 1.0f }; // Array to specify position
GLfloat ambientLight = { 0.3f, 0.3f, 0.3f, 1.0f }; //the postion of AMBIENT light
GLfloat diffuseLight = { 1.7f, 1.7f, 1.7f, 1.0f };
GLfloat specular = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref = { 1.0f, 1.0f, 1.0f, 1.0f };
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside
// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,70.0f);
glEnable(GL_LIGHT0);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT,GL_SHININESS,128); // All materials hereafter have full specular reflectivity with a high shine
// Light blue background
//glClearColor(0.65f, 0.65f, 65.0f, 1.0f ); //clear buffer
glEnable(GL_NORMALIZE);
/*
///////////////////////////////////////////////////////////
//FOG FOG FOG FOG
// Setup Fog parameters
glEnable(GL_FOG); // Turn Fog on
glFogfv(GL_FOG_COLOR, fLowLight); // Set fog color to match background
glFogf(GL_FOG_START, 1.0f); // How far away does the fog start
glFogf(GL_FOG_END, 30.0f); // How far away does the fog stop
glFogi(GL_FOG_MODE, GL_LINEAR); // Which fog equation do I use?
*/
}
/////////////////////////////////////////////////////
// Handle arrow keys
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
//CAMERA
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (height==0)
// CLIPPING PLANE: field of view of 150 degrees, near and far clipping planes 1.0 and 800
gluPerspective ( 50, float(width), 1.0, 1000.0 );
else
gluPerspective ( 50, float(width)/height, 1.0, 1000.0); //-1.0 tæt på kamera
glMatrixMode(GL_MODELVIEW);
}
//CAMERA
int main(int argc, char* argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); //GLUT_DOUBLE, dobbel buffer
glutInitWindowSize(600, 600 );
glutInitWindowPosition(100,100);
glutCreateWindow(“Cube Animation”);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
glutReshapeFunc(reshape);
glEnable(GL_DEPTH_TEST);
SetupRC();
glutMainLoop();
return 0;
}