Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Triangle {
- unsigned int vao; // vertex array object id
- float sx, sy; // scaling
- float wTx, wTy; // translation
- BezierCurve bc1;
- BezierCurve bc2;
- BezierCurve bc3;
- BezierCurve bc4;
- BezierCurve bc5;
- BezierCurve bc6;
- BezierSurface bs;
- int vertexCoordsSize = 0;
- int colorSize = 0;
- public:
- Triangle() {
- Animate(0);
- }
- void Create() {
- for (int i = 0;i < 5;i++) {
- bc1.AddControlPoint(vec4(-1 + i*0.5, 1 , 0, 1));
- bc2.AddControlPoint(vec4(-1 + i*0.5, 1 - 0.5, (-(pow(i-2,2))/4)+0.9, 1));
- bc3.AddControlPoint(vec4(-1 + i*0.5, 1 - 1 , (-(pow(i - 2, 2)) / 4) + 1.9, 1));
- bc4.AddControlPoint(vec4(-1 + i*0.5, 1 - 1.5, (-(pow(i - 2, 2)) / 4) + 0.9, 1));
- bc5.AddControlPoint(vec4(-1 + i*0.5, 1 - 2 , 0, 1));
- }
- bs.AddCurve(bc1);
- bs.AddCurve(bc2);
- bs.AddCurve(bc3);
- bs.AddCurve(bc4);
- bs.AddCurve(bc5);
- /*vec4 a = bs.r(0, 0) * camera.Pinv() * camera.Vinv();
- vec4 b = bs.r(0, 0.25)* camera.Pinv() * camera.Vinv();
- vec4 c = bs.r(0.1, 0)* camera.Pinv() * camera.Vinv();
- vec4 d = bs.r(0.1, 0.25)* camera.Pinv() * camera.Vinv();
- vec4 e = bs.r(0.2, 0)* camera.Pinv() * camera.Vinv();
- vec4 f = bs.r(1, 1)* camera.Pinv() * camera.Vinv();*/
- glGenVertexArrays(1, &vao); // create 1 vertex array object
- glBindVertexArray(vao); // make it active
- unsigned int vbo[2]; // vertex buffer objects
- glGenBuffers(2, &vbo[0]); // Generate 2 vertex buffer objects
- // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
- static float vertexCoords[90000]; // vertex data on the CPU
- static float height[90000];
- for (int i = 0; i < 21; i++) {
- for (int j = 0; j < 21; j++)
- {
- float u = j * 0.05f;
- float v = i * 0.05f;
- vec4 vect = bs.r(u, v) * camera.Pinv() * camera.Vinv();
- vertexCoords[vertexCoordsSize] = vect.v[0];
- vertexCoords[vertexCoordsSize + 1] = vect.v[1];
- height[colorSize] = vect.v[2];
- float u2 = j * 0.05f;
- float v2 = (i+1)*0.05f;
- vec4 vect2 = bs.r(u2, v2) * camera.Pinv() * camera.Vinv();
- vertexCoords[vertexCoordsSize + 2] = vect2.v[0];
- vertexCoords[vertexCoordsSize + 3] = vect2.v[1];
- height[colorSize + 1] = vect2.v[2];
- vertexCoordsSize += 4;
- colorSize += 2;
- }
- vertexCoords[vertexCoordsSize] = vertexCoords[vertexCoordsSize - 2];
- vertexCoords[vertexCoordsSize + 1] = vertexCoords[vertexCoordsSize - 1];
- height[colorSize] = height[colorSize - 1];
- vertexCoords[vertexCoordsSize + 2] = (bs.r(0, (i+1)*0.05f) * camera.Pinv() * camera.Vinv()).v[0];
- vertexCoords[vertexCoordsSize + 3] = (bs.r(0, (i+1)*0.05f) * camera.Pinv() * camera.Vinv()).v[1];
- height[colorSize + 1] = (bs.r(0, (i+1)*0.05f) * camera.Pinv() * camera.Vinv()).v[2];
- vertexCoordsSize += 4;
- colorSize += 2;
- }
- /*vertexCoords[vertexCoordsSize++] = a.v[0];
- vertexCoords[vertexCoordsSize++] = a.v[1];
- vertexCoords[vertexCoordsSize++] = b.v[0];
- vertexCoords[vertexCoordsSize++] = b.v[1];
- vertexCoords[vertexCoordsSize++] = c.v[0];
- vertexCoords[vertexCoordsSize++] = c.v[1];
- vertexCoords[vertexCoordsSize++] = d.v[0];
- vertexCoords[vertexCoordsSize++] = d.v[1];
- vertexCoords[vertexCoordsSize++] = e.v[0];
- vertexCoords[vertexCoordsSize++] = e.v[1];
- vertexCoords[vertexCoordsSize++] = f.v[0];
- vertexCoords[vertexCoordsSize++] = f.v[1];*/
- glBufferData(GL_ARRAY_BUFFER, // copy to the GPU
- sizeof(vertexCoords), // number of the vbo in bytes
- vertexCoords, // address of the data array on the CPU
- GL_STATIC_DRAW); // copy to that part of the memory which is not modified
- // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
- glEnableVertexAttribArray(0);
- // Data organization of Attribute Array 0
- glVertexAttribPointer(0, // Attribute Array 0
- 2, GL_FLOAT, // components/attribute, component type
- GL_FALSE, // not in fixed point format, do not normalized
- 0, NULL); // stride and offset: it is tightly packed
- // vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
- glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
- static float vertexColors[90000];
- int a = 0;
- for (int i = 0;i < colorSize;i++) { // vertex data on the CPU
- vertexColors[a] = height[i];
- vertexColors[a + 1] = 1- height[i];
- vertexColors[a + 2] = 0;
- a += 3;
- }
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColors), vertexColors, GL_STATIC_DRAW); // copy to the GPU
- // Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
- glEnableVertexAttribArray(1); // Vertex position
- // Data organization of Attribute Array 1
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
- }
- void Animate(float t) {
- sx = 1; // sinf(t);
- sy = 1; //cosf(t);
- wTx = 0; //4 * cosf(t / 2);
- wTy = 0; //4 * sinf(t / 2);
- }
- void Draw() {
- mat4 Mscale(sx, 0, 0, 0,
- 0, sy, 0, 0,
- 0, 0, 0, 0,
- 0, 0, 0, 1); // model matrix
- mat4 Mtranslate(1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 0, 0,
- wTx, wTy, 0, 1); // model matrix
- mat4 MVPTransform = Mscale * Mtranslate * camera.V() * camera.P();
- // set GPU uniform matrix variable MVP with the content of CPU variable MVPTransform
- int location = glGetUniformLocation(shaderProgram, "MVP");
- if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform); // set uniform variable MVP to the MVPTransform
- else printf("uniform MVP cannot be set\n");
- glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
- glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCoordsSize/2); // draw a single triangle with vertices defined in vao
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement