尝试在OpenGL中对特定场景进行建模,并在场景中的一盏灯中放置一个聚光灯。我无法让照明与聚光灯一起工作。我知道问题出在 myinit 函数上,但我不确定是哪一个。
//Most Recent Testing Results
//Top of the Table seems off
//Table Legs seem to be drawing wrong
//Stem looks too Wide
//Screen isn't displaying properly
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;
static GLdouble viewer[]= {50.0, 50.0, 20.0}; // initial viewer location
static GLint on = 1; //Stores if the light is currently on, 1 is yes, 0 is no
void myinit(void)
{
GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[] = {40,57,-16.5,1};
GLfloat lmodel_ambient[] = { .1, .1, .1, 1.0 };
GLfloat spotdir[] = {0,-1,0};
GLfloat local_view[] = { 0.0 };
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
GLfloat att_constant = 0.0;
GLfloat att_linear = 1.0;
GLfloat att_quadratic = 1.0;
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotdir );
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 5);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, att_constant);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, att_linear);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, att_quadratic);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glClearColor(0.0, 0.1, 0.1, 0.0);
glEnable(GL_AUTO_NORMAL);
}
void lighting()
{
}
void keys(unsigned char key, int x, int y)
{
/* Use x, X, y, Y, z, and Z keys to move viewer */
if(key == 'x') viewer[0]-= 1.0;
if(key == 'X') viewer[0]+= 1.0;
if(key == 'y') viewer[1]-= 1.0;
if(key == 'Y') viewer[1]+= 1.0;
if(key == 'z') viewer[2]-= 1.0;
if(key == 'Z') viewer[2]+= 1.0;
if(key == 'q' || key == 'Q') exit(0); //Quits the Program
if(key == 'o' || key == 'O') on *= -1; //Toggles the Light on or Off
display();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
/* Use a perspective view */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
2.0* (GLfloat) h / (GLfloat) w, 2.0, 200.0);
else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
2.0* (GLfloat) w / (GLfloat) h, 2.0, 200.0);
/* Or we can use gluPerspective */
glMatrixMode(GL_MODELVIEW);
}
/*Need a method for Will use methods, Display Lists are not needed?
Table
Keyboard & Mouse
AIO system
Physical Lamp
*/
void tabledraw() //Method for drawing the table
{ //It will have a high shininess propertery,
//and will need to experiment with other values to determine others
glMaterialf(GL_FRONT, GL_SHININESS, 70.0); //Sets High Shininess
glColor3f(.75,.38,.1); //Sets the color to Brown
glNormal3f(0,1,0); //Sets the normal Vector
//Legs will be 5x30x5
glBegin(GL_QUADS);
//Table Top - A 80x3x40 Rectangle
//Bottom of Top
glVertex3f(10,35,0);
glVertex3f(90,35,0);
glVertex3f(90,35,-40);
glVertex3f(10,35,-40);
//Top of Top
glVertex3f(10,40,0);
glVertex3f(90,40,0);
glVertex3f(90,40,-40);
glVertex3f(10,40,-40);
//Left of Top
glVertex3f(10,40,0);
glVertex3f(10,35,0);
glVertex3f(10,35,-40);
glVertex3f(10,40,-40);
//Right of Top
glVertex3f(90,40,0);
glVertex3f(90,35,0);
glVertex3f(90,35,-40);
glVertex3f(90,40,-40);
//Back of top
glVertex3f(90,40,-40);
glVertex3f(90,35,-40);
glVertex3f(10,35,-40);
glVertex3f(10,40,-40);
//Front of Top
glVertex3f(90,40,0);
glVertex3f(90,35,0);
glVertex3f(10,35,0);
glVertex3f(90,40,0);
//Front Left Leg 5x30x5 Rectangle
//Bottom
glVertex3f(10,7,0);
glVertex3f(15,7,0);
glVertex3f(15,7,-5);
glVertex3f(10,7,-5);
//Don't Need a top
//Left
glVertex3f(10,7,0);
glVertex3f(10,37,0);
glVertex3f(10,37,-5);
glVertex3f(10,7,-5);
//Right
glVertex3f(15,7,0);
glVertex3f(15,37,0);
glVertex3f(15,37,-5);
glVertex3f(15,7,-5);
//Back
glVertex3f(15,7,-5);
glVertex3f(15,37,-5);
glVertex3f(10,37,-5);
glVertex3f(10,7,-5);
//Front
glVertex3f(15,7,0);
glVertex3f(15,37,0);
glVertex3f(10,37,0);
glVertex3f(10,7,0);
//Front Right Leg
//Bottom
glVertex3f(85,7,0);
glVertex3f(90,7,0);
glVertex3f(90,7,-5);
glVertex3f(85,7,-5);
//Don't Need a top
//Left
glVertex3f(85,7,0);
glVertex3f(85,37,0);
glVertex3f(85,37,-5);
glVertex3f(85,7,-5);
//Right
glVertex3f(90,7,0);
glVertex3f(90,37,0);
glVertex3f(90,37,-5);
glVertex3f(90,7,-5);
//Back
glVertex3f(90,7,-5);
glVertex3f(90,37,-5);
glVertex3f(85,37,-5);
glVertex3f(85,7,-5);
//Front
glVertex3f(90,7,0);
glVertex3f(90,37,0);
glVertex3f(85,37,0);
glVertex3f(85,7,0);
//Back Left Leg
//Bottom
glVertex3f(10,7,-35);
glVertex3f(15,7,-35);
glVertex3f(15,7,-40);
glVertex3f(10,7,-40);
//Don't Need a top
//Left
glVertex3f(10,7,-35);
glVertex3f(10,37,-35);
glVertex3f(10,37,-40);
glVertex3f(10,7,-40);
//Right
glVertex3f(15,7,-35);
glVertex3f(15,37,-35);
glVertex3f(15,37,-40);
glVertex3f(15,7,-40);
//Back
glVertex3f(15,7,-40);
glVertex3f(15,37,-40);
glVertex3f(10,37,-40);
glVertex3f(10,7,-40);
//Front
glVertex3f(15,7,-35);
glVertex3f(15,37,-35);
glVertex3f(10,37,-35);
glVertex3f(10,7,-35);
//Back Right Leg
//Bottom
glVertex3f(85,7,-35);
glVertex3f(90,7,-35);
glVertex3f(90,7,-40);
glVertex3f(85,7,-40);
//Don't Need a top
//Left
glVertex3f(85,7,-35);
glVertex3f(85,37,-35);
glVertex3f(85,37,-40);
glVertex3f(85,7,-40);
//Right
glVertex3f(90,7,-35);
glVertex3f(90,37,-35);
glVertex3f(90,37,-40);
glVertex3f(90,7,-40);
//Back
glVertex3f(90,7,-40);
glVertex3f(90,37,-40);
glVertex3f(85,37,-40);
glVertex3f(85,7,-40);
//Front
glVertex3f(90,7,-35);
glVertex3f(90,37,-35);
glVertex3f(85,37,-35);
glVertex3f(85,7,-35);
glEnd();
}
void kbmdraw()
{
//Needs to be a rectangle, silver and dull
//Bottom will be on the table top (y = 40), and will be 5 points tall.
glMaterialf(GL_FRONT,GL_SHININESS, .2);
glColor4f(.5,.5,.5,1); //Sets Color to silver
glBegin(GL_QUADS);
//Bottom of Keyboard
glVertex3f(60,40, -10 );
glVertex3f(60, 40, -20);
glVertex3f(40,40,-20);
glVertex3f(40,40,-10);
//Top of Keyboard
glVertex3f(60,42, -10 );
glVertex3f(60, 42, -20);
glVertex3f(40,42,-20);
glVertex3f(40,42,-10);
//Left of Keyboard
glVertex3f(40,42,-10);
glVertex3f(40,42,-20);
glVertex3f(40,40,-20);
glVertex3f(40,40,-10);
//Right of Keyboard
glVertex3f(60,42,-10);
glVertex3f(60,42,-20);
glVertex3f(60,40,-20);
glVertex3f(60,40,-10);
//Back of Keyboard
glVertex3f(60,42,-20);
glVertex3f(60,40,-20);
glVertex3f(40,40,-20);
glVertex3f(40,42,-20);
//Front of Keyboard
glVertex3f(60,42,-10);
glVertex3f(60,40,-10);
glVertex3f(40,40,-10);
glVertex3f(40,42,-10);
//Mouse will be the same, just shiny instead of Dull and white
//Will be a 5x5x10 square
glColor4f(1,1,1,1);
glMaterialf(GL_FRONT, GL_SHININESS,.8); //Sets the Shininess to a high value
//Bottom of Mouse
glVertex3f(65,40,-10);
glVertex3f(65,40,-20);
glVertex3f(70,40,-20);
glVertex3f(70,40,-10);
//Top of Mouse
glVertex3f(65,42,-10);
glVertex3f(65,42,-20);
glVertex3f(70,42,-20);
glVertex3f(70,42,-10);
//Left of Mouse
glVertex3f(65,40,-10);
glVertex3f(65,40,-20);
glVertex3f(65,42,-20);
glVertex3f(65,42,-10);
//Right of Mouse
glVertex3f(70,40,-10);
glVertex3f(70,40,-20);
glVertex3f(70,42,-20);
glVertex3f(70,42,-10);
//Back of Mouse
glVertex3f(65,40,-20);
glVertex3f(70,40,-20);
glVertex3f(70,42,-20);
glVertex3f(65,42,-20);
//Front of Mouse
glVertex3f(65,40,-10);
glVertex3f(70,40,-10);
glVertex3f(70,42,-10);
glVertex3f(65,42,-10);
glEnd();
}
void aiodraw()
{
/*Basic shape will be
__________
| ______ |
| | | |3
| |__4__| |
----------
| | 2
------
------ 1
*/
//The center Square will be black and shiny, the rest of it will be silver and dull
//The AIO computer will have four main shapes
//1. The base > a 10 x 3 x10 rectangle, Dull Silver
//2. The Stem > a 5 x 20 x 3 Rectangle placed partially inside the base
//3. Then core system > a 22 x 18 x 3 Silver Dull Rectange part of the stem might go inside it
//4. The Viewing port > a 20 x 15 x 1 Square placed slightly above center on the AIO system
glColor3f(.5,.5,.5); //Sets Color to Silver
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,.3); //Low Shininess Value
glBegin(GL_QUADS);
//AIO BASE #1
//Bottom of Base
glVertex3f(65,40,-25);
glVertex3f(65,40,-35);
glVertex3f(55,40,-35);
glVertex3f(55,40,-25);
//Top of Base
glVertex3f(65,43,-25);
glVertex3f(65,43,-35);
glVertex3f(55,43,-35);
glVertex3f(55,43,-25);
//Left of Base
glVertex3f(55,40,-25);
glVertex3f(55,43,-25);
glVertex3f(55,43,-35);
glVertex3f(55,40,-35);
//Right of Base
glVertex3f(65,40,-25);
glVertex3f(65,43,-25);
glVertex3f(65,43,-35);
glVertex3f(65,40,-35);
//Back of Base
glVertex3f(55,40,-35);
glVertex3f(55,43,-35);
glVertex3f(65,43,-35);
glVertex3f(65,40,-35);
//Front of Base
glVertex3f(55,40,-25);
glVertex3f(55,43,-25);
glVertex3f(65,43,-25);
glVertex3f(65,40,-25);
//AIO Stem - a 4 x 10 x 3 Rectangle
//Bottom of Stem
glVertex3f(58,43,-30);
glVertex3f(58,43,-32);
glVertex3f(62,43,-32);
glVertex3f(62,43,-30);
//Top of Stem
glVertex3f(58,53,-30);
glVertex3f(58,53,-32);
glVertex3f(62,53,-32);
glVertex3f(62,53,-30);
//Left of Stem
glVertex3f(58,43,-30);
glVertex3f(58,53,-30);
glVertex3f(58,53,-32);
glVertex3f(58,43,-32);
//Right of Stem
glVertex3f(62,43,-30);
glVertex3f(62,53,-30);
glVertex3f(62,53,-32);
glVertex3f(62,43,-32);
//Back of Stem
glVertex3f(58,43,-32);
glVertex3f(58,53,-32);
glVertex3f(62,53,-32);
glVertex3f(62,43,-32);
//Front of Stem
glVertex3f(58,43,-30);
glVertex3f(58,53,-30);
glVertex3f(62,53,-30);
glVertex3f(62,43,-30);
//AIO Core - 22 x 18 x 3 Rectangle. WIll look like it is hanging off the stem
//Bottom of the Core
glNormal3f(0,0,1);
glVertex3f(49,48, -30);
glVertex3f(49,48,-27);
glVertex3f(71,48,-27);
glVertex3f(71,48,-30);
//Top of the core
glVertex3f(49,66, -30);
glVertex3f(49,66,-27);
glVertex3f(71,66,-27);
glVertex3f(71,66,-30);
//Left of the Core
glVertex3f(49,66,-30);
glVertex3f(49,48,-30);
glVertex3f(49,48,-27);
glVertex3f(49,66,-27);
//Right of the core
glVertex3f(71,66,-30);
glVertex3f(71,48,-30);
glVertex3f(71,48,-27);
glVertex3f(71,66,-27);
//Back of the Core
glVertex3f(49,66,-30);
glVertex3f(49,48,-30);
glVertex3f(71,48,-30);
glVertex3f(71,66,-30);
//Front of the Core
glVertex3f(49,66,-27);
glVertex3f(49,48,-27);
glVertex3f(71,48,-27);
glVertex3f(71,66,-27);
//AIO Screen- A shiny black Square
glColor3f(0,0,0);
glMaterialf(GL_FRONT,GL_SHININESS,.9); //Very high Shininess
glMaterialf(GL_FRONT,GL_DIFFUSE,.1); //Low Diffuse value
glVertex3f(50,65,-26.9);
glVertex3f(50,50,-26.9);
glVertex3f(70,50,-26.9);
glVertex3f(70,65,-26.9);
glEnd();
}
void lampdraw()
{
//Made up of three Sections
//1. Base: a 3x1x3 Box
//2. Stem: a 1x10x1 Rectangle
//3. Lamp: a 3x3x3 box, with an open section, angled towards the Keyboard
glColor3f(.5,.5,.5); //Sets Color to Silver
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,.3); //Low Shininess Value
glBegin(GL_QUADS);
//Base
//Don't need bottom
//Top
glVertex3f(23,41,-15);
glVertex3f(23,41,-18);
glVertex3f(28,41,-18);
glVertex3f(28,41,-15);
//Left
glVertex3f(23,41,-15);
glVertex3f(23,40,-15);
glVertex3f(23,40,-18);
glVertex3f(23,41,-18);
//Right
glVertex3f(28,41,-15);
glVertex3f(28,40,-15);
glVertex3f(28,40,-18);
glVertex3f(28,41,-18);
//Back
glVertex3f(23,40,-18);
glVertex3f(23,41,-18);
glVertex3f(28,41,-18);
glVertex3f(28,40,-18);
//Front
glVertex3f(23,40,-15);
glVertex3f(23,41,-15);
glVertex3f(28,41,-15);
glVertex3f(28,40,-15);
//Stem: 1 x 10 x1 Rectangle
//Left
glVertex3f(25,41,-16);
glVertex3f(25,57,-16);
glVertex3f(25,57,-17);
glVertex3f(25,41,-17);
//Right
glVertex3f(26,41,-16);
glVertex3f(26,57,-16);
glVertex3f(26,57,-17);
glVertex3f(26,41,-17);
//Back
glVertex3f(25,41,-17);
glVertex3f(25,57,-17);
glVertex3f(26,57,-17);
glVertex3f(26,41,-17);
//Front
glVertex3f(25,41,-16);
glVertex3f(25,57,-16);
glVertex3f(26,57,-16);
glVertex3f(26,41,-16);
//Lamp Head- an open 3x3x3 Rectangle
//Bottom
glVertex3f(27,56,-15);
glVertex3f(24,57,-15);
glVertex3f(24,57,-18);
glVertex3f(27,56,-18);
//Top
glVertex3f(27,59,-15);
glVertex3f(24,60,-15);
glVertex3f(24,60,-18);
glVertex3f(27,59,-18);
//Left
glVertex3f(24,57,-15);
glVertex3f(24,60,-15);
glVertex3f(24,60,-18);
glVertex3f(24,57,-18);
//NO right
//Back
glVertex3f(27,56,-18);
glVertex3f(24,57,-18);
glVertex3f(24,60,-18);
glVertex3f(27,59,-18);
//Front
glVertex3f(27,56,-15);
glVertex3f(24,57,-15);
glVertex3f(24,60,-15);
glVertex3f(27,59,-15);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Update viewer position in modelview matrix */
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 50.0, 45.0, -10.0, 0.0, //X,Y,Z COP | XYZ of Target | XYZ of Tilt
1.0, 0.0);
tabledraw();
kbmdraw();
aiodraw();
lampdraw();
glFlush();
if(on == 1) glEnable(GL_LIGHT0);
else glDisable(GL_LIGHT0);
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Table Lighting");
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
目前灯在灯的外面,为了简单起见笔直指向下方。我试过修改值。光点方向截止导致分段错误。