Block terrain 2d [已关闭]

Block terrain 2d [closed]

提问人:Diamant3500 提问时间:11/14/2023 最后编辑:genpfaultDiamant3500 更新时间:11/14/2023 访问量:57

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

8天前关闭。

我想像这张照片一样制作一个方块地形(当然没有阴影)。

我试着自己做,但没有用:

#include <glew.h>
#define GLEW_STATIC
#include <glfw3.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "linmath.h"
#include "stb_image.cpp"
#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat2x3.hpp>
#include <glm/mat2x4.hpp>
#include <glm/mat3x2.hpp>
#include <glm/mat3x3.hpp>
#include <glm/mat3x4.hpp>
#include <glm/mat4x2.hpp>
#include <glm/mat4x3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vector_relational.hpp>
#include <glm/integer.hpp>
#include <glm/trigonometric.hpp>
#include <glm/packing.hpp>
#include <glm/matrix.hpp>
#include <glm/geometric.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <imgui-master/imgui.h>
#include <stdio.h>
#include <vector>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <map>

GLuint programm;

const char* VertexShaderSource = R"(
    #version 460 core
    layout(location=0)in vec3 aPos;
    uniform mat4 view;
    uniform mat4 trans;
    uniform mat4 model;
    void main()
    {
        gl_Position = trans * view * model *vec4(aPos, 1.0);
    }
)";

const char* FragmentShaderSource = R"(
    #version 460 core
    uniform vec3 color;
    out vec4 facecolor;
    void main()
    {
        facecolor = vec4(color, 1.0);
    }
)";

struct Cube {
    glm::vec3 position;
    glm::vec3 size;
};
std::vector< Cube >cubes;

void Createblock(glm::vec3 position, glm::vec3 size)
{
    GLfloat vertices[] =
    {
        -0.5, -0.5, 0.0,
        0.5, -0.5, 0.0,
        -0.5, 0.5, 0.0,

        -0.5, 0.5, 0.0,
        0.5, 0.5, 0.0,
        0.5, -0.5, 0.0
    };
    GLuint VBO, VAO;
    glGenBuffers(1, &VBO);
    glGenVertexArrays(1, &VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBindVertexArray(VAO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);
    glEnableVertexAttribArray(0);

    glm::mat4 view = glm::mat4(1.0);
    glm::mat4 trans = glm::mat4(1.0);
    glm::mat4 model = glm::mat4(1.0);

    trans = glm::perspective(glm::radians(45.0f), 1920.0f / 1080.0f, 0.1f, 100.0f);
    model = glm::scale(model, size);
    model = glm::translate(model, position);
    view = glm::translate(view, glm::vec3(0.0, 0.0, -3.0));

    GLuint colorLoc = glGetUniformLocation(programm, "color");
    glUniform3f(colorLoc, 0, 1, 0);
    GLuint transLoc = glGetUniformLocation(programm, "trans");
    glUniformMatrix4fv(transLoc, 1, GL_FALSE, glm::value_ptr(trans));
    GLuint viewLoc = glGetUniformLocation(programm, "view");
    glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
    GLuint modelLoc = glGetUniformLocation(programm, "model");
    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

    glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices));
}

int main()
{
    glfwInit();
    GLFWwindow* window = glfwCreateWindow(1920, 1080, "TEST", NULL, NULL);
    if (!window)
    {
        std::cout << "Failed to create window!" << std::endl;
        return -1;
    }
    glfwMakeContextCurrent(window);
    glewInit();
    GLuint vertexshader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexshader, 1, &VertexShaderSource, NULL);
    glCompileShader(vertexshader);

    GLint succes;
    char infolog[512];
    glGetShaderiv(vertexshader, GL_COMPILE_STATUS, &succes);
    if (!succes)
    {
        glGetShaderInfoLog(vertexshader, 512, 0, infolog);
        std::cerr << infolog << std::endl;
    }

    GLuint fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentshader, 1, &FragmentShaderSource, NULL);
    glCompileShader(fragmentshader);

    glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &succes);
    if (!succes)
    {
        glGetShaderInfoLog(fragmentshader, 512, 0, infolog);
        std::cerr << infolog << std::endl;
    }

    programm = glCreateProgram();
    glAttachShader(programm, vertexshader);
    glAttachShader(programm, fragmentshader);
    glLinkProgram(programm);

    int index2{};
    for (float x{}; x < 1; ++x)
    {
        for (float y{}; y < 15; ++y)
        {
            for (int index{}; index < 16; index++)
            {
                if (index2 > 15)
                {

                }
                else
                {
                    Cube newCube{};
                    newCube.position = glm::vec3(x, y, 0);
                    newCube.size = glm::vec3(0.1, 0.1, 0.0);
                    cubes.push_back(newCube);
                    index2++;
                }
            }
        }
        
    }
    std::cout << index2 << std::endl;
    glViewport(0, 0, 1920, 1080);
    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(programm);
        for (Cube& cube2 : cubes)
        {
            Createblock(
                cube2.position,
                cube2.size
            );
        }
        glClearColor(0.0, 0.0, 1.0, 1.0);
        glfwPollEvents();
        glfwSwapBuffers(window);
    }
    return 0;
}

我期望有一个方块的地形。

C++ OpenGL 2D GLM-数学 地形

评论

4赞 463035818_is_not_an_ai 11/14/2023
“didn't work” 是什么意思?
0赞 Diamant3500 11/14/2023
463035818_is_not_an_ai我的意思是,有一次它只显示了一个立方体,有一次它向我展示了一个有孔的大立方体(对不起,这个糟糕的解释)

答: 暂无答案