У вас две ошибки первая это шейдер, вторая это отрисовка шейдера.
Первая проблема в том что в шейдере не прописана переменная(вектор) отвечающая за смещение.
вот шейдер из проекта где "движение" есть.
#version 330 core
layout (location = 0) in vec3 pos;
uniform vec2 posOffset;
void main()
{
gl_Position = vec4(pos.x + posOffset.x, pos.y + posOffset.y, pos.z, 1.0);
}
Как видите в отличие в нём есть вектор posOffset
отвечающий за смещение по X и Y.
Вторая ошибка шейдер стоит писать не строкой(с ним сложнее работать когда он виде строки), а в виде файла .vert
. Также заметил что в вашем коде шейдер статичен, ибо даже такой код не приводит к 'движению' треугола :3
const GLchar *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x + 0.01, position.y + 0.01, position.z, 1.0);\n"
"}";
По сему предлагаю посмотреть тот вариант кода который я предоставил.
Вот проект в котором реализовано движение прямоугольника, а также изменение его цвета (без заголовочного .pro файла):
main.cpp
#include <iostream>
#include <sstream>
#define GLEW_STATIC
#include "GL/glew.h" // Important - this header must come before glfw3 header
#include "GLFW/glfw3.h"
#include "glm/glm.hpp"
#include "ShaderProgram.h"
// Global Variables
const char* APP_TITLE = "Introduction to Modern OpenGL - Shaders";
const int gWindowWidth = 800;
const int gWindowHeight = 600;
GLFWwindow* gWindow = NULL;
bool gWireframe = false;
// Function prototypes
void glfw_onKey(GLFWwindow* window, int key, int scancode, int action, int mode);
void glfw_onFramebufferSize(GLFWwindow* window, int width, int height);
void showFPS(GLFWwindow* window);
bool initOpenGL();
//-----------------------------------------------------------------------------
// Main Application Entry Point
//-----------------------------------------------------------------------------
int main()
{
if (!initOpenGL())
{
// An error occured
std::cerr << "GLFW initialization failed" << std::endl;
return -1;
}
// Set up our quad
// 1. Set up an array of vertices for a quad (2 triangls) with an index buffer data
// (What is a vertex?)
GLfloat vertices[] = {
-0.5f, 0.5f, 0.0f, // Top left
0.5f, 0.5f, 0.0f, // Top right
0.5f, -0.5f, 0.0f, // Bottom right
-0.5f, -0.5f, 0.0f // Bottom left
};
GLuint indices[] = {
0, 1, 2, // First Triangle
0, 2, 3 // Second Triangle
};
// 2. Set up buffers on the GPU
GLuint vbo, ibo, vao;
glGenBuffers(1, &vbo); // Generate an empty vertex buffer on the GPU
glBindBuffer(GL_ARRAY_BUFFER, vbo); // "bind" or set as the current buffer we are working with
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy the data from CPU to GPU
glGenVertexArrays(1, &vao); // Tell OpenGL to create new Vertex Array Object
glBindVertexArray(vao); // Make it the current one
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Define a layout for the first vertex buffer "0"
glEnableVertexAttribArray(0); // Enable the first attribute or attribute "0"
// Set up index buffer
glGenBuffers(1, &ibo); // Create buffer space on the GPU for the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindVertexArray(0); // unbind to make sure other code doesn't change it
ShaderProgram shaderProgram;
shaderProgram.loadShaders("shaders/basic.vert", "shaders/basic.frag");
// Rendering loop
while (!glfwWindowShouldClose(gWindow))
{
showFPS(gWindow);
// Poll for and process events
glfwPollEvents();
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT);
// Render the quad
// Must be called BEFORE setting uniforms because setting uniforms is done
// on the currently active shader program.
shaderProgram.use();
GLfloat time = (GLfloat)glfwGetTime();
GLfloat blueColor = (sin(time) / 2) + 0.5f;
glm::vec2 pos;
pos.x = sin(time) / 2;
pos.y = cos(time) / 2;
shaderProgram.setUniform("vertColor", glm::vec4(0.0f, 0.0f, blueColor, 1.0f));
shaderProgram.setUniform("posOffset", pos);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// Swap front and back buffers
glfwSwapBuffers(gWindow);
}
// Clean up
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ibo);
glfwTerminate();
return 0;
}
//-----------------------------------------------------------------------------
// Initialize GLFW and OpenGL
//-----------------------------------------------------------------------------
bool initOpenGL()
{
// Intialize GLFW
// GLFW is configured. Must be called before calling any GLFW functions
if (!glfwInit())
{
// An error occured
std::cerr << "GLFW initialization failed" << std::endl;
return false;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // forward compatible with newer versions of OpenGL as they become available but not backward compatible (it will not run on devices that do not support OpenGL 3.3
// Create an OpenGL 3.3 core, forward compatible context window
gWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, APP_TITLE, NULL, NULL);
if (gWindow == NULL)
{
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return false;
}
// Make the window's context the current one
glfwMakeContextCurrent(gWindow);
// Initialize GLEW
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW" << std::endl;
return false;
}
// Set the required callback functions
glfwSetKeyCallback(gWindow, glfw_onKey);
glfwSetFramebufferSizeCallback(gWindow, glfw_onFramebufferSize);
glClearColor(0.23f, 0.38f, 0.47f, 1.0f);
// Define the viewport dimensions
glViewport(0, 0, gWindowWidth, gWindowHeight);
return true;
}
//-----------------------------------------------------------------------------
// Is called whenever a key is pressed/released via GLFW
//-----------------------------------------------------------------------------
void glfw_onKey(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_W && action == GLFW_PRESS)
{
gWireframe = !gWireframe;
if (gWireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
//-----------------------------------------------------------------------------
// Is called when the window is resized
//-----------------------------------------------------------------------------
void glfw_onFramebufferSize(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
//-----------------------------------------------------------------------------
// Code computes the average frames per second, and also the average time it takes
// to render one frame. These stats are appended to the window caption bar.
//-----------------------------------------------------------------------------
void showFPS(GLFWwindow* window)
{
static double previousSeconds = 0.0;
static int frameCount = 0;
double elapsedSeconds;
double currentSeconds = glfwGetTime(); // returns number of seconds since GLFW started, as double float
elapsedSeconds = currentSeconds - previousSeconds;
// Limit text updates to 4 times per second
if (elapsedSeconds > 0.25)
{
previousSeconds = currentSeconds;
double fps = (double)frameCount / elapsedSeconds;
double msPerFrame = 1000.0 / fps;
// The C++ way of setting the window title
std::ostringstream outs;
outs.precision(3); // decimal places
outs << std::fixed
<< APP_TITLE << " "
<< "FPS: " << fps << " "
<< "Frame Time: " << msPerFrame << " (ms)";
glfwSetWindowTitle(window, outs.str().c_str());
// Reset for next average.
frameCount = 0;
}
frameCount++;
}
ShaderProgram.h
#ifndef SHADER_H
#define SHADER_H
#include <string>
#include <map>
#include "GL/glew.h"
#include "glm/glm.hpp"
using std::string;
class ShaderProgram
{
public:
ShaderProgram();
~ShaderProgram();
enum ShaderType
{
VERTEX,
FRAGMENT,
PROGRAM
};
// Only supports vertex and fragment (this series will only have those two)
bool loadShaders(const char* vsFilename, const char* fsFilename);
void use();
GLuint getProgram() const;
void setUniform(const GLchar* name, const glm::vec2& v);
void setUniform(const GLchar* name, const glm::vec3& v);
void setUniform(const GLchar* name, const glm::vec4& v);
private:
string fileToString(const string& filename);
void checkCompileErrors(GLuint shader, ShaderType type);
// We are going to speed up looking for uniforms by keeping their locations in a map
GLint getUniformLocation(const GLchar * name);
GLuint mHandle;
std::map<string, GLint> mUniformLocations;
};
#endif // SHADER_H
ShaderProgram.cpp
#include "ShaderProgram.h"
#include <fstream>
#include <iostream>
#include <sstream>
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
ShaderProgram::ShaderProgram()
: mHandle(0)
{}
//-----------------------------------------------------------------------------
// Destructor
//-----------------------------------------------------------------------------
ShaderProgram::~ShaderProgram()
{
// Delete the program
glDeleteProgram(mHandle);
}
//-----------------------------------------------------------------------------
// Loads vertex and fragment shaders
//-----------------------------------------------------------------------------
bool ShaderProgram::loadShaders(const char* vsFilename, const char* fsFilename)
{
string vsString = fileToString(vsFilename);
string fsString = fileToString(fsFilename);
const GLchar* vsSourcePtr = vsString.c_str();
const GLchar* fsSourcePtr = fsString.c_str();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vs, 1, &vsSourcePtr, NULL);
glShaderSource(fs, 1, &fsSourcePtr, NULL);
glCompileShader(vs);
checkCompileErrors(vs, VERTEX);
glCompileShader(fs);
checkCompileErrors(fs, FRAGMENT);
mHandle = glCreateProgram();
if (mHandle == 0)
{
std::cerr << "Unable to create shader program!" << std::endl;
return false;
}
glAttachShader(mHandle, vs);
glAttachShader(mHandle, fs);
glLinkProgram(mHandle);
checkCompileErrors(mHandle, PROGRAM);
glDeleteShader(vs);
glDeleteShader(fs);
mUniformLocations.clear();
return true;
}
//-----------------------------------------------------------------------------
// Opens and reads contents of ASCII file to a string. Returns the string.
// Not good for very large files.
//-----------------------------------------------------------------------------
string ShaderProgram::fileToString(const string& filename)
{
std::stringstream ss;
std::ifstream file;
try
{
file.open(filename, std::ios::in);
if (!file.fail())
{
// Using a std::stringstream is easier than looping through each line of the file
ss << file.rdbuf();
}
file.close();
}
catch (std::exception ex)
{
std::cerr << "Error reading shader filename!" << std::endl;
}
return ss.str();
}
//-----------------------------------------------------------------------------
// Activate the shader program
//-----------------------------------------------------------------------------
void ShaderProgram::use()
{
if (mHandle > 0)
glUseProgram(mHandle);
}
//-----------------------------------------------------------------------------
// Checks for shader compiler errors
//-----------------------------------------------------------------------------
void ShaderProgram::checkCompileErrors(GLuint shader, ShaderType type)
{
int status = 0;
if (type == PROGRAM)
{
glGetProgramiv(mHandle, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
GLint length = 0;
glGetProgramiv(mHandle, GL_INFO_LOG_LENGTH, &length);
// The length includes the NULL character
string errorLog(length, ' '); // Resize and fill with space character
glGetProgramInfoLog(mHandle, length, &length, &errorLog[0]);
std::cerr << "Error! Shader program failed to link. " << errorLog << std::endl;
}
}
else
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
GLint length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
// The length includes the NULL character
string errorLog(length, ' '); // Resize and fill with space character
glGetShaderInfoLog(shader, length, &length, &errorLog[0]);
std::cerr << "Error! Shader failed to compile. " << errorLog << std::endl;
}
}
}
//-----------------------------------------------------------------------------
// Returns the active shader program
//-----------------------------------------------------------------------------
GLuint ShaderProgram::getProgram() const
{
return mHandle;
}
//-----------------------------------------------------------------------------
// Sets a glm::vec2 shader uniform
//-----------------------------------------------------------------------------
void ShaderProgram::setUniform(const GLchar* name, const glm::vec2& v)
{
GLint loc = getUniformLocation(name);
glUniform2f(loc, v.x, v.y);
}
//-----------------------------------------------------------------------------
// Sets a glm::vec3 shader uniform
//-----------------------------------------------------------------------------
void ShaderProgram::setUniform(const GLchar* name, const glm::vec3& v)
{
GLint loc = getUniformLocation(name);
glUniform3f(loc, v.x, v.y, v.z);
}
//-----------------------------------------------------------------------------
// Sets a glm::vec4 shader uniform
//-----------------------------------------------------------------------------
void ShaderProgram::setUniform(const GLchar* name, const glm::vec4& v)
{
GLint loc = getUniformLocation(name);
glUniform4f(loc, v.x, v.y, v.z, v.w);
}
//-----------------------------------------------------------------------------
// Returns the uniform identifier given it's string name.
// NOTE: Shader must be currently active first.
//-----------------------------------------------------------------------------
GLint ShaderProgram::getUniformLocation(const GLchar* name)
{
std::map<string, GLint>::iterator it = mUniformLocations.find(name);
// Only need to query the shader program IF it doesn't already exist.
if (it == mUniformLocations.end())
{
// Find it and add it to the map
mUniformLocations[name] = glGetUniformLocation(mHandle, name);
}
// Return it
return mUniformLocations[name];
}
basic.frag
#version 330 core
uniform vec4 vertColor;
out vec4 frag_color;
void main()
{
frag_color = vertColor;
}
basic.vert
#version 330 core
layout (location = 0) in vec3 pos;
uniform vec2 posOffset;
void main()
{
gl_Position = vec4(pos.x + posOffset.x, pos.y + posOffset.y, pos.z, 1.0);
}
Также заметил что у вас есть спрос на качественные уроки по opengl. Лично мне понравились уроки от Gameinstitute - OpenGL Graphics Programming, у них как раз есть как видео так и файлы, а также интуитивно-понятное изложение.