Commit e92ee1a2 authored by Håkon Berg Borhaug's avatar Håkon Berg Borhaug
Browse files

Merge branch 'revert-8c8bf051' into 'master'

Revert "Started implementing use of vertex buffers"

See merge request hbo038/DTE_3609!1
parents 8c8bf051 11529594
......@@ -38,11 +38,6 @@ set( INCLUDE
include/Snow.hpp
include/Text.hpp
include/Minimap.hpp
include/IndexBuffer.h
include/Shader.h
include/VertexArray.h
include/VertexBuffer.h
include/VertexBufferLayout.h
include/GLErrorHandler.h
include/stb_image.h
......@@ -56,10 +51,6 @@ set( SRCS
src/Snow.cpp
src/Text.cpp
src/Minimap.cpp
src/IndexBuffer.cpp
src/Shader.cpp
src/VertexArray.cpp
src/VertexBuffer.cpp
src/GLErrorHandler.cpp
src/stb_image.cpp
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.15.2, 2022-05-05T16:25:40. -->
<!-- Written by QtCreator 4.15.2, 2022-04-30T16:14:05. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......@@ -80,9 +80,6 @@
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
<valuemap type="QVariantMap" key="CppEditor.QuickFix">
<value type="bool" key="UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data>
......
#pragma once
#include <GL/glew.h>
#include <iostream>
#include <GL/glew.h>
#define ASSERT(x) if (!(x)) __debugbreak();
#define GLCall(x) GLClearError();\
x;\
......
#pragma once
class IndexBuffer {
private:
unsigned int m_RendererID;
unsigned int m_Count;
public:
IndexBuffer(const unsigned int* data, unsigned int count);
~IndexBuffer();
void Bind() const;
void Unbind() const;
inline unsigned int GetCount() const { return m_Count; }
};
\ No newline at end of file
#pragma once
#include "../include/VertexArray.h"
#include "../include/VertexBuffer.h"
#include "../include/VertexBufferLayout.h"
#include "../include/IndexBuffer.h"
#include <windows.h>
#include <GL/freeglut.h>
#include <GL/gl.h>
......@@ -21,8 +16,8 @@ class Landscape : public SceneObject
protected:
virtual void privateInit();
virtual void privateRender();
virtual void privateUpdate();
virtual void privateRender();
virtual void privateUpdate();
private:
std::vector< glm::vec3 > vertexArray_; // Maybe two-dim vector and several arrays
......@@ -30,16 +25,5 @@ class Landscape : public SceneObject
std::vector< glm::vec3 > normalArray_;
// texture coord array
std::vector< glm::vec3 > textureArray_;
unsigned int indices_[12] = {
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11
};
VertexArray vao;
VertexBuffer vbo;
VertexBufferLayout layout;
IndexBuffer ibo;
};
......@@ -3,10 +3,8 @@
#include <windows.h>
#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include "SceneObject.hpp"
#include <GL/gl.h>
class Minimap : public SceneObject {
public:
......
#pragma once
#include <string>
#include <unordered_map>
#include "../../glm-master/glm/glm.hpp"
struct ShaderProgramSource {
std::string VertexSource;
std::string FragmentSource;
};
class Shader {
private:
std::string m_FilePath;
unsigned int m_RendererID;
std::unordered_map<std::string, int> m_UniformLocationCache;
public:
Shader(const std::string& filepath);
~Shader();
void Bind() const;
void Unbind() const;
// Set uniforms
void SetUniform1i(const std::string& name, int value);
void SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3);
void SetUniformMat4f(const std::string& name, const glm::mat4& matrix);
private:
int GetUniformLocation(const std::string& name);
unsigned int CompileShader(unsigned int type, const std::string& source);
ShaderProgramSource ParseShader(const std::string& filepath);
unsigned int CreateShader(const std::string& vertexShaderCode, const std::string& fragmentShaderCode);
};
#pragma once
#include "VertexBuffer.h"
#include "VertexBufferLayout.h"
class VertexArray {
private:
unsigned int m_RendererID;
public:
VertexArray();
~VertexArray();
void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout);
void Bind() const;
void Unbind() const;
};
\ No newline at end of file
#pragma once
class VertexBuffer {
private:
unsigned int m_RendererID;
public:
VertexBuffer(const void* data, unsigned int size);
~VertexBuffer();
void Bind() const;
void Unbind() const;
};
\ No newline at end of file
#pragma once
#include <vector>
//#include <GL/glew.h>
#include "GLErrorHandler.h"
struct VertexBufferElement {
unsigned int type;
unsigned int count;
unsigned char normalized;
static unsigned int GetSizeOfType(unsigned int type) {
switch (type)
{
case GL_FLOAT:
return 4;
break;
case GL_UNSIGNED_INT:
return 4;
break;
case GL_UNSIGNED_BYTE:
return 1;
break;
default:
ASSERT(false);
return 0;
break;
}
}
};
class VertexBufferLayout {
private:
std::vector<VertexBufferElement> m_Elements;
unsigned int m_Stride;
public:
VertexBufferLayout()
: m_Stride(0) {}
template<typename T>
void Push(unsigned int count) {
//static_assert(false);
}
template<>
void Push<float>(unsigned int count) {
m_Elements.push_back({ GL_FLOAT, count, GL_FALSE });
m_Stride += VertexBufferElement::GetSizeOfType(GL_FLOAT) * count;
}
template<>
void Push<unsigned int>(unsigned int count) {
m_Elements.push_back({ GL_UNSIGNED_INT, count, GL_FALSE });
m_Stride += VertexBufferElement::GetSizeOfType(GL_UNSIGNED_INT) * count;
}
template<>
void Push<unsigned char>(unsigned int count) {
m_Elements.push_back({ GL_UNSIGNED_BYTE, count, GL_TRUE });
m_Stride += VertexBufferElement::GetSizeOfType(GL_UNSIGNED_BYTE) * count;
}
inline unsigned int GetStride() const { return m_Stride; }
inline const std::vector<VertexBufferElement> GetElements() const { return m_Elements; }
};
......@@ -4,7 +4,7 @@ void GLClearError() {
while (glGetError() != GL_NO_ERROR) {
}
}
};
bool GLLogCall(const char* function, const char* file, int line) {
while (GLenum error = glGetError()) {
......@@ -13,4 +13,4 @@ bool GLLogCall(const char* function, const char* file, int line) {
}
return true;
}
};
#include "../include/IndexBuffer.h"
#include "../include/GLErrorHandler.h"
IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count)
: m_Count(count)
{
ASSERT(sizeof(unsigned int) == sizeof(GLuint));
GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID));
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW));
}
IndexBuffer::~IndexBuffer()
{
GLCall(glDeleteBuffers(1, &m_RendererID));
}
void IndexBuffer::Bind() const
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID));
}
void IndexBuffer::Unbind() const
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
......@@ -33,14 +33,6 @@ void Landscape::privateInit()
vertexArray_.push_back(glm::vec3(1000.0f, -1000.0f, -500.0f));
vertexArray_.push_back(glm::vec3(1000.0f, 1000.0f, -500.0f));
vertexArray_.push_back(glm::vec3(-1000.0f, 1000.0f, -500.0f));
vbo = VertexBuffer(vertexArray_.data(), vertexArray_.size() * 3 * sizeof(float));
layout.Push<float>(3);
vao.AddBuffer(vbo, layout);
// 4 points in a quad, 3 quads in total
ibo = IndexBuffer(indices_, 4 * 3);
}
void Landscape::privateRender()
......
......@@ -3,9 +3,16 @@
#include <GL/glut.h>
#include <GL/gl.h>
#include "../include/Minimap.hpp"
#include "../include/GLErrorHandler.h"
//GLuint g_fbo; // frame buffer object
//GLuint g_pbo; // pixel buffer object
//GLuint g_texture; // texture object
//int g_iTexWidth = 512; // texture width
//int g_iTexHeight = 512; // texture height
Minimap::Minimap() {
}
......
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "../include/Shader.h"
#include "../include/GLErrorHandler.h"
Shader::Shader(const std::string& filepath)
: m_FilePath(filepath), m_RendererID(0)
{
ShaderProgramSource source = ParseShader(filepath);
m_RendererID = CreateShader(source.VertexSource, source.FragmentSource);
}
Shader::~Shader()
{
GLCall(glDeleteProgram(m_RendererID));
}
void Shader::Bind() const
{
GLCall(glUseProgram(m_RendererID));
}
void Shader::Unbind() const
{
GLCall(glUseProgram(0));
}
void Shader::SetUniform1i(const std::string& name, int value)
{
GLCall(glUniform1i(GetUniformLocation(name), value));
}
void Shader::SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3)
{
GLCall(glUniform4f(GetUniformLocation(name), v0, v1, v2, v3));
}
void Shader::SetUniformMat4f(const std::string& name, const glm::mat4& matrix)
{
GLCall(glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, &matrix[0][0]));
}
int Shader::GetUniformLocation(const std::string& name)
{
if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end()) {
return m_UniformLocationCache[name];
}
GLCall(int location = glGetUniformLocation(m_RendererID, name.c_str()));
if (location == -1) {
std::cout << "Warning: uniform " << name << " does not exist!" << std::endl;
}
m_UniformLocationCache[name] = location;
return location;
}
ShaderProgramSource Shader::ParseShader(const std::string& filepath) {
std::ifstream stream(filepath);
enum class ShaderType {
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream stringStream[2];
ShaderType type = ShaderType::NONE;
while (getline(stream, line)) {
if (line.find("shader") != std::string::npos) {
if (line.find("vertex") != std::string::npos) {
type = ShaderType::VERTEX;
}
else if (line.find("fragment") != std::string::npos) {
type = ShaderType::FRAGMENT;
}
}
else if (line.find("//") == std::string::npos) {
stringStream[(int)type] << line << '\n';
}
}
return { stringStream[0].str(), stringStream[1].str() };
}
unsigned int Shader::CompileShader(unsigned int type, const std::string& source) {
// Get shader source code in string format and try to compile it
GLCall(unsigned int id = glCreateShader(type));
const char* src = source.c_str();
GLCall(glShaderSource(id, 1, &src, nullptr));
GLCall(glCompileShader(id));
// Get compile result
int result;
GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
// Print potential compile errors
if (result == GL_FALSE) {
int length;
GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
char* message = (char*)alloca(length * sizeof(char));
GLCall(glGetShaderInfoLog(id, length, &length, message));
std::cout << (type == GL_VERTEX_SHADER ? "Vertex" : "Fragment") << " shader compilation error." << std::endl;
std::cout << message << std::endl;
GLCall(glDeleteShader(id));
return 0;
}
return id;
}
unsigned int Shader::CreateShader(const std::string& vertexShaderCode, const std::string& fragmentShaderCode) {
// Create empty program and compile shader source code
GLCall(unsigned int program = glCreateProgram());
unsigned int vertexShader = CompileShader(GL_VERTEX_SHADER, vertexShaderCode);
unsigned int fragmentShader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderCode);
// Attach and link shaders into program
GLCall(glAttachShader(program, vertexShader));
GLCall(glAttachShader(program, fragmentShader));
GLCall(glLinkProgram(program));
GLCall(glValidateProgram(program));
// Delete original shaders (shaders already in program)
GLCall(glDeleteShader(vertexShader));
GLCall(glDeleteShader(fragmentShader));
return program;
}
#include "../include/VertexArray.h"
#include "../include/GLErrorHandler.h"
VertexArray::VertexArray()
{
GLCall(glGenVertexArrays(1, &m_RendererID));
}
VertexArray::~VertexArray()
{
GLCall(glDeleteVertexArrays(1, &m_RendererID));
}
void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout)
{
Bind();
vb.Bind();
const auto& elements = layout.GetElements();
unsigned int offset = 0;
for (unsigned int i = 0; i < elements.size(); i++) {
const auto& element = elements[i];
GLCall(glEnableVertexAttribArray(i));
GLCall(glVertexAttribPointer(i, element.count, element.type,
element.normalized, layout.GetStride(), (const void*) offset));
offset += element.count * VertexBufferElement::GetSizeOfType(element.type);
}
}
void VertexArray::Bind() const
{
GLCall(glBindVertexArray(m_RendererID));
}
void VertexArray::Unbind() const
{
GLCall(glBindVertexArray(0));
}
#include "../include/VertexBuffer.h"
#include "../include/GLErrorHandler.h"
VertexBuffer::VertexBuffer(const void* data, unsigned int size) {
GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID));
GLCall(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW));
}
VertexBuffer::~VertexBuffer() {
GLCall(glDeleteBuffers(1, &m_RendererID));
}
void VertexBuffer::Bind() const {
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID));
}
void VertexBuffer::Unbind() const {
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment