problems making a spotlight, please help!
I''m pretty new to open gl and c++, so if you can keep it simple. I decided to try making a flashlight program, and I want a specular light source to point out the end of my flashlight (pretty obvious), but I''m not to sure how to do it. If anyone wants to take a look at this code, I''d appreciate - thanks:
#include // Header File For Windows
#include // Header File For Standard Input/Output
#include // Header File For The OpenGL32 Library
#include // Header File For The GLu32 Library
#include // Header File For The Glaux Library
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
bool lp; // L Pressed?
bool fp; // F Pressed?
bool sp; // Spacebar Pressed? ( NEW )
GLfloat xrot = 0; // X Rotation
GLfloat xspeed; // X Rotation Speed
GLfloat z=-5.0f; // Depth Into The Screen
GLUquadricObj *quadratic; // Storage For Our Quadratic Objects ( NEW )
GLfloat LightSpecular[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLuint filter; // Which Filter To Use
GLuint texture[1];
GLuint object=0; // Which Object To Draw (NEW)
GLfloat lightPos[] = { 0.0f, 0.0f, 75.0f, 1.0f };
GLfloat specular[] = { 1.0f, 0.0f, 0.0f, 1.0f};
GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f };
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{
height=1;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid)
{
if (!LoadGLTextures())
{
return FALSE; }
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glFrontFace(GL_CCW);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,60.0f);
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,100.0f);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT, GL_SHININESS,100);
quadratic=gluNewQuadric();
gluQuadricNormals(quadratic, GLU_SMOOTH);
gluQuadricTexture(quadratic, GL_TRUE);
return TRUE;
}
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Flashlight Model
glLoadIdentity();
glTranslatef(0.0f,0.0f,-40.0f);
glRotatef(180, 1.0f,0.0f,0.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
if(xrot > 100)
xrot = -100;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glColor3f(0.1f, 0.1f, 0.1f);
glTranslatef(0.0f,0.0f,-1.5f);
gluCylinder(quadratic,1.0f,1.75f,1.5f,32,32);
glDisable(GL_TEXTURE_2D);
gluCylinder(quadratic,0.75f,1.50f,1.5f,32,32);
glEnable(GL_TEXTURE_2D);
glTranslatef(0.0f, 0.0f, 2.8f);
glColor4f(0.0f, 0.2f, 0.9f, 0.6f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
gluDisk(quadratic,0.0f,1.75f,32,10);
glDisable(GL_BLEND);
glTranslatef(0.0f, 0.0f, -2.8f);
glColor4f(1.0f,1.0f,1.0f,1.0f);
gluCylinder(quadratic,0.9f,1.55f,1.5f,32,32);
glTranslatef(0.0f, 0.0f, 1.44f);
gluCylinder(quadratic,1.75f,1.75f,1.5f,32,32);
glColor3f(0.8f, 0.8f, 0.8f);
glDisable(GL_TEXTURE_2D);
gluCylinder(quadratic,1.50f,1.50f,1.5f,32,32);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glEnable(GL_TEXTURE_2D);
glTranslatef(0.0f, 0.0f, -4.5f);
gluCylinder(quadratic,1.0f,1.0f,3.2f,32,32);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTranslatef(0.0f, 0.0f, -6.5f);
gluCylinder(quadratic,1.0f,1.0f,7.0f,32,32);
gluDisk(quadratic,0.0f,1.01f,32,10);
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, 11.5f);
glDisable(GL_TEXTURE_2D);
gluSphere(quadratic, 1.0f, 15, 15);
//Here is the spotlight
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir);
glTranslatef(0.0f, 0.0f, -1.0f);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glDisable(GL_TEXTURE_2D);
//************************************************
xrot += 2;
if(xrot > 360)
xrot = 0.0f;
return TRUE;
}
Thanks Again
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement