add ImplicitCloth demo, by Stan Melax

add SimpleOpenGL3App, used in ImplicitCloth demo. The Bullet 3.x demos should use this as a template, it will clean up some of the mess.
This commit is contained in:
erwincoumans
2013-11-09 12:12:33 -08:00
parent a6bc61ad1a
commit 8f3051fdf1
35 changed files with 3297 additions and 2035 deletions

View File

@@ -322,7 +322,7 @@ static GLint lines_position=0;
static GLint lines_colour=0;
GLuint lineVertexBufferObject=0;
GLuint lineVertexArrayObject=0;
GLuint lineIndexVbo = 0;
@@ -720,17 +720,21 @@ void GLInstancingRenderer::InitShaders()
int SCALE_BUFFER_SIZE = (m_maxNumObjectCapacity*sizeof(float)*3);
linesShader = gltLoadShaderPair(linesVertexShader,linesFragmentShader);
glLinkProgram(linesShader);
glUseProgram(linesShader);
lines_ModelViewMatrix = glGetUniformLocation(linesShader, "ModelViewMatrix");
lines_ProjectionMatrix = glGetUniformLocation(linesShader, "ProjectionMatrix");
lines_colour=glGetUniformLocation(linesShader, "colour");
lines_position=glGetAttribLocation(linesShader, "position");
glLinkProgram(linesShader);
glUseProgram(linesShader);
glGenBuffers(1, &lineVertexBufferObject);
glGenVertexArrays(1, &lineVertexArrayObject);
glBindVertexArray(lineVertexArrayObject);
glGenBuffers(1, &lineVertexBufferObject);
glGenBuffers(1, &lineIndexVbo);
glBindVertexArray(0);
//glGetIntegerv(GL_ALIASED_LINE_WIDTH_RANGE, range);
glGetIntegerv(GL_SMOOTH_LINE_WIDTH_RANGE, lineWidthRange);
@@ -1315,16 +1319,85 @@ void GLInstancingRenderer::drawPoints(const float* positions, const float color[
glBindBuffer(GL_ARRAY_BUFFER, lineVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, numPoints*pointStrideInBytes, positions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, lineVertexBufferObject);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
// glBindBuffer(GL_ARRAY_BUFFER, lineVertexBufferObject);
glEnableVertexAttribArray(0);
int numFloats = 3;//pointStrideInBytes/sizeof(float);
glVertexAttribPointer(0, numFloats, GL_FLOAT, GL_FALSE, pointStrideInBytes, 0);
int numFloats = pointStrideInBytes/sizeof(float);
glVertexAttribPointer(0, numFloats, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, numPoints);
glBindVertexArray(0);
glPointSize(1);
}
void GLInstancingRenderer::drawLines(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, const unsigned int* indices, int numIndices, float pointDrawSize)
{
glBindBuffer(GL_ARRAY_BUFFER, m_data->m_vbo);
GLint err = glGetError();
b3Assert(err==GL_NO_ERROR);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,0);
int curOffset = 0;
err = glGetError();
b3Assert(err==GL_NO_ERROR);
glUseProgram(linesShader);
glUniformMatrix4fv(lines_ProjectionMatrix, 1, false, &projectionMatrix[0]);
glUniformMatrix4fv(lines_ModelViewMatrix, 1, false, &modelviewMatrix[0]);
glUniform4f(lines_colour,color[0],color[1],color[2],color[3]);
// glPointSize(pointDrawSize);
glBindVertexArray(lineVertexArrayObject);
err = glGetError();
b3Assert(err==GL_NO_ERROR);
glBindBuffer(GL_ARRAY_BUFFER, lineVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, numPoints*pointStrideInBytes, positions, GL_STATIC_DRAW);
err = glGetError();
b3Assert(err==GL_NO_ERROR);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, lineVertexBufferObject);
glEnableVertexAttribArray(0);
err = glGetError();
b3Assert(err==GL_NO_ERROR);
int numFloats = pointStrideInBytes/sizeof(float);
glVertexAttribPointer(0, numFloats, GL_FLOAT, GL_FALSE, 0, 0);
err = glGetError();
b3Assert(err==GL_NO_ERROR);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, lineIndexVbo);
int indexBufferSizeInBytes = numIndices*sizeof(int);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBufferSizeInBytes, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER,0,indexBufferSizeInBytes,indices);
glDrawElements(GL_LINES, numIndices, GL_UNSIGNED_INT,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// for (int i=0;i<numIndices;i++)
// printf("indicec[i]=%d]\n",indices[i]);
err = glGetError();
b3Assert(err==GL_NO_ERROR);
glBindVertexArray(0);
err = glGetError();
b3Assert(err==GL_NO_ERROR);
glPointSize(1);
err = glGetError();
b3Assert(err==GL_NO_ERROR);
}
void GLInstancingRenderer::drawLine(const float from[4], const float to[4], const float color[4], float lineWidth)
{
GLint err = glGetError();

View File

@@ -90,6 +90,7 @@ public:
struct GLInstanceRendererInternalData* getInternalData();
void drawLine(const float from[4], const float to[4], const float color[4], float lineWidth=1);
void drawLines(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, const unsigned int* indices, int numIndices, float pointDrawSize);
void drawPoints(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, float pointDrawSize);
void drawPoint(const float* position, const float color[4], float pointSize=1);
void updateCamera();

View File

@@ -288,6 +288,8 @@ void GLPrimitiveRenderer::drawTexturedRect(float x0, float y0, float x1, float y
glUniform2fv(m_data->m_positionUniform, 1, (const GLfloat *)&p);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
err = glGetError();
assert(err==GL_NO_ERROR);
err = glGetError();
@@ -316,6 +318,7 @@ void GLPrimitiveRenderer::drawTexturedRect(float x0, float y0, float x1, float y
err = glGetError();
assert(err==GL_NO_ERROR);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
err = glGetError();
assert(err==GL_NO_ERROR);

View File

@@ -3,7 +3,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "Bullet3Common/b3Logging.h"
// Load the shader from the source text
void gltLoadShaderSrc(const char *szShaderSrc, GLuint shader)
@@ -90,8 +90,8 @@ GLuint gltLoadShaderPair(const char *szVertexProg, const char *szFragmentProg)
&actualLen,
infoLog);
b3Error("Warning/Error in GLSL shader:\n");
b3Error("%s\n",infoLog);
printf("Warning/Error in GLSL shader:\n");
printf("%s\n",infoLog);
glDeleteProgram(hReturn);
return (GLuint)NULL;
}

View File

@@ -0,0 +1,215 @@
#include "SimpleOpenGL3App.h"
#ifdef __APPLE__
#include "OpenGLWindow/MacOpenGLWindow.h"
#else
#include "GL/glew.h"
#ifdef _WIN32
#include "OpenGLWindow/Win32OpenGLWindow.h"
#else
//let's cross the fingers it is Linux/X11
#include "OpenGLWindow/X11OpenGLWindow.h"
#endif //_WIN32
#endif//__APPLE__
#include "OpenGLWindow/GLPrimitiveRenderer.h"
#include "OpenGLWindow/GLInstancingRenderer.h"
#include "Bullet3Common/b3Vector3.h"
#include "../btgui/OpenGLTrueTypeFont/fontstash.h"
#include "../btgui/OpenGLWindow/TwFonts.h"
struct SimpleInternalData
{
GLuint m_fontTextureId;
};
static SimpleOpenGL3App* gApp=0;
void SimpleResizeCallback( float width, float height)
{
gApp->m_instancingRenderer->resize(width,height);
gApp->m_primRenderer->setScreenSize(width,height);
}
static GLuint BindFont(const CTexFont *_Font)
{
GLuint TexID = 0;
glGenTextures(1, &TexID);
glBindTexture(GL_TEXTURE_2D, TexID);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, _Font->m_TexWidth, _Font->m_TexHeight, 0, GL_RED, GL_UNSIGNED_BYTE, _Font->m_TexBytes);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
return TexID;
}
SimpleOpenGL3App::SimpleOpenGL3App( const char* title, int width,int height)
{
gApp = this;
m_data = new SimpleInternalData;
m_window = new b3gDefaultOpenGLWindow();
b3gWindowConstructionInfo ci;
ci.m_title = title;
ci.m_width = width;
ci.m_height = height;
m_window->createWindow(ci);
m_window->setWindowTitle(title);
glClearColor(1,1,1,1);
m_window->startRendering();
#ifndef __APPLE__
glewInit();
#endif
m_primRenderer = new GLPrimitiveRenderer(width,height);
m_instancingRenderer = new GLInstancingRenderer(128*1024,4*1024*1024);
m_instancingRenderer->init();
m_instancingRenderer->resize(width,height);
m_instancingRenderer->InitShaders();
m_window->setMouseMoveCallback(b3DefaultMouseMoveCallback);
m_window->setMouseButtonCallback(b3DefaultMouseButtonCallback);
m_window->setKeyboardCallback(b3DefaultKeyboardCallback);
m_window->setWheelCallback(b3DefaultWheelCallback);
m_window->setResizeCallback(SimpleResizeCallback);
TwGenerateDefaultFonts();
m_data->m_fontTextureId = BindFont(g_DefaultNormalFont);
}
void SimpleOpenGL3App::drawText( const char* txt, int posX, int posY)
{
//
//printf("str = %s\n",unicodeText);
int xpos=0;
int ypos=0;
float dx;
int measureOnly=0;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/*
if (m_useTrueTypeFont)
{
float yoffset = 0.f;
if (m_retinaScale==2.0f)
{
yoffset = -12;
}
Translate(r);
sth_draw_text(m_font,
1,m_fontScaling,
r.x,r.y+yoffset,
unicodeText,&dx, m_screenWidth,m_screenHeight,measureOnly,m_retinaScale);
} else
*/
{
//float width = 0.f;
int pos=0;
float color[]={0.2f,0.2,0.2f,1.f};
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,m_data->m_fontTextureId);
//float width = r.x;
float extraSpacing = 0.;
int startX = posX;
int startY = posY;
while (txt[pos])
{
int c = txt[pos];
//r.h = g_DefaultNormalFont->m_CharHeight;
//r.w = g_DefaultNormalFont->m_CharWidth[c]+extraSpacing;
int endX = startX+g_DefaultNormalFont->m_CharWidth[c];
int endY = startY+g_DefaultNormalFont->m_CharHeight;
//Gwen::Rect rect = r;
//Translate( rect );
float currentColor[]={0.2f,0.2,0.2f,1.f};
m_primRenderer->drawTexturedRect(startX, startY, endX, endY, currentColor,g_DefaultNormalFont->m_CharU0[c],g_DefaultNormalFont->m_CharV0[c],g_DefaultNormalFont->m_CharU1[c],g_DefaultNormalFont->m_CharV1[c]);
//DrawTexturedRect(0,r,g_DefaultNormalFont->m_CharU0[c],g_DefaultNormalFont->m_CharV0[c],g_DefaultNormalFont->m_CharU1[c],g_DefaultNormalFont->m_CharV1[c]);
// DrawFilledRect(r);
startX = endX;
//startY = endY;
pos++;
}
glBindTexture(GL_TEXTURE_2D,0);
}
glDisable(GL_BLEND);
}
void SimpleOpenGL3App::drawGrid(int gridSize)
{
b3Vector3 gridColor = b3MakeVector3(0.5,0.5,0.5);
for(int i=-gridSize;i<=gridSize;i++)
{
GLint err = glGetError();
b3Assert(err==GL_NO_ERROR);
m_instancingRenderer->drawLine(b3MakeVector3(float(i),0,float(-gridSize)),b3MakeVector3(float(i),0,float(gridSize)),gridColor);
err = glGetError();
b3Assert(err==GL_NO_ERROR);
m_instancingRenderer->drawLine(b3MakeVector3(float(-gridSize),0,float(i)),b3MakeVector3(float(gridSize),0,float(i)),gridColor);
}
m_instancingRenderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(1,0,0),b3MakeVector3(1,0,0),3);
m_instancingRenderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(0,1,0),b3MakeVector3(0,1,0),3);
m_instancingRenderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(0,0,1),b3MakeVector3(0,0,1),3);
m_instancingRenderer->drawPoint(b3MakeVector3(1,0,0),b3MakeVector3(1,0,0),6);
m_instancingRenderer->drawPoint(b3MakeVector3(0,1,0),b3MakeVector3(0,1,0),6);
m_instancingRenderer->drawPoint(b3MakeVector3(0,0,1),b3MakeVector3(0,0,1),6);
}
SimpleOpenGL3App::~SimpleOpenGL3App()
{
delete m_primRenderer ;
m_window->closeWindow();
delete m_window;
delete m_data ;
}
void SimpleOpenGL3App::swapBuffer()
{
m_window->endRendering();
m_window->startRendering();
}

