OpenGL texturing level - c++

I'm trying to learn OpenGL, and I want to start texturing but I got stuck. I've created a level with two rooms and a corridor, so I want diferent textures for different parts (that is, a texture for the floor, texture for the walls and so on) but after looking at the NeHe tutorial I'm still stuck, where do I need to put it in my code?
#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
#include <gl\glu.h>
void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);
int is_depth; /* depth testing flag */
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(40, 40);
glutCreateWindow("The Cube World");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
/* This time we're going to keep the aspect ratio
constant by trapping the window resizes. */
glutReshapeFunc(resize);
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
is_depth = 1;
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
if (is_depth)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
else
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
//floor
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 0.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 0.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -25.0);
//left wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 00.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 25.0, -25.0);
//right wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 00.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -25.0);
//roof
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 25.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 25.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -25.0);
//right panel
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -25.0);
//left panel
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -25.0);
//top panel
glColor3f(0.2f, 0.2f, 0.2f);
//bottom right
glVertex3f(50.0, 15.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
//top right
glVertex3f(50.0, 25.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
//top left
glVertex3f(-50.0, 25.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
//bottom left
glVertex3f(-50.0, 15.0, -25.0);
// corridor floor
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 0.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 0.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 0.0, -25.0);
// corridor left wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 00.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-10.0, 15.0, -25.0);
// corridor right wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 00.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 15.0, -25.0);
//corridor roof
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 15.0, -25.0);
//right panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -275.0);
//left panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -275.0);
//top panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 15.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 25.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 15.0, -275.0);
//right wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 00.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -275.0);
//left wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 00.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 25.0, -275.0);
//roof room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 25.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -275.0);
//back wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -325.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -325.0);
//floor room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 0.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 0.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -275.0);
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
case 'A':
glTranslatef(5.0, 0.0, 0.0);
break;
case 'd':
case 'D':
glTranslatef(-5.0, 0.0, 0.0);
break;
case 'w':
case 'W':
glTranslatef(0.0, 0.0, 5.0);
break;
case 's':
case 'S':
glTranslatef(0.0, 0.0, -5.0);
break;
case 't':
case 'T':
if (is_depth)
{
is_depth = 0;
glDisable(GL_DEPTH_TEST);
}
else
{
is_depth = 1;
glEnable(GL_DEPTH_TEST);
}
}
display();
}
void resize(int width, int height)
{
if (height == 0)
height = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* Note we divide our width by our height to get the aspect ratio. */
gluPerspective(45.0, width / height, 1.0, 400.0);
/* Set initial position */
glTranslatef(0.0, -5.0, -150.0);
glMatrixMode(GL_MODELVIEW);
}

You don't seem to have any code for texture loading. I'd suggest you head back to NeHe and read this tutorial http://nehe.gamedev.net/tutorial/texture_mapping/12038/
It feels like you are getting too far ahead of yourself, and you should be going through most (if not all) of the tutorials in order. If you're trying to make a first person shooter, I'd also suggest loading the level from a model file (there are articles about this on NeHe too)

Related

Rotating object spotlight shadow

I have to rotate an object in front of a background. I have to light it with a spotlight and show the shadow on the object and on the background.
My problem is when I rotate the object the shadow doesn't change.
Here is my code:
#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
GLfloat qaBlack[] = { 0.0, 0.0, 0.0, 1.0 }; //Black Color
GLfloat qaGreen[] = { 0.0, 1.0, 0.0, 1.0 }; //Green Color
GLfloat qaWhite[] = { 1.0, 1.0, 1.0, 1.0 }; //White Color
GLfloat qaRed[] = { 1.0, 0.0, 0.0, 1.0 }; //White Color
GLfloat angleShape = 0.0f;
void CreateShape()
{
// Set material properties
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, qaGreen);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, qaGreen);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, qaWhite);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20);
glBegin(GL_TRIANGLES); // Begin drawing the pyramid with 4 triangles
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
// Right
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
// Back
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// Left
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd(); // Done drawing the pyramid
}
void Spotlight()
{
glEnable(GL_LIGHTING);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.f);
glLightModelf(GL_LIGHT_MODEL_AMBIENT, 0.2f);
GLfloat light0Pos[] = { -3,-3,10,1 };
glLightfv(GL_LIGHT0, GL_POSITION, light0Pos);
GLfloat light0Diffuse[] = { 1,1,1 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0Diffuse);
GLfloat light0Specular[] = { 1,1,1 };
glLightfv(GL_LIGHT0, GL_SPECULAR, light0Specular);
glEnable(GL_LIGHT0);
//glDisable(GL_LIGHTING);
}
void display2()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Spotlight();
glTranslatef(-0.5f, 0.0f, -6.0f);
glRotatef(angleShape, 1.0f, 1.0f, 0.0f);
CreateShape();
angleShape -= 5.f;
glLoadIdentity();
glTranslatef(2.0f, 2.5f, -7.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 0.5f);
glVertex3f(0.5f, -3.0f, -5.0f);
glVertex3f(-7.0f, -6.0f, -5.0f);
glVertex3f(1.0f, -6.0f, -5.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(-6.5f, -3.0f, -5.0f);
glVertex3f(0.5f, -3.0f, -5.0f);
glVertex3f(-7.0f, -6.0f, -5.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(-6.5f, -3.0f, -5.0f);
glVertex3f(0.5f, -3.0f, -5.0f);
glVertex3f(0.5f, 2.0f, -5.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(-6.5f, -3.0f, -5.0f);
glVertex3f(-6.5f, 2.0f, -5.0f);
glVertex3f(0.5f, 2.0f, -5.0f);
glEnd();
glutSwapBuffers();
}
void initGL() {
glClearColor(0.0, 0.7, 1.0, 1.0);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void reshape(GLsizei width, GLsizei height) {
if (height == 0) height = 1;
GLfloat aspect = (GLfloat)width / (GLfloat)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(640, 480);
glutCreateWindow("OpenGL #1");
glutDisplayFunc(display2);
glutReshapeFunc(reshape);
initGL();
glutMainLoop();
return 0;
}

How to display specular spot in an object in OpenGL

I am creating a sphere. The light source (ambient and diffuse lighting) works well. Only the specular light that doesn't show up in any part of the sphere.
The material code seems not working. I deleted the line and the result is also the same.
The result I want is the sphere rotates with white spot on it in order to look real.
Am I missing any required configuration for lighting?
Is there any solution?
Thanks.
float angle = 0.0f;
void initRendering(){
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
}
void draw(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glRotatef(angle, 0.0, 1.0, 0.0);
glutSolidSphere(0.5, 20, 20);
glPopMatrix();
GLfloat ambientColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat diffuseColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specularColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat lightPosition[] = {0.0f, 5.0f, -3.0f, 0.0f};
GLfloat mat_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMateriali(GL_FRONT, GL_SHININESS, 100);
angle+=0.1f;
glutPostRedisplay();
glutSwapBuffers();
}
main(){
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("Sphere");
glClearColor(1.0, 1.0, 1.0, 1.0);
initRendering();
glutDisplayFunc(draw);
glutMainLoop();
}
The default for specular light is to add to the colour evaulated in the diffuse illumination step. The specular intensity is modulated by the specular set. You've set this colour to "black", so it adds literally zero.
GLfloat mat_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
// …
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
I'd start by actually not inhibiting the specular term.

OpenGL Object Rendering Order

Problem: Objects aren't rendering correctly.
What I tried to do: Searched Stack Overflow. Read that I needed to enable Depth_Test and did so. Still doesn't work. Read that I possibly needed glDepthFunc(GL_LEQUAL);, did not work. Is there something in the way I wrote reshape/initscene/myDisplay that causes these common approaches not to work?
Code below.
void reshape (int w, int h)
{
viewport.w = w;
viewport.h = h;
glViewport(0,0,viewport.w,viewport.h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h) {
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
}
else {
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the color buffer
glMatrixMode(GL_MODELVIEW); // indicate we are specifying camera transformations
glLoadIdentity(); // make sure transformation is "zero'd"
glScalef(zoom, zoom, zoom);
gluLookAt(0.0, -9.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 1.0);
glRotatef(y_rotation, 0.0, 1.0, 0.0);
glRotatef(x_rotation, 1.0, 0.0, 0.0);
renderObjects();
glFlush();
glutSwapBuffers(); // swap buffers (we earlier set double buffer)
}
void initScene(){
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
reshape(viewport.w, viewport.h);
glShadeModel(GL_FLAT);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
GLfloat red[] = {1.0f, 0.2f, 0.2f, 1.0f};
GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat gray[] = {0.5f, 0.5f, 0.5f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, gray);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, gray);
GLfloat lightPos[] = {0.0f, 0.0f, -10.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
}

OpenGL only 2 quads showing

I am trying to create a room in openGL and i have the following quads however wall2 and wall4 do not show. I am guessing this is something to do with perspectives as if i go outside of the room i can see they have been rendered.
glColor3f(0.1f, 0.9f, 0.9f);
//Wall1
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glVertex3f(-10,0,-10);
glVertex3f( 10,0,-10);
glVertex3f( 10,5,-10);
glVertex3f(-10,5,-10);
glEnd();
//Wall2
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glVertex3f(-10,0, 10);
glVertex3f( 10,0, 10);
glVertex3f( 10,5, 10);
glVertex3f(-10,5, 10);
glEnd();
//Wall3
glColor3f(0.4f, 0.9f, 0.1f);
glBegin(GL_QUADS);
glNormal3f(-1,0, 0);
glVertex3f( 10,0,-10);
glVertex3f( 10,0,10);
glVertex3f( 10,5,10);
glVertex3f( 10,5,-10);
glEnd();
//Wall4
glColor3f(0.1f, 0.2f, 0.2f);
glBegin(GL_QUADS);
glNormal3f(1, 0, 0);
glVertex3f( -10,0,-10);
glVertex3f( -10,0,10);
glVertex3f( -10,5,10);
glVertex3f( -10,5,-10);
glEnd();
Try disabling face culling! :)

Opengl fixed light is still moving

I want to set a light in my 3d world, positioned in a corner and when I move with my mouse I want it to stay there and just be.
When I use the function from glut: glutSolidSphere, everything looks okay.
But when I add a quad in my world and I move with my mouse, the lighting on the quad changes. Any idea how to solve this?
void World::paint(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
float no_mat[] = {0.0f, 0.0f, 0.0f, 1.0f};
float mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
float mat_ambient_color[] = {0.8f, 0.8f, 0.2f, 1.0f};
float mat_diffuse[] = {0.1f, 0.5f, 0.8f, 1.0f};
float mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
float no_shininess = 0.0f;
float low_shininess = 5.0f;
float high_shininess = 100.0f;
float mat_emission[] = {0.3f, 0.2f, 0.2f, 0.0f};
camera->setup();
light->assignComponentsToGLLightX();
glEnable(GL_LIGHTING);
glMatrixMode( GL_MODELVIEW );
// WORKS
float temp[] = {0.7f, 0.0f, 0.0f, 1.0f};
glPushMatrix();
glTranslatef(3.75f, 3.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, temp);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere( 3.0, 25, 25 );
glPopMatrix();
// Doesn't work
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glBegin(GL_QUADS);
//Front
//glNormal3f(0.0f, 0.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//Right
//glNormal3f(1.0f, 0.0f, 0.0f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//Back
//glNormal3f(0.0f, 0.0f, -1.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//Left
//glNormal3f(-1.0f, 0.0f, 0.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glEnd();
glPopMatrix();
void Camera::setup() const
{
glRotatef(_rotX, 1.0,0.0,0.0);
glRotatef(_rotY, 0.0,1.0,0.0);
glTranslated(-_moveX,-_moveY,-_moveZ);
gluLookAt(3.0 , 5.0 , 25.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0 );
}
void Light::assignComponentsToGLLightX() const
{
glLightfv(_light, GL_AMBIENT, _ambientLight);
glLightfv(_light, GL_DIFFUSE, _diffuseLight);
glLightfv(_light, GL_SPECULAR, _specularLight);
glLightfv(_light, GL_POSITION, _position);
}
So I setted the light call after the camera_setup()
But now the quad is still not working as I want it to. The light still changes and I am pretty sure that my normals are correct (checked them twice).
I was thinking, is this possible because I don't change my _camPosX, Y, Z values when I move/rotate?
Calling gluLookAt before glRotate / glTranslate should help...
Other thoughts :
call glMatrixMode and glLoadIdentity only once, in camera::setup.
don't call glRotate and glTranslate in camera::setup. This has nothing to do with the camera. This is your object's tranformation
Wrap glRotate and glTranslate inside glPushMatrix/glPopMatrix. This way, the next time you draw an object, transformations won't be cummulated.
glTranslatef(0.0f, 0.0f, 0.0f) is useless. It means : add a null displacement to the current matrix. In other words: don't do anything
wrapping pushmatrix/popmatrix around this is thus useless
As datenwolf said, I don't see where you actually indicate the light's position
You must set the lights' positions after moving the world / setting the camera i.e. add
glLightfv(GL_LIGHT0+n, GL_POSITION, light_position)
calls right after
camera->setup();