是什么导致了我的OpenGL游戏的平铺渲染中的奇怪效果?

yx2lnoni  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(119)

我正在用OpenGL制作一个游戏,我想用小方块来制作世界。每个图块都是一个数组中的std::map,它存储图块的颜色。游戏的每一帧都会循环通过数组并绘制所有的瓦片。
然而,当试图绘制瓷砖时,它会产生奇怪的效果:

这就是代码:

/*
 * GL01Hello.cpp: Test OpenGL C/C++ Setup
 */
#include <iostream>
#include <windows.h>  // For MS Windows
#include <GL/glew.h>
#include <GL/glut.h>  // GLUT, includes glu.h and gl.h
   // has to be included before gl.h, or any header that includes gl.h
#include <GL/freeglut.h>'
#include "pixel.cpp"
/* Handler for window-repaint event. Call back when the window first appears and
   whenever the window needs to be re-painted. */
float X = 0;
//creates the game loop
void Timer(int) {
    //std::cout << "No need to store this string";
    X = X + 0.001;
    glutPostRedisplay();
    glutTimerFunc(15,Timer,0);
}

//draws the graphics
void display(void) {
    glClearColor(0.25,0.25,1,0);
    glClear(GL_COLOR_BUFFER_BIT);
    DrawPixels();
   glutSwapBuffers();
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
    createLevel();
   glutInit(&argc, argv);
   int winW = glutGet(GLUT_WINDOW_WIDTH);
   int winH = glutGet(GLUT_WINDOW_HEIGHT);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(winW, winH);
   glutInitWindowPosition(100, 100);
   glutCreateWindow("OpenGL Setup Test");
   glutFullScreen();
   glutDisplayFunc(display);
   glutTimerFunc(15,Timer,0);
   glutMainLoop();
   return 0;
}
#include <map>

//gets the data needed
GLfloat PixelWidth = 0.005;
GLfloat PixelHeight = 0.005;
GLint columbAmount = 2/PixelWidth;
GLint rowAmount = 2/PixelHeight;

//array of tiles
std::map<std::string,GLfloat> level[400][400];

//generates array of little sqaures
void createLevel() {
    //color of top row
    GLfloat colorIntensity = 1;
    //how much the color changes per row
    GLfloat DeltaColorIntensity = 0.0025;
    //2 for loops to add the tiles to the array
    for(int i = 0;i < columbAmount/2;i++) {
        for(int j = 0;j < rowAmount;j++) {
            level[i+200][j]["color"] = colorIntensity;
    }
    //subtracts from the color variable after row is complete to create a gradiant
    colorIntensity = colorIntensity - DeltaColorIntensity;
}
}
//draws them onscreen
void DrawPixels(void) {
    //loops through the array
    for(int i = 0;i < columbAmount/2;i++) {
        for(int j = 0;j < rowAmount;j++){
        //calculates the tiles x and y position
        GLfloat CalculateX = j*PixelWidth;
        GLfloat CalculateY = i*PixelHeight;
        //sets the tile to the correct color
        glColor3f(level[i + 200][j]["color"],level[i+200][j]["color"],0);
        //draws the squares
            glBegin(GL_POLYGON);
                glVertex3f(-1 + CalculateX, 0 - CalculateY , 0.0);
                glVertex3f(-0.995 + CalculateX, 0.0, 0.0);
                glVertex3f(-0.995 + CalculateX, 0.005 - CalculateY, 0.0);
                glVertex3f(0.0, 0.005 - CalculateY, 0.0);
            glEnd();
        }

    }
}

我希望瓷砖有一个干净的梯度,其中每一行瓷砖是黑暗,然后前一个,但它是创造这种奇怪的外观

b4wnujal

b4wnujal1#

对于绘制的每个平铺,八个坐标中有两个是恒定的:

glVertex3f(-1 + CalculateX, 0 - CalculateY , 0.0);
glVertex3f(-0.995 + CalculateX, 0.0, 0.0);           // <---- Y is constant
glVertex3f(-0.995 + CalculateX, 0.005 - CalculateY, 0.0);
glVertex3f(0.0, 0.005 - CalculateY, 0.0);            // <---- X is constant

这会导致绘制一些扭曲的多边形,而不是矩形平铺。您应该通过正确计算坐标来修复它。我通常会这样做:

// calculate the tile top-left and bottom-right corners:
GLfloat x0 = -1 + j*PixelWidth, y0 = -i*PixelHeight;
GLfloat x1 = -1 + (j+1)*PixelWidth, y1 = -(i+1)*PixelHeight;

然后,您可以在glVertex调用中巧妙地使用它们(这里按CCW顺序):

glVertex2f(x0, y0);
glVertex2f(x0, y1);
glVertex2f(x1, y1);
glVertex2f(x1, y0);

相关问题