PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

How to draw a 3D model using OpenGL

Slither2006 · 25 Jan 2008, 02:36 UTC
Hi Edge,

I'm trying to render a simple 3d object using the OpenGL ES library. I couldn't find examples of using the Edgelib 3d Rendering engine so I decided to try OpenGL ES Rasteroid in the end.

When the program opens on my WM2003 device the screen is blank. I have declared the E3DSurface as a member variable and have done the following :

- Created the 3D surface.
- Set it's Polygons and Vertices
- Set the Vertice colours
- Set the Normal
- Set texture co-ordinates (Probably not required as I'm not binding or uploading any texture to the 3D surface.)
- Set Perspective 3D
- Set Shading
- Set the camera to be a certain distance away from the 3D model

- Set light source
- Render the 3d Model

This program compiles with no errors and opens up on my WM2003 device but all that shows is a black background.

Here is my Code :

Code:
ERESULT ClassMain::OnDisplayInit(ENATIVETYPE instance, ClassEDisplay *display)
{
//Create 3D surface
display->CreateSurface(&es3dRectangle;, 4, 2);

//Set Vertex's and Polygons
es3dRectangle.SetPolygon(0, 0, 2, 1);
es3dRectangle.SetPolygon(1, 1, 2, 3);
es3dRectangle.SetVertex(0, -2048, -2048, 0); es3dRectangle.SetColor(0, E_RGBA(255, 0, 0, 255)); es3dRectangle.SetNormal(0, 0, 0, 127);//tl
es3dRectangle.SetVertex(1, 2048, -2048, 0); es3dRectangle.SetColor(1, E_RGBA(255, 0, 0, 255)); es3dRectangle.SetNormal(1, 0, 0, 127);//tr
es3dRectangle.SetVertex(2, -2560, 2048, 0); es3dRectangle.SetColor(2, E_RGBA(255, 0, 0, 255)); es3dRectangle.SetNormal(2, 0, 0, 127);//bl
es3dRectangle.SetVertex(3, 2560, 2048, 0); es3dRectangle.SetColor(3, E_RGBA(255, 0, 0, 255)); es3dRectangle.SetNormal(3, 0, 0, 127);//br

//Set Texture Co-Ordinates
es3dRectangle.SetTexCoord(0, 0, 0);
es3dRectangle.SetTexCoord(1, 0, 65536);
es3dRectangle.SetTexCoord(2, 65536, 0);
es3dRectangle.SetTexCoord(3, 65536, 65536);

return(E_OK);
}

ERESULT ClassMain::OnNextFrame(ClassEDisplay *display, unsigned long timedelta)
{
//Set 3D perspective, Camera position and shading.
display->Perspective3D(display->GetWidth(), display->GetHeight());
display->SetShading(E3D_GOURAUD);
display->WorldReset();
display->WorldTranslate(0, 0, -6000);

//Set lighting
display->SetLightSource(0, 4096, 4096, 4096, ECOLOR_WHITE, E_RGBX(32, 32, 32));
display->UseLight(0);

//Render 3D Model
display->Render(&es3dRectangle;);
}


Here is my Entry CPP file

Code:
/////////////////////////////////////////////////////////////////////
// Code/view3d.cpp
// One of the Edge samples
// Shows how to create a 3D model manually and how to load a .3DS
// file. Rotate the model based on input.
//
// Copyright (c) Elements Interactive B.V.
// http://www.edgelib.com
//
// Show a 3D model on the screen, rotate it with the d-pad or stylus
/////////////////////////////////////////////////////////////////////

//Use Windows Desktop OpenGL by default
#if !defined(__EDGEIDE__)
#define EGL_USEGLES
#endif

/////////////////////////////////////////////////////////////////////
// Include and link the library //
/////////////////////////////////////////////////////////////////////

//Use a GL library if an implementation has been defined in the workspace
#if defined(EGL_PC) || defined(EGL_SYMBIAN) || defined(EGL_RASTEROID) || defined(EGL_POWERVR) || defined(EGL_NVIDIA)
#define EGL_USEGL
#if !defined(EGL_PC)
#define EGL_USEGLES
#endif
#endif

//Include Edge
#include "edgemain.h"

//Link the Edge static library
#pragma comment(lib, "edge.lib")

//Include headers and link OpenGL libraries
#if defined(EGL_USEGL)
#if defined(EGL_USEGLES)
#include "GLES\\gl.h"
#include "GLES\\egl.h"
#else
#include "GL\\gl.h"
#endif
#if defined(DEVICE_WIN32)
#if defined(EGL_PC)
#pragma comment(lib, "plugingl.lib")
#else
#if defined(DEVICE_DESKTOP)
#if defined(EGL_POWERVR)
#pragma comment(lib, "pluginpowervr.lib")
#elif defined(EGL_RASTEROID)
#pragma comment(lib, "pluginrasteroid.lib")
#endif
#else
#pragma comment(lib, "plugin1-0.lib")
#endif
#endif
#if defined(EGL_PC)
#pragma comment(lib, "opengl32.lib")
#elif defined(EGL_POWERVR)
#pragma comment(lib, "libgles_cl.lib")
#elif defined(EGL_NVIDIA)
#pragma comment(lib, "libgles_cm.lib")
#elif defined(EGL_RASTEROID)
#pragma comment(lib, "libEGL.lib")
#pragma comment(lib, "libGLES_CM_NoE.lib")
#endif
#endif
#endif

//Contains ClassMain, the application framework
#include "main.h"


/////////////////////////////////////////////////////////////////////
// The program entry point //
/////////////////////////////////////////////////////////////////////

ClassEdge *EdgeMain(EDGESTARTUP *data){ return(new ClassMain); }


Any help would be appreciated thanks! :)

