Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
i want help modifying this code please.

1. i want to reduce the number of generated pixels to 400x200.
2. i want it to generate with a scroll bar such that i can scroll back and forth through the generations (instead of it generating one by one with 'g' key, and not being able to view the previous images.
I also want to know if there is any way to make the program play the generationss like a video. (instead of using the scroll bar metioned above)
i am going to be using 'C language' with the VS2010 Express compiler for compiling.
i cannot seem to find a complete enough reference file on the internet for opengl's glut.h/glut32.dll.

C#
//*************************************************************************
//
//  File Name   : PixGen.c
//  Author      : Ali BaderEddin
//  Created     : December 2003
//  Modified    : March 2010
//
//  Description : Generate new image with random colors on mouse click.
//
//*************************************************************************

#include <stdlib.h>
#include <time.h>
#include <gl/glut.h>

//  Avoid showing up the console window
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

//  constants representing the window size
#define WINDOW_WIDTH 512
#define WINDOW_HEIGHT 512

//  Initialization
void init ();

//  Callback functions
void display (void);
void mouse (int button, int state, int x, int y);
void keyboard (unsigned char key, int x, int y);

//  Support Functions
void centerOnScreen ();

//  define the window position on screen
int window_x;
int window_y;

//  variable representing the window title
char *window_title = "Image Generator";

//  Tells whether to display the window full screen or not
//  Press Alt + Esc to exit a full screen.
int full_screen = 0;

//  Generates a random image...
void generateImage ();

//  Represents the pixel buffer in memory
GLubyte buffer[WINDOW_WIDTH][WINDOW_HEIGHT][3];

//-------------------------------------------------------------------------
//  Set OpenGL program initial state.
//-------------------------------------------------------------------------
void init ()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    srand (time(NULL));
    generateImage();
}

//-------------------------------------------------------------------------
//  This function is passed to glutDisplayFunc in order to display
//  OpenGL contents on the window.
//-------------------------------------------------------------------------
void display (void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawPixels(WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGB,
                    GL_UNSIGNED_BYTE, buffer);
    glutSwapBuffers ();
}

//-------------------------------------------------------------------------
//  This function is passed to the glutMouseFunc and is called
//  whenever the mouse is clicked.
//-------------------------------------------------------------------------
void mouse (int button, int state, int x, int y)
{
    if (state == GLUT_DOWN)
    {
        generateImage ();
        glutPostRedisplay ();
    }
}

//-------------------------------------------------------------------------
//  This function is passed to the glutKeyboardFunc and is called
//  whenever the user hits a key.
//-------------------------------------------------------------------------
void keyboard (unsigned char key, int x, int y)
{
    switch (key)
    {
        case 'g':
            generateImage ();
            glutPostRedisplay ();
            break;
        case 27:
            exit (0);
    }
}

//-------------------------------------------------------------------------
//  This function sets the window x and y coordinates
//  such that the window becomes centered
//-------------------------------------------------------------------------
void centerOnScreen ()
{
    window_x = (glutGet (GLUT_SCREEN_WIDTH) - WINDOW_WIDTH)/2;
    window_y = (glutGet (GLUT_SCREEN_HEIGHT) - WINDOW_HEIGHT)/2;
}

//-------------------------------------------------------------------------
//  Generate new image with random colors
//-------------------------------------------------------------------------
void generateImage ()
{
    int i, j;

    for (i = 0; i < WINDOW_WIDTH; i++)
    {
        for (j = 0; j < WINDOW_HEIGHT; j++)
        {
            buffer[i][j][0] = (GLubyte) (rand () % 256);
            buffer[i][j][1] = (GLubyte) (rand () % 256);
            buffer[i][j][2] = (GLubyte) (rand () % 256);
        }
    }
}

//-------------------------------------------------------------------------
//  Program Main method.
//-------------------------------------------------------------------------
void main (int argc, char **argv)
{
    //  Connect to the windowing system
    glutInit(&argc, argv);

    //  create a window with the specified dimensions
    glutInitWindowSize (WINDOW_WIDTH, WINDOW_HEIGHT);

    //  Set the window x and y coordinates such that the
    //  window becomes centered
    centerOnScreen ();

    //  Position Window
    glutInitWindowPosition (window_x, window_y);

    //  Set Display mode
    glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE);

    //  Create window with the specified title
    glutCreateWindow (window_title);

    //  View in full screen if the full_screen flag is on
    if (full_screen)
        glutFullScreen ();

    //  Set OpenGL program initial state.
    init();

    // Set the callback functions
    glutDisplayFunc (display);
    glutKeyboardFunc (keyboard);
    glutMouseFunc (mouse);

    //  Start GLUT event processing loop
    glutMainLoop();
}



Thanks.
Posted
Comments
Gokulnath007 17-Mar-11 7:09am    
Try to point out the error alone. Its very difficult to follow. Improve your question..

1 solution

As of right now the size of the image you are creating is based on your window dimensions as you can see in your generateImage() function. If you would like to change the dimensions of that then you would need to either

1) Draw onto a new canvas and then display that canvas in your window
2) Change the window dimensions to the desired image dimensions

As far as being able to look through generations, I would recommend pushing your resulting images into an array and then having a scroll bar attached to the current array index (which would change based on where the slider resides).

If you want to turn this into a movie then you would need to integrate time into your program. You can check against system time every time you call a certain function and see how much time has elapsed since the last time you called it. I don't know the syntax of C that well so I can't write a function for you but basically have a publicly visible variable that gets updated every time a function is called with the current system time. Therefore you will be able to check how much time has elapsed since the last call and thusly display a new image if x seconds have passed.

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900