gpt4 book ai didi

c - 过剩 reshape 功能不起作用

转载 作者:行者123 更新时间:2023-11-30 16:01:23 28 4
gpt4 key购买 nike

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

这是 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;
}
}

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

最佳答案

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

关于c - 过剩 reshape 功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6721612/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com