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

  • SEARCH
  • Home
  • 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 6146729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:59:37+00:00 2026-05-23T18:59:37+00:00

When I try to resize my glut window, the screen goes blank. This is

  • 0

When I try to resize my glut window, the screen goes blank.

This is the code for the reshape callback funciton:

void Resize(int width, int height) 
{  
    CurrentWidth = width;
    CurrentHeight = height;


    glViewport(0, 0, (GLsizei)CurrentWidth, (GLsizei)CurrentHeight); 

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    glOrtho(0, CurrentWidth, CurrentHeight, 0, NearPlane, FarPlane);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glutPostRedisplay();
}

I am pretty new to the opengl world but from what I have learned this is supposed to work.

And this is all of the code put together:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <gl/glut.h>

#include "Utils.h"

int LEFT = 0;
int RIGHT = 0;
int UP = 0;
int DOWN = 0;

int CurrentWidth = 800,
    CurrentHeight = 800,
    WindowHandle = 0;

float NearPlane = 1.0f,
      FarPlane = 100.0f;

float lightX,
      lightY;

void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void Idle(void);
void Resize(int, int);
void KeyPressed(unsigned char, int, int);
void SpecialPressed(int, int, int);
void SpecialReleased(int, int, int);
void Update(void);
void Render(void);
void FillZBuffer(void);
void ClearAlpha(void);
void RenderLightAlpha(float);
void GeometryPass(void);
void Draw(void);

int main (int argc, char* argv[]) 
{  
    Initialize(argc, argv);

    glutMainLoop(); 

    exit(EXIT_SUCCESS);
}

void Initialize(int argc, char* argv[])
{
    InitWindow(argc, argv);

    fprintf(
        stdout,
        "INFO: OpenGL Version: %s\n",
        glGetString(GL_VERSION)
    );

    lightX = 300.0f;
    lightY = 300.0f;
}

void InitWindow(int argc, char* argv[])
{
    glutInit(&argc, argv);

    glutInitContextVersion(3, 3);
    glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);

    glutSetOption(
        GLUT_ACTION_ON_WINDOW_CLOSE,
        GLUT_ACTION_GLUTMAINLOOP_RETURNS
    );

    glutInitWindowSize (CurrentWidth, CurrentHeight); 
    glutInitWindowPosition (100, 100);

    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA);

    WindowHandle = glutCreateWindow ("Shadows");  

    if(WindowHandle < 1) {
        fprintf(
            stderr,
            "ERROR: Could not create a new rendering window.\n"
        );
        exit(EXIT_FAILURE);
    }

    glutDisplayFunc(Render); 
    glutReshapeFunc(Resize);
    glutIdleFunc(Idle);
    glutKeyboardFunc(KeyPressed);
    glutSpecialFunc(SpecialPressed);
    glutSpecialUpFunc(SpecialReleased);
} 

void Update()
{
    int speed = 10;
    if (LEFT)
    {
        lightX -= speed;
    }
    if (RIGHT)
    {
        lightX += speed;
    }
    if (UP)
    {
        lightY -= speed;
    }
    if (DOWN)
    {
        lightY += speed;
    }
}

void Draw()
{
    float x = 200;
    float y = 200;
    float w = 100;
    float h = 100;
    float depth = 0.0f;

    // floor
    glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
    depth = -10.0f;
    glBegin(GL_QUADS);
    {
        glVertex3f(0, 0, depth);
        glVertex3f((float)CurrentWidth, 0, depth);
        glVertex3f((float)CurrentWidth, (float)CurrentHeight, depth);
        glVertex3f(0, (float)CurrentHeight, depth);
    }
    glEnd();

    // square
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    depth = -5.0;
    glBegin(GL_QUADS);
    {
        glVertex3f( x,  y, depth);
        glVertex3f( x + w, y, depth);
        glVertex3f(x + w, y + h, depth);
        glVertex3f(x,  y + h, depth);
    }
    glEnd();
}

void Render() 
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDisable(GL_CULL_FACE);

    Update();

    FillZBuffer();

    ClearAlpha();

    RenderLightAlpha(1.0f);

    GeometryPass();

    glutSwapBuffers();
    glutPostRedisplay(); 
}  

void FillZBuffer()
{
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glDepthMask(GL_TRUE);

    Draw();

    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glDepthMask(GL_FALSE);
}

