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

Method render

testbed/common/Box.cpp:100–155  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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