Recent

Author Topic: Trouble with OpenGLControl? [SOLVED]  (Read 7693 times)

joseywales72

  • New Member
  • *
  • Posts: 20
Trouble with OpenGLControl? [SOLVED]
« on: June 29, 2011, 04:21:01 am »
Hi,
I use Lazarus to mainly create small apps for my work, GUI stuff. No problem with that. Now I try to dive into graphics, especially OpenGL, not very fancy stuff, simple things. I am considering to try Nehe tutorials on Lazarus for this.  For this, I installed OpenGLControl package for Lazarus... and I stuck..
I use 09.30 on 64 bit Linux with GTK2 widgetset. I created a form with a 400x300 OpenGLControl. Now, how can I draw, say, a triangle, like the example below on that OpenGLControl. (Taken from Lazarus CCR.)
Code: [Select]
program simpleshape;



{$mode objfpc}{$H+}



uses

  gl, glu, glut;



const

  AppWidth = 640;

  AppHeight = 480;



procedure InitializeGL;

begin

  glClearColor(0.18, 0.20, 0.66, 0);

end;



procedure DrawGLScene; cdecl;

begin

  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);



  glLoadIdentity;

  glTranslatef(0, 0, -5);



  glBegin(GL_TRIANGLES);

    glColor3f(1, 0, 0);

    glVertex3f(-1, -1, 0);



    glColor3f(0, 1, 0);

    glVertex3f(1, -1, 0);



    glColor3f(0, 0, 1);

    glVertex3f(0, 1, 0);

  glEnd;

 

  glutSwapBuffers;

end;



procedure ReSizeGLScene(Width, Height: Integer); cdecl;

begin

  if Height = 0 then

    Height := 1;



  glViewport(0, 0, Width, Height);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity;

  gluPerspective(45, Width / Height, 0.1, 1000);



  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity;

end;



procedure GLKeyboard(Key: Byte; X, Y: Longint); cdecl;

begin

  if Key = 27 then

    Halt(0);

end;



procedure glutInitPascal(ParseCmdLine: Boolean);

var

  Cmd: array of PChar;

  CmdCount, I: Integer;

begin

  if ParseCmdLine then

    CmdCount := ParamCount + 1

  else

    CmdCount := 1;

  SetLength(Cmd, CmdCount);

  for I := 0 to CmdCount - 1 do

    Cmd[I] := PChar(ParamStr(I));

  glutInit(@CmdCount, @Cmd);

end;



var

  ScreenWidth, ScreenHeight: Integer;

begin

  glutInitPascal(True);

  glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH);

  glutInitWindowSize(AppWidth, AppHeight);

  ScreenWidth := glutGet(GLUT_SCREEN_WIDTH);

  ScreenHeight := glutGet(GLUT_SCREEN_HEIGHT);

  glutInitWindowPosition((ScreenWidth - AppWidth) div 2, (ScreenHeight - AppHeight) div 2);

  glutCreateWindow('OpenGL Tutorial :: Simple Shape');



  InitializeGL;



  glutDisplayFunc(@DrawGLScene);

  glutReshapeFunc(@ReSizeGLScene);

  glutKeyboardFunc(@GLKeyboard);



  glutMainLoop;

end.


I realize that I don't need any GLUT related stuff on OpenGLControl of Lazarus, but how will I display the triangle on the control.

I'm really sorry if my question sounds too dumb but...
Thank you in advance.
Anil
« Last Edit: June 29, 2011, 04:21:23 pm by joseywales72 »

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: Trouble with OpenGLControl?
« Reply #1 on: June 29, 2011, 01:30:40 pm »
Opengl is not an easy subject for a beginner, now more then ever. the code you have posted is considered obsolete. but is still used alot in the opengl community. this is because the new code is much harder then the old and the Pascal community has fallen way behind. there is one demo that uses  OpenGLControl you will find it here lazarus/examples/openglcontrol. many people use sdl for opengl
http://www.freepascal-meets-sdl.net/
and for an easier 2d engine that uses opengl
http://code.google.com/p/zengl/
you can also get more help here
http://www.pascalgamedevelopment.com/forum/


joseywales72

  • New Member
  • *
  • Posts: 20
Re: Trouble with OpenGLControl?
« Reply #2 on: June 29, 2011, 03:59:09 pm »
Thank you for your help Carver413, but those libraries are not what I look for.  They are more game oriented I suppose. What I need is a GUI application with OpenGL pane. OpenGLControl seems to fit my needs but I need a starting point.
The snippet I posted is just a little example. What I need is how to initialize and begin drawing some simple graphics on OpenGLControl.
I should be more specific perhaps, sorry about that. Also, I had already taken a look to the very example you mentioned before I posted my question. But I could not understand it. I only need a simple nudge toward using OpenGLcontrol with an how-to to draw a simle triangle or a line in it.
Thank you.

joseywales72

  • New Member
  • *
  • Posts: 20
Re: Trouble with OpenGLControl? [SOLVED]
« Reply #3 on: June 29, 2011, 04:22:38 pm »
I just found an example as I exactly wanted.
http://www.pascalgamedevelopment.com/archive/index.php/t-5825.html
Thank you.

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: Trouble with OpenGLControl? [SOLVED]
« Reply #4 on: June 29, 2011, 11:48:39 pm »
I'm glad you found what you needed. just remember that any opengl code is a source for learning even if you don't want to make games. when you can't quite figure out how to use some function you can use these source files and lazarus's find in files to look for answers. I have a ton of code that I use for this purpose and it has saved me a few times.

 

TinyPortal © 2005-2018