feat: add time uniform to shader
This commit is contained in:
parent
1c5350b67d
commit
c73f7f74db
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.27)
|
cmake_minimum_required(VERSION 3.27)
|
||||||
project(HelloGLEW)
|
project(HelloTriangle)
|
||||||
|
|
||||||
# Define platform-specific paths and libraries
|
# Define platform-specific paths and libraries
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
uniform float _Time;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
FragColor = vec4(1.0, 0.5, 0.2, 1.0);
|
FragColor = vec4(1.0, 0.5, 0.2, 1.0) * sin(_Time) / 2.0 + 0.5;
|
||||||
}
|
}
|
||||||
|
|
22
hello.cpp
22
hello.cpp
|
@ -40,6 +40,8 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
std::string readFile(const std::string& filePath)
|
std::string readFile(const std::string& filePath)
|
||||||
{
|
{
|
||||||
std::ifstream fileStream(filePath, std::ios::in);
|
std::ifstream fileStream(filePath, std::ios::in);
|
||||||
|
@ -82,13 +84,21 @@ GLuint compileShader(const std::string& shaderPath, GLenum shaderType)
|
||||||
return shader;
|
return shader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now();
|
||||||
|
float getTime()
|
||||||
|
{
|
||||||
|
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
|
||||||
|
std::chrono::duration<float> timeSpan = std::chrono::duration_cast<std::chrono::duration<float>>(now - startTime);
|
||||||
|
return timeSpan.count();
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Set up GLFW, OpenGL and GLEW.
|
// Set up GLFW, OpenGL and GLEW.
|
||||||
if (!glfwInit())
|
if (!glfwInit())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello GLEW", NULL, NULL);
|
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
|
||||||
if (!window)
|
if (!window)
|
||||||
{
|
{
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
@ -150,12 +160,22 @@ int main()
|
||||||
glDeleteShader(vertShader);
|
glDeleteShader(vertShader);
|
||||||
glDeleteShader(fragShader);
|
glDeleteShader(fragShader);
|
||||||
|
|
||||||
|
// Main loop
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
glClearColor(0.2, 0.3, 0.3, 1.0);
|
glClearColor(0.2, 0.3, 0.3, 1.0);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
|
|
||||||
|
GLint timeLocation = glGetUniformLocation(shaderProgram, "_Time");
|
||||||
|
if (timeLocation == -1)
|
||||||
|
{
|
||||||
|
std::cerr << "Could not find uniform: _Time" << std::endl;
|
||||||
|
}
|
||||||
|
float timeValue = getTime();
|
||||||
|
glUniform1f(timeLocation, timeValue);
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue