My gluSphere is Missing Patches - opengl

I'm trying to draw a sphere using gluSphere. The sphere appears but certain patches are missing whether I draw it as a filled polygons or a wire frame.
int width=800, height=600;
void myinit(void)
{
// set up overall light data, including specular=ambient=light colors
GLfloat light_position[]={ 10.0, 10.0, -10.0, 1.0 };
GLfloat light_color[]={ 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient_color[]={ 0.0, 0.2, 0.2, 1.0 };
GLfloat mat_specular[]={ 1.0, 1.0, 1.0, 1.0 };
glClearColor( 0.0, 1.0, 1.0, 0.0 );
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular );
glLightfv(GL_LIGHT0, GL_POSITION, light_position );
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color );
glLightfv(GL_LIGHT0, GL_SPECULAR, light_color );
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color );
/* attributes */
glEnable(GL_LIGHTING); // so lighting models are used
glEnable(GL_LIGHT0); // we'll use LIGHT0
glEnable(GL_DEPTH_TEST); // allow z-buffer display
//glClearDepth(1);
}
void drawHead(){
GLUquadric *sphere=gluNewQuadric();
gluQuadricDrawStyle( sphere, GLU_FILL);
gluQuadricNormals( sphere, GLU_SMOOTH);
gluQuadricOrientation( sphere, GLU_OUTSIDE);
gluQuadricTexture( sphere, GL_TRUE);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPushMatrix();
glTranslated(0,0,-5);
glRotated(30,1,1,1);
gluSphere(sphere,2.0,15,15);
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
drawHead();
glutSwapBuffers();
}
void main(int argc, char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width,height);
glutInitWindowPosition(100,200);
glutCreateWindow("Automaton");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.0,20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
myinit();
glutDisplayFunc(display);
glutMainLoop();
}

As per the docs, "...zNear must never be set to 0."
Bump your zNear out a bit, and your zFar a bit more:
gluPerspective( 90.0, 1.0, 1.0, 200.0 );
Complete:
#include <GL/glut.h>
int width=800, height=600;
void myinit(void)
{
// set up overall light data, including specular=ambient=light colors
GLfloat light_position[]={ 10.0, 10.0, -10.0, 1.0 };
GLfloat light_color[]={ 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient_color[]={ 0.0, 0.2, 0.2, 1.0 };
GLfloat mat_specular[]={ 1.0, 1.0, 1.0, 1.0 };
glClearColor( 0.0, 1.0, 1.0, 0.0 );
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular );
glLightfv(GL_LIGHT0, GL_POSITION, light_position );
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color );
glLightfv(GL_LIGHT0, GL_SPECULAR, light_color );
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color );
/* attributes */
glEnable(GL_LIGHTING); // so lighting models are used
glEnable(GL_LIGHT0); // we'll use LIGHT0
glEnable(GL_DEPTH_TEST); // allow z-buffer display
}
void drawHead()
{
GLUquadric *sphere=gluNewQuadric();
gluQuadricDrawStyle( sphere, GLU_FILL);
gluQuadricNormals( sphere, GLU_SMOOTH);
gluQuadricOrientation( sphere, GLU_OUTSIDE);
gluQuadricTexture( sphere, GL_TRUE);
glPushMatrix();
glTranslated(0,0,-5);
glRotated(30,1,1,1);
gluSphere(sphere,2.0,15,15);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
drawHead();
glutSwapBuffers();
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width,height);
glutInitWindowPosition(100,200);
glutCreateWindow("Automaton");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 90.0, 1.0, 1.0, 200.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
myinit();
glutDisplayFunc(display);
glutMainLoop();
}

Related

OPENGL lighting result comes out wrong

