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

Method render

testbed/common/Dumbbell.cpp:117–173  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

115
116// Render the sphere at the correct position and with the correct orientation
117void Dumbbell::render(openglframework::Shader& shader,
118 const openglframework::Matrix4& worldToCameraMatrix) {
119
120 // Bind the shader
121 shader.bind();
122
123 // Set the model to camera matrix
124 shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
125 shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
126
127 // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
128 // model-view matrix)
129 const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
130 const openglframework::Matrix3 normalMatrix =
131 localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
132 shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
133
134 // Set the vertex color
135 rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
136 openglframework::Color currentColor = rigidBody != nullptr && rigidBody->isSleeping() ? mSleepingColor : mColor;
137 openglframework::Vector4 color(currentColor.r, currentColor.g, currentColor.b, currentColor.a);
138 shader.setVector4Uniform("globalVertexColor", color, false);
139
140 // Bind the VAO
141 mVAO.bind();
142
143 mVBOVertices.bind();
144
145 // Get the location of shader attribute variables
146 GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
147 GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
148
149 glEnableVertexAttribArray(vertexPositionLoc);
150 glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
151
152 mVBONormals.bind();
153
154 if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
155 if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
156
157 // For each part of the mesh
158 for (unsigned int i=0; i<getNbParts(); i++) {
159 glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
160 }
161
162 glDisableVertexAttribArray(vertexPositionLoc);
163 if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
164
165 mVBONormals.unbind();
166 mVBOVertices.unbind();
167
168 // Unbind the VAO
169 mVAO.unbind();
170
171 // Unbind the shader
172 shader.unbind();
173}
174

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