Results 1 to 7 of 7

Thread: Completely clear the screen

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    21

    Completely clear the screen

    I'm pretty new to openGL, and im stuck in a certain game im trying to create. It is a basic artillery game where you select weapons before the game. The weapons selection screen is to be completely different

    Now, how do i completely clear the screen?

    When i use
    glColor3f(0.2, 0.8, 0.2);
    glClear(GL_COLOR_BUFFER_BIT);

    Just the background seems to clear to the specified color. Maybe im doing something completely wrong?

    Thanks.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2001
    Location
    Princeton, NJ
    Posts
    380

    Re: Completely clear the screen

    you can set the clear color with
    glClearColor

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    21

    Re: Completely clear the screen

    err, actually that IS the command i am using (i mistyped it the first time without any reference). Here is the command im using straight from the code (at least as far as i have gotten):

    /* Clear the window to black */
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    and here is the code in entirety, just for reference:

    #include <windows.h>
    #include <gl/glut.h>

    #include <iostream.h>
    #include <string.h>

    int x1=100;
    int y1=100;

    // Keep track of windows changing width and height
    GLfloat windowWidth=600;
    GLfloat windowHeight=800;

    // Constant font size
    void *font=GLUT_BITMAP_HELVETICA_18;

    // Variable to hold background texture
    GLubyte* TextureLoadBitmap(char* filename, int *w, int *h);

    //----------------------------
    // Available fonts |
    // |
    // GLUT_BITMAP_8_BY_13 |
    // GLUT_BITMAP_9_BY_15 |
    // GLUT_BITMAP_TIMES_ROMAN_10 |
    // GLUT_BITMAP_TIMES_ROMAN_24 |
    // GLUT_BITMAP_HELVETICA_10 |
    // GLUT_BITMAP_HELVETICA_12 |
    // GLUT_BITMAP_HELVETICA_18 |
    //----------------------------

    //************************************************** ***************************************
    // Function Prototypes
    //************************************************** ***************************************
    void SplashScreen();
    void test();
    void DispTank();
    void init();
    void displayfancychar(GLfloat x, GLfloat y, char *text);
    void initialsetup();

    //************************************************** ***************************************
    // Function to display fancier text on the screen, with antialiasing
    //************************************************** ***************************************
    void displayfancychar(GLfloat x, GLfloat y, char *text)
    {
    char *p;

    glPushMatrix();
    glTranslatef(x, y, 0);

    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glLineWidth(20.0);
    for (p = text; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
    glPopMatrix();
    }
    //************************************************** ***************************************
    // Function to display text on the screen
    //************************************************** ***************************************
    void bitmapfontoutput(int x, int y, char *string, void *font)
    { int len, i;
    glRasterPos2i(x, y); // Locate Raster Position in 2d-space
    len = (int) strlen(string); // Find length of string
    for (i = 0; i < len; i++) // Loop through plotting all characters in font style
    glutBitmapCharacter(font, string[i]);

    }


    //************************************************** ***************************************
    // Displays the intro screen
    //************************************************** ***************************************
    void SplashScreen()
    {
    /*int teximageWidth, teximageHeight;
    GLubyte *teximage;*/

    glClear(GL_COLOR_BUFFER_BIT);

    /*
    teximage = TextureLoadBitmap("a:BITMAP.bmp",&teximageWidth,
    &teximageHeight);

    glTexImage2D(GL_TEXTURE_2D, 0, 3, teximageWidth, teximageHeight,
    0, GL_RGB, GL_UNSIGNED_BYTE, teximage);

    glEnable(GL_TEXTURE);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, teximageWidth, teximageHeight, 0,
    GL_RGB, GL_UNSIGNED_BYTE, teximage);
    glDisable(GL_TEXTURE);
    */

    glColor3f(0.2, 0.8, 0.2);
    displayfancychar(175,600,"Ballistics");
    glColor3f(1.0, 1.0, 1.0);
    bitmapfontoutput(750,25,"Version 1.0",GLUT_BITMAP_TIMES_ROMAN_10);
    glColor3f(0.0, 0.0, 1.0);
    bitmapfontoutput(325,150,"Press Enter",GLUT_BITMAP_TIMES_ROMAN_24);

    glFlush();
    glutSwapBuffers(); // Flush drawing commands
    }

    //************************************************** **********************
    // Called by GLUT library when the window has chanaged size
    //************************************************** **********************
    void ChangeSize(GLsizei windowWidth, GLsizei windowHeight)
    {
    // Prevent a divide by zero
    if(windowHeight == 0)
    windowHeight = 1;

    // Set Viewport to window dimensions
    glViewport(0, 0, windowWidth, windowHeight);

    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Keep the square square, this time, save calculated
    // width and height for later use
    if (windowWidth <= windowHeight)
    {
    windowHeight = windowHeight*windowHeight/windowWidth;
    windowWidth = 600;
    }
    else
    {
    windowWidth = windowWidth*windowWidth/windowWidth;
    windowHeight = 800;
    }

    // Set the clipping volume
    gluOrtho2D(0, windowWidth, 0, windowHeight);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    //************************************************** ***************************************
    // Accepts an initial x and y value for position, and draws a tank facing left
    //************************************************** ***************************************
    void DispTank()
    {
    // Set current drawing color to brown
    // R G B
    glColor3f(0.55f, 0.4f, 0.12f);

    bitmapfontoutput(10,500,"Here is the tank",font);

    // Set Drawing color to green
    glColor3f(0.1, 0.9, 0.1);

    // Draw lower part of tank
    glBegin(GL_POLYGON);
    glVertex2i(x1+10, y1+7);
    glVertex2i(x1+10, y1+5);
    glVertex2i(x1+12, y1+1);
    glVertex2i(x1+26, y1+1);
    glVertex2i(x1+28, y1+5);
    glVertex2i(x1+28, y1+7);
    glEnd();

    // Draw turret ex1cept gun
    glBegin(GL_POLYGON);
    glVertex2i(x1+15, y1+7);
    glVertex2i(x1+15, y1+9);
    glVertex2i(x1+18, y1+11);
    glVertex2i(x1+23, y1+11);
    glVertex2i(x1+23, y1+7);
    glEnd();

    //glutSwapBuffers();
    }

    //************************************************** ***************************************
    // Function used for display tests
    //************************************************** ***************************************
    void test()
    {
    /* Clear the window to black */
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    /* Draw a vertical line down the center of the window */
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINES);
    glVertex2i(12, 0);
    glVertex2i(20, 5);
    glEnd();

    glutSwapBuffers();
    }


    //************************************************** ***************************************
    // Function to check what key is pressed, and determine a result
    //************************************************** ***************************************
    void HandleKeyboard(unsigned char keyval, int xloc, int yloc)
    {

    switch(keyval)
    {
    case 13:
    // Set Viewport to window dimensions
    glViewport(0, 0, windowWidth, windowHeight);
    glColor3i(0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);

    break;
    /*case 27:
    close();
    break;*/
    default:
    break;

    }
    glutPostRedisplay();

    }

    //************************************************** ***************************************
    // Sets up the initial display properties
    //************************************************** ***************************************
    void initialsetup()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.0,0.0,0.0,1.0);
    }
    //************************************************** ***************************************
    // Main program
    //************************************************** ***************************************
    void main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 600) ; // set window size
    glutInitWindowPosition(0,0) ; // set the window position correctly on screen

    glutCreateWindow("Ballistics");
    //glutFullScreen();
    initialsetup();
    glutDisplayFunc(SplashScreen);
    glutKeyboardFunc(HandleKeyboard);

    //glutSpecialFunc(ProcessSpecialKey);

    glutReshapeFunc(ChangeSize);

    glutMainLoop();

    }

    [This message has been edited by darkenreaper57 (edited 02-22-2004).]

  4. #4
    Senior Member Regular Contributor
    Join Date
    Oct 2001
    Location
    Princeton, NJ
    Posts
    380

    Re: Completely clear the screen

    I;m a bit confused as to your problem, what exactly are you trying to do and what is going wrong?

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    21

    Re: Completely clear the screen

    All i want to do is display the splash screen, which eventually will either be a .bmp textured to a quad, and then when the user presses enter, clear the screen entirely (of displayed text, etc.) to another screen, where he or she will select weapons. Note that i have not yet coded a lot of this yet, but i want to know how to do this in advance.

    Previously, when i used the commands

    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.0,0.0,0.0,1.0);

    in the function

    void HandleKeyboard(unsigned char keyval, int xloc, int yloc)
    {

    switch(keyval)
    {
    case 13:
    // Set Viewport to window dimensions
    glViewport(0, 0, windowWidth, windowHeight);
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.0,0.0,0.0,1.0);

    break;
    /*case 27:
    close();
    break;*/
    default:
    break;

    }
    glutPostRedisplay();

    }

    Nothing happened. Do i need to make a new viewport or something?

    Thanks.

  6. #6
    Senior Member Regular Contributor
    Join Date
    Mar 2004
    Posts
    143

    Re: Completely clear the screen

    Yes you clear the screen but your drawing code is still the same.

    The way OpenGL works:

    -clear buffers
    -draw stuff
    -swap buffers

    Since you dont change the "draw stuff" portion you will always see the same thing no matter how often your clear the buffers (especialy when you call glutPostRedisplay() after that).

    Code :
    glutDisplayFunc(SplashScreen);
    This sets the "draw stuff" to your SplashScreen() function. You dont change that in your code nor is there a if-then-else statement in your code that would either draw this-or-that.

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    21

    Re: Completely clear the screen

    ok, i think i understand it now, thanks

Similar Threads

  1. Clear Screen
    By Velovix in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 08-08-2011, 04:15 PM
  2. how to clear portion of screen
    By rohit in forum OpenGL: Windows
    Replies: 1
    Last Post: 09-17-2004, 05:21 AM
  3. how to clear screen after drawing?
    By coda in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 12-13-2003, 11:29 PM
  4. clear screen
    By suresh kannan in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 03-24-2001, 12:32 AM
  5. Clear screen
    By SKSlayer in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-07-2000, 05:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean