Skip to content
Merged
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
26 changes: 26 additions & 0 deletions Sandbox/assets/shaders/FlatColor.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Flat Color Shader

#type vertex
#version 330 core

layout(location = 0) in vec3 a_Position;

uniform mat4 u_ViewProjection;
uniform mat4 u_Transform;

void main()
{
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0);
}

#type fragment
#version 330 core

layout(location = 0) out vec4 color;

uniform vec4 u_Color;

void main()
{
color = u_Color;
}
3 changes: 2 additions & 1 deletion Sandbox/assets/shaders/Texture.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

in vec2 v_TexCoord;

uniform vec4 u_Color;
uniform sampler2D u_Texture;

void main()
{
color = texture(u_Texture, v_TexCoord);
color = texture(u_Texture, v_TexCoord * 10.0) * u_Color;
}
72 changes: 72 additions & 0 deletions Sandbox/src/Sandbox2D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "Sandbox2D.h"

#include "imgui/imgui.h"

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

Sandbox2D::Sandbox2D()
:Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true)
{

}

void Sandbox2D::OnAttach()
{
m_CheckerboardTexture = StarStudio::Texture2D::Create("assets/textures/Checkerboard.png");
}

void Sandbox2D::OnDetach()
{

}

void Sandbox2D::OnUpdate(StarStudio::Timestep ts)
{
// Update
m_CameraController.OnUpdate(ts);
// Render
StarStudio::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 });
StarStudio::RenderCommand::Clear();

StarStudio::Renderer2D::BeginScene(m_CameraController.GetCamera());
StarStudio::Renderer2D::DrawQuad({ -1.0f, 0.0f }, { 0.8f, 0.8f }, { 0.8f, 0.2f, 0.3f, 1.0f });
StarStudio::Renderer2D::DrawQuad({ 0.5f, -0.5f }, { 0.5f, 0.75f }, { 0.2f, 0.3f, 0.8f, 1.0f });
StarStudio::Renderer2D::DrawQuad({ 0.0f, 0.0f, -0.1f }, { 10.0f, 10.0f }, m_CheckerboardTexture);
StarStudio::Renderer2D::EndScene();
}

void Sandbox2D::OnImGuiRender()
{
//Camera Info
ImGui::Begin("Camera Info");

ImGui::Text("Camera Position");
ImGui::Text("X: %.2f", m_CameraController.GetCamera().GetPosition().x);
ImGui::Text("Y: %.2f", m_CameraController.GetCamera().GetPosition().y);
ImGui::Text("Z: %.2f", m_CameraController.GetCamera().GetPosition().z);

ImGui::Text("Camera Rotation: %.2f", m_CameraController.GetCamera().GetRotation());

if (ImGui::Button("Reset")) {
m_CameraController.GetCamera().SetPosition(glm::vec3(0.0f));
m_CameraController.GetCamera().SetRotation(0.0f);
}

//Color Picker
ImGui::Text("Color Picker");

ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor));

ImGui::Text("Frame Counter");

ImGui::Text("FPS: %.u", ImGui::GetIO().Framerate);

ImGui::End();

}

void Sandbox2D::OnEvent(StarStudio::Event& e)
{
m_CameraController.OnEvent(e);
}
27 changes: 27 additions & 0 deletions Sandbox/src/Sandbox2D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "StarStudio.h"
#include "StarStudio/Renderer/OrthographicCameraController.h"

class Sandbox2D : public StarStudio::Layer
{
public:
Sandbox2D();
virtual ~Sandbox2D() = default;

virtual void OnAttach() override;
virtual void OnDetach() override;

void OnUpdate(StarStudio::Timestep ts) override;
virtual void OnImGuiRender() override;
void OnEvent(StarStudio::Event& e) override;
private:
StarStudio::OrthographicCameraController m_CameraController;

// Temp
StarStudio::Ref<StarStudio::VertexArray> m_SquareVA;
StarStudio::Ref<StarStudio::Shader> m_FlatColorShader;

StarStudio::Ref<StarStudio::Texture2D> m_CheckerboardTexture;

glm::vec4 m_SquareColor = { 0.2f, 0.3f, 0.8f, 1.0f };
};
70 changes: 27 additions & 43 deletions Sandbox/src/SandboxApp.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#include <StarStudio.h>
#include <StarStudio/Core/EntryPoint.h>

#include "Platform/OpenGL/OpenGLShader.h"

#include "imgui/imgui.h"

#include <glm/gtc/matrix_transform.hpp>

#include "Sandbox2D.h"

#include "StarStudio/Renderer/OrthographicCameraController.h"
#include "glm/gtc/type_ptr.hpp"