my desired outcome(in red)
Hi, I'm trying to make my code in opengl c++ to produce this(with light coming from (1,1,1)), and it doesn't work and produces this. Can anyone teach me why?
I thought it was about matrices calculation or lighting usage at first, but not so sure anymore.
light function
void init_light(void) {
glLoadIdentity();
glGetFloatv(GL_PROJECTION_MATRIX, ProjectionMat);
ProjectionMatrix = glm::make_mat4(ProjectionMat);
glGetFloatv(GL_MODELVIEW_MATRIX, ViewMat);
ViewMatrix = glm::make_mat4(ViewMat);
glm::mat4 InverseProjectionMatrix = glm::inverse(ProjectionMatrix);
glm::mat4 InverseViewMatrix = glm::inverse(ViewMatrix);
glm::mat4 M = glm::inverse(ProjectionMatrix * ViewMatrix);
glm::mat4 Matr = ProjectionMatrix * ViewMatrix;
glClearColor(1.0, 1.0, 1.0, 0.0);
//glClearDepth(0.0);
GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
//GLfloat position0[] = { 1., 1., 1., 0.0 };
//GLfloat position1[] = { 1., 1., 1., 1.0 };
GLfloat pos[] = { (GLfloat)(InverseViewMatrix * glm::make_vec4(position0))[0],
(GLfloat)(InverseViewMatrix * glm::make_vec4(position0))[1],
(GLfloat)(InverseViewMatrix * glm::make_vec4(position0))[2],
(GLfloat)(InverseViewMatrix * glm::make_vec4(position0))[3] };
GLfloat spot_direction[] = { 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 1.0);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 80.);
//glLightfv(GL_LIGHT0, GL_POSITION, position0);
//glLightfv(GL_LIGHT0, GL_POSITION, pos);
//glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 2.0);
//glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
//glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
//glLightfv(GL_LIGHT1, GL_POSITION, position1);
GLfloat mat_ambient[] = { 0.2, 0.2, 0.8, 0.0 };
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat mat_specular[] = { 0.5, 0.5, 0.5, 0.0 };
//GLfloat mat_emissive[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat mat_shininess[] = { 120.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emissive);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColorMaterial(GL_FRONT, GL_SHININESS);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}
rendering scene
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
double ratio = window_size[0] * 1.0 / window_size[1];
if (rotationbool == 1) {
xRot = temp_xRot;
yRot = temp_yRot;
}
glPopMatrix();
gluPerspective(fovy, ratio, zNear + (zoom_factor)* ratio, zFar + (zoom_factor)* ratio);
glLightfv(GL_LIGHT0, GL_POSITION, position0);
glLightfv(GL_LIGHT1, GL_POSITION, position1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glPopMatrix();
glRotatef(xRot, 1.0, .0, .0);
glRotatef(yRot, .0, 1.0, .0);
glTranslatef(xTrans, yTrans, zTrans);
glPushMatrix(); ...
}
main function
int main(int argc, char* argv[]) {
bool cad_draw = true;
if (cad_draw) {
glutInit(&argc, argv);
}
double L_base[3] = { 0.1, 0.1, 0.1 };
if (cad_draw) {
finemost_limit[0] = L_base[0];
finemost_limit[1] = L_base[1];
finemost_limit[2] = L_base[2];
window_params_init();
//glDepthRange(1.0, 0.);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(window_size[0], window_size[1]);
glutCreateWindow("Interactive boundary design");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
//init_sfc();
//init_light();
// register callbacks
glDepthFunc(GL_LEQUAL);
//glDepthFunc(GL_GEQUAL);
glDepthRange(1.0, 0.); // left handed에서 right hanaded로 변경
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
init_light();
///glutMouseWheelFunc(render_wheel);
glutMouseFunc(mousePress);
glutMotionFunc(mouseDrag);
glutPassiveMotionFunc(passive_mouse);
glutKeyboardFunc(simple_keyboard_binding);
// enter GLUT event processing cycle
glutMainLoop();
}
return 0;
}

How do I set material only for one object in OpenGL?

