Jump to content
  • Advertisement
Sign in to follow this  
redrum237

OpenGL applyng textured to a 2d scene

This topic is 3640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i am rendering a 2d image in OpenGL, and do not know how to apply textures. here is my code, if anyone could point me in the right direction in how to apply the textures than that would be great /******************************************************* 1. Include necessary header files *******************************************************/ #include "include.h" /******************************************************* 2. Decleare types, global variables and constants *******************************************************/ // Define keyboard constants - used for keyboard input processing #define NUMPAD_1_NL 97 #define NUMPAD_2_NL 98 #define NUMPAD_3_NL 99 #define NUMPAD_4_NL 100 #define NUMPAD_5_NL 101 #define NUMPAD_6_NL 102 #define NUMPAD_7_NL 103 #define NUMPAD_8_NL 104 #define NUMPAD_9_NL 105 // Pi const float pi = 3.142f; struct vector2D { float x, y; }; typedef vector2D vertex2D; // vertex same as vector // circle structure struct circle { vertex2D pos; // position float r; // radius }; // hierarchical model of a human - struct model struct craneModel2D { vector2D torsoPos; float torsoAngle; vector2D body_to_top_connector; // relative to torsoPos vector2D upper_body; vector2D top_of_crane; vector2D crane_to_claw; vector2D wheel_1; vector2D wheel_2; vector2D wheel_3; vector2D wheel_4; // parameters to describe the position and orientation of the Upper Arm (ua), // the Left Lower Arm (lla) and Left Hand (lh) vector2D ua_pos, ma_pos, la_pos; float ua_angle, ma_angle, la_angle; }; // Instance of person struct craneModel2D p1; /******************************************************* 3. Declare function prototypes *******************************************************/ int WINAPI WinMain(HINSTANCE h_instance, HINSTANCE h_prev_instance, LPSTR lp_cmd_line, int show_cmd); LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); void renderScene(void); // person rendering functions void draw_crane(craneModel2D); void draw_main_body(void); void draw_upper_body(void); void draw_body_to_top_connector(void); void draw_top_of_crane(void); void draw_crane_to_claw(void); void draw_upper_arm(void); void draw_middle_arm(void); void draw_lower_arm(void); void draw_box(float, float, float, float); void draw_wheel_1(void); void draw_wheel_2(void); void draw_wheel_3(void); void draw_wheel_4(void); /******************************************************* 4. Function implementation *******************************************************/ // Main start point for program int WINAPI WinMain(HINSTANCE h_instance, HINSTANCE h_prev_instance, LPSTR lp_cmd_line, int show_cmd) { WNDCLASSEX wndclass; HWND hwnd_MainWindow = NULL; MSG msg; int nPixelFormat; PIXELFORMATDESCRIPTOR pfd; HDC hDC = NULL;// windows device context HGLRC hRC = NULL;// openGL rendering context BOOL failOpenGLInitialisation=FALSE; TCHAR appName [] = TEXT("CS3S05"); // initialise window class wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;//characteristics of window wndclass.lpfnWndProc = WinProc;// event handler wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = GetModuleHandle(NULL); wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);//NULL; wndclass.hCursor = LoadCursor(NULL, IDC_CROSS);//NULL; wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = appName; wndclass.hIconSm = NULL; // register window class (must be done before we actually create an instance of the window) if (!RegisterClassEx(&wndclass)) { MessageBox(hwnd_MainWindow, "Error initialising application", "Error", MB_OK); return(0); } // create main window hwnd_MainWindow = CreateWindowEx(WS_EX_APPWINDOW, appName, "CS3S05", WS_OVERLAPPEDWINDOW, 0, 0, 800, 800, NULL, NULL, h_instance, NULL); if (!hwnd_MainWindow) { MessageBox(hwnd_MainWindow, "Error initialising window", "Error", MB_OK); return(0); } ShowWindow(hwnd_MainWindow, show_cmd); UpdateWindow(hwnd_MainWindow); SetFocus(hwnd_MainWindow); // If we want random numbers, we need to seed(initialise) the random number generator srand( (unsigned)time( NULL ) ); // initialise OpenGL ZeroMemory(&pfd,sizeof(pfd)); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; hDC = GetDC(hwnd_MainWindow); // main steps to setup OpenGL if (!(nPixelFormat = ChoosePixelFormat(hDC, &pfd))) failOpenGLInitialisation=TRUE; if (!SetPixelFormat(hDC, nPixelFormat, &pfd)) failOpenGLInitialisation=TRUE; if (!(hRC = wglCreateContext(hDC))) failOpenGLInitialisation=TRUE; if (!wglMakeCurrent(hDC,hRC)) failOpenGLInitialisation=TRUE; if (failOpenGLInitialisation) { MessageBox(hwnd_MainWindow, "Error initialising OpenGL", "Error", MB_OK); return(0); } // Initialise OpenGL properties /**************************** // (i) Set Projection *****************************/ glMatrixMode(GL_PROJECTION); // select the projection matrix glLoadIdentity(); // reset the projection matrix glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); // (ii) Clear all previous transformations glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // (iii) Set rendering properties glShadeModel(GL_FLAT); // shading model glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // specify background color to black // function for blending colours glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Depth sorting parameters glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); // // Initialise personModel p1 declared above // // initialise torso position and orientation p1.torsoPos.x = 0.0; p1.torsoPos.y = 0.0; p1.torsoAngle = 0.0; p1.upper_body.x = 0.1; p1.upper_body.y = 0.0; // head component p1.body_to_top_connector.x = 0.0; // head relative to torso p1.body_to_top_connector.y = 0.2f; p1.top_of_crane.x = 0.0; p1.top_of_crane.y = 0.3f; p1.crane_to_claw.x = -0.01; p1.crane_to_claw.y = 0.4f; // left arm components p1.ua_pos.x = 0.0f; // upper arm - relative to torso p1.ua_pos.y = 0.35f; p1.ua_angle = -45.0f; p1.ma_pos.x = -0.2f; // lower arm - relative to upper arm p1.ma_pos.y = 0.0; p1.ma_angle = 90.0; p1.la_pos.x = -0.42f; // hand - relative to lower arm p1.la_pos.y = 0.0; p1.la_angle = 10.0; p1.wheel_1.x = 0.2; p1.wheel_1.y = -0.28f; p1.wheel_2.x = 0.05; p1.wheel_2.y = -0.28f; p1.wheel_3.x = -0.10; p1.wheel_3.y = -0.28f; p1.wheel_4.x = -0.25; p1.wheel_4.y = -0.28f; // main program loop (receive and process Windows events) while(1) { if (PeekMessage(&msg, NULL,0,0,PM_REMOVE)) { if (WM_QUIT == msg.message) break; TranslateMessage(&msg); DispatchMessage(&msg); } } // release app resources wglMakeCurrent(NULL, NULL); if (hRC) wglDeleteContext(hRC); hRC = NULL; ReleaseDC(hwnd_MainWindow, hDC); DestroyWindow(hwnd_MainWindow); // exit to windows return(0); } // Main event handler for project LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { int virtual_code; switch(msg) { case WM_CREATE: break; case WM_PAINT: { HDC hdc = GetDC(hwnd); renderScene(); // do main rendering when screen in being updated SwapBuffers(hdc); // swap rendering buffers for window ReleaseDC(hwnd, hdc); } break; case WM_SYSCOMMAND: switch(wparam) { case SC_KEYMENU: case SC_MONITORPOWER: case SC_SCREENSAVE: return(0); break; default: return(DefWindowProc(hwnd, msg, wparam, lparam)); } break; case WM_KEYDOWN: // get keycode virtual_code = (int)wparam; switch(virtual_code) { case VK_ESCAPE: // Escape PostQuitMessage(0); break; // control torso case 'Q': p1.torsoAngle++; break; case 'W': p1.torsoAngle--; break; case NUMPAD_8_NL: p1.torsoPos.y+=0.01f; break; case NUMPAD_2_NL: p1.torsoPos.y-=0.01f; break; case NUMPAD_4_NL: p1.torsoPos.x-=0.01f; break; case NUMPAD_6_NL: p1.torsoPos.x+=0.01f; break; // control left arm components case VK_UP: // up p1.ua_angle++; break; case VK_DOWN: // down p1.ua_angle--; break; case VK_LEFT: // left p1.ma_angle--; break; case VK_RIGHT: // right p1.ma_angle++; break; case VK_INSERT: // insert p1.la_angle--; break; case VK_PRIOR: // page up p1.la_angle++; break; default: break; } break; case WM_CLOSE: SendMessage(hwnd, WM_DESTROY, 0, 0); break; case WM_DESTROY: PostQuitMessage(0); break; default: return(DefWindowProc(hwnd, msg, wparam, lparam)); } return(0); } void renderScene(void) { // 1. Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 2. Put rendering code here glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // render personModel p1 glPushMatrix(); glLineWidth(3.0); // apply torso/personModel transformations - this is the "root" of the personModel hierarchy glTranslatef(p1.torsoPos.x, p1.torsoPos.y, 0.0); glRotatef(p1.torsoAngle, 0, 0, 1); // draw personModel p1 object draw_crane(p1); glPopMatrix(); } // Functions to support human rendering // drawPerson void draw_crane(craneModel2D p) { // First draw main body draw_main_body(); glPushMatrix(); // save current transformations // draw the head component glTranslatef(p.body_to_top_connector.x, p.body_to_top_connector.y, 0.0); draw_body_to_top_connector(); glPopMatrix(); // restore transformation prior to head transformations glPushMatrix(); glTranslatef(p.upper_body.x, p.upper_body.y, 0.0); draw_upper_body(); glPushMatrix(); glTranslatef(p.top_of_crane.x, p.top_of_crane.y, 0.0); draw_top_of_crane(); glPopMatrix(); glPushMatrix(); glTranslatef(p.crane_to_claw.x, p.crane_to_claw.y, 0.0); draw_crane_to_claw(); glPopMatrix(); glPushMatrix(); glTranslatef(p.wheel_1.x, p.wheel_1.y, 0.0); draw_wheel_1(); glPopMatrix(); glPushMatrix(); glTranslatef(p.wheel_2.x, p.wheel_2.y, 0.0); draw_wheel_2(); glPopMatrix(); glPushMatrix(); glTranslatef(p.wheel_3.x, p.wheel_3.y, 0.0); draw_wheel_3(); glPopMatrix(); glPushMatrix(); glTranslatef(p.wheel_4.x, p.wheel_4.y, 0.0); draw_wheel_4(); glPopMatrix(); // now draw left arm components relative to the torso glPushMatrix(); // save current transformations // translate to and draw the upper arm positions // note : transformations RELATIVE to torso position/orientation glTranslatef(p.ua_pos.x, p.ua_pos.y, 0.0); glRotatef(p.ua_angle, 0, 0, 1); draw_upper_arm(); // translate to draw the lower arm component glPushMatrix(); // save current transformations glTranslatef(p.ma_pos.x, p.ma_pos.y, 0.0); glRotatef(p.ma_angle, 0, 0, 1); draw_middle_arm(); // translate to draw the left hand component glPushMatrix(); // save current transformations // draw left hand glTranslatef(p.la_pos.x, p.la_pos.y, 0.0); glRotatef(p.la_angle, 0, 0, 1); draw_lower_arm(); // left hand bottom of hierarchy - so move back up to lower arm level glPopMatrix(); // since nothing left to draw relative to lower arm, move back up to upper arm glPopMatrix(); // since nothing left to draw relative to upper arm, move back up to torso transformation // level glPopMatrix(); } void draw_main_body(void) { draw_box(0.18f, -0.09, 0.30f, 0.05f); } void draw_upper_body(void) { draw_box(0.1f, 0.05, 0.2f, -0.09); } void draw_body_to_top_connector(void) { draw_box(0.2f, -0.04f, 0.05f, 0.0195f); } void draw_top_of_crane(void) { draw_box(0.1f, 0.03f, 0.08f, 0.15f); } void draw_crane_to_claw(void) { draw_box(0.01f, -0.05f, 0.02f, 0.05f); } void draw_upper_arm(void) { draw_box(-0.1f, -0.0, 0.1f, 0.025f); } void draw_middle_arm(void) { draw_box(-0.2f, 0.0, 0.2f, 0.025f); } void draw_lower_arm(void) { draw_box(-0.08f, 0.0, 0.15f, 0.025f); } void draw_wheel_1(void) { draw_box(0.1f, 0.09, 0.05f, 0.05f); } void draw_wheel_2(void) { draw_box(0.1f, 0.09, 0.05f, 0.05f); } void draw_wheel_3(void) { draw_box(0.1f, 0.09, 0.05f, 0.05f); } void draw_wheel_4(void) { draw_box(0.1f, 0.09, 0.05f, 0.05f); } // draw a box offset by dx, dy from the origin which has width=2w and height=2h void draw_box(float dx, float dy, float w, float h) { glPushMatrix(); glTranslatef(dx, dy, 0.0); glBegin(GL_LINE_LOOP); glVertex2f(-w, h); glVertex2f(w, h); glVertex2f(w, -h); glVertex2f(-w, -h); glEnd(); glPopMatrix(); }

Share this post


Link to post
Share on other sites
Advertisement
http://www.nullterminator.net/gltexture.html

First result in google when searching for "textures in opengl" *shakes head* It would have been easier for you to search then to post here, try it in future.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!