为啥我的 glDrawArrays 没有绘制我存储的点?
Posted
技术标签:
【中文标题】为啥我的 glDrawArrays 没有绘制我存储的点?【英文标题】:Why does my glDrawArrays is not drawing the points I have stored?为什么我的 glDrawArrays 没有绘制我存储的点? 【发布时间】:2020-01-28 19:29:37 【问题描述】:我正在尝试创建一个简单的程序来绘制我单击的点。
我声明了一个struct
,它包含每个点的坐标和颜色。
我声明了一个包含点列表的全局vector< Point >
。 鼠标输入被很好地记录并打印在cout
上,但没有绘制点。
知道为什么我在 GLUT 的初始化中丢失了吗?
#include <GL/freeglut_std.h>
#include <GL/gl.h>
#include <vector>
#include <iostream>
using namespace std;
struct Point
float x, y;
unsigned char r, g, b, a;
;
vector< Point > points;
void drawPoints()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// draw
glColor3ub( 255, 255, 255 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
glPointSize( 3.0 );
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glFlush();
glutSwapBuffers();
void OnMouseClick(int button, int state, int x, int y)
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
//store the x,y value where the click happened
Point p = Point();
p.x = (float)x;
p.y = (float)y;
p.r = 255;
p.g = 255;
p.b = 255;
p.a = 255;
points.push_back(p);
for (vector<Point>::iterator it = points.begin(); it != points.end(); ++it)
cout << it->x << " " << it->y << " " << flush;
cout << endl;
glutPostRedisplay();
int main(int argc, char **argv)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL - Drawing points");
glutDisplayFunc(drawPoints);
glutMouseFunc(OnMouseClick);
glutMainLoop();
return 0;
【问题讨论】:
【参考方案1】:坐标系不匹配,要么将 GLUT 的鼠标坐标转换为您选择的 glOrhto()
坐标系,要么更改该调用以匹配 GLUT 的鼠标坐标系。
第二种方式的例子:
#include <GL/glut.h>
#include <vector>
using namespace std;
struct Point
float x, y;
unsigned char r, g, b, a;
;
vector< Point > points;
void drawPoints()
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
GLdouble w = glutGet( GLUT_WINDOW_WIDTH );
GLdouble h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, h, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// draw
if( !points.empty() )
glColor3ub( 255, 255, 255 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer( 2, GL_FLOAT, sizeof( Point ), &points[ 0 ].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( Point ), &points[ 0 ].r );
glPointSize( 3.0 );
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glutSwapBuffers();
void OnMouseClick( int button, int state, int x, int y )
if( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
//store the x,y value where the click happened
Point p = Point();
p.x = (float)x;
p.y = (float)y;
p.r = 255;
p.g = 255;
p.b = 255;
p.a = 255;
points.push_back( p );
glutPostRedisplay();
int main( int argc, char **argv )
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 500, 500 );
glutInitWindowPosition( 100, 100 );
glutCreateWindow( "OpenGL - Drawing points" );
glutDisplayFunc( drawPoints );
glutMouseFunc( OnMouseClick );
glutMainLoop();
return 0;
【讨论】:
以上是关于为啥我的 glDrawArrays 没有绘制我存储的点?的主要内容,如果未能解决你的问题,请参考以下文章
在 glDrawArrays() 上调试 SIGKILL。苹果手机
glDrawArrays 仅在原点绘制一个点(在 ubuntu 上带有 glut 的 OpenGL)