class ExampleLayer : public StarStudio::Layer
{
public:
ExampleLayer()
: Layer("Example"), m_Camera(-1.6f, 1.6f, -0.9f, 0.9f), m_CameraPosition(0.0f)
: Layer("Example"), m_CameraController(1280.0f / 720.0f, true)
{
m_VertexArray.reset(StarStudio::VertexArray::Create());
m_VertexArray = StarStudio::VertexArray::Create();

float vertices[3 * 7] = {
-0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f,
Expand All @@ -35,7 +40,7 @@ class ExampleLayer : public StarStudio::Layer
indexBuffer.reset(StarStudio::IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t)));
m_VertexArray->SetIndexBuffer(indexBuffer);

m_SquareVA.reset(StarStudio::VertexArray::Create());
m_SquareVA = StarStudio::VertexArray::Create();

float squareVertices[5 * 4] = {
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
Expand Down Expand Up @@ -139,28 +144,14 @@ class ExampleLayer : public StarStudio::Layer

void OnUpdate(StarStudio::Timestep ts) override
{
if (StarStudio::Input::IsKeyPressed(SS_KEY_LEFT))
m_CameraPosition.x -= m_CameraMoveSpeed * ts;
else if (StarStudio::Input::IsKeyPressed(SS_KEY_RIGHT))
m_CameraPosition.x += m_CameraMoveSpeed * ts;

if (StarStudio::Input::IsKeyPressed(SS_KEY_UP))
m_CameraPosition.y += m_CameraMoveSpeed * ts;
else if (StarStudio::Input::IsKeyPressed(SS_KEY_DOWN))
m_CameraPosition.y -= m_CameraMoveSpeed * ts;

if (StarStudio::Input::IsKeyPressed(SS_KEY_A))
m_CameraRotation += m_CameraRotationSpeed * ts;
if (StarStudio::Input::IsKeyPressed(SS_KEY_D))
m_CameraRotation -= m_CameraRotationSpeed * ts;
// Update
m_CameraController.OnUpdate(ts);

// Render
StarStudio::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 });
StarStudio::RenderCommand::Clear();

m_Camera.SetPosition(m_CameraPosition);
m_Camera.SetRotation(m_CameraRotation);

StarStudio::Renderer::BeginScene(m_Camera);
StarStudio::Renderer::BeginScene(m_CameraController.GetCamera());

glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));

Expand Down Expand Up @@ -199,34 +190,31 @@ class ExampleLayer : public StarStudio::Layer
ImGui::Begin("Camera Info");

ImGui::Text("Camera Position");
ImGui::Text("X: %.2f", m_CameraPosition.x);
ImGui::Text("Y: %.2f", m_CameraPosition.y);
ImGui::Text("Z: %.2f", m_CameraPosition.z);
ImGui::Text("X: %.2f", m_CameraController.GetCamera().GetPosition().x);
ImGui::Text("Y: %.2f", m_CameraController.GetCamera().GetPosition().y);
ImGui::Text("Z: %.2f", m_CameraController.GetCamera().GetPosition().z);

ImGui::Text("Camera Rotation: %.2f", m_CameraRotation);
ImGui::Text("Camera Rotation: %.2f", m_CameraController.GetCamera().GetRotation());

if (ImGui::Button("Reset")) {
m_CameraPosition = glm::vec3(0.0f);
m_CameraRotation = 0.0f;
m_CameraController.GetCamera().SetPosition(glm::vec3(0.0f));
m_CameraController.GetCamera().SetRotation(0.0f);
}

ImGui::End();
/*

//Color Picker
ImGui::Begin("Color Picker");

ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor));
if (ImGui::Button("Reset"))
{
m_SquareColor = { 0.2f, 0.2f, 0.2f};
}

ImGui::End();
*/
}
void OnEvent(StarStudio::Event& event) override
{

}

void OnEvent(StarStudio::Event& e) override
{
m_CameraController.OnEvent(e);
}

private:
Expand All @@ -239,12 +227,7 @@ class ExampleLayer : public StarStudio::Layer

StarStudio::Ref<StarStudio::Texture2D> m_Texture, m_starLogTexture;

StarStudio::OrthographicCamera m_Camera;
glm::vec3 m_CameraPosition;
float m_CameraMoveSpeed = 5.0f;

float m_CameraRotation = 0.0f;
float m_CameraRotationSpeed = 180.0f;
StarStudio::OrthographicCameraController m_CameraController;

