过剩重塑功能不起作用

发布于 2024-11-24 16:08:24 字数 6772 浏览 1 评论 0原文

当我尝试调整过剩窗口的大小时,屏幕变成空白。

这是 reshape 回调函数的代码:

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();
}

我对 opengl 世界还很陌生,但从我了解到的情况来看,这应该是可行的。

这是所有代码的组合:

#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;
    }
}

如果您需要更多信息,请告诉我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2024-12-01 16:08:24

在 FillZBuffer 函数中,深度掩模在末尾被禁用,并且仅在同一函数的开头重新启用。因此,当再次调用 Render 时,清除深度缓冲区位的调用不会执行任何操作,因为深度掩模已被禁用。

要解决此问题,必须在调用清除深度缓冲区位之前重新启用深度掩码。

这就是渲染应该的样子。

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(); 
} 

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(); 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文