Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:19:24+00:00 2026-05-26T04:19:24+00:00

While drawing a simple cube using opengl and glfw, the faces of the cube

  • 0

While drawing a simple cube using opengl and glfw, the faces of the cube appear to be transparent.
Here’s the code. Use the arrow keys to rotate. I’ve just encapsulated into a class, in my program.
Using Visual C++ Ultimate 2010.

#include "GAME.h"
using namespace std;
GAME::GAME() 
{
    glfwInit();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_LIGHT1); 
    glEnable(GL_NORMALIZE); 
}
int GAME::execute() 
{
    glfwOpenWindow(640, 320, 16, 16, 16, 16, 16, 16, GLFW_WINDOW);
    glfwSetWindowTitle("Viraj");
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glfwSetKeyCallback(events);
    running = true;
    while(glfwGetWindowParam(GLFW_OPENED))
    {
        glfwPollEvents();
        loop();
        render();
    }
    return 0;
}
void GAME::events(int key, int action)
{
    switch(key)
    {
    case GLFW_KEY_UP:
        glRotatef(10, 1, 0, 0);
        break;
    case GLFW_KEY_DOWN:
        glRotatef(-10, 1, 0, 0);
        break;
    case GLFW_KEY_RIGHT:
        glRotatef(10, 0, 1, 0);
        break;
    case GLFW_KEY_LEFT:
        glRotatef(-10, 0, 1, 0);
        break;
    }
}
int GAME::loop()
{
    return 0;
}
int GAME::render()
{
    int win_width;
    int win_height;
    glfwGetWindowSize(&win_width, &win_height);
    const float win_aspect = (float)win_width / (float)win_height;
    glViewport(0, 0, win_width, win_height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //glOrtho(-win_aspect, win_aspect, -1., 1., -1., 1.);
    gluPerspective(90, win_aspect, 1, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, 3.0, 0, 0, 0, 0.0, 1.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_LIGHT1); 
    glEnable(GL_NORMALIZE); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_QUADS);
    glRotatef(-1, 0, 1, 0);
    glColor3f(0.0f, 0.0f, 0.0f);
    //Front
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0);
    glVertex3f(1.0, 1.0, 0.0);
    glVertex3f(0.0, 1.0, 0.0);

    glColor3f(1.0f, 0.0f, 0.0f);
    //Left
    glVertex3f(1.0, 0.0, 0.0);
    glVertex3f(1.0, 0.0, -1.0);
    glVertex3f(1.0, 1.0, -1.0);
    glVertex3f(1.0, 1.0, 0.0);

    glColor3f(0.0f, 1.0f, 0.0f);
    //Back
    glVertex3f(1.0, 0.0, -1.0);
    glVertex3f(1.0, 1.0, -1.0);
    glVertex3f(0.0, 1.0, -1.0);
    glVertex3f(0.0, 0.0, -1.0);

    glColor3f(0.0f, 0.0f, 1.0f);
    //Right
    glVertex3f(0.0, 0.0, -1.0);
    glVertex3f(0.0, 1.0, -1.0);
    glVertex3f(0.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);

    glColor3f(1.0f, 0.0f, 1.0f);
    //Top
    glVertex3f(0.0, 1.0, -0.0);
    glVertex3f(0.0, 1.0, -1.0);
    glVertex3f(1.0, 1.0, -1.0);
    glVertex3f(1.0, 1.0, 0.0);

    glColor3f(1.0f, 1.0f, 0.0f);
    //Bottom
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0);
    glVertex3f(1.0, 0.0, -1.0);
    glVertex3f(0.0, 0.0, -1.0);

    glEnd();
    glfwSwapBuffers();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:19:25+00:00Added an answer on May 26, 2026 at 4:19 am

    I’m sorry to tell you, but your code is broken on several levels. Let me break it down for you:

    #include "GAME.h"
    using namespace std;
    GAME::GAME() 
    {
        glfwInit();
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_COLOR_MATERIAL);
        glEnable(GL_LIGHTING); 
        glEnable(GL_LIGHT0); 
        glEnable(GL_LIGHT1); 
        glEnable(GL_NORMALIZE); 
    }
    

    Here lies the first error: GLFW is a C library, and it needs to be initialized exactly once. The call to glfwInit() belongs into the main function, not a class constructor. The other function calls are OpenGL calls, however they require an active OpenGL context. At this point of the program there is no OpenGL context however, so all the calls you do have not effect at all.

    int GAME::execute() 
    {
        glfwOpenWindow(640, 320, 16, 16, 16, 16, 16, 16, GLFW_WINDOW);
        glfwSetWindowTitle("Viraj");
        glClearColor(1.0, 1.0, 1.0, 1.0);
    

    Again the nature of GLFW doesn’t go along with being used as part of a class. You can have only one window in GLFW and there’s only one event loop. This doesn’t map so well to classes and objects. You can have of course a class EventLoop or similar, but you wouldn’t use it like that.

    And then there is this next one line I’m surprised it actually compiled:

        glfwSetKeyCallback(events);
    

    events if a member function of class GAME, and unless this is a static member function, you can not use a class member function as callback, especially not to a C library, which doesn’t know classes. How should it know, which instance that event function belongs to? C++ doesn’t have the concept of closures or delegates, which was required for this (other languages do, for that very reason).

        running = true;
        while(glfwGetWindowParam(GLFW_OPENED))
        {
            glfwPollEvents();
            loop();
            render();
        }
        return 0;
    }
    

    What comes now is a classical newbie misconception:

    void GAME::events(int key, int action)
    {
        switch(key)
        {
        case GLFW_KEY_UP:
            glRotatef(10, 1, 0, 0);
            break;
        case GLFW_KEY_DOWN:
            glRotatef(-10, 1, 0, 0);
            break;
        case GLFW_KEY_RIGHT:
            glRotatef(10, 0, 1, 0);
            break;
        case GLFW_KEY_LEFT:
            glRotatef(-10, 0, 1, 0);
            break;
        }
    }
    

    The matrix manipulation calls make only sense withing the drawing code. Calling glRotate here will just mess with the matrix stack, but every sane OpenGL rendering function will initialize all state so some sane values at the beginning.

    What you want to do in a event handler is accumulating all input into variables, which you later use in the drawing code to setup and control rendering.

    int GAME::loop()
    {
        return 0;
    }
    

    If this is meant to loop, why is there no loop?

    int GAME::render()
    {
        int win_width;
        int win_height;
        glfwGetWindowSize(&win_width, &win_height);
        const float win_aspect = (float)win_width / (float)win_height;
    

    Only one detail missing: You need to set the viewport here. No big deal: glViewport(0, 0, win_width, win_height);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        //glOrtho(-win_aspect, win_aspect, -1., 1., -1., 1.);
        gluPerspective(90, win_aspect, 1, 100.0);
    

    Now this is actually good! You do setup the projection matrix in the render function. Way to go, stick to this pattern!

    UPDATE However the next line is wrong:

        gluLookAt(0, 0, 3.0, 0, 0, 0, 0.0, 1.0, 0.0);
    

    gluLookAt is a function meant to be performed on the modelview matrix. The modelview matrix is responsible for both placing models in world space and aligning the world to the view, hence the transformation are model→world, world→view of which you can reduce away the interim world step so it’s only model→view.

        glMatrixMode(GL_MODELVIEW);
    

    Here you would call glLoadIdentity(); gluLookAt(...);. It should become obvious now, why doing matrix manipulations in event handlers makes no sense.

    You should actually set the modelview matrix, with all transformations done from scratch here.

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    

    It’s a bit unusual to call glClear after setting up the matrices, viewport, etc., but not wrong. You may leave it that way.

    However before you begin rendering, you should, acutually must set all OpenGL state required. Remember those “initializing” OpenGL calls in the constructor. They belong here.

        glBegin(GL_QUADS);
    
        glColor3f(0.0f, 0.0f, 0.0f);
        //Front
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(1.0, 1.0, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
    
        glColor3f(1.0f, 0.0f, 0.0f);
        //Left
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, -1.0);
        glVertex3f(1.0, 1.0, -1.0);
        glVertex3f(1.0, 1.0, 0.0);
    
        glColor3f(0.0f, 1.0f, 0.0f);
        //Back
        glVertex3f(1.0, 0.0, -1.0);
        glVertex3f(1.0, 1.0, -1.0);
        glVertex3f(0.0, 1.0, -1.0);
        glVertex3f(0.0, 0.0, -1.0);
    
        glColor3f(0.0f, 0.0f, 1.0f);
        //Right
        glVertex3f(0.0, 0.0, -1.0);
        glVertex3f(0.0, 1.0, -1.0);
        glVertex3f(0.0, 1.0, 0.0);
        glVertex3f(0.0, 0.0, 0.0);
    
        glColor3f(1.0f, 0.0f, 1.0f);
        //Top
        glVertex3f(0.0, 0.0, -0.0);
        glVertex3f(0.0, 0.0, -1.0);
        glVertex3f(1.0, 0.0, -1.0);
        glVertex3f(1.0, 0.0, 0.0);
    
        glColor3f(1.0f, 1.0f, 0.0f);
        //Bottom
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, -1.0);
        glVertex3f(0.0, 0.0, -1.0);
    
        glEnd();
    

    If you want lighting you need to supply normals. But I’d not bother adding a large bunch of glNormal calls up there: The Immediate Mode (glBegin, glEnd) is outdated, I strongly recommend you learn about Vertex Arrays and Vertex Buffer Objects.

        glfwSwapBuffers();
        return 0;
    }
    

    Summarizing: Get the GLFW calls out of the class. GLFW is not object oriented. Just use it globally from the main function. Passing events to classes is in order, but you can’t use class member functions as callbacks to GLFW. You need to write some helper function

    extern GAME *pGame;
    void eventhandler(int key, int action)
    {
        pGame->event(key, action);
    }
    

    You may also have the GAME class manage a static list of all instances and provide a static member function, that passes the event to all instances in the list. Or use a singleton (however I think singletons are an anti pattern, and should be avoided).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using System.Drawing.Image in .Net to do a simple conversion from png to jpeg.
I am trying to make a simple drawing program in python using pygame. I
While running my application (which does a good bit of custom drawing) I get
I'm trying to get a simple triangle drawn in Java using LWJGL. I'm trying
Here is my very simple xaml: <Window x:Class=Test.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=Window1 Height=300 Width=300 Background=Black
I'm currently developping an iPad app which is using OpenGL to draw some very
Using .NET's System.Drawing.Graphics GDI stuff, I have a shape consting of two arrays of
I'm using listview control with the following parameters set: this.listView1.BackColor = System.Drawing.Color.Gainsboro; this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[]
I'm coding a simple car racing game. While trying to add shadows in my
I'm attempting to write a simple chat application in C using ncurses, both of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.