gpt4 book ai didi

c++ - 将缓冲区内容返回到 OpenGL 中的纹理

转载 作者:行者123 更新时间:2023-11-30 04:29:34 28 4
gpt4 key购买 nike

为了让我的迷宫类游戏更快,我决定将我绘制的球放在纹理中,因为我必须为每个房间绘制一次,并且我使用模板缓冲区将其绘制为凹多边形,这需要更多时间比使用纹理。问题是,当我渲染游戏开始后的第三帧时,我从后台缓冲区正确地将它放入纹理中,我的问题是,为什么会这样?

当我使用 thirst frame 的纹理时,我的纹理是纯白色的,所以里面什么都没有。当我使用第二帧的纹理时,我只有所需纹理的黑色背景,而当我使用第三帧的纹理时,我就有了所需的纹理。对于帧计数,我在“drawTexture”函数中使用静态变量“done”。

从第一帧复制:

enter image description here

从第二帧复制:

enter image description here

从第三帧复制(期望的结果):

enter image description here

void DrawBall::drawTexture(float imageD) {
static int done = 0;

if (done < 3) {
drawToTexture(imageD);
done++;
}

glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture);

glColor3f(1, 1, 1);
glBegin (GL_QUADS);
glTexCoord2f (0.0, 0.0); glVertex3f (0.0, 0.0, -imageD);
glTexCoord2f (1.0, 0.0); glVertex3f (5.0, 0.0, -imageD);
glTexCoord2f (1.0, 1.0); glVertex3f (5.0, 5.0, -imageD);
glTexCoord2f (0.0, 1.0); glVertex3f (0.0, 5.0, -imageD);
glEnd ();

glDisable(GL_TEXTURE_2D);
}

void DrawBall::drawToTexture(float imageD) {
int viewport[4];
glGetIntegerv(GL_VIEWPORT, (int*) viewport);

int textureWidth = 64;
int textureHeight = 64;

texture = genEmptyTexture(textureWidth, textureHeight);

glViewport(0, 0, textureWidth, textureHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/*
This function calculates the vertexes for the ball
inside a vector<vector<float>> variable "test"
*/
_calculateCircleVertexes(0.0f, 0.0f, -2.0f, 0.249f, &test, 20);

_displayBall(&test, 0.0f, 0.0f, 0.5f, -2.0f, &*smallBallColor);

glBindTexture(GL_TEXTURE_2D, texture);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, textureWidth, textureHeight, 0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)viewport[2] / (GLfloat)viewport[3], 1.0f, imageD + 10.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

GLuint DrawBall::genEmptyTexture(unsigned int width, unsigned int height) {
GLuint txtIndex;

glGenTextures(1, &txtIndex);
glBindTexture(GL_TEXTURE_2D, txtIndex);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, NULL);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

return txtIndex;
}

void DrawBall::_displayBall(vector<vector<GLfloat>> *vertexes, GLfloat x, GLfloat y
, GLfloat imageW, GLfloat imageD, color *color) {
glTranslatef(x, y, imageD);

glClearStencil(0);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_NEVER, 0, 1);
glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);

glBegin(GL_POLYGON);
vector<vector<GLfloat>>::iterator it = vertexes->begin();
for (; it != vertexes->end(); it++) {
glVertex3f((*it)[0], (*it)[1], 0.0f);
}
glEnd();

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);

glColor3f(color->r, color->g, color->b);
glBegin(GL_QUADS);
glVertex3f(-(imageW / 2.0f), -(imageW / 2.0f), 0.0f);
glVertex3f( (imageW / 2.0f), -(imageW / 2.0f), 0.0f);
glVertex3f( (imageW / 2.0f), (imageW / 2.0f), 0.0f);
glVertex3f(-(imageW / 2.0f), (imageW / 2.0f), 0.0f);
glEnd();

glDisable(GL_STENCIL_TEST);

glTranslatef(x, y, -imageD);
}

最佳答案

您不应该使用窗口帧缓冲区(包括后台和前台缓冲区)进行渲染到纹理操作。它很容易破裂(您已经体验过)。而是使用所谓的 Framebuffer 对象,并将纹理作为渲染目标。

关于c++ - 将缓冲区内容返回到 OpenGL 中的纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9282443/

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