#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glaux.lib")
#include "windows.h"
#include "stdio.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include "GL/glaux.h"
#include "stdafx.h"
#include "SCI.h"
#include "MilkshapeModel.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
HDC hDC = NULL;
HGLRC hRC = NULL;
HWND hWnd = NULL;
HINSTANCE hInstance;
Model *pModel = NULL;
bool keys[256];
bool active = TRUE;
bool fullscreen = TRUE;
//X Rotation
GLfloat xrot;
//Y Rotation
GLfloat yrot;
//Z Rotation
GLfloat zrot;
//X Rotation Speed
GLfloat xspeed;
//Y Rotation Speed
GLfloat yspeed;
// Depth into the screen
GLfloat z = -5.0f;
//Which filter to use
GLuint filter;
//Storage for 3 textures
GLuint texture[3];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
AUX_RGBImageRec *LoadBMP(const char *Filename)
{
//File handle
FILE *File = NULL;
//Check filename was given
if(!Filename)
{
return NULL;
}
// Check to see if the file exists
File = fopen(Filename, "r");
//Does the file exist?
if(File)
{
// close the handle
fclose(File);
//Load the bitmap and return the pointer
return auxDIBImageLoad(Filename);
}
//if load failed to return null
return NULL;
}
GLuint LoadGLTexture(const char *filename)
{
// Create storage space for the texture
AUX_RGBImageRec *pImage;
GLuint texture = 0;
pImage = LoadBMP(filename);
//Load the bitmap, check for errors, If bitmap's not found quit
if(pImage!=NULL && pImage -> data != NULL)
{
//Create 3 textures
glGenTextures(1, &texture);
//Create nearest filtered texture
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, pImage->sizeX, pImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
free(pImage->data);
free(pImage);
}
return texture;
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
// Prevents a divide by 0
if(height == 0)
{
height = 1;
}
// Reset the current viewport
glViewport(0,0,width,height);
//Select the Modelview matrix
glMatrixMode(GL_PROJECTION);
//Reset the Projection Matrix
glLoadIdentity();
//calculate the aspect ratio of the window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);
//Select the Modelview Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid)
{
pModel ->reloadTextures();
glEnable(GL_TEXTURE_2D);
// Enable smooth shading
glShadeModel(GL_SMOOTH);
// Black Background (red, green, blue, alpha)
// Range 0.0f(darkest)-1.0f(brightest)
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
// Depth buffer setup
glClearDepth(1.0f);
// Enables depth testing
glEnable(GL_DEPTH_TEST);
// The Type of Depth Test To Do
glDepthFunc(GL_LEQUAL);
// Perspective calculations for the view to look better
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}
int DrawGLScene(GLvoid)
{
// Clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the current modelview matrix
glLoadIdentity();
gluLookAt(300, 300, 300, 0, 0, 0, 0, 1, 0);
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
pModel->draw();
return TRUE; // Keep Going
}
// Function to properly kill the window
GLvoid KillGLWindow(GLvoid)
{
// In full screen mode?
if(fullscreen)
{
// If so switch back to the desktop
ChangeDisplaySettings(NULL, 0);
// Show mouse pointer
ShowCursor(TRUE);
}
// Check for rendering context
if(hRC)
{
// Check to see if able to release RC and DC context
if(!wglMakeCurrent(NULL,NULL))
{
MessageBox(NULL, "Release of DC and RC Failed", "SHUTDOWN ERROR", MB_OK|MB_ICONINFORMATION);
}
// Check to see if able to delete RC
if(!wglDeleteContext(hRC))
{
// Error message if not able to.
MessageBox(NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
}
// Set the RC to Null
hRC = NULL;
}
//Check to see if able to release the DC
if(hDC && !ReleaseDC(hWnd, hDC))
{
// Error message if unable to release the DC
MessageBox(NULL, "Release device context failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
// Set DC to null.
hDC = NULL;
}
//Check to see if able to destroy the window
if(hWnd && !DestroyWindow(hWnd))
{
MessageBox(NULL, "Could not release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
// Set hWnd to null.
hWnd = NULL;
}
// Check to see if unable to register class
if(!UnregisterClass("OpenGL", hInstance))
{
MessageBox(NULL, "Could not unregister class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
hInstance = NULL;
}
}
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
// Contains the result after searching for a match
GLuint PixelFormat;
// Windows class structure
WNDCLASS wc;
// Window Extended Style
DWORD dwExStyle;
// Window Style
DWORD dwStyle;
// Grabs Rectangle Upper Left/ Lower Right
RECT WindowRect;
/*****Values*****/
// Set left value to 0
WindowRect.left = (long)0;
// Set right value to requested width
WindowRect.right = (long) width;
// Set top value to 0.
WindowRect.top = (long)0;
// Set bottom value to requested height
WindowRect.bottom = (long) height;
//Set the global fullscreen flag
fullscreen = fullscreenflag;
// Grab an instance for the window
hInstance = GetModuleHandle(NULL);
//Redraw On Size, And Own DC for Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
// WndProc Handling messages
wc.lpfnWndProc = (WNDPROC) WndProc;
// No extra window data
wc.cbClsExtra = 0;
// No extra window data
wc.cbWndExtra = 0;
// Set the instance
wc.hInstance = hInstance;
// Load the default icon
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
// Load the Arrow Pointer
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// No background required.
wc.hbrBackground = NULL;
// No Menu
wc.lpszMenuName = NULL;
// Set the class name
wc.lpszClassName = "OpenGL";
// Check to see if class is registered
if(!RegisterClass(&wc))
{
MessageBox(NULL, "Failed to register the window class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
//Try fullscreen mode
if(fullscreen)
{
// Device Mode
DEVMODE dmScreenSettings;
// Clear Memory
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
// Size of DEVMODE structure
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
// Selected screen width
dmScreenSettings.dmPelsWidth = width;
// Selected screen height
dmScreenSettings.dmPanningHeight = height;
//Seleted bits per pixel
dmScreenSettings.dmBitsPerPel = bits;
dmScreenSettings.dmFields = DM_BITSPERPEL| DM_PELSWIDTH | DM_PELSHEIGHT;
//Select Mode
if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
if(MessageBox(NULL, "The Requested Fullscreen Mode is Not Supported By \n Your Video Card. Use Windowed Mode Instead?", "FSL GL", MB_YESNO|MB_ICONEXCLAMATION) ==
IDYES)
{
// Windowed mode selected
fullscreen = FALSE;
}
else
{
//Program closing message
MessageBox(NULL, "Program Closing", "ERROR", MB_OK|MB_ICONSTOP);
return FALSE;
}
}
}
// Check to see if in Full Screen Mode
if(fullscreen)
{
//Window extended Style
dwExStyle = WS_EX_APPWINDOW;
//Windows style
dwStyle = WS_POPUP;
// Hide Mouse pointer
ShowCursor(FALSE);
}
else
{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;
}
//Adjust window to requested size
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
//Create Window
if(!(hWnd = CreateWindowEx( dwExStyle, // Extended Style For The Window
"OpenGL", // Class Name
title,
dwStyle| // Selected Window Style // Window Title
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Adjusted Window Width
WindowRect.bottom-WindowRect.top, // Calculate Adjusted Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Don't Pass Anything To WM_CREATE
{
KillGLWindow();
MessageBox(NULL, "Window Creation Error", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
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
bits, // Select Our 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
16, // 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
};
//Check to see if we recieved device context
if(!(hDC=GetDC(hWnd)))
{
// Reset the display
KillGLWindow();
MessageBox(NULL, "Can't create a GL device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
//Check for matching pixel format
if(!(PixelFormat=ChoosePixelFormat(hDC, &pfd)))
{
// Reset the display
KillGLWindow();
MessageBox(NULL, "Can't Find a suitable pixel format.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
//Check to see if able to set pixel format
if(!SetPixelFormat(hDC, PixelFormat, &pfd))
{
// Reset the display
KillGLWindow();
MessageBox(NULL, "Can't set the pixel format.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// Check for Rendering Context
if(!(hRC = wglCreateContext(hDC)))
{
// Reset the display
KillGLWindow();
MessageBox(NULL, "Can't create a GL device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// Check to see if able to get Rendering Context
if(!(hRC = wglCreateContext(hDC)))
{
// Reset the display
KillGLWindow();
MessageBox(NULL, "Can't create a GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// Try to activate the rendering context
if(!wglMakeCurrent(hDC, hRC))
{
// Reset the display
KillGLWindow();
MessageBox(NULL, "Can't create a GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
//Show the window.
ShowWindow(hWnd, SW_SHOW);
//Giving it higher priority (foreground window)
SetForegroundWindow(hWnd);
//Set the keyboard focus to this window
SetFocus(hWnd);
//Set up our perspective GL screen
ReSizeGLScene(width, height);
//Initialize our newly created GL
if(!InitGL())
{
// Reset the display
KillGLWindow();
MessageBox(NULL, "Initialization Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// Check for windows message
switch (uMsg)
{
// Watch for windows activate message
case WM_ACTIVATE:
{
//Check minimazation state
if(!HIWORD(wParam))
{
// Program is active
active=TRUE;
}
else
{
//Program is no longer active
active=FALSE;
}
return 0;
}
// Intercept system command
case WM_SYSCOMMAND:
{
// check system calls
switch(wParam)
{
// Check to see if screen saver trying to start
case SC_SCREENSAVE:
// Check to see if the monitor is trying to enter power save
case SC_MONITORPOWER:
// Prevent from happening
return 0;
}
break;
}
// Did we recieve a close message
case WM_CLOSE:
{
// Send a quit message
PostQuitMessage(0);
// Jump Back
return 0;
}
// Check to see if a key is being held down
case WM_KEYDOWN:
{
keys[wParam]=TRUE;
return 0;
}
// Check to see if a key has been released
case WM_KEYUP:
{
keys[wParam]=FALSE;
return 0;
}
// Resize the OpenGL window
case WM_SIZE:
{
// LoWord = Width, HiWord = Height
ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
BOOL done=FALSE;
int count=0;
int counta=0;
int rightLeft=0;
int rotback=0;
int xchange = 0;
int ychange = 0;
int count1 = 0;
int count2 = 0;
int count_x_right = 0;
int count_x_left = 0;
int count_y_up = 0;
int count_y_down = 0;
int count_x_right_little = 0;
int count_x_left_little = 0;
int count_y_up_little = 0;
int count_y_down_little = 0;
// varibale to store address of character
unsigned char ua;
// Variables to acquire calibration values
unsigned char Xoff, Yoff, Zoff, Xmax, Ymax, Zmax;
// Variables to get x, y, and z values
unsigned char TiltX, TiltY, TiltZ;
int BadHandShakeTilt;
pModel = new MilkshapeModel();
if(pModel ->loadModelData("data/model.ms3d") == false)
{
MessageBox(NULL, "Couldn't load the model data\\model.ms3d", "Error", MB_OK|MB_ICONERROR);
return 0;
}
// Ask the User which screen mode they prefer?
if(MessageBox(NULL, "Would you like to run in Fullscreen Mode?", "Start Fullscreen", MB_YESNO|MB_ICONQUESTION) == IDNO)
{
//Windowed mode
fullscreen = FALSE;
}
//Open up communication port.
bool bOK=SCIopen();
if (!bOK)
{
// If COM 4 does not open then Kill window and display error message.
KillGLWindow();
MessageBox(NULL, "COM 4 Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
//Handshake with hardware
SCIwrite("R", 1);
ua = ' ';
SCIread(ua,1);
if(ua != 'N')
{
KillGLWindow();
MessageBox(NULL, "Handshake failed!", "ERROR", MB_OK | MB_ICONEXCLAMATION);
}
//Get Calibration Values
//bool bSCI = SCIgetCal(Xoff, Xmax, Yoff, Ymax, Zoff, Zmax);
Xoff = 133;
Yoff = 142;
Zoff = 137;
Xmax = 189;
Ymax = 189;
Zmax = 152;
BadHandShakeTilt = 0;
//Create our OpenGLWindow
if(!CreateGLWindow("FSL OpenGL Framework", 640, 480, 16, fullscreen))
{
// Quit if the window has not been created
return 0;
}
// Loop that runs while done=False
while(!done)
{
bool bSCI2 = SCIgetXYZ(TiltX, TiltY, TiltZ);
if(!bSCI2)
{
TiltX=TiltY=TiltZ=0;
BadHandShakeTilt++;
}
if(BadHandShakeTilt > 10)
{
BadHandShakeTilt = 0;
SCIHandShake();
MessageBox(NULL, "Bad Tilt Values.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
}
// Check to see if message is waiting
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Check to see if quit message has been received
if(msg.message==WM_QUIT)
{
done=TRUE;
}
// Deal with window message
else
{
// Translate the message
TranslateMessage(&msg);
// Dispatch the message
DispatchMessage(&msg);
}
}
else
{
//Draw the Scene. Watch for ESC key and Quit Messages from DrawGLScene
if(active)
{
// Was ESC Pressed
if(keys[VK_ESCAPE])
{
// ESC signaled a quit
done = TRUE;
}
// Not time to quit update the screen
else
{
//Draw the scene
DrawGLScene();
// Swap buffers (double buffering)
SwapBuffers(hDC);
//Rotation for X axis Loop
// X Rotation Right
xchange = TiltX - Xoff;
if( xchange > 40)
{
//rotates it to center point
//rotates it to center point
if(count_x_left != 0)
{
count_x_left--;
yrot -= 0.9f;
}
if(count_x_left_little != 0)
{
count_x_left_little--;
yrot -= 0.5f;
}
if(count_x_left == 0 && count_x_left_little == 0 && count_x_right < 2)
{
yrot -= 9.0f;
count_x_right++;
}
}
if(xchange > 20 && xchange < 40)// If So, Decrease xspeed?
{
if(count_x_right !=0)
{
count_x_right--;
yrot +=4.5f;
count_x_right_little++;
}
//rotates it to center point
if(count_x_left != 0)
{
count_x_left--;
yrot -= 0.9f;
}
if(count_x_left_little != 0)
{
count_x_left_little--;
yrot -= 0.5f;
}
if(count_x_left_little == 0 && count_x_left == 0 && count_x_right_little < 2)
{
yrot -= 0.9f;
count_x_right_little++;
}
}
//X Rotation Left
if(xchange < -20 && xchange > -40)// If So, Decrease xspeed?
{
//rotate to center point
if(count_x_right != 0)
{
count_x_right--;
yrot += 0.05f;
//rightLeft = 2; //Left causes this to be 2
}
if(count_x_right == 0 && count_x_left < 500)
{
count_x_left++;
yrot += 0.05f;
}
}
if(xchange < 20 && xchange > -20)
{
if(count_x_right != 0)
{
count_x_right--;
yrot += 0.05f;
}
if(count_x_left != 0)
{
count_x_left--;
yrot -= 0.05f;
}
}
// X Rotation Right
ychange = TiltY - Yoff;
if(ychange > 20)// If So, Decrease xspeed?
{
//rotates it to center point
if(count_y_down != 0)
{
count_y_down--;
xrot -= 0.05f;
}
if(count_y_down == 0 && count_y_up < 500)
{
xrot -= 0.05f;
count_y_up++;
}
}
//X Rotation Left
if(ychange < -20)// If So, Decrease xspeed?
{
//rotate to center point
if(count_y_up != 0)
{
count_y_up--;
xrot += 0.05f;
}
if(count_y_up == 0 && count_y_down < 500)
{
count_y_down++;
xrot += 0.05f;
}
}
if(ychange < 20 && ychange > -20)
{
if(count_y_up != 0)
{
count_y_up--;
xrot += 0.05f;
}
if(count_y_down != 0)
{
count_y_down--;
xrot -= 0.05f;
}
}
}
}
// Is F1 being pressed?
if (keys[VK_F1])
{
//if so make key FALSE
keys[VK_F1]=FALSE;
// Kill our current window
KillGLWindow();
// Toggle Full Screen/ Windowed Mode
fullscreen = !fullscreen;
//Recreate OpenGL Window
if(!CreateGLWindow("FSL OpenGL Framework", 640,480,16,fullscreen))
{
return 0;
}
}
}
}
// Shutdown
// Kill The window
KillGLWindow();
//Exit the program
return(1);//msg.wParam);
return 0;
}
Lesson 31 - Bringing skins into image
Hi all:
I created my 3D model of a car and used some of lesson 31 to import my image. When the image is imported its completly white. But on Milkshape I have colored it with the materials. So on Milkshape its totally colored but once imported its completly white. Does anyone have any ideas on how to fix this, or how to work with it?
Below is my main source:
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement