Треугольники не отображаются должным образом из-за плохих индексов [закрыто]C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Треугольники не отображаются должным образом из-за плохих индексов [закрыто]

Сообщение Anonymous »

Я пытаюсь создать 2D-сетку Perlin Noise из вершин и индексов, но не могу понять, как правильно соединить треугольники из каждой точки.
Я знаю, что это так. вероятно, проблема с моим кодом для генерации индексов, а не вершин, поскольку при использовании glPolygonMode(GL_POINT) вершины выглядят так, как будто они расположены правильно. Однако треугольники выглядят очень необычно.

Код: Выделить всё

...
const int sizeOfMesh = 25;

// indices at the edge will be reused twice
// the rest will be used six times
const int sizeOfIndices = (sizeOfMesh*4) + (sizeOfMesh-2)*(sizeOfMesh-2)*6;
...

#include 
#include 

#define STB_IMAGE_IMPLEMENTATION
#include 

#include 
#include 
#include 

#include 

#include 
#include 
#include 

#include "PerlinNoise.h"

float vertices[] = {
// positions front   // colors           // texture coords
0.5f,  0.5f,-0.5f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
0.5f, -0.5f,-0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
-0.5f, -0.5f,-0.5f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
-0.5f,  0.5f,-0.5f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f,    // top left
// positions left    // colors           // texture coords
-0.5f,  0.5f,-0.5f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
-0.5f, -0.5f,-0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
-0.5f, -0.5f, 0.5f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
-0.5f,  0.5f, 0.5f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f,    // top left
// positions right   // colors           // texture coords
0.5f,  0.5f, 0.5f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
0.5f, -0.5f, 0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
0.5f, -0.5f,-0.5f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
0.5f,  0.5f,-0.5f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f,    // top left
// positions top     // colors           // texture coords
0.5f,  0.5f, 0.5f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
0.5f,  0.5f,-0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
-0.5f,  0.5f,-0.5f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
-0.5f,  0.5f, 0.5f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f,    // top left
// positions bottom  // colors           // texture coords
0.5f, -0.5f, 0.5f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
0.5f, -0.5f,-0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
-0.5f, -0.5f,-0.5f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
-0.5f, -0.5f, 0.5f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f,    // top left
// positions back    // colors           // texture coords
-0.5f,  0.5f, 0.5f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
-0.5f, -0.5f, 0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
0.5f, -0.5f, 0.5f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
0.5f,  0.5f, 0.5f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f,    // top left

};

unsigned int indices[] = {
// front
0, 1, 3,
1, 2, 3,

// left
4, 5, 7,
5, 6, 7,

// right
8, 9, 11,
9, 10, 11,

// top
12, 13, 15,
13, 14, 15,

// bottom
16, 17, 19,
17, 18, 19,

// back
20, 21, 23,
21, 22, 23,
};

const int sizeOfMesh = 10;

// indices at the edge will be used once
// the rest will be used four times
const int sizeOfIndices = 6*(sizeOfmesh-1)*(sizeOfMesh-1);

enum class ShaderType {
Vertex,
Fragment,
ShaderProgram
};

struct MeshData {
float vertices[sizeOfMesh*3];
unsigned int indices[sizeOfIndices*6];
};

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void HandleShaderCompilerErrors(unsigned int shader,  ShaderType shaderType);
void RenderLoop(GLFWwindow* window);
void Initialize();
MeshData GenerateMesh();
void ProcessInput(GLFWwindow*);

char* GetCStringFromFilePath(const char* file_path_c_string);

bool isWireframeRenderer = false;

unsigned int EBO;
unsigned int VBO;
unsigned int VAO;

unsigned int fragmentShaderID;
unsigned int vertexShaderID;
unsigned int shaderProgram;
unsigned int texture1;
unsigned int texture2;

char *vertexShaderSource;
char *fragmentShaderSource;

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

glm::vec3 cameraPos;
glm::vec3 cameraUp;
glm::vec3 cameraFront;

float mouseSensitivity = 0.1f;

int lastXPos, lastYPos = 0;

float yaw = -90.0f, pitch = 0.0f;

int main() {
// config global variables
cameraPos    = glm::vec3(0.0f, 0.0f, 3.0f);
cameraUp     = glm::vec3(0.0f, 1.0f, 0.0f);
cameraFront  = glm::vec3(0.0f, 0.0f, -1.0f);

// configure stbi for OpenGL
// most images have (0, 0) located at the top left
// but OpenGL for some reason has it located at the bottom left
stbi_set_flip_vertically_on_load(true);

// initialize GLFW (OpenGL 3.3)

if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);

#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

// create window object and check if done successfully
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "LearnOpenGL", NULL, NULL);

if (window == nullptr) {
std::cout 

Подробнее здесь: [url]https://stackoverflow.com/questions/78351351/triangles-not-rendering-properly-due-to-bad-indices[/url]
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»