...
 
Commits (2)
cmake_minimum_required(VERSION 2.8)
include(${CMAKE_MODULE_PATH}/DefaultExecutable.cmake)
\ No newline at end of file
#include "Renderer.h"
void Renderer::cleanUp() { glDeleteBuffers(2, mVbos); }
void Renderer::render(SplineSurface& surface, const glm::mat4& projection,
const glm::mat4& cam) {
glm::vec3 black = glm::vec3(0.0, 0.0, 0.0);
glm::vec3 red = glm::vec3(1.0, 0.0, 0.0);
mShaderProgram->useProgram();
// update camera info
glUniformMatrix4fv(mProjectionHandle, 1, false, glm::value_ptr(projection));
glUniformMatrix4fv(mViewHandle, 1, false, glm::value_ptr(cam));
// draw control points
glPointSize(10.0f);
glUniform3fv(mColorHandle, 1, glm::value_ptr(black));
glBindBuffer(GL_ARRAY_BUFFER, mVbos[0]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * surface.getControlPoints().size(),
&surface.getControlPoints()[0], GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, surface.getControlPoints().size());
// draw curve points
glPointSize(5.0f);
glUniform3fv(mColorHandle, 1, glm::value_ptr(red));
glBindBuffer(GL_ARRAY_BUFFER, mVbos[1]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * surface.getVertices().size(),
&surface.getVertices()[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, surface.getVertices().size());
// unbind the VBO, we don't need it anymore
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Renderer::~Renderer() { cleanUp(); }
GLFWwindow* Renderer::initialize(int width, int height) {
glfwInit();
CVK::useOpenGL33CoreProfile();
GLFWwindow* window =
glfwCreateWindow(width, height, "Spline-Surface", 0, 0);
glfwSetWindowPos(window, 600, 50);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize OpenGL context" << std::endl;
return nullptr;
}
glClearColor(1, 1, 1, 1);
// compile a shader program
std::vector<std::string> shadernames = {
CVK::State::getInstance()->getShaderPath() +
"/SplineSurface/surface.vert",
CVK::State::getInstance()->getShaderPath() +
"/SplineSurface/surface.frag"};
mShaderProgram = std::make_unique<CVK::ShaderSet>(
VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT, shadernames);
// use the shader program
mShaderProgram->useProgram();
mColorHandle =
glGetUniformLocation(mShaderProgram->getProgramID(), "color");
mProjectionHandle =
glGetUniformLocation(mShaderProgram->getProgramID(), "projection");
mViewHandle = glGetUniformLocation(mShaderProgram->getProgramID(), "view");
GLuint vaoHandle;
glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
glGenBuffers(3, mVbos);
return window;
}
#include <memory>
#ifndef RENDERER_H
#define RENDERER_H
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <memory>
#include <CVK_2/CVK_Framework.h>
#include <CVK_2/CVK_ShaderSet.h>
#include "SplineSurface.h"
class Renderer {
public:
virtual ~Renderer();
GLFWwindow* initialize(int width, int height);
void render(SplineSurface& surface, const glm::mat4& projection,
const glm::mat4& cam);
void cleanUp();
private:
std::unique_ptr<CVK::ShaderSet> mShaderProgram = nullptr;
GLint mColorHandle{};
GLint mProjectionHandle{};
GLint mViewHandle{};
GLuint mVbos[3]{};
};
#endif
#include "SplineSurface.h"
SplineSurface::SplineSurface(std::vector<glm::vec3>& cpoints, int numSamplesX,
int numSamplesY)
: mControllPoints(cpoints) {}
//
// Created by nils on 11.05.17.
//
#ifndef BEZIERSURFACE_H
#define BEZIERSURFACE_H
#include <glm/glm.hpp>
#include <vector>
class SplineSurface {
public:
SplineSurface(std::vector<glm::vec3>& cpoints, int numSamplesX,
int numSamplesY);
inline std::vector<glm::vec3>& getVertices() { return mVertices; }
inline std::vector<glm::vec3>& getControlPoints() {
return mControllPoints;
}
protected:
std::vector<glm::vec3>& mControllPoints;
std::vector<glm::vec3> mVertices;
};
#endif // BEZIERSURFACE_H
CVK_2
\ No newline at end of file
#include "Renderer.h"
#include <CVK_2/CVK_Trackball.h>
std::vector<glm::vec3> cpoints; // controlpoints
int num_samples = 100; // number of vertices
int k = 3; // order
const int width = 800; // window width
const int height = 800; // window height
static Renderer renderer;
int main() {
// create control points beware that the order matters!
cpoints.push_back(glm::vec3(-0.5, -0.25, -0.5));
cpoints.push_back(glm::vec3(-0.5, 0.5, -0.25));
cpoints.push_back(glm::vec3(-0.5, -0.25, 0.0));
cpoints.push_back(glm::vec3(0, 0, -0.5));
cpoints.push_back(glm::vec3(0, 0.5, -0.25));
cpoints.push_back(glm::vec3(0, 0, 0.0));
cpoints.push_back(glm::vec3(0.5, -0.25, -0.5));
cpoints.push_back(glm::vec3(0.5, 0.5, -0.25));
cpoints.push_back(glm::vec3(0.5, -0.25, 0.0));
SplineSurface splineSurface(cpoints, num_samples, num_samples);
// render stuff
GLFWwindow* window = renderer.initialize(width, height);
// trackball camera
CVK::Perspective proj(glm::radians(75.f), 1, 0.1, 10);
CVK::Trackball cam(window, width, height, &proj);
cam.setRadius(1);
double lastTime = glfwGetTime();
double deltaTime = 0, nowTime = 0;
while (!glfwWindowShouldClose(window)) {
nowTime = glfwGetTime();
deltaTime = nowTime - lastTime;
lastTime = nowTime;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cam.update(deltaTime);
renderer.render(splineSurface, cam.getProjection().getProjMatrix(),
cam.getView());
// show what's been drawn
glfwSwapBuffers(window);
glfwPollEvents();
}
renderer.cleanUp();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
......@@ -67,6 +67,7 @@ void FBO::create(int width, int height, int numColorTextures, bool depthTexture,
}
// cleanup
delete [] drawBuffers;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
......
#version 330 core
uniform vec3 color;
out vec4 fragmentColor;
void main(){
fragmentColor = vec4(color, 1);
}
\ No newline at end of file
#version 330 core
layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
uniform mat4 projection;
uniform mat4 view;
out vec3 passPosition;
out vec3 passNormal;
void main()
{
gl_Position = projection * view * position;
//transform the position correctly into view space
//and pass it to the fragment shader
passPosition = (view * position).xyz;
//transform the normal correctly into view space
//and pass it to the fragment shader
mat3 normalMatrix = mat3(transpose(inverse(view)));
passNormal = normalize(normalMatrix * normal);
}
\ No newline at end of file