Witam,
czy ma ktoś jakiś przykład przeźroczystej tekstury (a konkretnie pikseli o jednakowym kolorze) w języku polskim w opengl albo najlepiej glut? Wiem, że to się wiąże z kanałem alfa, ale w necie jak na mnie jest to za bardzo namieszane
Witam,
czy ma ktoś jakiś przykład przeźroczystej tekstury (a konkretnie pikseli o jednakowym kolorze) w języku polskim w opengl albo najlepiej glut? Wiem, że to się wiąże z kanałem alfa, ale w necie jak na mnie jest to za bardzo namieszane
ja mam
trzeba umiec ladowac tekstury 32 bitowe lub nauczyc sie je tworzyc
jezeli tekstura ma byc tylko jednorodnie przezroczytsa to wystarczy ze sie narysuje bez tekstury odpowiednio przezroczysty kwadrat trojkat? przy pomocy glEnablre(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_OE);
glColor4f(1,0,0,0.5);
rysuj
glDisable(GL_BLEND);
właśnie chodzi o to, że nie ma być jednorodnie przezroczysta, chodziło mi o to, żeby np jeden kolor w teksturze był uznawany za przezroczysty a reszta nie
To wez se pan narysuj napierw maske Czarno biala a pozniej narysuj, albo sa takie opcje jak glalphafunc
chyba tez alphacolor
i glcolor4f itp. itd
to co Komorkowy_dzony napisał działa
mi chodzi o to, że mam na przykład obrazek w połowie czarny i w połowie biały i to co jest białe było przeźroczyste a to co czarne pozostało czarne. Mam napisaną taką funkcję:
int LoadBitmap(const char *filename, GLuint tex)
{
FILE *fp = fopen(filename, "r");
if(!fp)
{
cout << "Could not open the file." << endl;
return false;
}
unsigned short uint;
unsigned int dword;
unsigned short word;
long llong;
// read some data we don't need
fread(&uint, sizeof(uint), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
fread(&uint, sizeof(uint), 1, fp);
fread(&uint, sizeof(uint), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
long width, height;
fread(&width, sizeof(long), 1, fp);
fread(&height, sizeof(long), 1, fp);
fread(&word, sizeof(word), 1, fp);
unsigned short bitcount;
fread(&bitcount, sizeof(unsigned short), 1, fp);
if(bitcount != 24)
{
cout << "invalid bitcount. Make a 24bit image." << endl;
return false;
}
unsigned long compression;
fread(&compression, sizeof(unsigned long), 1, fp);
if(compression != 0)
{
cout << "invalid compression. Make sure the BMP is not using RLE." << endl;
return false;
}
fread(&dword, sizeof(dword), 1, fp);
fread(&llong, sizeof(long), 1, fp);
fread(&llong, sizeof(long), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
unsigned char *imagedata;
imagedata = new unsigned char[width * height * 4];
for(int x=0; x < width; x++)
{
for(int y=0; y < height; y++)
{
fread(&imagedata[(x * height + y)*4+0], sizeof(char), 1, fp);
fread(&imagedata[(x * height + y)*4+1], sizeof(char), 1, fp);
fread(&imagedata[(x * height + y)*4+2], sizeof(char), 1, fp);
int r = imagedata[(x * height + y)*4+2];
int b = imagedata[(x * height + y)*4+0];
imagedata[(x * height + y)*4+0] = r;
imagedata[(x * height + y)*4+2] = b;
if(imagedata[(x * height + y)*4+0] == 255 &&
imagedata[(x * height + y)*4+1] == 0 &&
imagedata[(x * height + y)*4+2] == 255)
imagedata[(x * height + y)*4+3] = 0;
else
imagedata[(x * height + y)*4+3] = 255;
}
}
// create your texture here
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
1, //0 for now
GL_RGB, //Format OpenGL uses for image
width, height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
imagedata); //The actual pixel data
delete[] imagedata;
return textureId;
}
potem w funkcji rysującej wstawiam
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, _textureId);
po czym rysuję sześciany i niestety po 1 nie nakłada mi tekstury, a po 2 przezroczystości też nie ma...
GL_SRC_ALPHA I GL_ONE_MINUS_SRC_ALPHA JAK I DST_ALPHA DZIALAJA JEDYNIE WTEDY KIEDY MASZ TEKSTURE Z KANALEM ALPHA
dobrym sposobem jest
void __fastcall TFCOpenGL::BMP_TO_GLTEXTURE(unsigned int &tex, Graphics::TBitmap*bmp)
{
int k = bmp->Height-1;
Byte*p; int i,j;
for(i=0; i<bmp->Height; i++) {
p = (Byte*)bmp->ScanLine[i];
for(j=0; j<bmp->Width; j++)
{
CAMtexture[k-i][j][0]=p[j*4+2];
CAMtexture[k-i][j][1]=p[j*4+1];
CAMtexture[k-i][j][2]=p[j*4+0];
}
}
// for (y = 240-1; y > 0; y--) {
// ky = ky + 1;
// memcpy(B->ScanLine[y],pdata+bpp*sw*(sh-1-y),bpp*sw);
// }
//Generate an ID for texture binding
// glGenTextures(1, &tex); //Texture binding
glBindTexture(GL_TEXTURE_2D, tex);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bmp->Width, bmp->Height, GL_RGB, GL_UNSIGNED_BYTE, CAMtexture);//(Byte*)bmp->ScanLine[0]);
p = NULL;
}
gdzie Byte CAMtexture[240][320][3];
zawsze z 3 mozna zmienic na 4 a w glubuild2dmipmaps mozna dac GL_RGBA
ta funikcja jest ustawiona na ladowanie tekstur odpowiednich rozmiarow mianowicie 320 x 240 latwo to zmienic
jak nie chcesz ladowac tekstury razem z kanalem alhpa to poproboj z GL_SRC_COLOR a nie z GL_SRC_ALPHA itp.
dodatkowo jak nie widzisz tekstury to znaczy ze nie masz wlacoznego glEnable(GL_TEXTURE_2D);
lub nie masz zdefiniowanych glTexCoord2f() dla glVertex()
lub masz wlaczona oswietlenie i zle oswietlasz, albo czegos nie zamknales dobrze lub urzywasz shaderow
----------------------------------------------------------------- A oto twoj kod
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
1, //0 for now
GL_RGB, //Format OpenGL uses for image
width, height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
imagedata); //The actual pixel data
Ja bym jak juz cos uzywal
glEnable(GL_TEXTURE_2D);
// FCOpenGL->DOPALACZ->draw();
glBindTexture(GL_TEXTURE_2D,GLCAM_TEX); //bindowanie tekstury
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, FCOpenGL->Width, FCOpenGL->Height, 0); //kopiujemy caly Viewport co mamy na ekranie do tekstury
//ten krok jeszcze raz binduje teksture i rysuje ja na billboardzie gdzies tam w przestrzeni
glBindTexture(GL_TEXTURE_2D,GLCAM_TEX);
drawbillboardF(0,6000,0,3000);
glDisable(GL_TEXTURE_2D);
gdzie GLCAM_TEX to unsigned int GLCAM_TEX;
Tylko takie cos dziala na GeForce'ach nie wiem czy na ATI i w ogole mi ta funkcja chociaz ja znajduje i podczepiam nie dziala na laptopie wiec nie moge testowac postefektow
Watpie ze dzieki glCopyTexImage2d czy tam glcopySUbTeximage2d czy glcopytexsubimage2d uzyskasz kanal przezroczystosci
ale dzieki gl_src_color, gl_ONE_MINUS_SRC_COLOR o ile dobrze wszystko zapamietalem z tym blendingiem bedzie dzialac jak chcesz
I jeszcze cos tam powinno byc chyba GL_RGBA jak ladujesz 32 bitowa bitmape
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
1, //0 for now
GL_RGB, //Format OpenGL uses for image
width, height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
imagedata);
szczerze to się zgubiłem... sory, ale dopiero zaczynam przygodę z opengl i dlatego... może będzie prościej jak dam cały swój kod i mi napiszesz co zrobiłem źle, bo już nie mam pomysłów co robię źle...
#include <iostream>
#include <stdlib.h>
#ifdef linux
#include <GL/glut.h>
#elif
#include <GL/freeglut.h>
#endif
//#include "cube.h"
//Cube * cube;
int count_cube = 0;
const int width = 800;
const int height = 600;
const int game_speed = 15;
float xpoz = 0;
float ypoz = 0;
GLint _textureId;
using namespace std;
int LoadBitmap(const char *filename, GLuint tex);
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
case 'A':
case 'a':
xpoz -= 0.1f;
break;
case 'D':
case 'd':
xpoz += 0.1f;
break;
case 'W':
case 'w':
ypoz += 0.1f;
break;
case 'S':
case 's':
ypoz -= 0.1f;
break;
}
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);
_textureId = LoadBitmap("nic.bmp", 255);
}
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
// This function loads a .bmp file and puts it in the GLuint specified
int LoadBitmap(const char *filename, GLuint tex)
{
FILE *fp = fopen(filename, "r");
if(!fp)
{
cout << "Could not open the file." << endl;
return false;
}
unsigned short uint;
unsigned int dword;
unsigned short word;
long llong;
// read some data we don't need
fread(&uint, sizeof(uint), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
fread(&uint, sizeof(uint), 1, fp);
fread(&uint, sizeof(uint), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
long width, height;
fread(&width, sizeof(long), 1, fp);
fread(&height, sizeof(long), 1, fp);
fread(&word, sizeof(word), 1, fp);
unsigned short bitcount;
fread(&bitcount, sizeof(unsigned short), 1, fp);
if(bitcount != 24)
{
cout << "invalid bitcount. Make a 24bit image." << endl;
return false;
}
unsigned long compression;
fread(&compression, sizeof(unsigned long), 1, fp);
if(compression != 0)
{
cout << "invalid compression. Make sure the BMP is not using RLE." << endl;
return false;
}
fread(&dword, sizeof(dword), 1, fp);
fread(&llong, sizeof(long), 1, fp);
fread(&llong, sizeof(long), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
fread(&dword, sizeof(dword), 1, fp);
unsigned char *imagedata;
imagedata = new unsigned char[width * height * 4];
for(int x=0; x < width; x++)
{
for(int y=0; y < height; y++)
{
fread(&imagedata[(x * height + y)*4+0], sizeof(char), 1, fp);
fread(&imagedata[(x * height + y)*4+1], sizeof(char), 1, fp);
fread(&imagedata[(x * height + y)*4+2], sizeof(char), 1, fp);
int r = imagedata[(x * height + y)*4+2];
int b = imagedata[(x * height + y)*4+0];
imagedata[(x * height + y)*4+0] = r;
imagedata[(x * height + y)*4+2] = b;
if(imagedata[(x * height + y)*4+0] == 255 &&
imagedata[(x * height + y)*4+1] == 0 &&
imagedata[(x * height + y)*4+2] == 255)
imagedata[(x * height + y)*4+3] = 0;
else
imagedata[(x * height + y)*4+3] = 255;
}
}
// create your texture here
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
1, //0 for now
GL_RGBA, //Format OpenGL uses for image
width, height, //Width and height
0, //The border of the image
GL_RGBA, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
imagedata); //The actual pixel data
delete[] imagedata;
return textureId;
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(xpoz, ypoz, -10.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, _textureId);
glRotatef(0.0, 0.0, 1.0, 0.0);
glColor3f(1.0, 1.0, 0.0);
//add abient light
GLfloat abientColor[]= {0.2, 0.2, 0.2, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, abientColor);
//add position light
GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};
GLfloat lightPos0[] ={4.0, 0.0, 8.0, 1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
GLfloat lightColor1[] = {0.5, 0.2, 0.2, 1.0};
GLfloat lightPos1[] ={-1.0, 0.5, 0.5, 0.0};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
glBegin(GL_QUADS);
//Front
glNormal3f(0.0f, 0.0f, 1.0f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//Back
glNormal3f(0.0f, 0.0f, -1.0f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//Left
glNormal3f(-1.0f, 0.0f, 0.0f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glEnd();
glutSwapBuffers();
}
void update (int value)
{
//ekran uległ zmianie - przerysuj!
glutPostRedisplay();
glutTimerFunc(game_speed, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
//Create the window
glutCreateWindow("Cubes 3D");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
/*count_cube = 3;
cube = new Cube[count_cube];
for (int i = 0; i < count_cube; i++)
{
cube[i].setX(i);
cube[i].setY(i);
cube[i].setZ(0);
cube[i].setUnit(1.0f);
cube[i].calculate();
}*/
glutTimerFunc(game_speed, update, 0);
glutMainLoop();
return 0;
}
teksturę mi chyba wczytuję, ponieważ zmienna _textureId zawiera wtedy 1, czyli chyba dobrze
masz tutaj najebane troche za bardzo nie mam sil ci tlumaczyc ze robisz to dobrze tylko nic nie widzisz bo masz wlaczone oswietlenie i zle rysujesz bo masz od razu wlaczony blending ale mi sie zdjae ze jak gl_lighting dasz do gldisable i nie bedziesz nic definiwal w gl_light0 i po glEnd(); w funckji void drawScene() dasz glDisable(GL_BLEND); to powinienes cos zoabczyc
nie wiem czy o to Ci chodziło
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(xpoz, ypoz, -10.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, _textureId);
glRotatef(0.0, 0.0, 1.0, 0.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_QUADS);
//Front
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
glVertex3f(1.5f, -1.0f, 1.5f);
glVertex3f(1.5f, 1.0f, 1.5f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
glVertex3f(1.5f, 1.0f, -1.5f);
glVertex3f(1.5f, 1.0f, 1.5f);
glVertex3f(1.5f, -1.0f, 1.5f);
//Back
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glVertex3f(1.5f, 1.0f, -1.5f);
glVertex3f(1.5f, -1.0f, -1.5f);
//Left
glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
glVertex3f(-1.5f, -1.0f, 1.5f);
glVertex3f(-1.5f, 1.0f, 1.5f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glEnd();
glDisable(GL_BLEND); //teraz dodałem
//add abient light
GLfloat abientColor[]= {0.2, 0.2, 0.2, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, abientColor);
//add position light
/*GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};
GLfloat lightPos0[] ={4.0, 0.0, 8.0, 1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);*/
GLfloat lightColor1[] = {0.5, 0.2, 0.2, 1.0};
GLfloat lightPos1[] ={-1.0, 0.5, 0.5, 0.0};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
glutSwapBuffers();
}
jeśli tak, to nie dalej nie działa nic, a jeśli nie to prosiłbym o jeszcze odrobinę cierpliwości ;)