C++ OpenGL网格错误位置

C++ OpenGL网格错误位置,c++,opengl,3d,sdl,glew,C++,Opengl,3d,Sdl,Glew,我正在尝试创建一个简单的三角形网格,在弄清楚为什么我刚得到一个blanc屏幕(由于某些原因,x64配置给我带来了问题)之后,我面临一个新问题: 我的顶点的位置不是我想要的。我不知道为什么。 我应该得到一个三角形,看起来像这样: 我的想法是: 我使用GLEW1.10.0加载OpenGL,GLM0.9.5.4加载OpenGL数学内容,SDL2.0.3加载窗口内容。在Visual Studio 2013 Ultimate的Windows 8.1上运行的所有内容都带有最新的Nvidia图形驱动程序

我正在尝试创建一个简单的三角形网格,在弄清楚为什么我刚得到一个blanc屏幕(由于某些原因,x64配置给我带来了问题)之后,我面临一个新问题:

我的顶点的位置不是我想要的。我不知道为什么。 我应该得到一个三角形,看起来像这样:

我的想法是:


我使用GLEW1.10.0加载OpenGL,GLM0.9.5.4加载OpenGL数学内容,SDL2.0.3加载窗口内容。在Visual Studio 2013 Ultimate的Windows 8.1上运行的所有内容都带有最新的Nvidia图形驱动程序


Main.cpp:

#include "Display.h"
#include "Shader.h"
#include "Mesh.h"

using namespace std;

int main(int argc, char** argv)
{
    Display display(1200, 800, "Hello World");

    Vertex vertecies[] =
    {
        Vertex(vec3(-0.5, -0.5, 0)),
        Vertex(vec3(0.5, -0.5, 0)),
        Vertex(vec3(0, 0.5, 0))
    };

    Mesh mesh(vertecies, sizeof(vertecies) / sizeof(vertecies[0]));
    Shader shader(".\\res\\BasicShader");

    while (!display.IsClosed())
    {
        display.Clear(1.0f, 1.0f, 1.0f, 1.0f);

        shader.Bind();
        mesh.Draw();

        display.Update();
    }

    return 0;
}
#include "Mesh.h"

Mesh::Mesh(Vertex* vertecies, unsigned int numVertecies)
{
    m_drawCount = numVertecies;

    glGenVertexArrays(1, &m_vertexArrayObject);
    glBindVertexArray(m_vertexArrayObject);

    glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies[0]) * numVertecies, vertecies, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, m_pos));

    glBindVertexArray(0);
}

Mesh::~Mesh()
{
    glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glDeleteVertexArrays(1, &m_vertexArrayObject);
}

void Mesh::Draw()
{
    glBindVertexArray(m_vertexArrayObject);

    glDrawArrays(GL_TRIANGLES, 0, m_drawCount);



    glBindVertexArray(0);
}
#include "Display.h"

Display::Display(int width, int height, string title)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
    m_glContext = SDL_GL_CreateContext(m_window);
    m_isClosed = false;

    GLenum status = glewInit();

    if (status != GLEW_OK)
        cerr << "Could not initialize GLEW!" << endl;
}

Display::~Display()
{
    SDL_GL_DeleteContext(m_glContext);
    SDL_DestroyWindow(m_window);
    SDL_Quit();
}

void Display::Clear(float r, float g, float b, float a)
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);
}

bool Display::IsClosed()
{
    return m_isClosed;
}

void Display::Update()
{
    SDL_GL_SwapWindow(m_window);

    SDL_Event e;

    while (SDL_PollEvent(&e))
    {
        if (e.type == SDL_QUIT)
            m_isClosed = true;
    }
}
#version 420 core

layout(location = 0) in vec3 position;

void main()
{
    gl_Position = vec4(position, 1.0);
}
#version 420 core

out vec4 frag;

void main()
{
    frag = vec4(1.0, 0.0, 0.0, 1.0);
}
#pragma once

#include <glm\glm.hpp>

using namespace glm;

struct Vertex
{

public:

    Vertex(const vec3& pos);
    virtual ~Vertex();

    vec3 m_pos;

};
#include "Vertex.h"

Vertex::Vertex(const vec3& pos)
{
    m_pos = pos;
}

