MCPcopy Create free account
hub / github.com/DanielChappuis/reactphysics3d / render

Method render

testbed/common/Capsule.cpp:96–151  ·  view source on GitHub ↗

Render the sphere at the correct position and with the correct orientation

Source from the content-addressed store, hash-verified

94
95// Render the sphere at the correct position and with the correct orientation
96void Capsule::render(openglframework::Shader& shader, const openglframework::Matrix4& worldToCameraMatrix) {
97
98 // Bind the shader
99 shader.bind();
100
101 // Set the model to camera matrix
102 shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
103 shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
104
105 // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
106 // model-view matrix)
107 const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
108 const openglframework::Matrix3 normalMatrix =
109 localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
110 shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
111
112 // Set the vertex color
113 rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
114 openglframework::Color currentColor = rigidBody != nullptr && rigidBody->isSleeping() ? mSleepingColor : mColor;
115 openglframework::Vector4 color(currentColor.r, currentColor.g, currentColor.b, currentColor.a);
116 shader.setVector4Uniform("globalVertexColor", color, false);
117
118 // Bind the VAO
119 mVAO.bind();
120
121 mVBOVertices.bind();
122
123 // Get the location of shader attribute variables
124 GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
125 GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
126
127 glEnableVertexAttribArray(vertexPositionLoc);
128 glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)nullptr);
129
130 mVBONormals.bind();
131
132 if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)nullptr);
133 if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
134
135 // For each part of the mesh
136 for (unsigned int i=0; i<getNbParts(); i++) {
137 glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)nullptr);
138 }
139
140 glDisableVertexAttribArray(vertexPositionLoc);
141 if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
142
143 mVBONormals.unbind();
144 mVBOVertices.unbind();
145
146 // Unbind the VAO
147 mVAO.unbind();
148
149 // Unbind the shader
150 shader.unbind();
151}
152
153// Create the Vertex Buffer Objects used to render with OpenGL.

Callers

nothing calls this directly

Calls 10

setMatrix4x4UniformMethod · 0.80
getUpperLeft3x3MatrixMethod · 0.80
setMatrix3x3UniformMethod · 0.80
isSleepingMethod · 0.80
setVector4UniformMethod · 0.80
getAttribLocationMethod · 0.80
bindMethod · 0.45
getTransposeMethod · 0.45
getInverseMethod · 0.45
unbindMethod · 0.45

Tested by

no test coverage detected