summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/beziercurve.cc
blob: fbdef8edf4f66b25374b638bfafca9520328e835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*****
 * drawbezierpatch.cc
 * Author: John C. Bowman
 *
 * Render a Bezier curve.
 *****/

#include "bezierpatch.h"
#include "beziercurve.h"

namespace camp {

#ifdef HAVE_GL

extern GLint noNormalShader;
extern GLint pixelShader;
extern void setUniforms(GLint shader); 

std::vector<vertexData1> BezierCurve::vertexbuffer;
std::vector<GLuint> BezierCurve::indices;

std::vector<pixelData> Pixel::vertexbuffer;

GLuint BezierCurve::vertsBufferIndex;
GLuint BezierCurve::elemBufferIndex;

void BezierCurve::init(double res, const triple& Min, const triple& Max)
{
  this->res=res;
  res2=res*res;
  this->Min=Min;
  this->Max=Max;

  const size_t nbuffer=10000;
  vertexbuffer.reserve(nbuffer);
  indices.reserve(nbuffer);
}

// Use a uniform partition to draw a Bezier patch.
// p is an array of 4 triples representing the control points.
// Ii are the vertices indices.
void BezierCurve::render(const triple *p, GLuint I0, GLuint I1)
{
  triple p0=p[0];
  triple p1=p[1];
  triple p2=p[2];
  triple p3=p[3];
  if(Distance1(p0,p1,p2,p3) < res2) { // Segment is flat
    triple P[]={p0,p3};
    if(!offscreen(2,P)) {
      indices.push_back(I0);
      indices.push_back(I1);
    }
  } else { // Segment is not flat
    if(offscreen(4,p)) return;
    triple m0=0.5*(p0+p1);
    triple m1=0.5*(p1+p2);
    triple m2=0.5*(p2+p3);
    triple m3=0.5*(m0+m1);
    triple m4=0.5*(m1+m2);
    triple m5=0.5*(m3+m4);
      
    triple s0[]={p0,m0,m3,m5};
    triple s1[]={m5,m4,m2,p3};
      
    GLuint i0=vertex(m5);
      
    render(s0,I0,i0);
    render(s1,i0,I1);
  }
}

void BezierCurve::render(const triple *p, bool straight) 
{
  GLuint i0=vertex(p[0]);
  GLuint i3=vertex(p[3]);
    
  if(straight) {
    indices.push_back(i0);
    indices.push_back(i3);
  } else
    render(p,i0,i3);
}
  
void BezierCurve::draw()
{
  if(indices.size() == 0)
    return;
  
  const size_t size=sizeof(GLfloat);
  static const size_t bytestride=sizeof(vertexData1);

  GLuint vao;
  glGenVertexArrays(1,&vao);
  glBindVertexArray(vao);
  createBuffers();
    
  camp::setUniforms(noNormalShader);
  
  const GLint posAttrib=glGetAttribLocation(noNormalShader, "position");
  const GLint materialAttrib=glGetAttribLocation(noNormalShader,"material");

  glBindBuffer(GL_ARRAY_BUFFER,vertsBufferIndex);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,elemBufferIndex);

  glVertexAttribPointer(posAttrib,3,GL_FLOAT,GL_FALSE,bytestride,(void *) 0);
  glEnableVertexAttribArray(posAttrib);

  glVertexAttribIPointer(materialAttrib,1,GL_INT,bytestride,(void *) (3*size));
  glEnableVertexAttribArray(materialAttrib);

  glFlush(); // Workaround broken MSWindows drivers for Intel GPU
  glDrawElements(GL_LINES,indices.size(),GL_UNSIGNED_INT,(void*)(0));
  
  glDisableVertexAttribArray(posAttrib);
  glDisableVertexAttribArray(materialAttrib);
  
  glBindBuffer(GL_ARRAY_BUFFER,0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
  glUseProgram(0);

  glBindVertexArray(0);
  glDeleteVertexArrays(1,&vao);
  
  glDeleteBuffers(1,&vertsBufferIndex);
  glDeleteBuffers(1,&elemBufferIndex);
}

void Pixel::queue(const triple& p, double width)
{
  vertex(p,width);
}

void Pixel::draw()
{
  if(vertexbuffer.size() == 0)
    return;

  const size_t size=sizeof(GLfloat);
  static const size_t bytestride=sizeof(pixelData);

  GLuint vbo;
  glGenBuffers(1,&vbo);
  
  GLuint vao;
  glGenVertexArrays(1,&vao);
  glBindVertexArray(vao);

  camp::setUniforms(pixelShader); 
  
  const GLint posAttrib=glGetAttribLocation(pixelShader, "position");
  const GLint materialAttrib=glGetAttribLocation(pixelShader,"material");
  const GLint widthAttrib=glGetAttribLocation(pixelShader,"width");

  glBindBuffer(GL_ARRAY_BUFFER,vbo);
  glBufferData(GL_ARRAY_BUFFER,bytestride*vertexbuffer.size(),vertexbuffer.data(),GL_STATIC_DRAW);

  glVertexAttribPointer(posAttrib,3,GL_FLOAT,GL_FALSE,bytestride,(void*)(0));
  glEnableVertexAttribArray(posAttrib);
  
  glVertexAttribIPointer(materialAttrib,1,GL_INT,bytestride,(void *) (3*size));
  glEnableVertexAttribArray(materialAttrib);
  
  glVertexAttribPointer(widthAttrib,1,GL_FLOAT,GL_FALSE,bytestride,(void *) (4*size));
  glEnableVertexAttribArray(widthAttrib);
  
  glDrawArrays(GL_POINTS,0,vertexbuffer.size());

  glDisableVertexAttribArray(posAttrib);
  glDisableVertexAttribArray(materialAttrib);
  glDisableVertexAttribArray(widthAttrib);
  
  glBindBuffer(GL_ARRAY_BUFFER,0);
  glUseProgram(0);

  glBindVertexArray(0);
  glDeleteVertexArrays(1,&vao);
}

#endif

} //namespace camp