Question

I am trying to create a simple triangle mesh and after figuring out why I just got a blanc screen (for some reason the x64 configuration was giving me problems) I am facing a new issue:

The position of my vertices don't turn out to be how I want them. And I have no idea why. What I should be getting is a triangle which looks about like this: enter image description here

What I am getting though is this:

enter image description here


I use GLEW 1.10.0 for loading OpenGL, GLM 0.9.5.4 for OpenGL math stuff and SDL 2.0.3 for window stuff. Everything running on Windows 8.1 in Visual Studio 2013 Ultimate with the latest Nvidia graphics drivers.


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;
}


Mesh.cpp:

#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);
}


Display.cpp:

#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;
    }
}


Vertex Shader:

#version 420 core

layout(location = 0) in vec3 position;

void main()
{
    gl_Position = vec4(position, 1.0);
}


Fragment Shader:

#version 420 core

out vec4 frag;

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


Vertex.h:

#pragma once

#include <glm\glm.hpp>

using namespace glm;

struct Vertex
{

public:

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

    vec3 m_pos;

};


Vertex.cpp:

#include "Vertex.h"

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

Vertex::~Vertex()
{
}


EDIT: Everything has now been fixed.

Was it helpful?

Solution

This is probably a data alignment issue where your Vertex class is being padded. OpenGL would then interpret the padding bytes as valid data.

You can verify this by printing the result of sizeof(Vertex) which would be 8 (you mention a 64 platform) if it is indeed padded.

This tells OpenGL that there are floats tightly packed in memory, without padding:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

A better way of setting the vertex pointer would be:

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

This also enables you to easily add more attributes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top