Vertex::~Vertex()
{
}

Mesh.cpp:

#include "Display.h"
#include "Shader.h"
#include "Mesh.h"

using namespace std;

int main(int argc, char** argv)
{
    Display display(1200, 800, "Hello World");

    Vertex vertecies[] =
    {
        Vertex(vec3(-0.5, -0.5, 0)),
        Vertex(vec3(0.5, -0.5, 0)),
        Vertex(vec3(0, 0.5, 0))
    };

    Mesh mesh(vertecies, sizeof(vertecies) / sizeof(vertecies[0]));
    Shader shader(".\\res\\BasicShader");

    while (!display.IsClosed())
    {
        display.Clear(1.0f, 1.0f, 1.0f, 1.0f);

        shader.Bind();
        mesh.Draw();

        display.Update();
    }

    return 0;
}
#include "Mesh.h"

Mesh::Mesh(Vertex* vertecies, unsigned int numVertecies)
{
    m_drawCount = numVertecies;

    glGenVertexArrays(1, &m_vertexArrayObject);
    glBindVertexArray(m_vertexArrayObject);

    glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies[0]) * numVertecies, vertecies, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, m_pos));

    glBindVertexArray(0);
}

Mesh::~Mesh()
{
    glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glDeleteVertexArrays(1, &m_vertexArrayObject);
}

void Mesh::Draw()
{
    glBindVertexArray(m_vertexArrayObject);

    glDrawArrays(GL_TRIANGLES, 0, m_drawCount);



    glBindVertexArray(0);
}
#include "Display.h"

Display::Display(int width, int height, string title)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
    m_glContext = SDL_GL_CreateContext(m_window);
    m_isClosed = false;

    GLenum status = glewInit();

    if (status != GLEW_OK)
        cerr << "Could not initialize GLEW!" << endl;
}

Display::~Display()
{
    SDL_GL_DeleteContext(m_glContext);
    SDL_DestroyWindow(m_window);
    SDL_Quit();
}

void Display::Clear(float r, float g, float b, float a)
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);
}

bool Display::IsClosed()
{
    return m_isClosed;
}

void Display::Update()
{
    SDL_GL_SwapWindow(m_window);

    SDL_Event e;

    while (SDL_PollEvent(&e))
    {
        if (e.type == SDL_QUIT)
            m_isClosed = true;
    }
}
#version 420 core

layout(location = 0) in vec3 position;

void main()
{
    gl_Position = vec4(position, 1.0);
}
#version 420 core

out vec4 frag;

void main()
{
    frag = vec4(1.0, 0.0, 0.0, 1.0);
}
#pragma once

#include <glm\glm.hpp>

using namespace glm;

struct Vertex
{

public:

    Vertex(const vec3& pos);
    virtual ~Vertex();

    vec3 m_pos;

};
#include "Vertex.h"

Vertex::Vertex(const vec3& pos)
{
    m_pos = pos;
}

Vertex::~Vertex()
{
}

Display.cpp:

#include "Display.h"
#include "Shader.h"
#include "Mesh.h"

using namespace std;

int main(int argc, char** argv)
{
    Display display(1200, 800, "Hello World");

    Vertex vertecies[] =
    {
        Vertex(vec3(-0.5, -0.5, 0)),
        Vertex(vec3(0.5, -0.5, 0)),
        Vertex(vec3(0, 0.5, 0))
    };

    Mesh mesh(vertecies, sizeof(vertecies) / sizeof(vertecies[0]));
    Shader shader(".\\res\\BasicShader");

    while (!display.IsClosed())
    {
        display.Clear(1.0f, 1.0f, 1.0f, 1.0f);

        shader.Bind();
        mesh.Draw();

        display.Update();
    }

    return 0;
}
#include "Mesh.h"

Mesh::Mesh(Vertex* vertecies, unsigned int numVertecies)
{
    m_drawCount = numVertecies;

    glGenVertexArrays(1, &m_vertexArrayObject);
    glBindVertexArray(m_vertexArrayObject);

    glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies[0]) * numVertecies, vertecies, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, m_pos));

    glBindVertexArray(0);
}

Mesh::~Mesh()
{
    glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glDeleteVertexArrays(1, &m_vertexArrayObject);
}

