PDA

View Full Version : Absolute begginner help



Gwynnnn
12-05-2006, 07:06 AM
Forgive me but we all have to start somewhere. I am totally new to opengl

I am trying to create the simplest window as a starting point. Compiling is ok. Linking fails. I need some help.

Details

Windows XP, Borland C++ Builder version 6
Code has been copied from one of the many web tutorials and it compiles fine.

The link error is as follows (2 errors)

[Linker Error] Unresolved external '_Form1' referenced from .....myfile.obj

and then a repeat with 'TForm1' instead of '_Form1'

Firstly I do not understand the error fully and secondly I have no direct references to these forms in the code.

Can anyone shed some light on this for me. Sorry if this is a dumb question. I have been looking at open gl for about 4 hours now (including getting C++ up and running) and most of that time has been on help and web to try to resolve this.

Gwynnnn
12-05-2006, 07:10 AM
If it helps I have copied the full code used to www.good-image.co.uk (http://www.good-image.co.uk) (no formatting)

Gwynnnn
12-05-2006, 07:15 AM
I may have been a little premature!

I have started again as I made a guess that the code was conflicting with something else that may have been left behind from another test.

I get it to compile, link, build and run now.

BUT

All I get is a blank window, no pretty triangle. I will keep looking at it. If anyone can help in this new aspect please see the code at good-image.co.uk and offer any advice.

Thanks

songho
12-05-2006, 07:45 AM
Gwynnnn,
I don't see projection settings in your code. The eye (viewpoint) is at (0,0,0) and looking along -Z axis by default. The view point may be on the same plane where the pretty triagle is placed.

Try to move the triangle backward a little, for example:

void display()
{
/* rotate a triangle around */
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3i(0, 1, -3);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3i(-1, -1, -3);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3i(1, -1, -3);
glEnd();

glFlush();
}

Gwynnnn
12-05-2006, 07:56 AM
Thanks for looking. I think my problems are way more basic than that. But I will appreciate your help never the less.

When I do a run I get a blank form (Form1) no colours, no trinagles, nothing. So my guess is that I am really doing something basic wrongly.

I notice that I have a bit more code that I should have put on to good-image that I had thought was irrelevant. Wrong probalby, so I will put up the complete code.

Please have a look and tell me why it draws up Form1 and does not attempt (apprantly) to draw any coloured triangle.

I think it explains why Form1 appears, but I am stumped as to where the triangle or window with a triangle is.

Gwynnnn
12-05-2006, 08:25 AM
Sorry I am having a bit of a panic attack. Copied the wrong file up. I have now copied the right file.

Gwynnnn
12-05-2006, 08:29 AM
I suspect that i need to put something onto Form1, presently I have put nothing at all. Any suggestions?

Gwynnnn
12-05-2006, 08:51 AM
I believe that the code shown does not touch the windowing or display parts when running. This is my own error thoughg a lack of understanding.

Can anyone suggest what I should be writing in order to get things up on the desired window?

Gwynnnn
12-05-2006, 09:32 AM
I think I have completely lost it.

Is there anyone I can ask to lead me through these initial stages.

I can't even get it to 'like' references to glut.

I despair right now.

Komat
12-05-2006, 10:23 AM
With default projection matrix (identity matrix) you need to have Z coordinate of the vertices inside the <-1,1> range. After that change (and removal of Borland specific stuff from file beginning) the triangle was shown when I compiled your source in Microsoft Visual C++.

While not directly related, the ReleaseDC expects hwnd as first parameter.

Gwynnnn
12-05-2006, 10:32 AM
Thank you for at least replying. I will be fine once i get at least one working situation.

I have now removed the borland specific stuff

(I presume you mean this part shown at the end of this text

however, I am totally new to open gl, how do I make the z coordinate range acceptable? Can you give me the example?

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

Gwynnnn
12-05-2006, 10:38 AM
ok this is showing just how little I understand at this time. I will get there but right now I'm on very thin ice.

I removed the offending code (At least I think that was the code I think you meant) and we went right back to the linker error problem of referencing a form being unresolved.

So can I go back to the very start. What sort of item do i create? Is it an application? A Unit? simple text?

All compile, none link without the top code added.

How about a simple 1,2,3 guide

Sorry to be so dumb.

Just how did you get yours to work. Can you help me. Perhaps post the code as is? and let me know what sort of application you created to make it work?

Gwynnnn
12-05-2006, 10:48 AM
Ok things are better.

I have removed the offending code at the top.

I have created a cpp file and it compiles, and builds. We are now back to the blank screen.

If it is just a coordinate problem then just to help me out can you let me know which lines need changing to what.

I will continue to bash away at it.

As I hav eno visual clues it is difficult for me to see where I may be going wrong

Gwynnnn
12-05-2006, 10:52 AM
I presume the z plane references you are referring to are here

void display()
{
/* rotate a triangle around */
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3i(0, 1, 0.5);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3i(-1, -1, -0.5);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3i(1, -1, -0.5);
glEnd();

glFlush();
}

Have I changed them (0.5, -0.5) in a way that will help or wreck it?

It still gives me a blank window anyway, whatever values I try.

Gwynnnn
12-05-2006, 11:07 AM
Can you tell me exactly what I should be seeing when the program runs? as it would help.

Should I be seeing a form only? Should there be another box or window inside that? (I think not). Should there be a coloured triangle only inside the form? (I think so).

marc2718
12-05-2006, 11:22 AM
To start out I think you should keep everything very simple, now I'm not sure about your code, but here is some very simple code all it does is initialize OGL and display a yellow rectangle.

In your header file define the following;

HDC m_hdc;
HGLRC m_glrc;


then in the source code file;


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


void TForm1::SetPixelFormatDescriptor()
{

PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |

PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
//use the default bit planes
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
PixelFormat = ChoosePixelFormat(hdc, &pfd); // choose and set the
SetPixelFormat(hdc, PixelFormat, &pfd); // appropriate pixelformat

}


void __fastcall TForm1::FormCreate(TObject *Sender)
{



hdc = GetDC(Handle); // get device context from main window
SetPixelFormatDescriptor();
hrc = wglCreateContext(hdc); // use device context to create
// a rendering context
if(hrc == NULL)
ShowMessage("Creating Rendering Context failed.");
wglMakeCurrent(hdc,hrc); // tell windows to use hdc and rdc
if(wglMakeCurrent(hdc, hrc) == false)
ShowMessage("MakeCurrent of rendering context failed.");


}


void __fastcall TForm1::FormPaint(TObject *Sender)
{

glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 0.0);//black background


glColor3f(1.0,1.0,0.0); //draw yellow

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0.0,10.0,0.0,10.0,-1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(2.0,4.0,0.0);
glVertex3f(8.0,4.0,0.0);
glVertex3f(8.0,6.0,0.0);
glVertex3f(2.0,6.0,0.0);
glEnd();



glFlush();
SwapBuffers(m_hdc); // make it visible


}

Gwynnnn
12-05-2006, 11:58 AM
Thank you for taking the trouble to post up the example code. I had taken the simplest code I could find previously.

I have now entered your code into a header file and a code fle. It compliles, makes and builds fine.

Unfortunately I still only get a Form1 which is very nice shade of buff brown, but absolutely nothing drawn inside it.

This is beggining to sound a lot more fundamental but i have absolutely no idea what is wrong at this stage.

Any ideas?

Komat
12-05-2006, 12:37 PM
Originally posted by Gwynnnn:
Can you tell me exactly what I should be seeing when the program runs? as it would help.

Should I be seeing a form only? Should there be another box or window inside that? (I think not). Should there be a coloured triangle only inside the form? (I think so). I am seeing this image (http://www2.webpark.cz/jiridvorak/ruzne/triangle.html) . I uploaded modified source code and compiled program to that page so you can try it if it works for you.

What graphic card do you have?

Gwynnnn
12-05-2006, 12:50 PM
Oh now that is the sort of thing I was expecting and wanting to see.

I have only recently built the new PC. It has an NVIDIA GeForce 7900 GT card. It is GL enabled as I have run a pre compiled item I found on the net yesterday.

My problems seem to be stemming from my lack of understanding of Borland C++ version 6. There must be something that i have not set or done.

What i see is like a normal empty form. I could be creating an dbuilding all wrong. The small window in your example image is what I expected to see but it is absent (or not visible).

Anyone want to take it step by step from opening the C++ IDe to the run itself?

Gwynnnn
12-05-2006, 12:54 PM
Ah ha. A step further. I deleted all forms that were present on start up of the C++ IDE. I then loaded up the example. Built it. and then ran it.

What did I get -

Absolutely nothing at all! A blink of the object viewer window 9as expected) and then all back to the code window.

My conclusion from this is that if the porogram is running then it is going straight through it and ending before i can see anything at all.

Any help?

Komat
12-05-2006, 01:08 PM
To run the source code from me you probably need to select some sort of "Windows application without forms" build/linking option otherwise the builder will create its own initialization code that tries to run the forms and will ignore the WinMain function.

I never used the Borland compiler so I do not know how to select that.

EDID: I read some tutorial on net and it appears that you need to create new application project using the "Console Wizard" type.

Gwynnnn
12-05-2006, 01:18 PM
Mmm that seems logical. So I have now created a console unit in borland C++.

The code compiles, makes, builds, runs and we now get a console window (as expected)

BUT

No graphics.

I really think that this is a very basic fundamental cock up on my part.

I am very grateful for your help and please keep it coming. At some point it must crack.

Gwynnnn
12-05-2006, 01:21 PM
Other points: the black console window appears to be very large. GFar larger than I woudl have expected.

It certainly is not 0,0,256,256

It seems wider and I have to scroll down a long way to get to the bottom.

So it may jus tbe a normal sized console window. ie not drawn up by the graphics application code.

Incidentally I am still using the code kindly posted here by an earlier contributor if you are interested.

Gwynnnn
12-05-2006, 01:22 PM
Yes, I think I am right as the console window shown when running has no title (Minimal) at all. Its just a console window. So this may or may not be the right direction.

Komat
12-05-2006, 01:30 PM
There are two application types you can create. With console (this is the one you created) and without console, both should be creatable using the "Console Wizard" (at least according to the tutorials (http://www.functionx.com/bcb/topics/wndfunds.htm) I read). In MSVC they each expect different main function and only the one without console expect the WinMain.

Code from marc2718 on the other hand requires the Borland forms however I do not have that compiler so I can not try that by myself.

Gwynnnn
12-05-2006, 01:38 PM
Is there anyone out there who has Borland C++ Builder version 6 and can lead me through setting up a siomple gl app (ie a window and a cbe of colour), step by step?

marc2718
12-05-2006, 02:02 PM
Gwynnn, sorry my code was inconsistent (the variable names), actually it should not have compiled for you.
Anyhow, the following is correct.
( I think this is as simple as it can get)

The best thing for this code just create a new regular c++ builder application.
NOT a console application, that is not the way you will be using C++ Builder.

Cut and paste this code into your app.
You need to create the OnCreate event and the OnPaint event then just add the code in there.
The only function you need to create manually is
the SetPixelFormatDescriptor function.


in the header file;

#include <gl\gl.h>


//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormPaint(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
private: // User declarations


HDC hdc; // device context
HGLRC hrc; //rendering context


void SetPixelFormatDescriptor();


public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


Now the source code file;


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


void TForm1::SetPixelFormatDescriptor()
{

PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |

PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
//use the default bit planes
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
int PixelFormat = ChoosePixelFormat(hdc, &pfd); // choose and set the
SetPixelFormat(hdc, PixelFormat, &pfd); // appropriate pixelformat

}


void __fastcall TForm1::FormCreate(TObject *Sender)
{


hdc = GetDC(Handle); // get device context from main window
SetPixelFormatDescriptor();
hrc = wglCreateContext(hdc); // use device context to create
// a rendering context
if(hrc == NULL)
ShowMessage("Creating Rendering Context failed.");
wglMakeCurrent(hdc,hrc); // tell windows to use hdc and rdc
if(wglMakeCurrent(hdc, hrc) == false)
ShowMessage("MakeCurrent of rendering context failed.");


}


void __fastcall TForm1::FormPaint(TObject *Sender)
{

glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 0.0);//black background


glColor3f(1.0,1.0,0.0); //draw yellow

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0.0,10.0,0.0,10.0,-1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(2.0,4.0,0.0);
glVertex3f(8.0,4.0,0.0);
glVertex3f(8.0,6.0,0.0);
glVertex3f(2.0,6.0,0.0);
glEnd();


glFlush();
SwapBuffers(hdc); // make it visible


}

Gwynnnn
12-05-2006, 02:13 PM
You lost me a little

You need to create the OnCreate event and the OnPaint event then just add the code in there.


Not sure what code you mean here


The only function you need to create manually is
the SetPixelFormatDescriptor function.

How and what do i put in it.

Sorry but I am really very new to all of this.

Thanks though

I am looking at it right now anyway to see if I can blunder my way through

marc2718
12-05-2006, 02:14 PM
By the way I tested this code in the compiler,I know it compiles, your video card is fine.
It has to work! :)

You should see a window with yellow rectangle in the center with a black background.

Gwynnnn
12-05-2006, 02:15 PM
Ah getting a bit clearer. Found the on create evewnt in the object inspector. I will shove the code into that.

Gwynnnn
12-05-2006, 02:16 PM
ok created the events, not sure what code exactly to shove in.

Gwynnnn
12-05-2006, 02:18 PM
Worked that one out too now.

Nearer to compiling - not ready to try just yet

Gwynnnn
12-05-2006, 02:20 PM
Only the pixeldescriptor bit left to figure out.

Gwynnnn
12-05-2006, 02:34 PM
Well bless me. It all works. I have a black rectangular box with an inner yellow box (both filled).

Oh yesssss.

I am so very very grateful to you all.

I felt that it was a bit tough going but then we all have ot go through the 'hello world' stage.

Thanks for your patience and advice

This has been a good and helpful thread to me.

Now all I have ot do is climb the rest of the 160000000 mile mountain.

Gwynnnn
12-05-2006, 02:39 PM
Noooo disaster.

I saved the files and reopened them and now I have 18 errors and nothing works.

Agh!!

Gwynnnn
12-05-2006, 02:42 PM
Phew, just a mild panic.

I redid the whole thing and had named the header file wrongly in my panic.

It's amazing how importnat a name can be!

I really must go and have a quiet drink!

Thanks once again.

Its nearly midnight here so I may just give it a rest until tomorrow night.

Gwynnnn
12-05-2006, 02:53 PM
Goodnight to you all. I will now sleep a lot more soundly. I have made some real progress this evening. Thank you