PDA

View Full Version : l3ds



Bompi
02-08-2005, 11:47 AM
hi,

I have found the library "L3ds" on the net, which I consider using to load my .3ds files in my 3d engine. But I don't know how to use it.. Is there anybody who's familiar with it and maybe could help me loading my 3ds files? Or is it an easier way to do it?

hope on a quick answer ;) i'm desperat :p

def
02-09-2005, 12:42 AM
There is a demo application included with l3ds which explains it pretty well. What is it you need help with?
I have just started using l3ds but will be switching to "EaseImport" for 3dmax since the 3ds format is very limited and inconsistent.

Bompi
02-09-2005, 09:14 AM
Ok.... I have tried the example that comes with the "l3ds", it doesn't work. I'm using Borland c++ Builder. So I programmed a code "on my own". But all I can print to the screen is a currupt pink box. The pinky box is originally a model of a face. I don't understand why this keep coming up. :confused: OHH. I hate that Pinky BOX! ;) What did you code, to not get this annoying box?

def
02-09-2005, 02:03 PM
Does the "draw3ds_sdl.exe" work with the included box.3ds file? Which would give you a green box, not pink. ;-)
delete the lines
glEnableClientState(GL_COLOR_ARRAY);
and
glColorPointer(3, GL_FLOAT, 0, &mesh.GetBinormal(0));

