开发者

OpenGL Not drawing

I am using GLFW as GUI for OpenGL projects. I am using my red book and testing code and well the first bit of code doesn't work at all. I want to say this is a GLFW problem because I don't have this problem in JOGL.

#include <iostream>
#include "GL/glfw.h"
#ifndef MAIN
#define MAIN
#include "GL/gl.h"
#include "GL/glu.h"
#endif
using namespace std;

int main()
{
    int running = GL_TRUE;
    glfwInit();

    if( !glfwOpenWindow( 300,300, 0,0,0,0,0,0, GLFW_WINDOW ) )
    {
        glfwTerminate();
        return 0;
    }

    while( running )
    {
        //GL Code here
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.0, 0.0, 0.0, 0.0);

        glColor3f(1.0, 1.0, 1.0);
        glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
        glBegin(GL_POLYGON);
            开发者_高级运维glVertex3f(0.25, 0.25, 0.0);
            glVertex3f(0.75, 0.25, 0.0);
            glVertex3f(0.75, 0.75, 0.0);
            glVertex3f(0.25, 0.75, 0.0);
        glEnd();
        glFlush();


        glfwSwapBuffers();
        // Check if ESC key was pressed or window was closed
        running = !glfwGetKey( GLFW_KEY_ESC ) &&
        glfwGetWindowParam( GLFW_OPENED );
    }

    glfwTerminate();

    return 0;
}


Adding glLoadIdentity(); fixed the code:

#include <iostream>
#include "GL/glfw.h"
#ifndef MAIN
#define MAIN
#include "GL/gl.h"
#include "GL/glu.h"
#endif
using namespace std;

int main()
{
    int running = GL_TRUE;
    glfwInit();

    if( !glfwOpenWindow( 640,480, 0,0,0,0,0,0, GLFW_WINDOW ) )
    {
        glfwTerminate();
        return 0;
    }

    while( running )
    {
        //GL Code here
        glLoadIdentity();

        glClearColor(0.0, 0.0, 0.0, 0.0);
        glClearDepth(1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glColor3f(1.0, 1.0, 1.0);
        glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
        glBegin(GL_POLYGON);
            glVertex3f(0.25, 0.25, 0.0);
            glVertex3f(0.75, 0.25, 0.0);
            glVertex3f(0.75, 0.75, 0.0);
            glVertex3f(0.25, 0.75, 0.0);
        glEnd();
        glFlush();


        glfwSwapBuffers();
        // Check if ESC key was pressed or window was closed
        running = !glfwGetKey( GLFW_KEY_ESC ) &&
        glfwGetWindowParam( GLFW_OPENED );
    }

    glfwTerminate();

    return 0;
}


you must specify wich matrix you want the glOrtho to transform

.....
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
......
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