How to draw Two Cubic Bezier Curve using 8 Points The last point of the first curve will be the starting point of second curve
What I am doing wrong ??
Please Help me solve this issue .
#include <array>
#include <fstream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
std::vector<glm::vec3> pointToDraw;
std::vector<glm::vec3> myControlPoints = {
glm::vec3(-0.5f, -0.5f, 0.0f),
glm::vec3(-0.5f, 0.5f, 0.0f),
glm::vec3( 0.5f, -0.5f, 0.0f),
glm::vec3( 0.5f, 0.0f, 0.0f),
};
#define numVBOs 1
#define numVAOs 1
GLuint VBO[numVBOs];
GLuint VAO[numVAOs];
GLuint renderingProgram;
bool checkOpenGLError() {
bool foundError = false;
int glErr = glGetError();
while (glErr != GL_NO_ERROR) {
cout << "glError: " << glErr << endl;
foundError = true;
glErr = glGetError();
}
return foundError;
}
void printShaderLog(GLuint shader) {
int len = 0;
int chWrittn = 0;
char* log;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
if (len > 0) {
log = (char*)malloc(len);
glGetShaderInfoLog(shader, len, &chWrittn, log);
cout << "Shader Info Log: " << log << endl;
free(log);
}
}
void printProgramLog(int prog) {
int len = 0;
int chWrittn = 0;
char* log;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
if (len > 0) {
log = (char*)malloc(len);
glGetProgramInfoLog(prog, len, &chWrittn, log);
cout << "Program Info Log: " << log << endl;
free(log);
}
}
string readShaderSource(const char* filePath) {
string content;
ifstream fileStream(filePath, ios::in);
string line = "";
while (!fileStream.eof()) {
getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
return content;
}
GLuint createShaderProgram()
{
GLint vertCompiled;
GLint fragCompiled;
GLint linked;
string vertShaderStr = readShaderSource("vertexShader.glsl");
string fragShaderStr = readShaderSource("fragmentShader.glsl");
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
const char* vertShaderSrc = vertShaderStr.c_str();
const char* fragShaderSrc = fragShaderStr.c_str();
glShaderSource(vShader, 1, &vertShaderSrc, NULL);
glShaderSource(fShader, 1, &fragShaderSrc, NULL);
glCompileShader(vShader);
checkOpenGLError();
glGetShaderiv(vShader, GL_COMPILE_STATUS, &vertCompiled);
if (vertCompiled != 1) {
cout << "vertex compilation failed" << endl;
printShaderLog(vShader);
}
glCompileShader(fShader);
checkOpenGLError();
glGetShaderiv(vShader, GL_COMPILE_STATUS, &fragCompiled);
if (fragCompiled != 1) {
cout << "fragment compilation failed" << endl;
printShaderLog(fShader);
}
// Shader program objektum létrehozása. Eltároljuk az ID értéket.
GLuint vfProgram = glCreateProgram();
glAttachShader(vfProgram, vShader);
glAttachShader(vfProgram, fShader);
glLinkProgram(vfProgram);
checkOpenGLError();
glGetProgramiv(vfProgram, GL_LINK_STATUS, &linked);
if (linked != 1) {
cout << "linking failed" << endl;
printProgramLog(vfProgram);
}
glDeleteShader(vShader);
glDeleteShader(fShader);
return vfProgram;
}
int NCR(int n, int r) {
if (r == 0) return 1;
if (r > n / 2) return NCR(n, n - r);
long res = 1;
for (int k = 1; k <= r; ++k) {
res *= n - k + 1;
res /= k;
}
return res;
}
GLfloat blending(GLint n, GLint i, GLfloat t) {
return NCR(n, i) * pow(t, i) * pow(1.0f - t, n - i);
}
void drawBezierCurve(std::vector<glm::vec3> controlPoints) {
glm::vec3 nextPoint;
GLfloat t = 0.0f, B;
GLfloat increment = 1.0f / 100.0f;
while (t <= 1.0f) {
nextPoint = glm::vec3(0.0f, 0.0f, 0.0f);
for (int i = 0; i < controlPoints.size(); i++) {
B = blending(controlPoints.size() - 1, i, t);
nextPoint.x += B * controlPoints.at(i).x;
nextPoint.y += B * controlPoints.at(i).y;
nextPoint.z += B * controlPoints.at(i).z;
}
pointToDraw.push_back(nextPoint);
t += increment;
}
}
void init(GLFWwindow* window) {
renderingProgram = createShaderProgram();
drawBezierCurve(myControlPoints);
glGenBuffers(numVBOs, VBO);
glGenVertexArrays(numVAOs, VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, pointToDraw.size() * sizeof(glm::vec3), pointToDraw.data(), GL_STATIC_DRAW);
glBindVertexArray(VAO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(renderingProgram);
glClearColor(0.0, 0.0, 0.0, 1.0);
}
void display(GLFWwindow* window, double currentTime) {
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO[0]);
glLineWidth(2.0f);
glDrawArrays(GL_LINE_STRIP, 0, pointToDraw.size());
glBindVertexArray(0);
}
I want to join two cubic Bezier curve Two Cubic Bezier Curve Sample
Second curve is starting from the end of first cubic Bezier curve