gpt4 book ai didi

c++ - glvertexpointer std::vector, struct memory opengl

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:10 25 4
gpt4 key购买 nike

我使用结构

struct Vertex
{
float pos[3];
float tex[2] = {0, 0};
float norm[3] = {0, 0, 0};
int index_mtl;
};

我用 vector

std::vector<Vertex> verts;

如何将“glvertexpointer”用于我的结构 vector 。

我想用那个函数画图。最后我没有成功。

void Obj_render::DrawObject()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, verts.data()->pos);

glDrawArrays(GL_TRIANGLES,0,3);
// deactivate vertex arrays after drawing
glDisableClientState(GL_VERTEX_ARRAY);

}

最佳答案

如果您使用这样的 struct,您的顶点不再紧密排列,您需要指定步幅:

glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, sizeof( Vertex ), &verts[0].pos );
glDrawArrays( GL_TRIANGLES, 0, 3 );
glDisableClientState(GL_VERTEX_ARRAY);

像这样:

#include <GL/glut.h>
#include <vector>

struct Vertex
{
float pos[3];
float tex[2];
float norm[3];
int index_mtl;
};

std::vector< Vertex > verts;
void display()
{
glClear( GL_COLOR_BUFFER_BIT );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -2, 2, -2, 2, -1, 1 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glColor3ub( 255, 0, 0 );

glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, sizeof( Vertex ), &verts[0].pos );
glDrawArrays( GL_TRIANGLES, 0, 3 );
glDisableClientState(GL_VERTEX_ARRAY);

glutSwapBuffers();
}

int main( int argc, char **argv )
{
Vertex tmp;
tmp.pos[0] = 0;
tmp.pos[1] = 0;
tmp.pos[2] = 0;
verts.push_back( tmp );
tmp.pos[0] = 1;
tmp.pos[1] = 0;
tmp.pos[2] = 0;
verts.push_back( tmp );
tmp.pos[0] = 1;
tmp.pos[1] = 1;
tmp.pos[2] = 0;
verts.push_back( tmp );

glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}

关于c++ - glvertexpointer std::vector, struct memory opengl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17703041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com