I have two 3D objects, and I need to set material only on one of them using glMaterial function. How can I accomplish this?
#include <GLUT/glut.h>
#include <cmath>
const int width = 1200, height = 600;
float xValue = 0.0;
float position = 0.0;
float angle = 0.0;
float shininess[] = { 1.0 };
float color[] = { 0.2, 0.2, 0.8 };
void init(void);
void display(void);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(85, 75);
glutCreateWindow("3D объекты - освещение");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_LIGHTING);
float lightAmbient[] = { 0.5, 0.5, 0.5, 0.0 };
float lightDiffuse[] = { 0.5, 0.5, 0.5, 0.0 };
float lightSpecular[] = { 0.5, 0.5, 0.5, 0.0 };
float lightPosition[] = { -10.0, 5.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-10.0, 10.0, -5.0, 5.0, -5.0, 5.0);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0.0, 0.0, width, height);
xValue += 0.01;
position = 5.0*cos(xValue);
angle = 270.0*sin(xValue);
glLineWidth(3);
glPushMatrix();
glTranslatef(position, 0.0, 0.0);
glRotatef(angle, 0, 1, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
glMaterialfv(GL_FRONT, GL_SPECULAR, color);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glutWireTorus(1, 2, 35, 35);
glPopMatrix();
glPushMatrix();
glTranslatef(-position, 0.0, 0.0);
glRotatef(angle, 0, 1, 0);
glutSolidTorus(0.5, 3.5, 35, 35);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
OpenGL is a state engine. Each state is kept until it is changed again, even beyond frames. For instance you can enable and disable lighting before drawing an object:
glEnable(GL_LIGHTING);
// darw object 1
glDisable(GL_LIGHTING);
// draw object 2
ALike the material settings can be set for each object individually, before drawing the object:
glMaterialfv(...);
// darw object 1
glMaterialfv(...);
// draw object 2

How to fix bitmap lighting in openGL?

I need help with my code in C++ using the OpenGL API. I have written a program that draws 4 3D cubes and text (using bitmap) onto the screen. Now I want to add lighting.
I have added glMaterial code to give a description of the material for the cubes. I do not want the material properties to be applied to the bitmap text. Therefore, I placed the code for the material before drawing the cube and I also placed the code for drawing the cube and the material between a pushMatrix and popMatrix pair. However, when I run the code, I find the text changes color.
Below is some of the code that I am using:
void init() {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ligAmb);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lig[0][0]);
glLightfv(GL_LIGHT0, GL_SPECULAR, lig[0][1]);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, ligDir);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, exp_one);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, cutoff);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lig[1][0]);
glLightfv(GL_LIGHT1, GL_SPECULAR, lig[1][1]);
glEnable(GL_LIGHT1);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void drawCube(Point3D colors[], Point3D vertices[]) {
glBegin(GL_QUADS);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glEnd();
}
void displayObject() {
glPushMatrix();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, ligPos[0]);
glLightfv(GL_LIGHT1, GL_POSITION, ligPos[1]);
typedef GLint vertex3[3];
Point3D vertices[8] = { {-1.0, -1.0, -1.0},
{-1.0, -1.0, 1.0},
{-1.0, 1.0, -1.0},
{-1.0, 1.0, 1.0},
{ 1.0, -1.0, -1.0},
{ 1.0, -1.0, 1.0},
{ 1.0, 1.0, -1.0},
{ 1.0, 1.0, 1.0} };
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_INT, 0, vertices);
GLubyte vertIndex[] = { 6,2,3,7,5,1,0,4,7,3,1,5,4,0,2,6,2,0,1,3,7,5,4,6
};
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, vertIndex);
glPopMatrix();
Point3D colorsb[8] = { {0.,0.,1.},
{0.,0.,1.},
{0.,0.,1.},
{0.,0.,1.},
{0.,0.,1.},
{0.,0.,1.},
{0.,0.,1.},
{0.,0.,1.} };
Point3D colorsg[8] = { {0.,1.,0.},
{0.,1.,0.},
{0.,1.,0.},
{0.,1.,0.},
{0.,1.,0.},
{0.,1.,0.},
{0.,1.,0.},
{0.,1.,0.} };
Point3D colorsr[8] = { {1.,0.,0.},
{1.,0.,0.},
{1.,0.,0.},
{1.,0.,0.},
{1.,0.,0.},
{1.,0.,0.},
{1.,0.,0.},
{1.,0.,0.} };
Point3D colorsy[8] = { {1.,1.,0.},
{1.,1.,0.},
{1.,1.,0.},
{1.,1.,0.},
{1.,1.,0.},
{1.,1.,0.},
{1.,1.,0.},
{1.,1.,0.} };
glPushMatrix();
glTranslatef(-0.5f, 4.0f, -6.0f);
glRotatef(10.0, 0.0, 1.0, 0.0);
glRotatef(loop, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat[0][0]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat[0][1]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat[0][2]);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shi[0]);
drawCube(colorsb, vertices);
glPopMatrix();
glPushMatrix();
glTranslatef(6.0f, -0.5f, -6.0f);
glRotatef(10.0, 1.0, 0.0, 0.0);
glRotatef(loop, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat[1][0]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat[1][1]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat[1][2]);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shi[1]);
drawCube(colorsg, vertices);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5f, -4.5f, -6.0f);
glRotatef(10.0, 0.0, 1.0, 0.0);
glRotatef(loop, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat[2][0]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat[2][1]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat[2][2]);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shi[2]);
drawCube(colorsr, vertices);
glPopMatrix();
glPushMatrix();
glTranslatef(-6.0f, -0.5f, -6.0f);
glRotatef(10.0, 1.0, 0.0, 0.0);
glRotatef(loop, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat[3][0]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat[3][1]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat[3][2]);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shi[3]);
drawCube(colorsy, vertices);
glPopMatrix();
}
void display() {
displayObject();
wordColor = "green";
char str[] = { "Red" };
glColor3f(0.0, 1.0, 0.0);
glRasterPos2f(-0.5, 0.0);
for (int i = 0; i < strlen(str); i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[i]);
}
glPopMatrix();
loop += 0.05;
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
OpenGL is a state machine, which means that it keeps a state around until you explicitly change it again. So when you take care to set the illumination only after drawing the text during drawing one frame, it will have happened before drawing the text of the next frame.
The solution is, that you always set every relevant state for whatever it is you're drawing, right before you draw it. In case of the text, it's as simple as disable lighting, right before drawing the text (actually before calling glRasterPos).