View File

@@ -0,0 +1,25 @@
#ifndef SIMPLE_OPENGL3_APP_H
#define SIMPLE_OPENGL3_APP_H
#include "OpenGLWindow/GLInstancingRenderer.h"
#include "OpenGLWindow/GLPrimitiveRenderer.h"
#include "OpenGLWindow/b3gWindowInterface.h"
struct SimpleOpenGL3App
{
struct SimpleInternalData* m_data;
class b3gWindowInterface* m_window;
class GLPrimitiveRenderer* m_primRenderer;
class GLInstancingRenderer* m_instancingRenderer;
SimpleOpenGL3App(const char* title, int width,int height);
virtual ~SimpleOpenGL3App();
void drawGrid(int gridSize=10);
void swapBuffer();
void drawText( const char* txt, int posX, int posY);
};
#endif //SIMPLE_OPENGL3_APP_H

View File

@@ -18,14 +18,14 @@ subject to the following restrictions:
#include "OpenGLInclude.h"
#include "Bullet3Common/b3Vector3.h"
//#include "Bullet3Common/b3Vector3.h"
#include "Win32InternalWindowData.h"
#include <stdio.h>
static void printGLString(const char *name, GLenum s) {
const char *v = (const char *) glGetString(s);
b3Printf("GL %s = %s\n", name, v);
printf("%s = %s\n",name, v);
}

View File

@@ -17,7 +17,7 @@ subject to the following restrictions:
#include "Win32Window.h"
#include "OpenGLInclude.h"
#include "Bullet3Common/b3Vector3.h"
#include <wchar.h>
static InternalData2* sData = 0;

View File

@@ -29,6 +29,8 @@
"GLInstancingRenderer.h",
"GLPrimitiveRenderer.h",
"GLPrimitiveRenderer.cpp",
"SimpleOpenGL3App.cpp",
"SimpleOpenGL3App.h",
"LoadShader.cpp",
"LoadShader.h",
"gwenWindow.cpp",