Regards,

Slither2006.
Hi Slither,

I think your rectangle is not facing the camera and it doesn't get drawn. The way to solve it is to change the order in how you link the vertices into polygons. For example:

Code:

es3dRectangle.SetPolygon(0, 0, 2, 1);//your order
es3dRectangle.SetPolygon(0, 0, 1, 2);//reversed order


Also, don't forget to clear the screen before rendering your model. Please refer to the View3D sample for more information.

The View3D sample also works using the EDGELIB software renderer. The difference between the software renderer and OpenGL can be clearly seen by reading the #ifdef macro's. The functions used (for example CreateSurface, Rotate, Translate and Render) work both for OpenGL and the internal renderer.
Slither2006 · 25 Jan 2008, 12:03 UTC
Hi Edge,

Thanks for your reply. Yes that worked perfectly. I love the performance it brings a whole new dimension to animation on a ppc device. Some of the things you can do with a surface on a small device are incredible.

Many Thanks!

Slither2006 :)
Slither2006 · 26 Jan 2008, 04:48 UTC
Hi Edge,

I ended up using OpenGL ES Rasteroid and it renders quite well and quick. The only thing is that the 3d Model I made is cuboid shape. The ratio is 3:4 to match my devices resolution. 4 texture 24-bit png files are loaded as E2DSurfaces. The PNG resolutions are 240x320.

Here is the code I used to render the cuboid shape. I created 4 individual surfaces so I could bind 4 seperate texture files to it's correspoding side.

The problem is that when I rotate I can clearly see that the sides are skewed and don't appear as they should.... i.e horizontal lines in the texture appear as V shaped lines as the 3D models when rotated.

Here's the code I'm using :