void Mesh::Draw()
{
    glBindVertexArray(m_vertexArrayObject);

    glDrawArrays(GL_TRIANGLES, 0, m_drawCount);



    glBindVertexArray(0);
}
#include "Display.h"

Display::Display(int width, int height, string title)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
    m_glContext = SDL_GL_CreateContext(m_window);
    m_isClosed = false;

    GLenum status = glewInit();

    if (status != GLEW_OK)
        cerr << "Could not initialize GLEW!" << endl;
}

Display::~Display()
{
    SDL_GL_DeleteContext(m_glContext);
    SDL_DestroyWindow(m_window);
    SDL_Quit();
}

void Display::Clear(float r, float g, float b, float a)
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);
}

bool Display::IsClosed()
{
    return m_isClosed;
}

void Display::Update()
{
    SDL_GL_SwapWindow(m_window);

    SDL_Event e;

    while (SDL_PollEvent(&e))
    {
        if (e.type == SDL_QUIT)
            m_isClosed = true;
    }
}
#version 420 core

layout(location = 0) in vec3 position;

void main()
{
    gl_Position = vec4(position, 1.0);
}
#version 420 core

out vec4 frag;

void main()
{
    frag = vec4(1.0, 0.0, 0.0, 1.0);
}
#pragma once

#include <glm\glm.hpp>

using namespace glm;

struct Vertex
{

public:

    Vertex(const vec3& pos);
    virtual ~Vertex();

    vec3 m_pos;

};
#include "Vertex.h"

Vertex::Vertex(const vec3& pos)
{
    m_pos = pos;
}

Vertex::~Vertex()
{
}

片段着色器:

#include "Display.h"
#include "Shader.h"
#include "Mesh.h"

using namespace std;

int main(int argc, char** argv)
{
    Display display(1200, 800, "Hello World");

    Vertex vertecies[] =
    {
        Vertex(vec3(-0.5, -0.5, 0)),
        Vertex(vec3(0.5, -0.5, 0)),
        Vertex(vec3(0, 0.5, 0))
    };

    Mesh mesh(vertecies, sizeof(vertecies) / sizeof(vertecies[0]));
    Shader shader(".\\res\\BasicShader");

    while (!display.IsClosed())
    {
        display.Clear(1.0f, 1.0f, 1.0f, 1.0f);

        shader.Bind();
        mesh.Draw();

        display.Update();
    }

    return 0;
}
#include "Mesh.h"

Mesh::Mesh(Vertex* vertecies, unsigned int numVertecies)
{
    m_drawCount = numVertecies;

    glGenVertexArrays(1, &m_vertexArrayObject);
    glBindVertexArray(m_vertexArrayObject);

    glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies[0]) * numVertecies, vertecies, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, m_pos));

    glBindVertexArray(0);
}

Mesh::~Mesh()
{
    glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glDeleteVertexArrays(1, &m_vertexArrayObject);
}

void Mesh::Draw()
{
    glBindVertexArray(m_vertexArrayObject);

    glDrawArrays(GL_TRIANGLES, 0, m_drawCount);



    glBindVertexArray(0);
}
#include "Display.h"

Display::Display(int width, int height, string title)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
    m_glContext = SDL_GL_CreateContext(m_window);
    m_isClosed = false;

    GLenum status = glewInit();

    if (status != GLEW_OK)
        cerr << "Could not initialize GLEW!" << endl;
}

Display::~Display()
{
    SDL_GL_DeleteContext(m_glContext);
    SDL_DestroyWindow(m_window);
    SDL_Quit();
}

void Display::Clear(float r, float g, float b, float a)
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);
}

bool Display::IsClosed()
{
    return m_isClosed;
}

void Display::Update()
{
    SDL_GL_SwapWindow(m_window);

    SDL_Event e;

    while (SDL_PollEvent(&e))
    {
        if (e.type == SDL_QUIT)
            m_isClosed = true;
    }
}
#version 420 core

layout(location = 0) in vec3 position;

void main()
{
    gl_Position = vec4(position, 1.0);
}
#version 420 core

out vec4 frag;

void main()
{
    frag = vec4(1.0, 0.0, 0.0, 1.0);
}
#pragma once

#include <glm\glm.hpp>

