Отрисовка картинки в OpenGL

Рейтинг: 1Ответов: 1Опубликовано: 13.06.2011

Итак есть картинка, загруженная с помощью Devil, и кучка черных квадратов.

1) Картинка почему-то показывается перевернутой, как сделать ее прямой?

2) Когда я отрисовываю черные кубики, я ставлю цвет так:

glColor3f(0.0f, 0.0f, 0.0f);

И при перерисовке получается, что текстура заполняется черным цветом. Как я могу отменить действие этой функции, чтобы перерисовать текстуру?

код:

#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <string.h>
#include <chipmunk/chipmunk.h>
#include <stdint.h>
#include <IL/ilut.h>
#include "Object.h"

const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
const int NUMBER_OF_CUBICKS = 100;
const int NUMBER_CUBICKS_BY_X = 20;
const int STEP_BY_Y = 20;
const int STEP_BY_X = 20;
const int START_POSITION_X = 50;
const int START_POSITION_Y = 370;

typedef union _LARGE_INTEGER {
  struct {
    uint32_t LowPart;
    long int HighPart;
  } ;
  struct {
    uint32_t LowPart;
    long int HighPart;
  } u;
  long long int QuadPart;
} LARGE_INTEGER, *PLARGE_INTEGER;

cpSpace *space;
cpVect cell_vertices[ 4 ] = { cpv( -CUBICK_WIDTH / 2.0f, CUBICK_HEIGHT / 2.0f ), cpv( CUBICK_WIDTH / 2.0f, CUBICK_HEIGHT / 2.0f ),  cpv( CUBICK_WIDTH / 2.0f, -CUBICK_HEIGHT / 2.0f ), cpv( -CUBICK_WIDTH / 2.0f, -CUBICK_HEIGHT / 2.0f ) };
cpVect ground_vertices[ 4 ] = { cpv( -GROUND_WIDTH / 2.0f, GROUND_HEIGHT / 2.0f ), cpv( GROUND_WIDTH / 2.0f, GROUND_HEIGHT / 2.0f ), cpv( GROUND_WIDTH / 2.0f, -GROUND_HEIGHT / 2.0f ), cpv( -GROUND_WIDTH / 2.0f, -GROUND_HEIGHT / 2.0f ) };
cpVect trian[ 4 ] = { cpv( -110 / 2.0f, SCREEN_HEIGHT-260 / 2.0f ),  cpv( 130 / 2.0f, SCREEN_HEIGHT-280 / 2.0f ), cpv( 110 / 2.0f, -SCREEN_HEIGHT-280 / 2.0f )};
bool start = false;

LARGE_INTEGER old_tacts, current_tacts, freq;
FILE* performance_file;
char seconds_buff[ 100 ];
int times = 0;

void DevilInit(){
    if ( ilGetInteger ( IL_VERSION_NUM ) < IL_VERSION )
    {
        fprintf ( stderr, "Incorrect devil.dll version\n" );
    }

    if ( iluGetInteger ( ILU_VERSION_NUM ) < ILU_VERSION )
    {
        fprintf ( stderr, "Incorrect ilu.dll version\n" );
    }

    if ( ilutGetInteger ( ILUT_VERSION_NUM ) < ILUT_VERSION )
    {
        fprintf ( stderr, "Incorrect ilut.dll version\n" );
    }

    ilInit   ();
    iluInit  ();
    ilutInit ();
    ilutRenderer ( ILUT_OPENGL );
    ilSetInteger ( IL_KEEP_DXTC_DATA, IL_TRUE );
    ilutEnable   ( ILUT_GL_AUTODETECT_TEXTURE_TARGET );
    ilutEnable   ( ILUT_OPENGL_CONV );
    ilutEnable   ( ILUT_GL_USE_S3TC );
}

void CreateGround()
{
    cpShape* ground_shape = cpPolyShapeNew( &space -> staticBody, 4, ground_vertices, cpv( 250.0f, 50.0f ) );
    ground_shape -> e = 0.5f;  
    ground_shape -> u = 0.5f;
    ground_shape -> data = new Object( Ground );
    cpSpaceAddShape( space, ground_shape );

    /*cpShape* triangle = cpPolyShapeNew( &space->staticBody, 4, trian, cpv( 250.0f, 50.0f ) );
    triangle->e = 0.5f;
    triangle->u = 0.5f;
    triangle->data = new Object(Ground);
    cpSpaceAddShape( space, triangle );*/
}

void CreateCubicks()
{
    cpBody *cell_body = NULL;
    cpShape* cell_shape = NULL;

    for ( int i = 0; i < NUMBER_OF_CUBICKS; i++ )
    {
        cell_body = cpBodyNew( 250.0f, cpMomentForPoly( 100.0f, 4, cell_vertices, cpv(0.0f, 0.0f) ) );
        cell_body -> p = cpv( START_POSITION_X + STEP_BY_X * ( i % NUMBER_CUBICKS_BY_X ), START_POSITION_Y - STEP_BY_Y * ( i / NUMBER_CUBICKS_BY_X ) );
        cell_body -> a = (float)(rand() % 361) / 360.0f * 2 * pi;
        cell_body -> data = new Object( Cubick );
        cell_shape = cpPolyShapeNew( cell_body, 4, cell_vertices, cpv( 0.0f, 0.0f ) );
        cell_shape -> e = 0.5f; 
        cell_shape -> u = 0.5f;
        cpSpaceAddBody( space, cell_body ); 
        cpSpaceAddShape( space, cell_shape );
    }
}

