2 Bullet Continuous Collision Detection and Physics Library
\r
3 Copyright (c) 2008 Advanced Micro Devices
\r
5 This software is provided 'as-is', without any express or implied warranty.
\r
6 In no event will the authors be held liable for any damages arising from the use of this software.
\r
7 Permission is granted to anyone to use this software for any purpose,
\r
8 including commercial applications, and to alter it and redistribute it freely,
\r
9 subject to the following restrictions:
\r
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
\r
13 3. This notice may not be removed or altered from any source distribution.
\r
17 #include "gl_win.h" //for OpenGL stuff
\r
19 #include "stb_image.h"
\r
23 #include "LinearMath/btScalar.h"
\r
27 struct vertex_struct
\r
35 class btVertexBufferDescriptor;
\r
37 class piece_of_cloth
\r
45 if(cpu_buffer) delete [] cpu_buffer;
\r
53 m_vertexBufferDescriptor = NULL;
\r
61 vertex_struct* cpu_buffer;
\r
62 unsigned int* indices;
\r
63 btVertexBufferDescriptor *m_vertexBufferDescriptor;
\r
65 double x_offset, y_offset, z_offset;
\r
79 #endif //USE_GPU_COPY
\r
83 glEnable(GL_TEXTURE_2D);
\r
84 glBindTexture (GL_TEXTURE_2D, m_texture);
\r
86 glEnable(GL_DEPTH_TEST);
\r
88 glColor3f(1.0f, 1.0f, 1.0f);
\r
91 glBindBuffer(GL_ARRAY_BUFFER, clothVBO);
\r
92 #ifndef USE_GPU_COPY
\r
93 // Upload data to VBO
\r
94 // Needed while we're not doing interop
\r
95 glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_struct)*width*height, &(cpu_buffer[0]), GL_DYNAMIC_DRAW);
\r
98 glEnableClientState(GL_VERTEX_ARRAY);
\r
100 glEnableClientState(GL_NORMAL_ARRAY);
\r
102 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
\r
104 glBindTexture(GL_TEXTURE_2D, m_texture);
\r
105 #ifdef USE_GPU_COPY
\r
106 error = glGetError();
\r
109 glVertexPointer( 3, GL_FLOAT, sizeof(vertex_struct), (const GLvoid *)0 );
\r
110 error = glGetError();
\r
111 glNormalPointer( GL_FLOAT, sizeof(vertex_struct), (const GLvoid *)(sizeof(float)*3) );
\r
112 error = glGetError();
\r
113 glTexCoordPointer( 2, GL_FLOAT, sizeof(vertex_struct), (const GLvoid *)(sizeof(float)*6) );
\r
114 error = glGetError();
\r
118 glVertexPointer( 3, GL_FLOAT, sizeof(vertex_struct), reinterpret_cast< GLvoid* >(&(cpu_buffer[0].pos[0])) );
\r
119 //glNormalPointer( 3, sizeof(vertex_struct), reinterpret_cast< GLvoid* >(&(cpu_buffer[0].normal[0])) );
\r
120 glTexCoordPointer( 2, GL_FLOAT, sizeof(vertex_struct), reinterpret_cast< GLvoid* >(&(cpu_buffer[0].texcoord[0])) );
\r
123 glDrawElements(GL_TRIANGLES, (height-1 )*(width-1)*3*2, GL_UNSIGNED_INT, indices);
\r
124 // glDisableClientState(GL_NORMAL_ARRAY);
\r
125 glDisableClientState(GL_VERTEX_ARRAY);
\r
126 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
\r
127 glBindTexture(GL_TEXTURE_2D, 0);
\r
128 #ifdef USE_GPU_COPY
\r
129 error = glGetError();
\r
130 glBindBuffer(GL_ARRAY_BUFFER, 0);
\r
131 error = glGetError();
\r
136 void create_texture(std::string filename)
\r
138 int width,height,n;
\r
139 unsigned char *data = stbi_load(filename.c_str(), &width, &height, &n, 0);
\r
142 //premake project happens to be 2 levels above the root of Bullet, so try this instead:
\r
143 std::string newname = "../../"+filename;
\r
144 data = stbi_load(newname.c_str(), &width, &height, &n, 0);
\r
147 GLubyte* image=new GLubyte[512*256*4];
\r
148 for(int y=0;y<256;++y)
\r
151 GLubyte* pi=image+y*512*4;
\r
152 for(int x=0;x<512;++x)
\r
155 const GLubyte b=180;
\r
156 GLubyte c=b+((s+t&1)&1)*(255-b);
\r
157 pi[0]=pi[1]=pi[2]=c;pi[3]=1;pi+=4;
\r
164 for (int i=0;i<width;i++)
\r
166 for (int j=0;j<height;j++)
\r
168 int offsetx = (512-width)/2;
\r
169 int offsety = (256-height)/2;
\r
171 GLubyte* pi=image+((j+offsety)*512+i+offsetx)*4;
\r
172 const GLubyte* src=data+(j*width+i)*4;
\r
184 printf("ERROR: could not load bitmap, using placeholder\n");
\r
187 glGenTextures(1,(GLuint*)&m_texture);
\r
188 glBindTexture(GL_TEXTURE_2D,m_texture);
\r
189 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
\r
190 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
\r
191 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
\r
192 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
\r
193 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
\r
194 gluBuild2DMipmaps(GL_TEXTURE_2D,4,512,256,GL_RGBA,GL_UNSIGNED_BYTE,image);
\r
198 void create_buffers(int width_, int height_)
\r
205 cpu_buffer = new vertex_struct[width*height];
\r
206 memset(cpu_buffer, 0, width*height*sizeof(vertex_struct));
\r
209 // Initial test data for rendering
\r
210 for(int y = 0; y < height; y++)
\r
212 for(int x = 0; x < width; x++)
\r
214 double coord = btSin(x/5.0)*0.01;
\r
217 cpu_buffer[y*width+x].pos[0] = (x/((float)(width-1)))*1;
\r
218 cpu_buffer[y*width+x].pos[1] = coord;
\r
219 cpu_buffer[y*width+x].pos[2] = (y/((float)(height-1)))*1;
\r
220 cpu_buffer[y*width+x].normal[0] = 1;
\r
221 cpu_buffer[y*width+x].normal[1] = 0;
\r
222 cpu_buffer[y*width+x].normal[2] = 0;
\r
223 cpu_buffer[y*width+x].texcoord[0] = 1*x/((float)(width-1));
\r
224 cpu_buffer[y*width+x].texcoord[1] = (1.f-4*y/((float)(height-1)));
\r
229 // Generate and fill index array for rendering
\r
230 indices = new unsigned int[width*3*2+2 + height*width*3*2];
\r
232 for(int y = 0; y < height-1; y++)
\r
234 for(int x = 0; x < width-1; x++)
\r
236 // *3 indices/triangle, *2 triangles/quad
\r
237 int baseIndex = (x + y*(width-1))*3*2;
\r
238 indices[baseIndex] = x + y*width;
\r
239 indices[baseIndex+1] = x+1 + y*width;
\r
240 indices[baseIndex+2] = x+width + y*width;
\r
243 indices[baseIndex+3] = x + 1 + y*width;
\r
244 indices[baseIndex+4] = x+(width+1) + y*width;
\r
245 indices[baseIndex+5] = x+width + y*width;
\r
248 #ifdef USE_GPU_COPY
\r
250 glGenBuffers(1, &clothVBO);
\r
251 glBindBuffer(GL_ARRAY_BUFFER, clothVBO);
\r
252 // Do initial upload to ensure that the buffer exists on the device
\r
253 glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_struct)*width*height, &(cpu_buffer[0]), GL_DYNAMIC_DRAW);
\r
254 int error = glGetError();
\r
255 glBindBuffer(GL_ARRAY_BUFFER, 0);
\r