using namespace glm;

struct Vertex
{

public:

    Vertex(const vec3& pos);
    virtual ~Vertex();

    vec3 m_pos;

};
#include "Vertex.h"

Vertex::Vertex(const vec3& pos)
{
    m_pos = pos;
}

Vertex::~Vertex()
{
}

Vertex.h:

#include "Display.h"
#include "Shader.h"
#include "Mesh.h"

using namespace std;

int main(int argc, char** argv)
{
    Display display(1200, 800, "Hello World");

    Vertex vertecies[] =
    {
        Vertex(vec3(-0.5, -0.5, 0)),
        Vertex(vec3(0.5, -0.5, 0)),
        Vertex(vec3(0, 0.5, 0))
    };

    Mesh mesh(vertecies, sizeof(vertecies) / sizeof(vertecies[0]));
    Shader shader(".\\res\\BasicShader");

    while (!display.IsClosed())
    {
        display.Clear(1.0f, 1.0f, 1.0f, 1.0f);

        shader.Bind();
        mesh.Draw();

        display.Update();
    }

    return 0;
}
#include "Mesh.h"

Mesh::Mesh(Vertex* vertecies, unsigned int numVertecies)
{
    m_drawCount = numVertecies;

    glGenVertexArrays(1, &m_vertexArrayObject);
    glBindVertexArray(m_vertexArrayObject);

    glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies[0]) * numVertecies, vertecies, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, m_pos));

    glBindVertexArray(0);
}

Mesh::~Mesh()
{
    glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glDeleteVertexArrays(1, &m_vertexArrayObject);
}

void Mesh::Draw()
{
    glBindVertexArray(m_vertexArrayObject);

    glDrawArrays(GL_TRIANGLES, 0, m_drawCount);



    glBindVertexArray(0);
}
#include "Display.h"

Display::Display(int width, int height, string title)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
    m_glContext = SDL_GL_CreateContext(m_window);
    m_isClosed = false;

    GLenum status = glewInit();

    if (status != GLEW_OK)
        cerr << "Could not initialize GLEW!" << endl;
}

Display::~Display()
{
    SDL_GL_DeleteContext(m_glContext);
    SDL_DestroyWindow(m_window);
    SDL_Quit();
}

void Display::Clear(float r, float g, float b, float a)
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);
}

bool Display::IsClosed()
{
    return m_isClosed;
}

void Display::Update()
{
    SDL_GL_SwapWindow(m_window);

    SDL_Event e;

    while (SDL_PollEvent(&e))
    {
        if (e.type == SDL_QUIT)
            m_isClosed = true;
    }
}
#version 420 core

layout(location = 0) in vec3 position;

void main()
{
    gl_Position = vec4(position, 1.0);
}
#version 420 core

out vec4 frag;

void main()
{
    frag = vec4(1.0, 0.0, 0.0, 1.0);
}
#pragma once

#include <glm\glm.hpp>

using namespace glm;

struct Vertex
{

public:

    Vertex(const vec3& pos);
    virtual ~Vertex();

    vec3 m_pos;

};
#include "Vertex.h"

Vertex::Vertex(const vec3& pos)
{
    m_pos = pos;
}

Vertex::~Vertex()
{
}


编辑:现在所有内容都已修复。

这可能是顶点类的数据对齐问题。然后OpenGL会将填充字节解释为有效数据

您可以通过打印
sizeof(Vertex)
的结果来验证这一点,如果确实对其进行了填充,则结果为8(您提到的是64平台)

这告诉OpenGL内存中有紧密压缩的浮动,没有填充:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
设置顶点指针的更好方法是:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, m_pos));

这也使您能够轻松添加更多属性。

顶点类/结构是如何布局的?你能发布它的声明吗?@Kollabi当然,只是添加了Vertex.h和Vertex.cpp…你不应该修改你的答案谢谢你的快速回答!终于成功了!唯一的问题是,
offsetof
需要两个参数,一个类型和一个类型的成员,所以我把我的顶点类变成了一个结构,将
m_pos
公开,并编写了
(const void*)offsetof(Vertex,m_pos)
而不是
(const void*)offsetof(Vertex.m_pos)
。啊,是的,对不起,这是我的一个错误。