1

Тема: OpenGL, з BGR в RGB

Хай. Я от завантажую картинку і накладаю її на прямокутничок, але кольори "навпаки".

Прихований текст

http://не-дійсний-домен/cip0y/00f740e740.png

В методі, котрий завантажує картинку є от такий цикл

    for (int i = 0; i < w * h; ++i)
    {
        int index = i * 3;
        unsigned char B, R;
        B = data[index];
        R = data[index + 2];

        data[index] = R;
        data[index + 2] = B;

    }

котрий мав би робити все нормально, але після його роботи виходить от що

Прихований текст

http://не-дійсний-домен/cip8t/6a74bd04e3.png

Ну, чому так??? xD

2

Re: OpenGL, з BGR в RGB

Чорно-біла картинка мені більше подобається )

3

Re: OpenGL, з BGR в RGB

Joker написав:

Чорно-біла картинка мені більше подобається )

Це все, що ви хтіли сказати?

4

Re: OpenGL, з BGR в RGB

Пане, ви досі вірите в телепатію?

5

Re: OpenGL, з BGR в RGB

koala написав:

Пане, ви досі вірите в телепатію?

за традицією чекав на це повідомлення, дивіться на display() та LoadTexture(const char * filename)

#include <cstdio>
#include <iostream>
#include <Windows.h>
#include <stdlib.h>
#include <gl\glut.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include "SOIL.h"
 
 
GLuint LoadTexture(const char * filename)
{
    glEnable(GL_TEXTURE_2D);
 
    GLuint texture1;
    unsigned char * data;
 
    FILE * file;
 
    int w, h;
    SOIL_load_image(filename, &w, &h, 0, SOIL_LOAD_RGB);
 
    fopen_s(&file, filename, "rb");
    //file = fopen_s(filename, "rb");
    if (file == NULL) return 0;
    data = new unsigned char[w * h * 3];
    //int size = fseek(file,);
    fread(data, w * h * 3, 1, file);
    fclose(file);
 
    for (int i = 0; i < w * h*3; i+=3)
    {
        int temp = data[i];
        data[i] = data[i + 2];
        data[i + 2] = temp;
    }
 
 
    glGenTextures(1, &texture1);
    glBindTexture(GL_TEXTURE_2D, texture1);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
 
 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);
    delete[] data;
 
    return texture1;
}
 
static float angle = 0,
width = 1024,
height = 720,
scale = 1,
eyex,
eyey = 0,
eyez = -1.5,
centerx,
centery,
centerz = 20,
upx,
upy = 1,
upz,
left = 1,
right = 1,
bottom = 1,
top = 1,
near1 = 0.1,
far1 = 100,
fov = 60,
mouseSpeed = 0.05,
oldMouseX = 0,
oldMouseY = 0,
currentScaleX = 1,
currentScaleZ = 1,
minScaleX = 0.5,
maxScaleX = 1.5,
currentJumpHeight,
minScaleY = 0.5,
maxScaleY = 1.5,
maxJumpHeight = 5,
jumpSpeed = 0.1,
scaleXstart,
time1 = 2, time2 = 1,
teapotPosX = 0, teapotPosY = 0, teapotPosZ = 0;
GLuint tex;
 
static bool up = true, stopTimer;
 
float getMouseMoveX(int x)
{
    float newX = (x - oldMouseX)*mouseSpeed;
    oldMouseX = x;
    return newX;
}
 
float getMouseMoveY(int y)
{
    float newY = (y - oldMouseY)*mouseSpeed;
    oldMouseY = y;
    return newY;
}
 
float getPixelWidth(int width1)
{
    return (((float)width1 / (float)width) * 2) - 1;
}
 
float getPixelHeight(int height1)
{
    return (((float)height1 / (float)height) * 2) - 1;
}
 
void init(void) {
    glClearColor(0, 0, 0, 0.0);
    glShadeModel(GL_SMOOTH);
    scaleXstart = (maxJumpHeight / 100.0) * 5.0;
    glEnable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);
    tex = (GLuint)LoadTexture("\\t.bmp");
}
 
