2012-10-18 29 views
1

我試圖做以下操作:有一個點的數組,通過GLfloats夫婦表示。用戶應點擊窗口中的一個點會出現另一個點。當添加一個點時,窗口將被重新繪製,點應顯示與一條線連接。因此,用戶應能夠點擊窗口的某些位置來繪製線條。
在這段代碼中,每當有一個點擊時,我會在向量中推一個點,但不添加點,所以我只看到沒有點的灰色窗口(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; 
} 
+0

不知道你的矩陣是正確的。在init()中,它調用glLoadIdentity(),但它從不明確表示什麼矩陣(這裏,我可能期望MODEL_VIEW)。然後有一個glOrtho()加載一個正交投影矩陣,但它在* glMatrixMode(GL_PROJECTION)之前稱爲*。另外,我可能會從一個固定的2D點開始,直到我得到渲染工作,只是爲了解耦GL問題來尋找內存alloc/dealloc - justathought ;-) –

回答

2

AgainglutInitDisplayMode()需要glutCreateWindow()之前調用

GL_FLOAT對於typeglDrawElements()不是有效值。另外,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; 
} 
+0

如果我在glutCreateWindow之前放入glutInitDisplayMode,我會得到一個白色的窗口,如果我把它放在init方法中,窗口繪製正確。 –

+0

是否將'glFlush()'改爲'glutSwapBuffers()'? – genpfault

+1

你說得對,我沒有使用glutSwapBuffer。 –