Skip to content

Add Restart Primitives project #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion OpenGLTechTalks/Basic/src/Basic.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "EntryPoint.h"
#include "IRenderer.h"
#include "Application.h"
#include "Application.h"
#include "Shader.h"
7 changes: 7 additions & 0 deletions OpenGLTechTalks/RestartPrimitives/rpF.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#version 460
in vec4 oColor;
out vec4 color;
void main()
{
color = oColor;
}
9 changes: 9 additions & 0 deletions OpenGLTechTalks/RestartPrimitives/rpV.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 460
layout(location = 0) in vec4 aPos;
layout(location = 1) in vec4 aColor;
out vec4 oColor;
void main()
{
gl_Position = aPos;
oColor = aColor;
}
127 changes: 127 additions & 0 deletions OpenGLTechTalks/RestartPrimitives/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <glad\glad.h>
#include "Basic.h"
#include <cmath>

#define USE_FIXED_INDEX 0

class RestartPrimitivesRenderer final : public IRenderer
{

private:
GLuint vertexBuffer, elementBuffer, vertexArray;
core::Shader* shader;
GLsizei indiciesAmount;
protected:
void Init() override
{

shader = new core::Shader("rpV.vert","rpF.frag");


static const GLfloat cube_Pos[] =
{
-0.5f, 0.0f, 0.0f, 1.0f,
-0.3f, -0.2f, 0.0f, 1.0f,
-0.4f, 0.1f, 0.0f, 1.0f,
-0.2f, 0.5f, 0.0f, 1.0f,
0.0f, 0.5f, 0.0f, 1.0f,
0.0f, -0.5f, 0.0f, 1.0f,
0.8f, 0.5f, 0.0f, 1.0f,
0.6f, -0.5f, 0.0f, 1.0f,
};

static const GLfloat cube_colors[] =
{
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f,
};

static const GLushort cube_indices[] =
{
0, 2, 1, 0xFFFF, 1, 4, 3, 0,
0xFFFF,
6, 7, 5, 0xFFFF, 5, 4, 7, 0xFFFF
};

indiciesAmount = sizeof(cube_indices) / sizeof(cube_indices[0]);

glGenBuffers(1, &elementBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_indices), cube_indices, GL_STATIC_DRAW);

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

glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_Pos) + sizeof(cube_colors), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube_Pos), cube_Pos);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube_Pos), sizeof(cube_colors), cube_colors);

glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)sizeof(cube_Pos));

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

}

void Update(float deltaTime) override
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

shader->SetActive(true);

glBindVertexArray(vertexArray);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);

#if USE_FIXED_INDEX
glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
glDrawElements(GL_TRIANGLE_STRIP, indiciesAmount, GL_UNSIGNED_SHORT, NULL);
glDisable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
#else
glEnable(GL_PRIMITIVE_RESTART_INDEX);
glPrimitiveRestartIndex(0xFFFF);
glDrawElements(GL_TRIANGLE_STRIP, indiciesAmount, GL_UNSIGNED_SHORT, NULL);
glDisable(GL_PRIMITIVE_RESTART_INDEX);
#endif
}

void PostUpdate(float) override
{
}

void Dispose() override
{
glDeleteVertexArrays(1, &vertexArray);
glDeleteBuffers(1, &vertexBuffer);
glDeleteBuffers(1, &elementBuffer);
delete shader;
}

void SetViewport(uint32_t width, uint32_t height) override
{
glViewport(0, 0, width, height);
}
};

class RestartPrimitivesApp : public Application
{
public:
RestartPrimitivesApp(IRenderer* render, const WindowData& windowData) : Application(render, windowData)
{

}
};

Application* CreateApplication()
{
return new RestartPrimitivesApp(new RestartPrimitivesRenderer(), WindowData("glRestartPromitives", 800, 600));
}
63 changes: 52 additions & 11 deletions OpenGLTechTalks/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ project "DrawArrays"
{
"DrawArrays/src",
"Basic/src",
"%{IncludeDir.GLFW}",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}"
}

Expand Down Expand Up @@ -129,7 +129,7 @@ project "MultiDrawArrays"
{
"MultiDrawArrays/src",
"Basic/src",
"%{IncludeDir.GLFW}",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}"
}

Expand Down Expand Up @@ -169,7 +169,7 @@ project "DrawElements"
{
"DrawElements/src",
"Basic/src",
"%{IncludeDir.GLFW}",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}"
}

Expand Down Expand Up @@ -209,7 +209,7 @@ project "MultiDrawElements"
{
"MultiDrawElements/src",
"Basic/src",
"%{IncludeDir.GLFW}",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}"
}

Expand All @@ -228,7 +228,7 @@ project "MultiDrawElements"
filter "configurations:Release"
runtime "Release"
optimize "On"

project "DrawElementsBase"
location "DrawElementsBase"
kind "ConsoleApp"
Expand All @@ -249,8 +249,8 @@ project "DrawElementsBase"
{
"DrawElementsBase/src",
"Basic/src",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}",
"%{IncludeDir.glm}"
}

Expand All @@ -269,7 +269,7 @@ project "DrawElementsBase"
filter "configurations:Release"
runtime "Release"
optimize "On"

project "Uniforms"
location "Uniforms"
kind "ConsoleApp"
Expand All @@ -290,8 +290,8 @@ project "Uniforms"
{
"DrawElementsBase/src",
"Basic/src",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}",
"%{IncludeDir.glm}"
}

Expand All @@ -309,4 +309,45 @@ project "Uniforms"

filter "configurations:Release"
runtime "Release"
optimize "On"
optimize "On"

project "RestartPrimitives"
location "RestartPrimitives"
kind "ConsoleApp"
language "C++"
cppdialect "C++17"
staticruntime "on"

targetdir ("bin/" .. outputdir .. "/%{prj.name}")
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

files
{
"%{prj.name}/src/**.h",
"%{prj.name}/src/**.cpp",
}

includedirs
{
"RestartPrimitives/src",
"Basic/src",
"%{IncludeDir.GLFW}",
"%{IncludeDir.Glad}",
"%{IncludeDir.glm}"
}

links
{
"Basic"
}

filter "system:windows"
systemversion "latest"

filter "configurations:Debug"
runtime "Debug"
symbols "On"

filter "configurations:Release"
runtime "Release"
optimize "On"