#include <Main.h>
#include <3ds.h>
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
#define FILE_NAME "face.3ds" // This is the 3D file we will load.
UINT g_Texture[MAX_TEXTURES] = {0}; // This holds the texture info, referenced by an ID
CLoad3DS g_Load3ds; // This is 3DS class. This should go in a good model class.
t3DModel g_3DModel; // This holds the 3D Model info that we load in
int g_ViewMode = GL_TRIANGLES;
bool g_bLighting = true; // Turn lighting on initially
float g_RotateX = 0.0f; // This is the current value at which the model is rotated
float g_RotationSpeed = 0.8f;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void LoadScene()
{
g_Load3ds.Import3DS(&g_3DModel, FILE_NAME); // Load our .3DS file into our model structure
// Go through all the materials
for(int i = 0; i < g_3DModel.numOfMaterials; i++)
{
// Check to see if there is a file name to load in this material
if(strlen(g_3DModel.pMaterials[i].strFile) > 0)
{
// Use the name of the texture file to load the bitmap, with a texture ID (i).
// We pass in our global texture array, the name of the texture, and an ID to reference it.
CreateTexture(g_Texture, g_3DModel.pMaterials[i].strFile, i);
}
// Set the texture ID for this material
g_3DModel.pMaterials[i].textureId = i;
}
// Here, we turn on a lighting and enable lighting. We don''t need to
// set anything else for lighting because we will just take the defaults.
// We also want color, so we turn that on
glEnable(GL_LIGHT0); // Turn on a light with defaults set
glEnable(GL_LIGHTING); // Turn on lighting
glEnable(GL_COLOR_MATERIAL);
}
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();
LoadScene();
// Give OpenGL our position, then view, then up vector
gluLookAt( 0, 1.5f, 8, 0, 0.5f, 0, 0, 1, 0);
// We want the model to rotate around the axis so we give it a rotation
// value, then increase/decrease it. You can rotate right of left with the arrow keys. // Increase the speed of rotation
// Since we know how many objects our model has, go through each of them.
for(int i = 0; i < g_3DModel.numOfObjects; i++)
{
// Make sure we have valid objects just in case. (size() is in the vector class)
if(g_3DModel.pObject.size() <= 0) break;
// Get the current object that we are displaying
t3DObject *pObject = &g_3DModel.pObject[i];
// Check to see if this object has a texture map, if so bind the texture to it.
if(pObject->bHasTexture) {
// Turn on texture mapping and turn off color
glEnable(GL_TEXTURE_2D);
// Reset the color to normal again
glColor3ub(255, 255, 255);
// Bind the texture map to the object by it''s materialID
glBindTexture(GL_TEXTURE_2D, g_Texture[pObject->materialID]);
} else {
// Turn off texture mapping and turn on color
glDisable(GL_TEXTURE_2D);
// Reset the color to normal again
glColor3ub(255, 255, 255);
}
// This determines if we are in wireframe or normal mode
glBegin(g_ViewMode); // Begin drawing with our selected mode (triangles or lines)
// Go through all of the faces (polygons) of the object and draw them
for(int j = 0; j < pObject->numOfFaces; j++)
{
// Go through each corner of the triangle and draw it.
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
// Get the index for each point of the face
int index = pObject->pFaces[j].vertIndex[whichVertex];
// Give OpenGL the normal for this vertex.
glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
// If the object has a texture associated with it, give it a texture coordinate.
if(pObject->bHasTexture) {
// Make sure there was a UVW map applied to the object or else it won''t have tex coords.
if(pObject->pTexVerts) {
glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
}
} else {
// Make sure there is a valid material/color assigned to this object.
// You should always at least assign a material color to an object,
// but just in case we want to check the size of the material list.
// if the size is at least one, and the material ID != -1,
// then we have a valid material.
if(g_3DModel.pMaterials.size() && pObject->materialID >= 0)
{
// Get and set the color that the object is, since it must not have a texture
BYTE *pColor = g_3DModel.pMaterials[pObject->materialID].color;
// Assign the current color to this model
glColor3ub(pColor[0], pColor[1], pColor[2]);
}
}
// Pass in the current vertex of the object (Corner of current face)
glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
}
}
glEnd(); // End the drawing
}
return TRUE;
SwapBuffers(hDC);
}
int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{
MSG msg; // Windows Message Structure
BOOL done=FALSE; // Bool Variable To Exit Loop
// 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)
{
fullscreen=FALSE; // Windowed Mode
}
// Create Our OpenGL Window
if (!CreateGLWindow("Raja''s 3ds game! i did it!",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE;
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (active) // Program Active?
{
if (keys[VK_ESCAPE]) // Was ESC Pressed?
{
done=TRUE;
for(int i = 0; i < g_3DModel.numOfObjects; i++)
{
delete [] g_3DModel.pObject[i].pFaces;
delete [] g_3DModel.pObject[i].pNormals;
delete [] g_3DModel.pObject[i].pVerts;
delete [] g_3DModel.pObject[i].pTexVerts;
}
}
else // Not Time To Quit, Update Screen
{
DrawGLScene(); // Draw The Scene
SwapBuffers(hDC);
}
if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("Raja''s 3ds game! i did it!",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
}
}
// Shutdown
KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}
[source/]
thanks
please reply
Help with 3ds loader error msg!
hi guys coudl you guys help me
when i run the program i get a assertion failed message
could you help me please?
here is the source code for main.cpp
Sir Darkan Fireblade
I dont suppose you have the game running in window mode? Or you can remote debug? Because then you could just step through and find out where it hits the offending ASSERT
Thanks for Replying the assertion says
Debug assertion failed
Program ...stdio/myprojects/opengl framework/debug/opengl
framework.exe
file dbgheap.c
line: 1017
expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
any Ideas?
Thanks
Please Reply
Sir Darkan Fireblade
Debug assertion failed
Program ...stdio/myprojects/opengl framework/debug/opengl
framework.exe
file dbgheap.c
line: 1017
expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
any Ideas?
Thanks
Please Reply
Sir Darkan Fireblade
Yes, that assert is VERY annoying. I''ve had it numerous times. As i recal it is caused by attempting to use either a NULL pointer or a an undefined pointer. I think both cases fail the debug assert found in the VC++ code. If you were to compile in Release you wouldn''t get the error, but your program would crash. TO fix this you should set the debugger to go line by line and keep hitting next until the error pops up, then go see what pointer is screwed and figure out how to fix it.
He who said money was the root of all evil knew little of the nature of money and less about the nature of man.
He who said money was the root of all evil knew little of the nature of money and less about the nature of man.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement