C++ OpenGL-网格未连接
我创建网格的代码是:C++ OpenGL-网格未连接,c++,opengl,grid,C++,Opengl,Grid,我创建网格的代码是: // Per-vertex position vectors static float planeVertices[16] = { 0,1,0,1, 0,0,0,1, 1,1,0,1, 1,0,0,1 }; // 12 faces each with 3 vertices (each face forms a triangle) (36 indices total) static unsigned short pla
// Per-vertex position vectors
static float planeVertices[16] =
{
0,1,0,1,
0,0,0,1,
1,1,0,1,
1,0,0,1
};
// 12 faces each with 3 vertices (each face forms a triangle) (36 indices total)
static unsigned short planeVertexIndices[4] =
{
0,1,2,3
};
拉丝机:
void drawPlane(float x, float y, float z){
glUseProgram(planeShader);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// Setup simple translation (model) matrix and pass to shader
GUMatrix4 planeModelTransform = GUMatrix4::translationMatrix(x, y, z);
static GLint planeShader_modelMatrixLocation = glGetUniformLocation(planeShader, "modelMatrix");
glUniformMatrix4fv(planeShader_modelMatrixLocation, 1, GL_FALSE, (const GLfloat*)&planeModelTransform);
glBindVertexArray(planeVAO);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, (const GLvoid*)1);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glBindVertexArray(0);
}
How its drawn in update:
int gridSize = 100;
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
for (float x = -gridSize/2; x < gridSize/2; x++){
for (float y = -gridSize/2; y < gridSize/2; y++){
drawPlane(x, y, z);
}
}
我从这一切中得到了什么结果:
要绘制平面,您需要获得
gridSize
并除以2
。由于它们都是int
,因此结果也将是int
。您可以通过将计算更改为resultfloat
(将其中一个强制转换为float,例如:=gridSize/2.0
)来修复它。尽管如此,如果一切正常,仍然会出现顶点断开连接的情况(因为精确性和z战斗)。如果出现这种情况,您可以通过在三角形之间共享顶点(例如,在索引数组中重新使用顶点)进行修复。要绘制平面,您需要获得gridSize
并除以2
。由于它们都是int
,因此结果也将是int
。您可以通过将计算更改为resultfloat
(将其中一个强制转换为float,例如:=gridSize/2.0
)来修复它。尽管如此,如果一切正常,仍然会出现顶点断开连接的情况(因为精确性和z战斗)。如果出现这种情况,可以通过在三角形之间共享顶点(例如,在索引数组中重新使用顶点)来修复。
#version 330
uniform float waveTime;
uniform float waveWidth;
uniform float waveHeight;
// UBO to store the camera projection and view matrices
uniform CameraMatrixBlock {
mat4 viewMatrix;
mat4 projectionMatrix;
} cam;
uniform mat4 modelMatrix;
layout (location=0) in vec4 vertexPos;
layout (location=1) in vec4 vertexColour;
out vec4 vp;
out vec4 colour;
void main(void) {
colour = vertexColour;
vec4 vp = vertexPos;
mat4 P = cam.projectionMatrix;
mat4 V = cam.viewMatrix;
mat4 mvpMatrix = P * V * modelMatrix;
vp.z = sin(waveWidth * vp.x + waveTime) * cos(waveWidth * vp.y + waveTime) * waveHeight;
gl_Position = mvpMatrix * vp;
}