Code:
ERESULT ClassMain::OnDisplayInit(ENATIVETYPE instance, ClassEDisplay *display)
{
myCoOrdValue = 0;
myRotateValue = 0;
bDirection = true;
//Create surfaces
if (display->CreateSurface(&cube;, 4, 2)== E_OK)
{
//Front
cube.SetPolygon(0, 0, 2, 1);
cube.SetPolygon(1, 1, 2, 3);
cube.SetVertex(0, -240, 320, 240);
cube.SetVertex(1, 240, 320, 240);
cube.SetVertex(2, -240, -320, 240);
cube.SetVertex(3, 240, -320, 240);
cube.SetNormal(0, 0, 0, 127); cube.SetTexCoord(0, 0, 0);
cube.SetNormal(1, 0, 0, 127); cube.SetTexCoord(1, 65536, 0);
cube.SetNormal(2, 0, 0, 127); cube.SetTexCoord(2, 0, 65536);
cube.SetNormal(3, 0, 0, 127); cube.SetTexCoord(3, 65536, 65536);
}

if (display->CreateSurface(&cube2;, 4, 2)== E_OK)
{
//Left
cube2.SetPolygon(0, 0, 2, 1);
cube2.SetPolygon(1, 1, 2, 3);
cube2.SetVertex(0, -240, 320, -240);
cube2.SetVertex(1, -240, 320, 240);
cube2.SetVertex(2, -240, -320, -240);
cube2.SetVertex(3, -240, -320, 240);
cube2.SetNormal(0, -127, 0, 0); cube2.SetTexCoord(0, 0, 0);
cube2.SetNormal(1, -127, 0, 0); cube2.SetTexCoord(1, 65536, 0);
cube2.SetNormal(2, -127, 0, 0); cube2.SetTexCoord(2, 0, 65536);
cube2.SetNormal(3, -127, 0, 0); cube2.SetTexCoord(3, 65536, 65536);
}

if (display->CreateSurface(&cube3;, 4, 2)== E_OK)
{
//Right
cube3.SetPolygon(0, 0, 1, 2);
cube3.SetPolygon(1, 1, 3, 2);
cube3.SetVertex(0, 240, 320, -240);
cube3.SetVertex(1, 240, 320, 240);
cube3.SetVertex(2, 240, -320, -240);
cube3.SetVertex(3, 240, -320, 240);
cube3.SetNormal(0, 127, 0, 0); cube3.SetTexCoord(0, 0, 0);
cube3.SetNormal(1, 127, 0, 0); cube3.SetTexCoord(1, 65536, 0);
cube3.SetNormal(2, 127, 0, 0); cube3.SetTexCoord(2, 0, 65536);
cube3.SetNormal(3, 127, 0, 0); cube3.SetTexCoord(3, 65536, 65536);
}

if (display->CreateSurface(&cube4;, 4, 2)== E_OK)
{
//Back
cube4.SetPolygon(0, 0, 1, 2);
cube4.SetPolygon(1, 1, 3, 2);
cube4.SetVertex(0, -240, 320, -240);
cube4.SetVertex(1, 240, 320, -240);
cube4.SetVertex(2, -240, -320, -240);
cube4.SetVertex(3, 240, -320, -240);
cube4.SetNormal(0, 0, 0, -127); cube4.SetTexCoord(0, 0, 0);
cube4.SetNormal(1, 0, 0, -127); cube4.SetTexCoord(1, 65536, 0);
cube4.SetNormal(2, 0, 0, -127); cube4.SetTexCoord(2, 0, 65536);
cube4.SetNormal(3, 0, 0, -127); cube4.SetTexCoord(3, 65536, 65536);
}




if (display->CreateSurface(&texture;, "Menu1.png") == E_OK)
display->UploadTexture(&texture;);

if (display->CreateSurface(&texture2;, "Menu2.png") == E_OK)
display->UploadTexture(&texture2;);

if (display->CreateSurface(&texture3;, "Menu3.png") == E_OK)
display->UploadTexture(&texture3;);

if (display->CreateSurface(&texture4;, "Menu4.png") == E_OK)
display->UploadTexture(&texture4;);

display->UseLight(0);
display->SetLightSource(0, 4096, 4096, -4096, ECOLOR_WHITE);
return(E_OK);
}