void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);
 
    glBindTexture(GL_TEXTURE_2D, tex);
 
    glColor3f(.5, .5, .5);
    glBegin(GL_QUADS);
    glTexCoord2d(0, 0);
    glVertex3d(0.3, -0.5, 0);
    glTexCoord2d(0, 1);
    glVertex3d(0.3, 0.5, 0);
    glTexCoord2d(1, 1);
    glVertex3d(-0.3, 0.5, 0);
    glTexCoord2d(1, 0);
    glVertex3d(-0.3, -0.5, 0);
    glEnd();
 
    glBindTexture(GL_TEXTURE_2D, 0);
 
    glutSwapBuffers();
    glFinish();
}
 
void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    width = w; height = h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fov, width/height, near1, far1);
    glMatrixMode(GL_MODELVIEW);
}
 
void timerF(int value)
{
    if (value == 0)
    {
 
        angle += 4;
        display();
        glutTimerFunc(16, timerF, 0);
    }
}
 
void keyFunc(unsigned char key, int x, int y)
{
    //system("cls");
    switch (key)
    {
    case 'w':eyez += 0.1; stopTimer = true; break;
    case 's':eyez -= 0.1; break;
    case 'a':eyex -= 0.1; break;
    case 'd':eyex += 0.1; break;
    case 'q':eyey -= 0.1; break;
    case 'e':eyey += 0.1; break;
    case 'W':centerz += 0.1; break;
    case 'S':centerz -= 0.1; break;
    case 'A':centerx -= 0.1; break;
    case 'D':centerx += 0.1; break;
    case 'Q':centery -= 0.1; break;
    case 'E':centery += 0.1; break;
    case '+':near1 += 0.1; break;
    case '-':near1 -= 0.1; break;
    case '*':far1 += 0.1; break;
    case '/':far1 -= 0.1; break;
    case '7':fov += 1; break;
    case '4':teapotPosX += 0.1; break;
    case '9':right += 0.1; break;
    case '6':teapotPosX -= 0.1; break;
    case '8':teapotPosZ += 0.1; break;
    case '5':top -= 0.1; break;
    case '2':teapotPosZ -= 0.1; break;
    case '0':bottom -= 0.1; break;
    default:
        break;
    }
    display();
    reshape(width, height);
    printf("%c\n", key);
    printf("Camera position:\n x\t y\t z\t \n %.1f \t %.1f \t %.1f \n", eyex, eyey, eyez);
    printf("Camera up is:\n x\t y\t z\t \n %.1f \t %.1f \t %.1f\n\n", upx, upy, upz);
    std::cout << "FoV is: " << fov << std::endl;
    printf("Frustrum is:\n near:\t far:\t left:\t right:\t top:\t bottom:\t\n %.1f\t %.1f\t %.1f\t %.1f\t %.1f\t %.1f\t ", near1, far1, left, right, top, bottom);
}
 
void MouseMotion(int x, int y)
{
    float newX = getMouseMoveX(x);
    float newY = getMouseMoveY(y);
    printf("Mouse move:\n x\t y\t \n %.1f \t %.1f\n\n", newX, newY);
    centerx += newX;
    centery += newY;
    printf("Camera look at:\n x\t y\t z\t \n %.1f \t %.1f \t %.1f\n\n", centerx, centery, centerz);
 
}
 
void MouseClick(int clickType, int state, int x, int y)
{
    oldMouseX = x;
    oldMouseY = y;
}
 
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE); //set the display to Double buffer  
    glutInitWindowSize(width, height);
    glViewport(0, 0, width, height);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyFunc);
    glutMotionFunc(MouseMotion);
    glutMouseFunc(MouseClick);
    glutTimerFunc(16, timerF, 0);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop(); //call the main loop  
    return 0;
}

6

Re: OpenGL, з BGR в RGB

А я не менш традиційно зачекаю, доки ви телепатично не прочитаєте всі мої вимоги...

7

Re: OpenGL, з BGR в RGB

FakiNyan написав:
Joker написав:

Чорно-біла картинка мені більше подобається )

Це все, що ви хтіли сказати?

Так, все. Вибачте, але за темою я нічим вам допомогти не можу, бо сам не знаю.

8 Востаннє редагувалося FakiNyan (19.10.2014 21:53:12)

Re: OpenGL, з BGR в RGB

Я просто не знав, що є оте GL_BGR_EXT, шукав GL_BGR, але не знаходив

Тре було замінити GL_RGB на GL_BGR_EXT в стрічці

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);

p.s. билін, картинка не вставилась, puush завис

Post's attachments

ss (2014-10-19 at 10.34.55).png 425.56 kb, 257 downloads since 2014-10-19