they just give you strange coloring.
Have you tried any other 3ds models, like any from www.3fcafe.com (http://www.3fcafe.com) ? I've tested with a few of them, no problem...

Bompi
02-10-2005, 08:01 AM
I tried the example, but I don't have the SDL files. I have tried to install them too, but i can't get it to work. Do I need SDL to open .3ds files? For I have this "testcode" (the one with the pinky box, but now is a big white wall :( .. I liked the pinkybox better, it was more like a model. This wall just irritates me more.. :mad: ).. Have I done something wrong? I have done something wrong ;) , but do you see it? (I can't) :(
</font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> //---------------------------------------------------------------------------

#include "l3ds.h" // Header file for the L3ds Library
#include "l3ds.cpp" // Cpp file for the L3ds Library
#include <float.h> // Header file for floating
#include <vcl.h>
#include <windows.h> // Header file for windows
#include <gl\gl.h> // Header file for the OpenGL32 library
#include <gl\glu.h> // Header file for the GLu32 library
#include <gl\glaux.h> // Header file for the GLaux library
#pragma hdrstop

//---------------------------------------------------------------------------
#pragma argsused

HGLRC hRC = NULL; // Permanent rendering context
HDC hDC = NULL; // Private GDI device context
HWND hWnd = NULL; // Holds our window handle
HINSTANCE hInstance = NULL; // 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


L3DS scene;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration for WndProc

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize and initialize the GL window
{
if (height == 0) // Prevent A Divide By Zero By
{
height = 1; // Making height equal One
}

glViewport(0, 0, width, height); // Reset the current viewport

glMatrixMode(GL_PROJECTION); // Select the projection matrix
glLoadIdentity(); // Reset the projection matrix

// Calculate the aspect ratio of the window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
gluLookAt(12.0f, 8.0f, 5.7f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW); // Select the modelview matrix
glLoadIdentity(); // Reset the modelview matrix
}

int InitGL(GLvoid) // All setup for OpenGL goes here
{
glShadeModel(GL_SMOOTH); // Enable smooth shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black background
glClearDepth(1.0f); // Depth buffer setup
glEnable(GL_DEPTH_TEST); // Enables depth testing
glDepthFunc(GL_LEQUAL); // The type of depth testing to do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really nice perspective calculations
return true; // Initialization went OK
}

int DrawGLScene(GLvoid) // Here's where we do all the drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer
glLoadIdentity(); // Reset the current modelview matrix


for (uint i= 0; i<scene.GetMeshCount(); i++)
{
LMesh &mesh = scene.GetMesh(i);

glVertexPointer(4, GL_FLOAT, 0, &mesh.GetVertex(0));
glNormalPointer(GL_FLOAT, 0, &mesh.GetNormal(0));
glColorPointer(3, GL_FLOAT, 0, &mesh.GetBinormal(0));
glDrawElements(GL_TRIANGLES, mesh.GetTriangleCount()*3,
GL_UNSIGNED_SHORT, &mesh.GetTriangle(0));

}

return true; // Done drawing the quad
}

GLvoid KillGLWindow(GLvoid) // Properly kill the window
{
if (fullscreen) // Are we in fullscreen mode?
{
ChangeDisplaySettings(NULL,0); // If so switch back to the desktop
ShowCursor(true); // Show mouse pointer
}

if (hRC) // Do we have a rendering context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are we able to release the DC and RC contexts?
{
MessageBox(NULL,"Release of DC and RC failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC)) // Are we able to delete the RC?
{
MessageBox(NULL,"Release rendering context failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC = NULL; // Set RC to NULL
}

if (hDC && !ReleaseDC(hWnd,hDC)) // Are we able to release the DC
{
MessageBox(NULL,"Release device context failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC = NULL; // Set DC to NULL
}

if (hWnd && !DestroyWindow(hWnd)) // Are we able to destroy the window?
{
MessageBox(NULL,"Could not release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd = NULL; // Set hWnd to NULL
}

if (!UnregisterClass("OpenGL",hInstance)) // Are we able to unregister class
{
MessageBox(NULL,"Could not unregister class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance = NULL; // Set hInstance to NULL
}
}

/* This Code Creates Our OpenGL Window. Parameters Are:
* title - Title To Appear At The Top Of The Window
* width - Width Of The GL Window Or Fullscreen Mode
* height - Height Of The GL Window Or Fullscreen Mode
* bits - Number Of Bits To Use For Color (8/16/24/32)
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)*/

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds the results after searching for a match
WNDCLASS wc; // Windows class structure
DWORD dwExStyle; // Window extended style
DWORD dwStyle; // Window style
RECT WindowRect; // Grabs rctangle upper left / lower right values
WindowRect.left = (long)0; // Set left value to 0
WindowRect.right = (long)width; // Set right value to requested width
WindowRect.top = (long)0; // Set top value to 0
WindowRect.bottom = (long)height; // Set bottom value to requested height

fullscreen = fullscreenflag; // Set the global fullscreen flag

hInstance = GetModuleHandle(NULL); // Grab an instance for our window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw on size, and own DC for window
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc handles messages
wc.cbClsExtra = 0; // No extra window data
wc.cbWndExtra = 0; // No extra window data
wc.hInstance = hInstance; // Set the Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load the default icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the arrow pointer
wc.hbrBackground = NULL; // No background required for GL
wc.lpszMenuName = NULL; // We don't want a menu
wc.lpszClassName = "OpenGL"; // Set the class name

if (!RegisterClass(&wc)) // Attempt to register the window class
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);

return false; // Return FALSE
}

if (fullscreen) // Attempt fullscreen mode?
{
DEVMODE dmScreenSettings; // Device mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes sure memory's cleared
dmScreenSettings.dmSize = sizeof(dmScreenSettings); // Size of the devmode structure
dmScreenSettings.dmPelsWidth = width; // Selected screen width
dmScreenSettings.dmPelsHeight = height; // Selected screen height
dmScreenSettings.dmBitsPerPel = bits; // Selected bits per pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWID TH|DM_PELSHEIGHT;

// Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCC ESSFUL)
{
// If the mode fails, offer two options. Quit or use windowed mode.
if (MessageBox(NULL,"The requested fullscreen mode is not supported by\nyour video card. Use windowed mode instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen = false; // Windowed mode selected. Fullscreen = FALSE
}
else
{
// Pop up a message box letting user know the program is closing.
MessageBox(NULL,"Program will now close.","ERROR",MB_OK|MB_ICONSTOP);
return false; // Return FALSE
}
}
}

if (fullscreen) // Are We Still In Fullscreen Mode?
{
dwExStyle = WS_EX_APPWINDOW; // Window extended style
dwStyle = WS_POPUP; // Windows style
ShowCursor(false); // Hide mouse pointer
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window extended style
dwStyle=WS_OVERLAPPEDWINDOW; // Windows style
}

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust window to true requested size

// Create the window
if (!(hWnd = CreateWindowEx(dwExStyle, // Extended Style For The Window
"OpenGL", // Class name
title, // Window title
dwStyle | // Defined window style
WS_CLIPSIBLINGS | // Required window style
WS_CLIPCHILDREN, // Required window style
0, 0, // Window position
WindowRect.right-WindowRect.left, // Calculate window width
WindowRect.bottom-WindowRect.top, // Calculate window height
NULL, // No parent window
NULL, // No menu
hInstance, // Instance
NULL))) // Dont pass anything to WM_CREATE
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false; // Return FALSE
}

static PIXELFORMATDESCRIPTOR pfd = // pfd tells windows how we want things to be
{
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
};

if (!(hDC=GetDC(hWnd))) // Did we get a device context?
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't create a GL device context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false; // Return FALSE
}

if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did windows find a matching pixel format?
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't find a suitable pixelformat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false; // Return FALSE
}

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are we able to set the pixel format?
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't set the pixelformat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false; // Return FALSE
}

if (!(hRC=wglCreateContext(hDC))) // Are we able to get a rendering context?
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't create a GL rendering context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false; // Return FALSE
}

if(!wglMakeCurrent(hDC,hRC)) // Try to activate the rendering context
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't activate the GL rendering context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false; // Return FALSE
}

ShowWindow(hWnd,SW_SHOW); // Show the window
SetForegroundWindow(hWnd); // Slightly higher priority
SetFocus(hWnd); // Sets keyboard focus to the window
ReSizeGLScene(width, height); // Set up our perspective GL screen

if (!InitGL()) // Initialize our newly created GL window
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Initialization failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false; // Return FALSE
}

return true; // Success
}

LRESULT CALLBACK WndProc(HWND hWnd, // Handle for this window
UINT uMsg, // Message for this window
WPARAM wParam, // Additional message information
LPARAM lParam) // Additional message information
{
switch (uMsg) // Check for windows messages
{
case WM_ACTIVATE: // Watch for window activate message
{
if (!HIWORD(wParam)) // Check minimization state
{
active = true; // Program is active
}
else
{
active = false; // Program is no longer active
}

return 0; // Return to the message loop
}

case WM_SYSCOMMAND: // Intercept system commands
{
switch (wParam) // Check system calls
{
case SC_SCREENSAVE: // Screensaver trying to start?
case SC_MONITORPOWER: // Monitor trying to enter powersave?
return 0; // Prevent from happening
}
break; // Exit
}

case WM_CLOSE: // Did we receive a close message?
{
PostQuitMessage(0); // Send a quit message
return 0; // Jump back
}

case WM_KEYDOWN: // Is a key being held down?
{
keys[wParam] = true; // If so, mark it as TRUE
return 0; // Jump back
}

case WM_KEYUP: // Has a key been released?
{
keys[wParam] = false; // If so, mark it as FALSE
return 0; // Jump back
}

case WM_SIZE: // Resize the OpenGL window
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord = Width, HiWord = Height
return 0; // Jump back
}
}

// Pass all unhandled messages to DefWindowProc
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg; // Windows message structure
bool done = false; // Bool variable to exit loop

_control87(MCW_EM, MCW_EM); // Floating problem solver
scene.LoadFile("Scene.3DS"); //Loads the 3ds file
// 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("Scene viewer",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; // If so 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 && !DrawGLScene()

knackered
02-10-2005, 08:15 AM
are the moderators on holiday?

Bompi
02-10-2005, 12:40 PM
SORRY! I did'nt know

def
02-11-2005, 12:58 AM
It would have been sufficient to post the InitGL() and DrawGLScene() code... ;)
You are missing these three calls in the InitGL():

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

And you are using glShadeModel(GL_SMOOTH) without any lights, that will give you completely black objects.
Btw. Where did you get your l3ds distribution? The example exe file and all needed files are included as far as I can see.

Bompi
02-11-2005, 09:41 AM
Hehe... thanks! Now it works! (almost) Another problem has occured; The colors are strange.. It's not the colors Displayed in 3ds max. It's accumulated by colors of green, red, yellow and blue. :eek: Not "eyecandy", to put it like this! Do you know how to fix this?

def
02-11-2005, 04:27 PM
Delete these two lines:
glEnableClientState(GL_COLOR_ARRAY);
and
glColorPointer(3, GL_FLOAT, 0, &mesh.GetBinormal(0));

Now you can read out the 3ds materials (see the l3ds docs) and define them as OpenGL materials before the glDrawElements() call.
Not really advanced coding...