Code:
ERESULT ClassMain::OnNextFrame(ClassEDisplay *display, unsigned long timedelta)
{
//Clear Screen
display->buffer.Clear();

//Reset the 3d Model
cube.Reset();
cube2.Reset();
cube3.Reset();
cube4.Reset();

//cube.Translate(0, 0, 0);



myRotateValue += 100;

//Set the scale and rotation of the 3d model
cube.Scale(1000000, 1000000, 1000000);
cube.Rotate(0,myRotateValue,0);

//Set the scale and rotation of the 3d model
cube2.Scale(1000000, 1000000, 1000000);
cube2.Rotate(0,myRotateValue,0);

//Set the scale and rotation of the 3d model
cube3.Scale(1000000, 1000000, 1000000);
cube3.Rotate(0,myRotateValue,0);

//Set the scale and rotation of the 3d model
cube4.Scale(1000000, 1000000, 1000000);
cube4.Rotate(0,myRotateValue,0);

//Set the perspective
display->Perspective3D(display->GetWidth(), display->GetHeight());

//Set the Camera
display->WorldReset();
display->WorldTranslate(0, 0, 0);
//display->WorldScale(65536 * 2, 65536 * 2, 65536 * 2);

//Set Shading
display->SetShading(E3D_TEXTURE);

//Set Texture to 3D Model
display->BindTexture(&texture;);
//Render 3D Model
display->Render(&cube;);

//Set Texture to 3D Model
display->BindTexture(&texture2;);
//Render 3D Model
display->Render(&cube2;);

//Set Texture to 3D Model
display->BindTexture(&texture3;);
//Render 3D Model
display->Render(&cube3;);

//Set Texture to 3D Model
display->BindTexture(&texture4;);
//Render 3D Model
display->Render(&cube4;);
return(E_OK);
}


I've also tried rotating the world and keeping the 3d models stationary and although it produces the same rotating effect as the code above it also produces the same graphical glitch with the texture surface.

I've also tried making the texture PNG files resolution 256 x 324 but the same issue graphical glitch occurs.

Could you please advise me why it would appear this way.

Many Thanks,

Slither2006.
Hi Slither,

It's hard to know the issue by reading your description. Perhaps you can attach a screenshot or photo with the rotating cube in action?

You can also try to load your texture in the View3D sample (replace texture.png with your own) and see how the cube is textured. Also check this using OpenGL and the internal renderer (in the sample it uses OpenGL by default, disable it by commenting out the #define EGL_USEGL definition near the top of view3d.cpp). We recommend testing the Windows Desktop build.
Slither2006 · 27 Jan 2008, 02:28 UTC
Hi Edge,

To clarify the issue I'm having I have included a copy of the View3D program provided in the Edgelib SDK except I have modified 3 things :

The World Scale
The Texture PNG file used
The World Translation


By doing this I have illustrated that the the texture is not a properly scaled as it would appear on a rotated surface texture mapped trapezium but to two clipped paralellograms that have been trimmed in half where the polygon sides meet to give the illusion of a trapiezium shape.

Normally this is fine from a distance or if the two polygons are coloured but it looks awful for textures with horizontal lines.

This issue occurs on both the OpenGL ES engine and the Edgelib engine.

Is there a E3DSurface function I should be calling or is this just a flaw. Could you please suggest a work around.

Regards,

Slither2006.
Attachments:
MyView3D.zip (20 KB)
Slither2006 wrote:By doing this I have illustrated that the the texture is not a properly scaled as it would appear on a rotated surface texture mapped trapezium but to two clipped paralellograms that have been trimmed in half where the polygon sides meet to give the illusion of a trapiezium shape.

...

This issue occurs on both the OpenGL ES engine and the Edgelib engine.


I see what you mean ;) I can't really help you with the EDGE stuff. However, I modified one of the Rasteroids sample files to map your texture onto a cube and the artifacts you describe are not shown.

I only got the internal EDGE renderer version going with your example, switching to Rasteroids only gave me a blue background.

Edit: Seems like glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) makes all the difference. If I use GL_FASTEST then I get the artifacts in Rasteroids as well.

HTH
Slither2006 · 27 Jan 2008, 12:01 UTC
Wow thanks for testing it so far kuroneko. I was hoping someone would understand what I was trying to say...I know it's a minor thing but it makes all the difference in the program I'm making.

I unfortunately have no idea how I could apply the
Code:
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
in Edgelib. Hopefully you might be able to shed some light please Edge.

Regards,

Slither2006.
Hi Slither,

You can post the code from Kuroneko directly in your sample.

You can mix EDGELIB function calls and native OpenGL functions without much trouble, in the View3D sample it also uses a similar method for clearing the screen.
Slither2006 · 28 Jan 2008, 01:25 UTC
Hi Edge,

I tried to add the line in but it I get 4 errors :