void RenderScene()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    cpArray *bodies = space -> bodies;
    cpBody *body = NULL;

    ILuint texID;
    GLuint id;
    ilGenImages(1, &texID);
    ilBindImage(texID);
    ilLoadImage("www.jpg");
    ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
    glEnable(GL_TEXTURE_2D);
    //glEnable(GL_BLEND);
    //glBlendFunc(GL_ONE, GL_SRC_COLOR);
    //glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    //glMatrixMode(GL_MODELVIEW);
    //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    //glClearDepth(0.0f);
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
            ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
            ilGetData());
    ilDeleteImages(1, &texID);
    /* Draw a quad */
    glBegin(GL_QUADS);
        glTexCoord2i(1, 1); glVertex2i(256, 256);
        glTexCoord2i(1, 0); glVertex2i(256, 0);
        glTexCoord2i(0, 0); glVertex2i(0,   0);
        glTexCoord2i(0, 1); glVertex2i(0,   256);
    glEnd();
    glutSwapBuffers();
    glDeleteTextures(1, &id);
    glDisable(GL_TEXTURE_2D);

    glColor3f(0.0f, 0.0f, 0.0f);

    glBegin( GL_QUADS );

        glVertex3f( 250 + ground_vertices[ 0 ].x, 50 + ground_vertices[ 0 ].y, 0.0f );
        glVertex3f( 250 + ground_vertices[ 1 ].x, 50 + ground_vertices[ 1 ].y, 0.0f );
        glVertex3f( 250 + ground_vertices[ 2 ].x, 50 + ground_vertices[ 2 ].y, 0.0f );
        glVertex3f( 250 + ground_vertices[ 3 ].x, 50 + ground_vertices[ 3 ].y, 0.0f );

        for(int i = 0, count = bodies -> num; i < count; i++ )
        {
            body = (cpBody *)bodies -> arr[i];

            cpVect left_top = Geom :: RotateByAngle( cell_vertices[ 0 ].x, cell_vertices[ 0 ].y,  body -> a );
            cpVect left_bottom = Geom :: RotateByAngle( cell_vertices[ 3 ].x, cell_vertices[ 3 ].y, body -> a );
            cpVect right_top = Geom :: RotateByAngle( cell_vertices[ 1 ].x, cell_vertices[ 1 ].y, body -> a );
            cpVect right_bottom = Geom :: RotateByAngle( cell_vertices[ 2 ].x, cell_vertices[ 2 ].y, body -> a );

            glVertex3f( body -> p.x + left_bottom.x, body -> p.y + left_bottom.y, 0.0f );
            glVertex3f( body -> p.x + right_bottom.x, body -> p.y + right_bottom.y, 0.0f );
            glVertex3f( body -> p.x + right_top.x, body -> p.y + right_top.y, 0.0f );
            glVertex3f( body -> p.x + left_top.x, body -> p.y + left_top.y, 0.0f );

        }
    glEnd();

    glFlush();  
    glutSwapBuffers();
}

void MyInit()
{
    glClearColor(255, 255, 255, 1.0);
    //glColor3f(0.0f, 0.0f, 0.0f);
    glPointSize( 4.0 );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 600);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0f, 0.0f, 10, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    glShadeModel(GL_FLAT);

    performance_file = fopen( "perfomance.txt", "w" );
    fclose( performance_file );
}

void ChipmunkInit()
{
    cpInitChipmunk();
    space = cpSpaceNew();
    space -> iterations = 10;
    cpSpaceResizeStaticHash( space, 30.0f, 1000 );
    cpSpaceResizeActiveHash( space, 30.0f, 1000 );
    space -> gravity = cpv( 0, -100 );
}

void NextStep( int a )
{
    times++;
    if ( times > 500 )
        exit( 0 );
    performance_file = fopen( "perfomance.txt", "a" );
    //QueryPerformanceCounter( &old_tacts );
    //QueryPerformanceFrequency(&freq);
    cpSpaceStep( space, 1.0f / 60.0f );
    //QueryPerformanceCounter( &current_tacts );
    //QueryPerformanceFrequency(&freq);
    fprintf( performance_file, "%f\n", ( ( ( float ) current_tacts.QuadPart / freq.QuadPart ) - ( ( float )old_tacts.QuadPart / freq.QuadPart ) )  );
    sprintf( seconds_buff, "%f\n", ( ( ( float ) current_tacts.QuadPart / freq.QuadPart ) - ( ( float )old_tacts.QuadPart / freq.QuadPart ) )  );
    glutSetWindowTitle( seconds_buff );
    fclose( performance_file );
    glutPostRedisplay();
    glutTimerFunc( 20, NextStep, 0 );
}

void KeyBoard(unsigned char key, int x, int y)
{
    if ( start )
        return;
    start = true;
    glutTimerFunc(10, NextStep, 0);
}

int main(int argc, char **argv)
{
    DevilInit();
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    glutInitWindowPosition(300, 200);   
    glutCreateWindow("Testing performance of Chipmunk");
    glutDisplayFunc( RenderScene );
    glutKeyboardFunc(KeyBoard);
    MyInit();
    ChipmunkInit();
    CreateCubicks();
    CreateGround();
    glutMainLoop();
    return 0;
}

Ответы

▲ 3Принят

Скорее всего было включено смешивание и при отрисовке картинка смешивалась с

glColor3f(0.0f, 0.0f, 0.0f);

А это абсолютно черный цвет.

Надо ставить перед рисованием все в (1., 1., 1.). Смешивание происходит покомпонентно, следовательно если надо с текстуры вывести только зеленый канал, можно поставить цвет в (0., 1., 0.) (это на будущее может кому пригодится).