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

Method render

testbed/common/Sphere.cpp:95–150  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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