Code:
1>\3dflip\main.h(264) : error C2065: 'GL_PERSPECTIVE_CORRECTION_HINT' : undeclared identifier
1>\3dflip\main.h(264) : error C2065: 'GL_NICEST' : undeclared identifier
1>\3dflip\3dflip\main.h(264) : error C3861: 'glHint': identifier not found
1>\3dflip\3dflip\main.h(267) : error C2146: syntax error : missing ';' before identifier 'display'


I can get this cube to work fine in opengl without the glhint line but get the graphical glitch. So I'm not sure what exactly isn't linked properly for the openGL statement above to be found.

Here are the Libraries I added to the project :

C:\projects\OpenGL ES 1.1\Library\lib\wince\pp2003\device
C:\projects\Edge\lib\ppc2003-arm_vc2005
C:\projects\Edge\lib\ppc2003-arm_vc2005\opengl

Here are the Include directories I added to the project :

C:\projects\OpenGL ES 1.1\Library\include
C:\projects\Edge\include


Does anyone know what I'm missing please?


Regards,

Slither2006.




[EDIT:] Ok so I changed the preprocessor definition

From :

Code:
#if !defined(__EDGEIDE__)
#define EGL_USEGLES
#endif



To :

Code:
#if !defined(__EDGEIDE__)
#define EGL_RASTEROID
#endif



It now compiles with no error but when I try to run the program on my phone I get this error in a message box :

The file '3DFlip cannot be opened. Either it is not signed with a trusted certificate, or one of its components cannot be found. If the problem persists, try reinstalling or restoring this file.


Anyone know what could be causing this?

[Edit Again :] Ok I found that I was linking the wrong OpenGL Library and setting the wrong preprocessor definition. I changed both ....the only problem is that it also doesn't like the glHint function either???

From :

#if !defined(__EDGEIDE__)
#define EGL_RASTEROID
#endif

C:\projects\OpenGL ES 1.1\Library\lib\wince\pp2003\device

To :

#if !defined(__EDGEIDE__)
#define EGL_USEGLES
#endif

C:\projects\OpenGL ES 1.1\Library\lib\win32

If I add the glHint line I get this error :

Code:
1>Entry.obj : error LNK2019: unresolved external symbol __imp_glHint,
referenced in function "public: virtual long __cdecl ClassMain::OnNextFrame(class ClassEDisplay *,unsigned long)",
(?OnNextFrame@ClassMain@@UAAJPAVClassEDisplay@@K@Z)
Slither2006 · 28 Jan 2008, 03:05 UTC
Hi Edge,

I believe I'm having a similar issue as Relo from this post : http://www.pocketmatrix.com/forums/view ... nal+symbol.

I'm trying to call an openGL function directly without going through the plugingl.lib rapper. Is this possible as I can't find any examples of being able to do this.

Regards,

Slither2006.
Hi Slither,

I think you almost found the solution when you got the '3DFlip cannot be opened. Either it is not signed with a trusted certificate, or one of its components cannot be found.' error.

I recommend doing the following:
When using (embedded) Visual Studio, open the project properties and go to the C/C++ tab. Then select preprocessor and add the EGL_RASTEROID definition. When using the EDGELIB Builder, open the project in notepad and add the following line to the project environment: macrodef = EGL_RASTEROID. You can also edit the c++ source code and define EGL_RASTEROID manually.

When the program compiles, copy the executable to your device. Most importantly, you also need to copy the rasteroid DLL files, otherwise you get the error mentioned above.

I hope this helps :)
Slither2006 · 28 Jan 2008, 12:08 UTC — Making progress
Hi Edge,

I followed your advice and copied across the appropriate Bin\Wince\PPC2003\Device\ dll files to the project folder on my device and have tried to set the preprocessor definition macro but am not sure if I've added it in the right place. I have attached a screenshot of VS2005 where I have tried to add it.

Currently with the changes I've made the program compiles. When I run it on my device it doesn't show any errors or message boxes but it doesn't do anything it doesn't show a black screen or anything. While it is trying to load it is non responsive. After it is done doing whatever it is doing it flickers once and then control of the device is possible.

It sounds like I'm getting closer but am not sure why the program won't start.
Attachments:
making progress.zip (218 KB)
Hi Slither,

I think you're successful in compiling and linking rasteroid. Perhaps there is a small error in your code. I recommend commenting out all code in OnDisplayInit (only enable the 'return(E_OK)' statement) and OnNextFrame. In OnNextFrame paste the following:

