使用opengl的简单glfw绘制点没有显示任何内容

时间:2012-11-14 19:37:06

标签: c opengl gcc glfw

我可以看到屏幕已经被清除为红色,但屏幕中央没有任何一点,这里的任何方式都是完整的代码:

#include "GL/glfw.h"
void render_loop()
{
    glClearColor ( .7, .1, .1, 1.0f );
    glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glViewport(0,0,1024,768);
    glMatrixMode(GL_PROJECTION);
    //gluPerspective( 65.0, (double)1024/(double)768, 1.0, 60.0 );
    glOrtho(0,1024,768,0,100,-100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPointSize(10);
    glBegin(GL_POINTS);
    glColor4f(1,1,1,1);
    glVertex3f(512,384,0);
    glEnd();

    glfwSwapBuffers();

}
int main ( int argc, char* argv[] )
{
    //init glfw
    glfwInit();

    glfwOpenWindow ( 1024, 768, 8, 8, 8, 8, 24, 0, GLFW_WINDOW );

    do {
        render_loop();
    } while ( glfwGetWindowParam ( GLFW_OPENED ) );

    glfwTerminate();
}

编译使用:

john@Linux:~> gcc --version
gcc (SUSE Linux) 4.7.1 20120723 [gcc-4_7-branch revision 189773]
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

如果我编译它使用gluPerspective( 65.0, (double)1024/(double)768, 1.0, 60.0 );仍然相同,屏幕中心没有任何意义,我使用这个makefile来编译它:

#makefile for example1 on linux

CC=gcc  -std=c99  -Wno-unused-function -Wno-unused-variable


SRC=\
a.c \

OBJS=$(SRC:.c=.o)

NAME=testglpoints

CFLAGS =   -Wall $(INCLUDE) 

LFLAGS =\
-lGL \
-lglut \
-lGLU \
-lGLEW \
-ldl \
-lpthread \
-lm \
-lglfw


all: debug

debug:override CFLAGS = -g3 -O0 -Wall 

debug:$(OBJS)
    $(CC)  $(CFLAGS) $(LFLAGS)   $(SRC)    -o $(NAME) 

clean:
    @rm -f *.o

1 个答案:

答案 0 :(得分:1)

glLoadIdentity()之前需要glOrtho()

glOrtho() 设置当前矩阵,乘以。因此,在调用之前,将当前矩阵设置为已知良好值(单位矩阵,通过glLoadIdentity())。