opengl c++ texturemapping
I tryed to get a cupe with on it some textures
The cupe was there but I only got one texture on every
side of the cupe this is my work:
// Programma die 2d textures op een 3d quart zet:
#include
#include
#include
#include
static HGLRC hRC;
static HDC hDC;
bool keys [256];
//bool light; // alleen true of false dus on/off
//bool blend;
//bool lp; //L ingedrukt?
//bool fp;
//bool bp;
//GLfloat xrot;
//GLfloat yrot;
//GLfloat zrot;
GLfloat rquad; // Angle For The Quad
GLfloat rquad1;
GLfloat rquad2;
GLuint texture[6];
//GLvoid LoadGLTexturesRAW( const char * filename, int wrap )
GLvoid LoadGLTextures()
{
//GLuint texture;
//int width, height;
//BYTE * data;
//FILE * file;
AUX_RGBImageRec *texture1;
texture1 = auxDIBImageLoad( "data/hallo3.bmp" );
// if (!texture1) exit(1);
AUX_RGBImageRec *texture2;
texture2 = auxDIBImageLoad( "data/hallo2.bmp" );
// if (!texture2) exit(1);
AUX_RGBImageRec *texture3;
texture3 = auxDIBImageLoad( "data/hallo1.bmp" );
// if (!texture3) exit(3);
AUX_RGBImageRec *texture4;
texture4 = auxDIBImageLoad( "data/screen.bmp" );
AUX_RGBImageRec *texture5;
texture5 = auxDIBImageLoad( "data/hallo4.bmp" );
AUX_RGBImageRec *texture6;
texture6 = auxDIBImageLoad( "data/hallo5.bmp" );
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
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, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
// Create Linear Filtered Texture
glGenTextures(2, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
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, 3, texture2->sizeX, texture2->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture2->data);
// Create MipMapped Texture
glGenTextures(3, &texture[2]);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture3->sizeX, texture3->sizeY, GL_RGB, GL_UNSIGNED_BYTE, texture3->data);
glGenTextures(3, &texture[3]);
glBindTexture(GL_TEXTURE_2D, texture[3]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture4->sizeX, texture4->sizeY, GL_RGB, GL_UNSIGNED_BYTE, texture4->data);
glGenTextures(3, &texture[4]);
glBindTexture(GL_TEXTURE_2D, texture[4]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture5->sizeX, texture5->sizeY, GL_RGB, GL_UNSIGNED_BYTE, texture5->data);
glGenTextures(3, &texture[5]);
glBindTexture(GL_TEXTURE_2D, texture[5]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture6->sizeX, texture6->sizeY, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
};
//open texture data
// file = fopen( "data/hallo.raw" );
// if ( file == NULL ) return 0;
// width = 256;
// height = 256;
// data = malloc( width * height * 3, 1, file );
// fclose( file );
// glGenTextures( 1, &texture );
// glBindTexture( GL_TEXTURE_2D, texture );
// glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
// GL_LINEAR_MIPMAP_NEAREST );
// glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
// wrap ? GL_REPEAT : GL_CLAMP );
// glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
// wrap ? GL_REPEAT : GL_CLAMP );
// gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,
// height, GL_RGB, GL_UNSIGNED_BYTE, data );
// free( data );
// return texture;
GLvoid InitGL(GLsizei Width, GLsizei Height)
{
// LoadGLTexturesRAW( const char * filename, int wrap );
LoadGLTextures ();
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
// glViewport(0, 0, 0, 0);
glMatrixMode(GL_MODELVIEW);
}
GLvoid ReSizeGLScene(GLsizei Width, GLsizei Height)
{
if (Height==0) // Prevent A Divide By Zero If The Window Is Too Small
Height=1;
glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}
GLvoid DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-5.0f);
// glRotatef(xrot,1.0f,0.0f,0.0f);
// glRotatef(yrot,0.0f,1.0f,0.0f);
// glRotatef(zrot,0.0f,0.0f,1.0f);
glRotatef(rquad,1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
// Front Face
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
// glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
// glLoadIdentity(); // Reset The View
// glTranslatef(0.0f,0.0f,-5.0f);
// glRotatef(rquad1,1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, texture[1]);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();
// glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
// glLoadIdentity(); // Reset The View
// glTranslatef(0.0f,0.0f,0.0f);
// glRotatef(rquad2,1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, texture[2]);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
rquad+=0.2f; // Decrease Rotation Variable For The Quad
// rquad1+=0.2f;
// rquad2+=0.2f;
//xrot+=0.3f;
//yrot+=0.2f;
//zrot+=0.4f;
}
LRESULT CALLBACK WndProc( HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
RECT Screen; // Used Later On To Get The Size Of The Window
GLuint PixelFormat;
static PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number (?)
PFD_DRAW_TO_WINDOW / // Format Must Support Window
PFD_SUPPORT_OPENGL / // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
128, // Select A 16Bit Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored (?)
0, // No Alpha Buffer
0, // Shift Bit Ignored (?)
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored (?)
128, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer (?)
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved (?)
0, 0, 0 // Layer Masks Ignored (?)
};
switch (message) // Tells Windows We Want To Check The Message
{
case WM_CREATE:
hDC = GetDC(hWnd); // Gets A Device Context For The Window
PixelFormat = ChoosePixelFormat(hDC, &pfd); // Finds The Closest Match To The Pixel Format We Set Above
if (!PixelFormat)
{
MessageBox(0,"Can't Find A Suitable PixelFormat.","Error",MB_OK/MB_ICONERROR);
PostQuitMessage(0); // This Sends A 'Message' Telling The Program To Quit
break; // Prevents The Rest Of The Code From Running
}
if(!SetPixelFormat(hDC,PixelFormat,&pfd))
{
MessageBox(0,"Can't Set The PixelFormat.","Error",MB_OK/MB_ICONERROR);
PostQuitMessage(0);
break;
}
hRC = wglCreateContext(hDC);
if(!hRC)
{
MessageBox(0,"Can't Create A GL Rendering Context.","Error",MB_OK/MB_ICONERROR);
PostQuitMessage(0);
break;
}
if(!wglMakeCurrent(hDC, hRC))
{
MessageBox(0,"Can't activate GLRC.","Error",MB_OK/MB_ICONERROR);
PostQuitMessage(0);
break;
}
GetClientRect(hWnd, &Screen);
InitGL(Screen.right, Screen.bottom);
break;
case WM_DESTROY:
case WM_CLOSE:
ChangeDisplaySettings(NULL, 0);
wglMakeCurrent(hDC,NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd,hDC);
PostQuitMessage(0);
break;
case WM_KEYDOWN:
keys[wParam] = TRUE;
break;
case WM_KEYUP:
keys[wParam] = FALSE;
break;
case WM_SIZE:
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0);
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg; // Windows Message Structure
WNDCLASS wc; // Windows Class Structure Used To Set Up The Type Of Window
HWND hWnd; // Storage For Window Handle
wc.style = CS_HREDRAW / CS_VREDRAW / CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL; //(HBRUSH)(COLOR_BACKGROUND); //(HBRUSH)getstockobject(WHITEBRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL WinClass";
if(!RegisterClass(&wc))
{
MessageBox(0,"Failed To Register The Window Class.","Error",MB_OK/MB_ICONERROR);
return FALSE;
}
hWnd = CreateWindow(
"OpenGL WinClass",
"de la Bram", // Title Appearing At The Top Of The Window
WS_POPUP /
WS_CLIPCHILDREN /
WS_CLIPSIBLINGS,
// WS_CAPTION / WS_POPUPWINDOW / WS_VISIBLE,
0, 0, // The Position Of The Window On The Screen
1024, 768, // The Width And Height Of The WIndow
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
{
MessageBox(0,"Window Creation Error.","Error",MB_OK/MB_ICONERROR);
return FALSE;
}
DEVMODE dmScreenSettings; // Developer Mode
memset(&dmScreenSettings, 0, sizeof(DEVMODE)); // Clear Room To Store Settings
dmScreenSettings.dmSize = sizeof(DEVMODE); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = 1024; // Screen Width
dmScreenSettings.dmPelsHeight = 768; // Screen Height
dmScreenSettings.dmFields = DM_PELSWIDTH / DM_PELSHEIGHT; // Pixel Mode
ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN); // Switch To Full Screen
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
SetFocus(hWnd);
wglMakeCurrent(hDC,hRC);
while (1)
{
// Process All Messages
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
return TRUE;
}
}
DrawGLScene();
SwapBuffers(hDC);
if (keys[VK_ESCAPE]) SendMessage(hWnd,WM_CLOSE,0,0);
}
}
Edited by - Bram01 on 2/6/00 1:59:29 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement