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

Method render

testbed/common/ConvexMesh.cpp:139–195  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

137
138// Render the sphere at the correct position and with the correct orientation
139void ConvexMesh::render(openglframework::Shader& shader,
140 const openglframework::Matrix4& worldToCameraMatrix) {
141
142 // Bind the shader
143 shader.bind();
144
145 // Set the model to camera matrix
146 shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
147 shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
148
149 // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
150 // model-view matrix)
151 const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
152 const openglframework::Matrix3 normalMatrix =
153 localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
154 shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
155
156 // Set the vertex color
157 rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
158 openglframework::Color currentColor = rigidBody != nullptr && rigidBody->isSleeping() ? mSleepingColor : mColor;
159 openglframework::Vector4 color(currentColor.r, currentColor.g, currentColor.b, currentColor.a);
160 shader.setVector4Uniform("globalVertexColor", color, false);
161
162 // Bind the VAO
163 mVAO.bind();
164
165 mVBOVertices.bind();
166
167 // Get the location of shader attribute variables
168 GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
169 GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
170
171 glEnableVertexAttribArray(vertexPositionLoc);
172 glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)nullptr);
173
174 mVBONormals.bind();
175
176 if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)nullptr);
177 if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
178
179 // For each part of the mesh
180 for (unsigned int i=0; i<getNbParts(); i++) {
181 glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)nullptr);
182 }
183
184 glDisableVertexAttribArray(vertexPositionLoc);
185 if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
186
187 mVBONormals.unbind();
188 mVBOVertices.unbind();
189
190 // Unbind the VAO
191 mVAO.unbind();
192
193 // Unbind the shader
194 shader.unbind();
195}
196

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