gpt4 book ai didi

c++ - OpenGL 错误。运行时立方体顶点位置错误?

转载 作者:行者123 更新时间:2023-11-28 07:49:42 25 4
gpt4 key购买 nike

我正在创建 Minecraft 的新手版本克隆。首先,我想渲染一个立方体并向所有面添加特定的纹理。我创建了一个 Block 类,它包含来自输​​入的顶点,并有一个名为 RenderBlock 的输出函数来渲染带有附加纹理的“ block ”。

这是输入 8 个数组的函数,代表 block 的 8 个顶点的 8 个坐标。

void Block::SetVertices(float UL[],float UR[],float ZR[],float ZL[],float UL2[],float           UR2[],float ZR2[],float ZL2[])
{
Front_Square[1][1]=UL[1];
Front_Square[1][2]=UL[2];
Front_Square[1][3]=UL[3];
std::cout << "Front Top Left Coordinates" << std::endl;
std::cout << Front_Square[1][1] << ","<<Front_Square[1][2] << "," << Front_Square[1][3] << std::endl;
Front_Square[2][1]=UR[1];
Front_Square[2][2]=UR[2];
Front_Square[2][3]=UR[3];
std::cout << "Front Top Right Coordinates" << std::endl;
std::cout << Front_Square[2][1] << ","<<Front_Square[2][2] << "," << Front_Square[2][3] << std::endl;
Front_Square[3][1]=ZR[1];
Front_Square[3][2]=ZR[2];
Front_Square[3][3]=ZR[3];
std::cout << "Front Bottom Right Coordinates" << std::endl;
std::cout << Front_Square[3][1] << ","<<Front_Square[3][2] << "," << Front_Square[3][3] << std::endl;
Front_Square[4][1]=ZL[1];
Front_Square[4][2]=ZL[2];
Front_Square[4][3]=ZL[3];
std::cout << "Front Bottom Left Coordinates" << std::endl;
std::cout << Front_Square[4][1] << ","<<Front_Square[4][2] << "," << Front_Square[4][3] << std::endl;
//--------------BACK SQUARE------------------------------------------------------------------------------------------------------------------------//
Back_Square[1][1]=UL2[1];
Back_Square[1][2]=UL2[2];
Back_Square[1][3]=UL2[3];

Back_Square[2][1]=UR2[1];
Back_Square[2][2]=UR2[2];
Back_Square[2][3]=UR2[3];

Back_Square[3][1]=ZR2[1];
Back_Square[3][2]=ZR2[2];
Back_Square[3][3]=ZR2[3];

Back_Square[4][1]=ZL2[1];
Back_Square[4][2]=ZL2[2];
Back_Square[4][3]=ZL2[3];
};

这里是渲染 block 的函数。

void Block::RenderBlock(const char*SideTexture)
{
glEnable(GL_TEXTURE_2D);
Tex[1]=loadTexture(SideTexture);
glBindTexture(GL_TEXTURE_2D, Tex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(Front_Square[1][1],Front_Square[1][2],Front_Square[1][3]);//FTL
glTexCoord2f(1.0f, 1.0f); glVertex3f(Front_Square[2][1],Front_Square[2][2],Front_Square[2][3]);//FTR
glTexCoord2f(1.0f, 0.0f); glVertex3f(Front_Square[3][1],Front_Square[3][2],Front_Square[3][3]);//FBR
glTexCoord2f(0.0f, 0.0f); glVertex3f(Front_Square[4][1],Front_Square[4][2],Front_Square[4][3]);//FBL
glEnd();

glBindTexture(GL_TEXTURE_2D, Tex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(Back_Square[1][1],Back_Square[1][2],Back_Square[1][3]);//BTL
glTexCoord2f(1.0f, 1.0f); glVertex3f(Back_Square[2][1],Back_Square[2][2],Back_Square[2][3]);//BTR
glTexCoord2f(1.0f, 0.0f); glVertex3f(Back_Square[3][1],Back_Square[3][2],Back_Square[3][3]);//BBR
glTexCoord2f(0.0f, 0.0f); glVertex3f(Back_Square[4][1],Back_Square[4][2],Back_Square[4][3]);//BBL
glEnd();


glBindTexture(GL_TEXTURE_2D, Tex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(Back_Square[1][1],Back_Square[1][2],Back_Square[1][3]);//BTL
glTexCoord2f(1.0f, 1.0f); glVertex3f(Front_Square[1][1],Front_Square[1][2],Front_Square[1][3]);//FTL
glTexCoord2f(1.0f, 0.0f); glVertex3f(Front_Square[4][1],Front_Square[4][2],Front_Square[4][3]);//FBL
glTexCoord2f(0.0f, 0.0f); glVertex3f(Back_Square[4][1],Back_Square[4][2],Back_Square[4][3]);//BBL
glEnd();

glBindTexture(GL_TEXTURE_2D, Tex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(Back_Square[2][1],Back_Square[2][2],Back_Square[2][3]);//BTR
glTexCoord2f(1.0f, 1.0f); glVertex3f(Front_Square[2][1],Front_Square[2][2],Front_Square[2][3]);//FTR
glTexCoord2f(1.0f, 0.0f); glVertex3f(Front_Square[3][1],Front_Square[3][2],Front_Square[3][3]);//FBR
glTexCoord2f(0.0f, 0.0f); glVertex3f(Back_Square[3][1],Back_Square[3][2],Back_Square[3][3]);//BBR
glEnd();

glBindTexture(GL_TEXTURE_2D, Tex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(Back_Square[4][1],Back_Square[4][2],Back_Square[4][3]);//BBL
glTexCoord2f(1.0f, 1.0f); glVertex3f(Back_Square[3][1],Back_Square[3][2],Back_Square[3][3]);//BBR
glTexCoord2f(1.0f, 0.0f); glVertex3f(Front_Square[3][1],Front_Square[3][2],Front_Square[3][3]);//FBR
glTexCoord2f(0.0f, 0.0f); glVertex3f(Front_Square[4][1],Front_Square[4][2],Front_Square[4][3]);//FBL
glEnd();

glBindTexture(GL_TEXTURE_2D, Tex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(Back_Square[1][1],Back_Square[1][2],Back_Square[1][3]);//BTL
glTexCoord2f(1.0f, 1.0f); glVertex3f(Back_Square[2][1],Back_Square[2][2],Back_Square[2][3]);//BTR
glTexCoord2f(1.0f, 0.0f); glVertex3f(Front_Square[2][1],Front_Square[2][2],Front_Square[2][3]);//FTR
glTexCoord2f(0.0f, 0.0f); glVertex3f(Front_Square[1][1],Front_Square[1][2],Front_Square[1][3]);//FTL
glEnd();
glDisable(GL_TEXTURE_2D);
};

2 个数组用于方 block 泥土和草地污垢

float TL[] = {0.0f,-1.0f,1.0f,0.0f};
float TR[] = {0.0f,1.0f,1.0f,0.0f};
float BR[] = {0.0f,1.0f,-1.0f,0.0f};
float BL[] = {0.0f,-1.0f,-1.0f,0.0f};
float TL2[] = {0.0f,-1.0f,1.0f,-2.0f};
float TR2[] = {0.0f,1.0f,1.0f,-2.0f};
float BR2[] = {0.0f,1.0f,-1.0f,-2.0f};
float BL2[] = {0.0f,-1.0f,-1.0f,-2.0f}

float DTL[] = {0.0f,-1.0f,1.0f+Grass_XChan,0.0f};
float DTR[] = {0.0f,1.0f,1.0f+Grass_XChan,0.0f};
float DBR[] = {0.0f,1.0f,-1.0f+Grass_XChan,0.0f};
float DBL[] = {0.0f,-1.0f,-1.0f+Grass_XChan,0.0f};
float DTL2[] = {0.0f,-1.0f,1.0f+Grass_XChan,-2.0f};
float DTR2[] = {0.0f,1.0f,1.0f+Grass_XChan,-2.0f};
float DBR2[] = {0.0f,1.0f,-1.0f+Grass_XChan,-2.0f};
float DBL2[] = {0.0f,-1.0f,-1.0f+Grass_XChan,-2.0f};

抱歉代码太长。如果你想看的话,这里是 Main.cpp。

#include <GL/glut.h>
#include <iostream>
#include <assert.h>
#include <fstream>
#include "block.h"
using namespace std;
float positionX = 0.0f;
float positionY = 0.0f;
float positionZ = -4.0f;
float angle = 0.0f;
float RotX=0.01f;
float RotY=0.0f;
float RotZ=0.0f;
float Grass_XChan=2.0f;
float Grass_ZChan=-2.0f;
bool Render = true;
Block Dirt;
Block Grass;
float TL[] = {0.0f,-1.0f,1.0f,0.0f};
float TR[] = {0.0f,1.0f,1.0f,0.0f};
float BR[] = {0.0f,1.0f,-1.0f,0.0f};
float BL[] = {0.0f,-1.0f,-1.0f,0.0f};
float TL2[] = {0.0f,-1.0f,1.0f,-2.0f};
float TR2[] = {0.0f,1.0f,1.0f,-2.0f};
float BR2[] = {0.0f,1.0f,-1.0f,-2.0f};
float BL2[] = {0.0f,-1.0f,-1.0f,-2.0f};

float DTL[] = {0.0f,-1.0f,1.0f+Grass_XChan,0.0f};
float DTR[] = {0.0f,1.0f,1.0f+Grass_XChan,0.0f};
float DBR[] = {0.0f,1.0f,-1.0f+Grass_XChan,0.0f};
float DBL[] = {0.0f,-1.0f,-1.0f+Grass_XChan,0.0f};
float DTL2[] = {0.0f,-1.0f,1.0f+Grass_XChan,-2.0f};
float DTR2[] = {0.0f,1.0f,1.0f+Grass_XChan,-2.0f};
float DBR2[] = {0.0f,1.0f,-1.0f+Grass_XChan,-2.0f};
float DBL2[] = {0.0f,-1.0f,-1.0f+Grass_XChan,-2.0f};
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(positionX, positionY, positionZ);
glPushMatrix();
glRotatef(angle,RotX,RotY,RotZ);
Dirt.RenderBlock("C:/Users/Progr_Anim/Desktop/textures/DirtBottom.bmp");
Grass.RenderBlock("C:/Users/Progr_Anim/Desktop/textures/GrassTop.bmp");

glPopMatrix();
glutSwapBuffers();
}
//-------------------------------------------------------------------------------------------------------------------------------------------
void HandleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}
void Camera(unsigned char key,int x,int y)
{
switch(key)
{
case 'w':
positionZ += 0.08f;
break;
case 's':
positionZ -= 0.08f;
break;
case 'a':
positionX += 0.08f;
break;
case 'd':
positionX -= 0.08f;
break;
case 'c':
if(angle>=360.0f)
{
angle=0.0f;
}
angle+=5.0f;
break;
case '1':
angle = 0.0f;
RotX=1.0f;
RotY=0.0f;
RotZ=0.0f;
break;
case '2':
angle = 0.0f;
RotX=0.0f;
RotY=1.0f;
RotZ=0.0f;
break;
case '3':
angle = 0.0f;
RotX=0.0f;
RotY=0.0f;
RotZ=1.0f;
break;
case 'q':
exit(0);
break;
}
}
int main(int argc, char** argv)
{
Dirt.SetVertices(TL,TR,BR,BL,TL2,TR2,BR2,BL2);
Grass.SetVertices(DTL,DTR,DBR,DBL,DTL2,DTR2,DBR2,DBL2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
//Create Window
glutInitWindowSize(640,480);
glutCreateWindow("Love");
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glutDisplayFunc(display);
glutReshapeFunc(HandleResize);
glutKeyboardFunc(Camera);
glutIdleFunc(display);
glutMainLoop();
return 0;
}

这是带有旋转立方体的视频,顶部是草方 block ,底部是泥土方 block 。 Link

最佳答案

我要赌一下这里的答案,因为我认为帖子中的信息不够。

这段代码中的数组被当作第 0 个元素不存在一样使用,并且使用的数组定义在前面有一个额外的零作为填充。

但是有些数组没有在此处的代码中定义,必须在别处声明,可能带有大小。我猜测这个大小对于 1 索引数组是错误的。

back_squarefront_square 可能声明如下:

float back_square[4][3];

而不是:

float back_square[5][4];

这就是它们的使用方式。

结果将是未定义的行为,在这种情况下可能是顶点的覆盖,折叠立方体的一个角。

无论哪种方式,我都强烈建议不要以这种方式使用数组,并习惯从零开始的事情。

关于c++ - OpenGL 错误。运行时立方体顶点位置错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107924/

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