OpenGL显示空白白屏

OpenGL显示空白白屏

我试图将openGL函数抽象到它们自己的类中。VertexBuffer和IndexBuffer班工作过。但是在尝试抽象VertexArray类之后。它不起作用,我似乎不能锻炼什么是问题所在。我提供了3个文件,主要应用程序文件,缓冲区类文件和VertexArray类文件。我遗漏了一些函数和一个文件,我认为这是不必要的

我试过用VS-2019进行调试。我在VS-2017中创建了这个项目,但是升级了。直到glEnableVertexAttribArray起作用为止,我已经检查了_Elem的值,它们看起来都是正确的。但我看不见三角形。但是,当我尝试在原始函数中执行时,它可以工作。

Main.cpp:

代码语言:javascript运行复制#include "Buffer.h"

#include "VertexArray.h"

int main(void)

{

GLFWwindow* window;

/* Initialize the library */

if (!glfwInit())

return -1;

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);

/* Create a windowed mode window and its OpenGL context */

window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);

if (!window)

{

glfwTerminate();

return -1;

}

/* Make the window's context current */

glfwMakeContextCurrent(window);

if (glewInit() != GLEW_OK)

{

glfwTerminate();

return -1;

}

glEnable(GL_DEBUG_CALLBACK_FUNCTION);

glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);

glDebugMessageCallback(GLErrLogger, nullptr);

{

float pos[] = {

0.0f, 0.5f,

0.5f, -0.5f,

-0.5f, -0.5f

};

unsigned int indices[] = {

0, 1, 2

};

// Some Shader stuff

VertexBuffer vb(_BufferDataParams(pos));

BufferLayout bLayout;

bLayout.Push(2);

//RefID va;

//glGenVertexArrays(1, &va);

//vb.BindSelf();

//glBindVertexArray(va);

VertexArray va(vb, bLayout);

//glEnableVertexAttribArray(0);

//glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);

IndexBuffer ib(_BufferDataParams(indices));

glUseProgram(shader);

GLint location = glGetUniformLocation(shader, "u_Color");

if (location != -1)

glUniform4f(location, 0.2f, 0.3f, 0.8f, 1.0f);

else

std::cerr << "u_Color doesn't exists.\n";

/* Loop until the user closes the window */

while (!glfwWindowShouldClose(window))

{

/* Render here */

glClear(GL_COLOR_BUFFER_BIT);

glDrawElements(GL_TRIANGLES, ib.GetCount(), ib.GetDataType(), nullptr);

/* Swap front and back buffers */

glfwSwapBuffers(window);

/* Poll for and process events */

glfwPollEvents();

}

glDeleteProgram(shader);

}

glfwTerminate();

return 0;

}布法尔

代码语言:javascript运行复制#pragma once

#define _LenArr(_Arr) std::extent::value

#define _BufferDataParams(_Arr) _Arr, sizeof(_Arr), _LenArr(_Arr)

using GLTYPE = unsigned int;

using Countable = unsigned int;

using LayoutElem = struct {

GLTYPE Type;

unsigned int Count;

size_t ElemSize;

GLTYPE Normalized;

};

struct BufferLayout

{

private:

std::vector Layout;

Countable Stride;

public:

BufferLayout() : Stride(0) {}

template

void Push(Countable Count, GLTYPE normalized = GL_FALSE)

{

Layout.push_back({ _GL_Ty, Count, _ElemSize, normalized });

Stride += Count * _ElemSize;

}

inline const std::vector& GetElements() const { return Layout; }

inline Countable GetStride() const { return Stride; }

};

template

class Buffer

{

private:

unsigned int mRefID;

size_t m_Count;

GLTYPE mDataType;

public:

Buffer(const void* data, size_t size, size_t count,

GLTYPE dataType = _DataType, unsigned int usage = GL_STATIC_DRAW)

: mRefID(0), m_Count(count), mDataType(dataType)

{

glGenBuffers(1, &mRefID);

glBindBuffer(TYPE, mRefID);

glBufferData(TYPE, size, data, usage);

}

inline unsigned int GetRefID() const { return mRefID; }

inline size_t GetCount() const { return m_Count; }

inline GLTYPE GetDataType() const { return mDataType; }

void SetData(const void* data, size_t size, size_t count,

GLTYPE dataType = _DataType, unsigned int usage = GL_STATIC_DRAW)

{

glBindBuffer(TYPE, mRefID);

glBufferData(TYPE, size, data, usage);

m_Count = count;

mDataType = dataType;

}

inline static void Bind(const Buffer& buf)

{ glBindBuffer(TYPE, buf.GetRefID()); }

void BindSelf() const { Bind(*this); }

inline static void Unbind() { glBindBuffer(TYPE, 0); }

void Delete() const { glDeleteBuffers(1, &mRefID); }

~Buffer() { glDeleteBuffers(1, &mRefID);}

};

using VertexBuffer = Buffer;

using IndexBuffer = Buffer;VertexArray.cpp:

代码语言:javascript运行复制#include "stdafx.h"

#include "VertexArray.h"

VertexArray::VertexArray()

: mRefID(0)

{ glGenVertexArrays(1, &mRefID);}

void VertexArray::Bind(const VertexArray& vArr)

{ glBindVertexArray(vArr.GetRefID()); }

void VertexArray::BindSelf() const { Bind(*this); }

void VertexArray::AddBuffer(const VertexBuffer& vBuf, const BufferLayout& bufLayout)

{

BindSelf();

vBuf.BindSelf();

const auto& Layout = bufLayout.GetElements();

Countable i = 0;

size_t offset = 0;

for (LayoutElem _Elem : Layout)

{

std::cout << _Elem.Count << " " << _Elem.Type << " " <<

_Elem.Normalized << " " << bufLayout.GetStride();

glEnableVertexAttribArray(i);

glVertexAttribPointer(i, _Elem.Count, _Elem.Type, _Elem.Normalized,

bufLayout.GetStride(), (const void*)&offset);

offset += _Elem.Count * _Elem.ElemSize;

i++;

}

}

VertexArray::VertexArray(const VertexBuffer& vBuf, const BufferLayout& bufLayout)

: mRefID(0)

{

glGenVertexArrays(1, &mRefID);

AddBuffer(vBuf, bufLayout);

}

void VertexArray::Unbind() { glBindVertexArray(0); }

inline void VertexArray::Delete() const { glDeleteVertexArrays(1, &mRefID); }

VertexArray::~VertexArray() { Delete(); }我希望看到一个带蓝色的三角形。为了让抽象概念发挥作用。

🎈 相关推荐

为什么会发生地震?
bat365手机版官网

为什么会发生地震?

📅 07-26 👀 3704
信鸽简笔画
bt365最新网址

信鸽简笔画

📅 10-20 👀 481
膘字的意思
bt365最新网址

膘字的意思

📅 09-19 👀 1433