Code:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
return(E_OK);


After changing the code, does your program show an empty screen on your device? If it works, then you can slowly enable your old code and see which part is responsible for the exiting the application.

Additionally, you can try to comment out the first 3 lines of view3d.cpp (where it checks for the __EDGEIDE__ macro).
Slither2006 · 28 Jan 2008, 22:48 UTC — Getting further
Hi Edge,

Thanks for your quick reply. I have commented out the EGL_RASTEROID redefinition. I commented out everything in OnDisplayInit and OnNextFrame. I then added the glClear line to the OnNextFrame. The program now shows the standard blank screen with Edgelib evaluation reminder at the bottom. The only funny thing is that the FUCHSIA color of the internal edgelib font is visible. But other than that it's looking better.

[Edit:] I've started to slowly uncomment my code and it is still loading a blank screen with the edgelib evaluation reminder in front of it's FUCHSIA background. Nothing else is showing up. To rule out it being a texture issue I tried setting the display shading to both E3D_TEXTURE and E3D_WIREFRAME...still same blank screen result. I can confirm that when using the edglib function display->buffer.Clear(); it causes the program to crash without loading and without any messages. Using the glClear(etc..) doesn't.

Here is my code :

Code:
ERESULT ClassMain::OnDisplayInit(ENATIVETYPE instance, ClassEDisplay *display)
{

if (display->CreateSurface(&cube;, 4, 2)== E_OK)
{
//Front
cube.SetPolygon(0, 0, 2, 1);
cube.SetPolygon(1, 1, 2, 3);
cube.SetVertex(0, -2048, 2048, 2048);
cube.SetVertex(1, 2048, 2048, 2048);
cube.SetVertex(2, -2048, -2048, 2048);
cube.SetVertex(3, 2048, -2048, 2048);
cube.CalculateNormals();
cube.SetTexCoord(0, 0, 0);
cube.SetTexCoord(1, 65536, 0);
cube.SetTexCoord(2, 0, 65536);
cube.SetTexCoord(3, 65536, 65536);
}

display->CreateSurface(&texture;, "Menu1.png");
display->UploadTexture(&texture;);

display->UseLight(0);
display->SetLightSource(0, 4096, 4096, -4096, ECOLOR_WHITE);

return(E_OK);
}

//Callback: Called every frame
ERESULT ClassMain::OnNextFrame(ClassEDisplay *display, unsigned long timedelta)
{
//Clear Screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Reset the 3d Model
cube.Reset();
cube.Translate(0, 0, 0);
display->Perspective3D(240, 320);

//Set the Camera
display->WorldReset();
display->WorldScale(65536 * 2, 65536 * 2, 65536 * 2);

//Set Texture to 3D Model
display->BindTexture(&texture;);

//Set Shading
display->SetShading(E3D_WIREFRAME);

//Render 3D Model

display->Render(&cube;);


return(E_OK);
}


[Edit Again:] As a little test to see if anything was being drawn on the screen I used an excerpt from the Edgelib 3D/OpenGL tutorial and placed this code just before the OnNextFrame returns(E_OK);

Here is what I added :

Code:
display->Perspective2D(display->GetWidth(), display->GetHeight(), false);
display->DrawFont(0, 0, &display-;>fontinternal, "Hello world");


I found that I now get a white oblong block shape where the text should be written. No text is visible there at all.

Is there no other way the perspective can be changed to the GL equivalent of glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) in edgelib as I'd rather use the edgelib 3d engine since it's so easy to work with.

Regards,

Slither2006.
Hi Slither,

When drawing your 3D models don't forget to call display->Perspective3D() first. Directly after calling Perspective3D, call the glHint function.

Probably the text isn't correctly displayed because the font texture isn't bound.

Perhaps you can also try loading the EDGELIB logo instead of creating the cube manually (paste the loading code from the View3D sample) and see if it renders.
Slither2006 · 02 Feb 2008, 02:03 UTC
Hi Edge,

I ended up giving up a while ago and just stuck with the what the edgelib renderer could do. I'll try your suggestion in the future.

Thanks for your help!

Regards,

Slither2006.