glm::vec3 m_SquareColor = { 0.2f, 0.2f, 0.2f};
};
Expand All @@ -254,7 +237,8 @@ class Sandbox : public StarStudio::Application
public:
Sandbox()
{
PushLayer(new ExampleLayer());
// PushLayer(new ExampleLayer());
PushLayer(new Sandbox2D());
}

~Sandbox()
Expand Down
8 changes: 8 additions & 0 deletions StarStudio/src/Platform/OpenGL/OpenGLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ namespace StarStudio {
SS_CORE_INFO(" Vendor: {0}", (const char*)glGetString(GL_VENDOR));
SS_CORE_INFO(" Renderer: {0}", (const char*)glGetString(GL_RENDERER));
SS_CORE_INFO(" Version: {0}", (const char*)glGetString(GL_VERSION));

#ifdef SS_ENABLE_ASSERTS
int versionMajor;
int versionMinor;
glGetIntegerv(GL_MAJOR_VERSION, &versionMajor);
glGetIntegerv(GL_MINOR_VERSION, &versionMinor);
SS_CORE_ASSERT(versionMajor > 4 || (versionMajor == 4 && versionMinor >= 5), "StarStudio requires at least OpenGL version 4.5!");
#endif
}

void OpenGLContext::SwapBuffers()
Expand Down
7 changes: 7 additions & 0 deletions StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace StarStudio
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
}

void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
{
glViewport(x, y, width, height);
}

void OpenGLRendererAPI::SetClearColor(const glm::vec4& color)
Expand All @@ -22,5 +28,6 @@ namespace StarStudio
void OpenGLRendererAPI::DrawIndexed(const Ref<VertexArray>& vertexArray)
{
glDrawElements(GL_TRIANGLES, vertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
1 change: 1 addition & 0 deletions StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace StarStudio
{
public:
virtual void Init() override;
virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) override;

virtual void SetClearColor(const glm::vec4& color) override;
virtual void Clear() override;
Expand Down
37 changes: 31 additions & 6 deletions StarStudio/src/Platform/OpenGL/OpenGLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,20 @@ namespace StarStudio {

const char* typeToken = "#type";
size_t typeTokenLength = strlen(typeToken);
size_t pos = source.find(typeToken, 0);
size_t pos = source.find(typeToken, 0); //Start of shader type declaration line
while (pos != std::string::npos)
{
size_t eol = source.find_first_of("\r\n", pos);
size_t eol = source.find_first_of("\r\n", pos); //End of shader type declaration line
SS_CORE_ASSERT(eol != std::string::npos, "Syntax error");
size_t begin = pos + typeTokenLength + 1;
size_t begin = pos + typeTokenLength + 1; //Start of shader type name (after "#type " keyword)
std::string type = source.substr(begin, eol - begin);
SS_CORE_ASSERT(ShaderTypeFromString(type), "Invalid shader type specified");

size_t nextLinePos = source.find_first_not_of("\r\n", eol);
pos = source.find(typeToken, nextLinePos);
shaderSources[ShaderTypeFromString(type)] = source.substr(nextLinePos, pos - (nextLinePos == std::string::npos ? source.size() - 1 : nextLinePos));
size_t nextLinePos = source.find_first_not_of("\r\n", eol); //Start of shader code after shader type declaration line
SS_CORE_ASSERT(nextLinePos != std::string::npos, "Syntax error");
pos = source.find(typeToken, nextLinePos); //Start of next shader type declaration line

shaderSources[ShaderTypeFromString(type)] = (pos == std::string::npos) ? source.substr(nextLinePos) : source.substr(nextLinePos, pos - nextLinePos);
}

return shaderSources;
Expand Down Expand Up @@ -154,7 +156,10 @@ namespace StarStudio {
}

for (auto id : glShaderIDs)
{
glDetachShader(program, id);
glDeleteShader(id);
}
}


Expand All @@ -168,6 +173,26 @@ namespace StarStudio {
glUseProgram(0);
}

void OpenGLShader::SetInt(const std::string& name, int value)
{
UploadUniformInt(name, value);
}

void OpenGLShader::SetFloat3(const std::string& name, const glm::vec3& value)
{
UploadUniformFloat3(name, value);
}

void OpenGLShader::SetFloat4(const std::string& name, const glm::vec4& value)
{
UploadUniformFloat4(name, value);
}

void OpenGLShader::SetMat4(const std::string& name, const glm::mat4& value)
{
UploadUniformMat4(name, value);
}

void OpenGLShader::UploadUniformInt(const std::string& name, int value)
{
GLint location = glGetUniformLocation(m_RendererID, name.c_str());
Expand Down
Loading
Loading