C++ OpenGL生成三角形网格

C++ OpenGL生成三角形网格,c++,arrays,opengl,mesh,opengl-4,C++,Arrays,Opengl,Mesh,Opengl 4,我是OpenGl的初学者,我正在尝试用OpenGl绘制三角形网格,我的问题是它没有绘制,我不明白为什么。此外,如果打印顶点数组,则所有顶点的x坐标和y坐标保持不变。x坐标和z坐标都应位于+1和-1之间。当m_meshResolution=1时,坐标似乎是正确的,否则就不正确了。我认为有一个更简单的方法来尝试这样做,所以所有的建议都是受欢迎的 int size = m_meshResolution * m_meshResolution * 2 * 3 * 3; float* positions =

我是OpenGl的初学者,我正在尝试用OpenGl绘制三角形网格,我的问题是它没有绘制,我不明白为什么。此外,如果打印顶点数组,则所有顶点的x坐标和y坐标保持不变。x坐标和z坐标都应位于+1和-1之间。当
m_meshResolution=1
时,坐标似乎是正确的,否则就不正确了。我认为有一个更简单的方法来尝试这样做,所以所有的建议都是受欢迎的

int size = m_meshResolution * m_meshResolution * 2 * 3 * 3;
float* positions = new float[size]();

double triangleWidth = 2 / m_meshResolution;
float startX = -1.0f;
float startZ = 1.0f;
float x1 = 0;
float x2 = 0;
float z1 = 0;
float z2 = 0;
int index = 0;
//For each row
for (int r = 0; r < m_meshResolution; r++) {
    //For each column
    for (int c = 0; c < m_meshResolution; c++) {
        x1 = startX + c*divider;
        x2 = startX + (c+1)*divider;
        z1 = startZ - r*divider;
        z2 = startZ -(r+1)*divider;
        //Generate one triangle
        positions[index++] = x1;
        positions[index++] = 0;
        positions[index++] = z1;
        positions[index++] = x2;
        positions[index++] = 0;
        positions[index++] = z2;
        positions[index++] = x2;
        positions[index++] = 0;
        positions[index++] = z1;
        //Generate other triangle
        positions[index++] = x1;
        positions[index++] = 0;
        positions[index++] = z1;
        positions[index++] = x1;
        positions[index++] = 0;
        positions[index++] = z2;
        positions[index++] = x2;
        positions[index++] = 0;
        positions[index++] = z2;
    }

//Set buffers
glGenBuffers(1, &m_positionBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);

glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/);
glEnableVertexAttribArray(0);

//Draw
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, m_meshResolution*m_meshResolution*2*3);
int size=m_网格分辨率*m_网格分辨率*2*3*3;
浮动*位置=新浮动[大小]();
双三角形宽度=2/m_网格分辨率;
浮点数startX=-1.0f;
浮球startZ=1.0f;
浮点x1=0;
浮动x2=0;
浮点数z1=0;
浮点数z2=0;
int指数=0;
//每行
对于(int r=0;r
positions
是一个指针,
sizeof(positions)
返回4或8个字节,这取决于体系结构,但
glBufferData
的第二个参数告诉我们

大小 指定缓冲区对象的新数据存储的大小(以字节为单位)


您应该使用
sizeof(float)*size
作为第二个参数。

positions
是指针,
sizeof(positions)
返回4或8个字节,这取决于体系结构,但
glBufferData
的第二个参数告诉我们

大小 指定缓冲区对象的新数据存储的大小(以字节为单位)


您应该使用
sizeof(float)*size
作为第二个参数。

双三角形宽度=2/m\u网格分辨率;
如果
m\u网格分辨率
是整数,则进行整数除法。哇,完全没有注意到这一点,谢谢,但绘图问题仍然存在。
双三角形宽度=2/m\u网格分辨率;
如果
m\u网格分辨率,则进行整数除法olution
是一个整数。哇,完全忽略了这一点,谢谢,但是绘图的问题仍然存在。非常感谢。这为我解决了绘图问题。我爱StackOverflow非常感谢。这为我解决了绘图问题。我爱StackOverflow