void ClearAlpha()
{
    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
    glColor4f(0.0f, 0.0f, 0.0f, 0.0f);

    glBegin (GL_QUADS);
    {
        glVertex2f(0, 0);
        glVertex2f((float)CurrentWidth, 0);
        glVertex2f((float)CurrentWidth, (float)CurrentHeight);
        glVertex2f(0, (float)CurrentHeight);
    }
    glEnd ();
}

void RenderLightAlpha(float intensity)
{
    float depth = -1.0f;  
    float radius = 300.0f;
    float angle;
    int numSubdivisions = 32;

    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glBegin(GL_TRIANGLE_FAN);
    {
        glColor4f(0.0f, 0.0f, 0.0f,  intensity);
        glVertex3f(lightX, lightY, depth);

        // Set edge colour for rest of shape
        glColor4f(0.0f, 0.0f, 0.0f, 0.0f);

        for (angle = 0; angle <= (float)PI * 2; angle += (((float)PI * 2) / numSubdivisions))
        {
            glVertex3f( radius*(float)cos(angle) + lightX, radius*(float)sin(angle) + lightY, depth);  
        }

        glVertex3f(lightX + radius, lightY, depth);
    }
    glEnd();
}

void GeometryPass()
{
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_BLEND);
    glBlendFunc(GL_DST_ALPHA, GL_ONE);

    Draw();
}

void Idle()
{
    glutPostRedisplay();
}

void Resize(int width, int height) 
{  
    CurrentWidth = width;
    CurrentHeight = height;


    glViewport(0, 0, (GLsizei)CurrentWidth, (GLsizei)CurrentHeight); 

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    glOrtho(0, CurrentWidth, CurrentHeight, 0, NearPlane, FarPlane);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glutPostRedisplay();
}

void KeyPressed(unsigned char key, int x, int y)
{
    UNREFERENCED_PARAMETER(x);
    UNREFERENCED_PARAMETER(y);

    // escape key
    if (key == 27)
    {
        exit(0);
    }
}

void SpecialPressed(int keyCode, int x, int y)
{
    UNREFERENCED_PARAMETER(x);
    UNREFERENCED_PARAMETER(y);

    if (keyCode == GLUT_KEY_LEFT)
    {
        LEFT = 1;
    }
    else if (keyCode == GLUT_KEY_RIGHT)
    {
        RIGHT = 1;
    }
    else if (keyCode == GLUT_KEY_UP)
    {
        UP = 1;
    }
    else if (keyCode == GLUT_KEY_DOWN)
    {
        DOWN = 1;
    }
}

void SpecialReleased(int keyCode, int x, int y)
{
    UNREFERENCED_PARAMETER(x);
    UNREFERENCED_PARAMETER(y);
    if (keyCode == GLUT_KEY_LEFT)
    {
        LEFT = 0;
    }
    else if (keyCode == GLUT_KEY_RIGHT)
    {
        RIGHT = 0;
    }
    else if (keyCode == GLUT_KEY_UP)
    {
        UP = 0;
    }
    else if (keyCode == GLUT_KEY_DOWN)
    {
        DOWN = 0;
    }
}

Let me know if you need anymore information.

  • 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-23T18:59:38+00:00Added an answer on May 23, 2026 at 6:59 pm

    In the FillZBuffer function the depth mask is disabled at the end and only re-enabled at the beginning of the same function. So when Render is called again, the call to clear the depth buffer bit does nothing because the depth mask is disabled.

    To fix this the depth mask must be re-enabled before the call to clear the depth buffer bit.

    So this is what Render should look like.

    void Render() 
    {
        glDepthMask(GL_TRUE); // insert this line
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glDisable(GL_CULL_FACE);
    
        Update();
    
        FillZBuffer();
    
        ClearAlpha();
    
        RenderLightAlpha(1.0f);
    
        GeometryPass();
    
        glutSwapBuffers();
        glutPostRedisplay(); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to resize the list the program crashes... Look at that code,
When I try to move my window(with a mouse) above my screen so that
I am trying to resize the window in IE. For this, I am using
I try to catch resize events with jquery and the $(window).resize(function() { } Everything
If you try to resize the window while my page is open: http://antonpug.com/mainepark/ You
Try to resize the DIV in this example -> http://jsbin.com/ixesak/edit#preview The cursor moves over
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
Try the following code public enum Color { Blue=1, Red=2, Green=3 } public List<Color>
try it out: volatile float bob = -344.0f; unsigned int fred = (unsigned int)bob;
Try running this in a .VBS file MsgBox(545.14-544.94) You get a neat little answer

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.