c - 用顶点数组 : nothing displayed 绘制一系列点

标签 c opengl glut vertex-array

我正在尝试执行以下操作:有一个点数组,通过几个 GLfloats 表示。用户应单击窗口中的一个点以显示另一个点。添加一个点时,窗口会被重绘,点会显示成一条线。这样用户就可以在窗口的某些位置点击绘制线。
在这段代码中,每当有点击时,我都会在 vector 中推一个点,但不会添加这些点,所以我只看到没有点的灰色窗口(APPLE_GRAY 在标题中定义)。

#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <stdlib.h>
#include "utility.h"

const int width=500;
const int height=500;

GLfloat* points;
size_t size=100;
int count=0;

void pushPoint(GLfloat x, GLfloat y)
{
    count++;
    if(count>size)
    {
        size+=100;
        points=(GLfloat*)realloc(points,2*size*sizeof(GLfloat));
    }
    points[2*count-2]=x;
    points[2*count-1]=y;
}

void init()
{
    points=(GLfloat*)malloc(2*size*sizeof(GLfloat));
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glLoadIdentity();
    glOrtho(0, width, height, 0, 0, 1);
    glViewport(0, 0, 500, 500);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
}

void display()
{
    glClearColor(APPLE_GRAY);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor4f(RED);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, points);
    glDrawElements(GL_LINE_LOOP,count,GL_FLOAT,points);
    glFlush();
    glDisableClientState(GL_VERTEX_ARRAY);
}

void mouse(int button, int state, int x, int y)
{
    if(state)
    {
        pushPoint(x, y);
        glutPostRedisplay();
    }
}

void motion(int x, int y)
{
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width,height);
    glutCreateWindow("Test");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    init();
    glutMainLoop();
    return 0;
}

最佳答案

Again , glutInitDisplayMode() 需要在 glutCreateWindow() 之前调用。

GL_FLOAT 不是 glDrawElements()type 的有效值.此外,points 不是索引数组。

使用glDrawArrays() :

#include <GL/glut.h>

const int width=500;
const int height=500;

GLfloat* points;
size_t size=100;
int count=0;

void pushPoint(GLfloat x, GLfloat y)
{
    count++;
    if(count>size)
    {
        size+=100;
        points=(GLfloat*)realloc(points,2*size*sizeof(GLfloat));
    }
    points[2*count-2]=x;
    points[2*count-1]=y;
}

void init()
{
    points=(GLfloat*)malloc(2*size*sizeof(GLfloat));
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, height, 0, 0, 1);
    glEnable(GL_DEPTH_TEST);
}

void display()
{
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3ub(255,0,0);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, points);
    glDrawArrays(GL_LINE_LOOP,0,count);
    glDisableClientState(GL_VERTEX_ARRAY);
    glutSwapBuffers();
}

void mouse(int button, int state, int x, int y)
{
    if(state)
    {
        pushPoint(x, y);
        glutPostRedisplay();
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width,height);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Test");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    init();
    glutMainLoop();
    return 0;
}

关于c - 用顶点数组 : nothing displayed 绘制一系列点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12963080/

相关文章:

linux - 为 Linux 构建 3D 窗口管理器

opengl - VBO 如何连接到 VAO

c++ - glutBitmapString() 未在此范围内声明

c++ - C++ 中的 23 位用户定义类型

c - Arduino伺服功能

c - 不要将 "make test"作为 Travis CI 的一部分运行?

opengl - 为什么 GL_FRONT 和 GL_BACK 在 opengl 3.2 以后被弃用

c++ - FPS 相机实现错误(OpenGL/GLUT/C++)

c - 通过循环遍历 vector 链接列表,使用 opengl1 绘制线条

c++ - 有人可以解释一下这个冒号#语法吗