Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6712733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:17:40+00:00 2026-05-26T08:17:40+00:00

I’m having troubles with creating a rectangular surface that has a cone-like spotlight pointing

  • 0

I’m having troubles with creating a rectangular surface that has a cone-like spotlight pointing on it. So far, I’ve tried to create a 2D cube surface (glutSolidCube()) and use GL_SPOT_XXX to create the spotlight. But I don’t for what reason, the light just keeps scattering all over the surface instead of gather into a spot.

So here is my code so far, you can just focus on the init_light() and display() functions. The other functions are there for you to reference if you need to 🙂

void rotateCamera()
{
    /* Rotate camera 2 degrees about selected axis */
    theta[axis] += dtheta;
    if (theta[axis] > 360.0 ) theta[axis] -= 360.0;
    glutPostRedisplay();
}

void computeCameraPosition() {
    GLdouble M[16];

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glTranslatef(centroid[0], centroid[1], centroid[2]);
    glRotatef(theta[0], 1.0, 0.0, 0.0);
    glRotatef(theta[1], 0.0, 1.0, 0.0);
    glRotatef(theta[2], 0.0, 0.0, 1.0);
    glTranslatef(-centroid[0], -centroid[1], -centroid[2]);
    glGetDoublev(GL_MODELVIEW_MATRIX, M);

    GLdouble newvec[16];

    glMultMatrixd(init_vec);
    glGetDoublev(GL_MODELVIEW_MATRIX, newvec);
    glPopMatrix();

    /* the 1st and 2nd columns are the eye position and new y-axis direction */
    memcpy(eye, newvec, sizeof(GLdouble)*4);
    memcpy(yaxis, newvec+4, sizeof(GLdouble)*4);
}

void init_light()
{
    GLfloat spot_direction[] = { 1.0, 1.0, 0.0 };
    glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction );
    glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 2.0 );
    glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, 45.0 );

    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
}


void display() {
    computeCameraPosition();
    glClearColor(0.9, 0.9, 0.9, 1.0);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(eye[0], eye[1], eye[2], 
          centroid[0], centroid[1], centroid[2], 
          yaxis[0], yaxis[1], yaxis[2]); 
    glutSolidCube(2.0);
    glFlush();
    glutSwapBuffers();
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800, 800);
    glutInitWindowPosition (200, 100);
    glutCreateWindow("Scene Editor");
    init_light();
    glutDisplayFunc(display);
    glutMainLoop();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:17:40+00:00Added an answer on May 26, 2026 at 8:17 am

    Standard fixed function OpenGL illumination is only evaluated at the vertex positions and the determined colours interpolated over the primitives. So get a spotlight effect, the illumination must be evaluated with high resolution. You can achieve this by either tesselating your plane, or by using a fragment shader that does per fragment lighting.


    EDIT to answer comment

    Tesselation

    Tesselation means, subdividing your plane into a large number of “patches”. Like this:

    +---------------------+
    |                     |
    |                     |
    |                     |
    |                     |
    |                     |
    |                     |
    |                     |
    |                     |
    |                     |
    |                     |
    +---------------------+
    

    →

    +-+-+-+-+-+-+-+-+-+-+-+
    | | | | | | | | | | | |
    +-+-+-+-+-+-+-+-+-+-+-+
    | | | | | | | | | | | |
    +-+-+-+-+-+-+-+-+-+-+-+
    | | | | | | | | | | | |
    +-+-+-+-+-+-+-+-+-+-+-+
    | | | | | | | | | | | |
    +-+-+-+-+-+-+-+-+-+-+-+
    | | | | | | | | | | | |
    +-+-+-+-+-+-+-+-+-+-+-+
    | | | | | | | | | | | |
    +-+-+-+-+-+-+-+-+-+-+-+
    

    Per fragment lighting

    Per fragment lighting means, that the lighting equation is evaluated at each rasterized fragment (a fragment is what’s commonly referred to a pixel). For this a fragment shader is used, i.e. a small program load into the graphics processor (GPU), that’s executed for each fragment/pixel of the rasterized primitive (triangle, line, point).

    Lighthouse3D has a tutorial about it: http://www.lighthouse3d.com/tutorials/glsl-tutorial/spot-light-per-pixel/ I recommend you read the full GLSL tutorial there, to get an idea, what’s going on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.