有关"C"的帮助和"OpenGL"项目. [英] Help with "C" and "OpenGL" project.

查看:67
本文介绍了有关"C"的帮助和"OpenGL"项目.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助修改此代码.

1.我想将生成的像素数减少到400x200.
2.我希望它使用滚动条生成,以便我可以在各代之间来回滚动(而不是使用"g"键逐个生成,而无法查看以前的图像.
我也想知道是否有任何方法可以使程序像视频一样播放几代人. (而不是使用上面提到的滚动条)
我将在VS2010 Express编译器中使用"C语言"进行编译.
我似乎在Internet上找不到针对opengl的glut.h/glut32.dll的足够完整的参考文件.

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.

<br />
<pre lang="cs">//*************************************************************************<br />
//<br />
//  File Name   : PixGen.c<br />
//  Author      : Ali BaderEddin<br />
//  Created     : December 2003<br />
//  Modified    : March 2010<br />
//<br />
//  Description : Generate new image with random colors on mouse click.<br />
//<br />
//*************************************************************************<br />
<br />
#include <stdlib.h><br />
#include <time.h><br />
#include <gl/glut.h><br />
<br />
//  Avoid showing up the console window<br />
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")<br />
<br />
//  constants representing the window size<br />
#define WINDOW_WIDTH 512<br />
#define WINDOW_HEIGHT 512<br />
<br />
//  Initialization<br />
void init ();<br />
<br />
//  Callback functions<br />
void display (void);<br />
void mouse (int button, int state, int x, int y);<br />
void keyboard (unsigned char key, int x, int y);<br />
<br />
//  Support Functions<br />
void centerOnScreen ();<br />
<br />
//  define the window position on screen<br />
int window_x;<br />
int window_y;<br />
<br />
//  variable representing the window title<br />
char *window_title = "Image Generator";<br />
<br />
//  Tells whether to display the window full screen or not<br />
//  Press Alt + Esc to exit a full screen.<br />
int full_screen = 0;<br />
<br />
//  Generates a random image...<br />
void generateImage ();<br />
<br />
//  Represents the pixel buffer in memory<br />
GLubyte buffer[WINDOW_WIDTH][WINDOW_HEIGHT][3];<br />
<br />
//-------------------------------------------------------------------------<br />
//  Set OpenGL program initial state.<br />
//-------------------------------------------------------------------------<br />
void init ()<br />
{<br />
    glClearColor (0.0, 0.0, 0.0, 0.0);<br />
    glShadeModel(GL_FLAT);<br />
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);<br />
    srand (time(NULL));<br />
    generateImage();<br />
}<br />
<br />
//-------------------------------------------------------------------------<br />
//  This function is passed to glutDisplayFunc in order to display<br />
//  OpenGL contents on the window.<br />
//-------------------------------------------------------------------------<br />
void display (void)<br />
{<br />
    glClear(GL_COLOR_BUFFER_BIT);<br />
    glDrawPixels(WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGB,<br />
                    GL_UNSIGNED_BYTE, buffer);<br />
    glutSwapBuffers ();<br />
}<br />
<br />
//-------------------------------------------------------------------------<br />
//  This function is passed to the glutMouseFunc and is called<br />
//  whenever the mouse is clicked.<br />
//-------------------------------------------------------------------------<br />
void mouse (int button, int state, int x, int y)<br />
{<br />
    if (state == GLUT_DOWN)<br />
    {<br />
        generateImage ();<br />
        glutPostRedisplay ();<br />
    }<br />
}<br />
<br />
//-------------------------------------------------------------------------<br />
//  This function is passed to the glutKeyboardFunc and is called<br />
//  whenever the user hits a key.<br />
//-------------------------------------------------------------------------<br />
void keyboard (unsigned char key, int x, int y)<br />
{<br />
    switch (key)<br />
    {<br />
        case ''g'':<br />
            generateImage ();<br />
            glutPostRedisplay ();<br />
            break;<br />
        case 27:<br />
            exit (0);<br />
    }<br />
}<br />
<br />
//-------------------------------------------------------------------------<br />
//  This function sets the window x and y coordinates<br />
//  such that the window becomes centered<br />
//-------------------------------------------------------------------------<br />
void centerOnScreen ()<br />
{<br />
    window_x = (glutGet (GLUT_SCREEN_WIDTH) - WINDOW_WIDTH)/2;<br />
    window_y = (glutGet (GLUT_SCREEN_HEIGHT) - WINDOW_HEIGHT)/2;<br />
}<br />
<br />
//-------------------------------------------------------------------------<br />
//  Generate new image with random colors<br />
//-------------------------------------------------------------------------<br />
void generateImage ()<br />
{<br />
    int i, j;<br />
<br />
    for (i = 0; i < WINDOW_WIDTH; i++)<br />
    {<br />
        for (j = 0; j < WINDOW_HEIGHT; j++)<br />
        {<br />
            buffer[i][j][0] = (GLubyte) (rand () % 256);<br />
            buffer[i][j][1] = (GLubyte) (rand () % 256);<br />
            buffer[i][j][2] = (GLubyte) (rand () % 256);<br />
        }<br />
    }<br />
}<br />
<br />
//-------------------------------------------------------------------------<br />
//  Program Main method.<br />
//-------------------------------------------------------------------------<br />
void main (int argc, char **argv)<br />
{<br />
    //  Connect to the windowing system<br />
    glutInit(&argc, argv);<br />
<br />
    //  create a window with the specified dimensions<br />
    glutInitWindowSize (WINDOW_WIDTH, WINDOW_HEIGHT);<br />
<br />
    //  Set the window x and y coordinates such that the<br />
    //  window becomes centered<br />
    centerOnScreen ();<br />
<br />
    //  Position Window<br />
    glutInitWindowPosition (window_x, window_y);<br />
<br />
    //  Set Display mode<br />
    glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE);<br />
<br />
    //  Create window with the specified title<br />
    glutCreateWindow (window_title);<br />
<br />
    //  View in full screen if the full_screen flag is on<br />
    if (full_screen)<br />
        glutFullScreen ();<br />
<br />
    //  Set OpenGL program initial state.<br />
    init();<br />
<br />
    // Set the callback functions<br />
    glutDisplayFunc (display);<br />
    glutKeyboardFunc (keyboard);<br />
    glutMouseFunc (mouse);<br />
<br />
    //  Start GLUT event processing loop<br />
    glutMainLoop();<br />
}<br />
</pre><br />
<br />




谢谢.




Thanks.

推荐答案

目前,您正在创建的图像大小基于窗口尺寸,如您在generateImage()函数中看到的那样.如果您想更改尺寸,则需要

1)绘制到新画布上,然后在窗口中显示该画布
2)将窗口尺寸更改为所需的图像尺寸

就能够遍历几代人而言,我建议将生成的图像推入数组中,然后在当前数组索引上附加一个滚动条(该滚动条会根据滑块所在的位置而变化).

如果要将其转换为电影,则需要将时间整合到程序中.您可以在每次调用某个函数时对照系统时间进行检查,以查看自上次调用该函数以来已花费了多少时间.我不太了解C的语法,因此我无法为您编写函数,但是基本上有一个公共可见的变量,该变量每次在当前系统时间调用函数时都会更新.因此,您可以检查自从上次呼叫以来已过去了多少时间,从而在经过x秒后显示了新图像.

希望对您有帮助
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


这篇关于有关"C"的帮助和"OpenGL"项目.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