IsosurfacesGL.cpp 6.2 KB
Newer Older
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//#if 0

#include <vector>
#include "MyGL.h"
#include "eprintf.h"
#include "ConfigFile.h" //for isocolours
#include "IsosurfacesGL.h"

float *CubeVertices=0;
int *CubeIndices=0;
int CurrentVertex=-1;
int CurrentIndex=0;

int vertex_cb(p_ply_argument argument);
int face_cb(p_ply_argument argument);

inline void mult (float *o/*[4]*/, const float *m/*[16]*/, const float* v/*[4]*/)  //o=m*v
{
	
	for (int i=0;i<4;i++) {
		o[i]=0;
		for (int j=0;j<4;j++)
			o[i]+=m[j*4+i]*v[j];
	}
}

inline void normalize (float o[3])
{
	float mod=0;
	for (int i=0;i<3;i++)
		mod += o[i]*o[i];
	mod=sqrtf(mod);
	mod=1.0f/mod;
	for (int i=0;i<3;i++)
		o[i]*=mod;
}

bool AddModelToScene( const float *mat/*[16]*/, std::vector<float> &vertdata, 
#ifndef INDICESGL32
	std::vector<short> & vertindices, 
#else
	std::vector<GLuint> & vertindices,
#endif
	const char * model, bool water, bool colours, int set) 
{

	CurrentVertex=-1;
	CurrentIndex=0;
	 p_ply ply = ply_open(model, NULL, 0, NULL);
        if (!ply) {
			//rgh: files may not exist in the case of relative densities (0 density)
            //eprintf("ply returned null: file %s\n", model);
            return false;
        }
        if (!ply_read_header(ply)) {
            eprintf("ply: bad header in ply: file %s\n", model);
            return false;
        }

		int nvertices = ply_set_read_cb(ply, "vertex", "x", vertex_cb, nullptr, 0);
        ply_set_read_cb(ply, "vertex", "y", vertex_cb, nullptr, 1);
        ply_set_read_cb(ply, "vertex", "z", vertex_cb, nullptr, 2);

        ply_set_read_cb(ply, "vertex", "nx", vertex_cb, nullptr, 3);
        ply_set_read_cb(ply, "vertex", "ny", vertex_cb, nullptr, 4);
        ply_set_read_cb(ply, "vertex", "nz", vertex_cb, nullptr, 5);

		ply_set_read_cb(ply, "vertex", "red", vertex_cb, nullptr, 6);
        ply_set_read_cb(ply, "vertex", "green", vertex_cb, nullptr, 7);
        ply_set_read_cb(ply, "vertex", "blue", vertex_cb, nullptr, 8);
        ply_set_read_cb(ply, "vertex", "alpha", vertex_cb, nullptr, 9);

		//ply_set_read_cb(ply, "vertex", "texture_u", vertex_cb, this, 10);
		//ply_set_read_cb(ply, "vertex", "texture_v", vertex_cb, this, 11);

		CubeVertices = new float[nvertices*numComponents]; //xyz, nx, ny, nz, uv, rgba

        int ntriangles = ply_set_read_cb(ply, "face", "vertex_indices", face_cb, NULL, 0);

        CubeIndices=new int[3*ntriangles];

        //dprintf("PLY %s: v=%ld, t=%ld\n", model, nvertices, ntriangles);
        if (!ply_read(ply)) {
            eprintf("Problem in ply_read");
            return false;
        }
        ply_close(ply);

		for (int i = 0; i < 3* ntriangles; i++) {
			vertindices.push_back(CubeIndices[i]);
		}

	
		for (int i = 0; i < nvertices; i++) {
			//pos
			float V1[4];
			for (int j=0;j<3;j++)
				V1[j] = CubeVertices[i * numComponents +j];
			V1[3]=1;
			float V[4];
			mult(V, mat, V1);
			for (int j=0;j<3;j++)
					vertdata.push_back(V[j]);
			
			//normals (FIXME should transform with inverse transform, but we think the matrix has uniform scaling and inv transpose = m)
			//rgh beware: normals are (nx, ny, nz, 0) to avoid being translated !!!
			for (int j=0;j<3;j++)
				V1[j] = 0.f; //CubeVertices[i * numComponents + 3+j];
			V1[3]=0;
			mult (V, mat, V1);
			normalize(V);
			for (int j=0;j<3;j++)
				vertdata.push_back(V[j]);
			//colors (untransformed)
			if (!colours) {
				for (int j = 0; j < 4; j++)
					vertdata.push_back(isocolours[set][j]);
			} else {
				for (int j = 0; j < 4; j++)
					vertdata.push_back(CubeVertices[i * numComponents + 6 + j]);
			}
		}
		delete[] CubeVertices;
		delete[] CubeIndices;
		CubeVertices=0;
		CubeIndices=0;
		return true;
}

int vertex_cb(p_ply_argument argument) {
    long what;
	int ret=ply_get_argument_user_data(argument, nullptr, &what);
	if (!ret)
		return 0;
	if (what == 0)
		CurrentVertex++;
    if (what <=5 /* || what >=10*/ ){ //no uvs in these meshes.
		CubeVertices[CurrentVertex*numComponents+what]=(float)ply_get_argument_value(argument);
    } else {
        CubeVertices[CurrentVertex*numComponents+what]=(float)(ply_get_argument_value(argument)/255.0);
    }

    return 1;
}

int face_cb(p_ply_argument argument) {
    long length, value_index;
//	int v;
    ply_get_argument_property(argument, nullptr, &length, &value_index);
    //discard the first call with a 3
	//if (value_index == 0 && 3 != (v = (int)ply_get_argument_value(argument)))
	//	dprintf("Non-triangular face: %d vertices\n", v);
    if (value_index>=0 && value_index<=2)
        CubeIndices[CurrentIndex++]=(int)(ply_get_argument_value(argument));

    return 1;
}

GLenum PrepareGLiso (GLuint vao, GLuint vertbuffer, const std::vector<float> &vertdata, GLuint indbuffer,
 #ifndef INDICESGL32
	std::vector<short> & vertindices 
#else
	std::vector<GLuint> & vertindices
#endif
	)
{
	GLenum e;
	GLsizei stride = sizeof(float) * numComponents; 

	glBindVertexArray(vao);
	glBindBuffer(GL_ARRAY_BUFFER, vertbuffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertdata.size(), &vertdata[0], GL_STATIC_DRAW);
	if ((e = glGetError()) != GL_NO_ERROR)
		eprintf("opengl error %d, glBufferData, l %d\n", e, __LINE__);
				int offset = 0;

	//now pos[3], normal[3], color[4]
	//pos
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (const void *)offset);

	if (glGetError() != GL_NO_ERROR)
		eprintf("opengl error attrib pointer 0\n");

	//normal
	offset += 3 * sizeof(GLfloat); //3 floats
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (const void *)offset);

	if ((e=glGetError()) != GL_NO_ERROR)
		eprintf("opengl error attrib pointer 1\n");

	//color
	offset += 3 * sizeof(GLfloat); //6 floats
	glEnableVertexAttribArray(2);
	glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, stride, (const void *)offset);

	if (glGetError() != GL_NO_ERROR)
		eprintf("opengl error attrib pointer 2\n");

		// populate the index buffer
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indbuffer);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, 
#ifndef INDICESGL32
		sizeof(uint16_t)
#else
		sizeof(GLuint)
#endif
		* vertindices.size(),
		&vertindices[0], GL_STATIC_DRAW);

	if ((e=glGetError()) != GL_NO_ERROR)
		eprintf("opengl error\n");

	glBindVertexArray(0);
	glDisableVertexAttribArray(0);
	glDisableVertexAttribArray(1);
	glDisableVertexAttribArray(2);
	return e;
}

//#endif