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

Method render

testbed/common/HeightField.cpp:91–147  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

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