I want to see the movement of the Sphere on the screen(OpenGL)

I want to see the movement of the Sphere on the screen.
but, If you run the code, you will not be able to update the screen.
How do I change the code to show a change in the location of the phrase I created?
void display() {
glLoadIdentity();
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
GLfloat diffuse1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position1[] = { 0, 0, 0, 1.0 };
GLfloat diffuse2[] = { 0.5, 0.5, 0.5, 1 };
GLfloat ambient2[] = { 0.1, 0.1, 0.1, 1 };
GLfloat specular2[] = { 0.5, 0.5, 0.5, 1 };
GLfloat emission2[] = { 0, 0, 0, 1 };
GLfloat shine = 300;
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient1);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular1);
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient2);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse2);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular2);
glMaterialfv(GL_FRONT, GL_EMISSION, emission2);
glMaterialf(GL_FRONT, GL_SHININESS, shine);
glRotatef(mer_revolution, 0, 0, 1);
glTranslatef(0, 0.8, 0);
glRotatef(mer_rotate, 0, 0, 1);
glutSolidSphere(1, 300, 300);
glPopMatrix();
glFlush();
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("");
glClearColor(0, 0, 0, 0);
glColor3f(1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glMatrixMode(GL_MODELVIEW);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(move);
glutMainLoop();
}
Move 'gL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT' at the top of the display.
The screen must be initialized when performing a function. Only then will the screen continue to change.

How to use spot light in opengl

I am trying to learn how to use a spot light in OpenGL. I wish to shine a spot (torch) light from a point marked (lighpos), and shown as grey dot, onto the side of a GLUT teapot centered at (possph) in 3D space. I have attached an example where I have tried to do this but I cannot get the light to focus on the teapot. I am expecting a almost touch light focus based on the parameters I have tried to use.
Could someone point out what I have missed / mistake I have made.
Thanks
Stuart
#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>
GLfloat lighpos[] = { -300., 200., 250., 1.0 }; // Location of light
GLfloat possph[] = { -50., 350., 150. }; // Position of teapot
GLfloat ligdir[] = { 250., 150., -100. }; // Direction from light to teapot
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_POSITION, lighpos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, ligdir);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10.0);
GLfloat ambientLight0[] = { 0.25, 0.25, 0.25, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight0);
GLfloat diffuseLight0[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
GLfloat specularLight0[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 128.0f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void drawAxis() {
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0); glVertex3f(-500., 0.0, 0.0); glVertex3f(500., 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, -500., 0.0); glVertex3f(0.0, 500., 0.0);
glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, -500.); glVertex3f(0.0, 0.0, 500.);
glEnd();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
drawAxis();
glPushMatrix();
glPointSize(10);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
glVertex3d(lighpos[0], lighpos[1], lighpos[2]);
glEnd();
glPopMatrix();
glColor3f(1., 0., 0.);
glPushMatrix();
glTranslated(possph[0], possph[1], possph[2]);
glutSolidTeapot(100.);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 4, 3000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1500.0, 1500.0, 1500.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(0.0, 0.0);
glutInitWindowSize(800, 800);
glutCreateWindow("spotlight");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Fixed function pipeline illumination, which is what you are using in your code, performs lighting calculations only at the vertices. If your spotlight illuminates only a few or a single vertex you'll not get a pleasing spotlight effect. One possible solution would be to highly refine your models' meshes. The better, much more efficient and elegant solution is to drop using the fixed function pipeline and implement a illumination fragment shader, so that lighting is calculated for each pixel.