Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cylinder.cpp
- //
- #include "stdafx.h"
- #include "GL/gl3w.h"
- #include "Cylinder.h"
- #include "Defs.h"
- #include "Constants.h"
- #include "GLHelpers.h"
- #include "glm/glm.hpp"
- #include "glm/detail/type_vec3.hpp"
- #include "glm/gtc/matrix_transform.hpp"
- #include "glm/gtc/type_ptr.hpp"
- inline float setFov(float fov) {
- #ifdef GLM_FORCE_RADIANS
- return (float)(fov / 180.0 * M_PI);
- #else
- // Documentation of glm which I read was not correct here
- return (float)(fov / 180.0 * M_PI);
- #endif
- }
- /**
- * struct that holds all of the object data
- */
- struct Vertex
- {
- glm::vec3 position;
- glm::vec3 normal;
- glm::tvec3<GLubyte> color;
- };
- /**
- * Generate the side of a cylinder centered at (0, 0, 0). Allocates vertData,
- * the ownership of which passes to the caller (who must dispose it).
- *
- * @param numFacets the number of flat sides that approximate the cylinder
- * @param height the height of the cylinder (spanning -height/2.0f to height/2.0f)
- * @param radius the radius of the cylinder
- * @param vertData array of Vertex structures that are output
- *
- * @returns the number of Vertices that have been generated
- */
- int genCylSide(int numFacets, float height, float radius, Vertex* &vertData)
- {
- int numVertices = numFacets * 2 + 2;
- vertData = new Vertex[numVertices];
- for (int i = 0; i < numVertices; i++)
- {
- float alpha = static_cast<float>(2.0 * M_PI * (i / 2) / numFacets);
- vertData[i].position.x = cos(alpha) * radius;
- vertData[i].position.y = i % 2 == 0 ? -height / 2.0f : height / 2.0f;
- vertData[i].position.z = sin(alpha) * radius;
- vertData[i].normal = glm::normalize(glm::vec3(vertData[i].position.x, 0.0, vertData[i].position.z));
- glm::vec3 colA, colB;
- colA = i % 2 == 0 ? glm::vec3(0.0, 0.0, 1.0) : glm::vec3(1.0, 0.0, 0.0);
- colB = i % 2 == 0 ? glm::vec3(1.0, 1.0, 0.0) : glm::vec3(0.0, 1.0, 0.0);
- vertData[i].color = glm::mix(colA, colB, 1.0 - abs((alpha / (2.0 * M_PI) * 2.0 - 1.0))) * 255.0f;
- }
- return numVertices;
- }
- /**
- * Generates a disc centered at (0, 0, 0). Allocates vertData,
- * the ownership of which passes to the caller (who must dispose it).
- *
- * @param numFacets the number of sides that approximate the disc
- * @param radius the radius of the disc
- * @param vertData array of Vertex structures that are output
- *
- * @returns the number of Vertices that have been generated
- */
- int genDisc(int numFacets, float radius, Vertex* &vertData)
- {
- // TODO Vertex daten fuer Zylinderscheibe generieren,
- // in vertData ablegen
- // und Anzahl der Vertices ausgegen, siehe analog
- // genCylSide
- int numVertices = numFacets * 2 + 2;
- vertData = new Vertex[numVertices];
- for (int i = 0; i < numVertices; i++)
- {
- float alpha = static_cast<float>(2.0 * M_PI * (i / 2) / numFacets);
- vertData[i].position.x = (i % 2 == 0) ? cos(alpha) * radius : 0;
- vertData[i].position.y = 0;
- vertData[i].position.z = (i % 2 == 0) ? sin(alpha) * radius : 0;
- vertData[i].color = glm::vec3(1.0, 1.0, 1.0) * 255.0f;
- }
- return numVertices;
- // END TODO
- }
- Cylinder::Cylinder(COGL4CoreAPI *Api) : RenderPlugin(Api) {
- this->myName = "MyPlugins/Cylinder";
- this->myDescription = "renders a single triangle";
- //draw = true;
- }
- Cylinder::~Cylinder() {
- }
- bool Cylinder::Activate(void) {
- std::string pathName = this->GetCurrentPluginPath();
- viewTrafoHandle = this->AddManipulator(
- "View",
- &this->viewTrafo,
- Manipulator::MANIPULATOR_ORBIT_VIEW_3D,
- false
- );
- boxTrafoHandle = this->AddManipulator(
- "Object",
- &this->boxTrafo,
- Manipulator::MANIPULATOR_OBJ_MOVE_SCALE_ROT);
- this->SelectCurrentManipulator(2);
- this->boxTrafo = glm::mat4();
- this->numFacets = 5;
- this->numFacets.Set(this, "numFacets", &Cylinder::NumFacetsChanged);
- this->numFacets.Register("min=3 step=1 max=90");
- vShaderName = pathName + std::string("shader/vert.glsl");
- fShaderName = pathName + std::string("shader/frag.glsl");
- shader.CreateProgramFromFile(
- vShaderName.c_str(),
- fShaderName.c_str()
- );
- this->fovY = 37.0f;
- this->fovY.Set(this, "fov", nullptr);
- this->fovY.Register("min=20 max=70");
- this->fNear = 0.001f;
- InitGeometry();
- return true;
- }
- bool Cylinder::Deactivate(void) {
- glDisableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glDeleteBuffers(1, &this->cylSideStrip);
- glDeleteBuffers(1, &this->cylTopStrip);
- glDeleteVertexArrays(1, &this->cylSideArray);
- glDeleteVertexArrays(1, &this->cylTopArray);
- shader.RemoveAllShaders();
- return true;
- }
- bool Cylinder::Init(void) {
- if (gl3wInit()) {
- fprintf(stderr, "Error: Failed to initialize gl3d.\n");
- return false;
- }
- glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
- glClearDepth(1.0f); // min. Tiefe 0.0, max. Tiefe 1.0
- glEnable(GL_DEPTH_TEST);
- // glEnable(GL_CULL_FACE);
- //glDisable(GL_CULL_FACE);
- return true;
- }
- bool Cylinder::Render(void) {
- glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_DEPTH_TEST);
- projMatrix = glm::perspective(
- setFov(this->fovY),
- aspectRatio,
- this->fNear,
- 1000.0f
- );
- shader.Bind();
- // TODO
- // Benoetigte Matrizen erzeugen
- // ModelView
- // ModelViewProjection
- // NormalModel
- // ViewInverse
- glm::mat4 mv_matrix = viewTrafo* boxTrafo;
- glm::mat4 mvp_matrix = projMatrix *mv_matrix;
- glm::mat3 normal_model = glm::mat3();
- glm::mat4 view_inv = glm::mat4();
- // Matrizen an passende shader variablen binden
- glUniformMatrix4fv(shader.GetUniformLocation("modelMatrix"), 1, GL_FALSE, mv_matrix);
- glUniformMatrix4fv(shader.GetUniformLocation("mvpMatrix"), 1, GL_FALSE, mvp_matrix);
- // END TODO
- glUniform3f(shader.GetUniformLocation("lightPosWCS"), 300.0f, 600.0f, 600.0f);
- glUniform3f(shader.GetUniformLocation("lightCol"), 1.0f, 1.0f, 1.0f);
- (glBindVertexArray(this->cylSideArray));
- (glDrawArrays(GL_TRIANGLE_STRIP, 0, this->cylSideLength));
- // TODO
- // Zylinder Boden und Deckel setzen
- // Entsprechende Matrizen anpassen und neu an Shader uebergeben
- // Nur eine Geometrie benutzen!
- // END TODO
- shader.Release();
- return false;
- }
- bool Cylinder::Keyboard(int key, int action, int mods, int x, int y)
- {
- if (action == OGLC_RELEASE) {
- std::cout << "key pressed: " << key << std::endl;
- switch (key) {
- case OGLC_KEY_R:
- {
- fprintf(stderr, "Reload shaders ...\n");
- shader.RemoveAllShaders();
- shader.CreateProgramFromFile(vShaderName.c_str(), fShaderName.c_str());
- return true;
- }
- case OGLC_KEY_S:
- {
- int w, h;
- this->GetWindowSize(w, h);
- std::cout << "saving frame to test.png" << std::endl;
- GrabFramebuffer("test.png", 0, 0, w, h);
- return true;
- }
- };
- }
- return false;
- }
- bool Cylinder::Resize(int width, int height)
- {
- aspectRatio = width / static_cast<float>(height);
- projMatrix = glm::perspective(
- setFov(this->fovY),
- aspectRatio,
- this->fNear,
- 1000.0f);
- this->windowWidth = width;
- this->windowHeight = height;
- return true;
- }
- bool Cylinder::InitGeometry(void)
- {
- Vertex *vertices;
- glGenVertexArrays(1, &this->cylSideArray);
- glGenBuffers(1, &this->cylSideStrip);
- glBindVertexArray(this->cylSideArray);
- glBindBuffer(GL_ARRAY_BUFFER, this->cylSideStrip);
- glEnableVertexAttribArray(0);
- this->cylSideLength = genCylSide(this->numFacets, 2.0f, 1.0f, vertices);
- glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * this->cylSideLength, vertices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const GLvoid*>(sizeof(glm::vec3)));
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 3, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), reinterpret_cast<const GLvoid*>(2 * sizeof(glm::vec3)));
- delete[]vertices;
- // TODO
- // Geometry fuer Zylinder Kopf in VertexArray this->cylTopArray
- // und Buffer this->cylTopStrip initialisieren
- // Funktion genDisc benutzen
- // END TODO
- return true;
- }
- void Cylinder::NumFacetsChanged(APIVar<Cylinder, IntVarPolicy>& numFacets)
- {
- InitGeometry();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement