PDA

View Full Version : selection buffer receved all objects from stack name



Alkog Alkog
02-27-2017, 07:57 AM
Hello I have a question that going me crazy. I can't find an error. There is a class where OpenGL objects are drawen.


unit ObjClass

{$mode objfpc}{$H+}

interface

uses
Classes, SysUtils, mathMetrix, gl, glu,GLX, OpenGLContext, ImagingOpenGL;
type
TObjClass = class(TObject)
private
public
procedure RelayDraw(BasePoint : TArray_BPoint);


end;
var
clsObj : TRelayClass;

implementation

procedure TObjClass.ObjDraw (BasePointX : GLDouble; BasePointY : GLDouble);
begin

glColor3d(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex2d(BasePointX - 0.01, BasePointY + 0.01 );
glVertex2d(BasePointX + 0.01, BasePointY + 0.01 );
glVertex2d(BasePointX + 0.01, BasePointY + 0.01 );
glVertex2d(BasePointX + 0.01, BasePointY - 0.01 );
glVertex2d(BasePointX + 0.01, BasePointY - 0.01 );
glVertex2d(BasePointX - 0.01, BasePointY - 0.01 );
glVertex2d(BasePointX - 0.01, BasePointY - 0.01 );
glVertex2d(BasePointX - 0.01, BasePointY + 0.01 );
glEnd();
end;

end.

unit Main;
//...

var
BasePointX,
BasePointY,
: Double;
buffer_len: integer = 63;
MoveFlag : Boolean;

ID0 : GLuint = 0;
ID1 : GLuint = 1;
ID2 : GLuint = 2;
ID3 : GLuint = 3;
ID4 : GLuint = 4;
ID5 : GLuint = 5;


implementation
//...
procedure Tfrm.OpenGLControl1Paint(Sender: TObject);
var
Obj0, Obj1, Obj2 : TObjClass;
begin

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glViewport(0, 0, OpenGLControl1.Width, OpenGLControl1.Height);
compWidth:=OpenGLControl1.Width;

glPushMatrix();
glLoadIdentity();
glInitNames();
glPushName(0);


glPushMatrix();
glLoadName(ID0);
Obj0 :=TObjClass.Create;
Obj0.ObjDraw (BasePointX, BasePointY);
glPopMatrix();


glTranslated(0.0, -0.4, 0.0);
glPushMatrix();
glLoadName(ID1);
Obj1 :=TObjClass.Create;
Obj1.ObjDraw (BasePointX, BasePointY);
glPopMatrix();

glTranslated(-0.4, 0.0, 0.0);
glPushMatrix();
glLoadName(ID2);
Obj2 :=TObjClass.Create;
Obj2.ObjDraw (BasePointX, BasePointY);
glPopMatrix();

glPopMatrix();

OpenGLControl1.SwapBuffers;

end;

procedure Tfrm.Selection(X : integer; Y : integer);
var selectBuf : array [0..63] of GLint;
viewport : array [0..3] of GLint;
fAspect : GLfloat;
ObjFound : GLint;
lowestDepth, selObject, i : integer;

begin
glSelectBuffer(buffer_len+1, 'at sign' selectBuf);
glGetIntegerv(GL_VIEWPORT, 'at sign' viewport);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glRenderMode(GL_SELECT);
glLoadIdentity();
gluPickMatrix(GLdouble(X), GLdouble(viewport[3]-Y), 0.1, 0.1, viewport);
OpenGLControl1Paint(nil);
ObjFound := glRenderMode(GL_RENDER);

glMatrixMode(GL_PROJECTION);
glPopMatrix();
if ObjFound>0 then
begin
lowestDepth := selectBuf[1];
selObject := selectBuf[3];

ShowMessage('selectBuf[0] ' + IntToStr(selectBuf[0]));
ShowMessage('selectBuf[1] ' + IntToStr(selectBuf[1]));
ShowMessage('selectBuf[2] ' + IntToStr(selectBuf[2]));
ShowMessage('selectBuf[3] ' + IntToStr(selectBuf[3]));
ShowMessage('selectBuf[4] ' + IntToStr(selectBuf[4]));
ShowMessage('selectBuf[5] ' + IntToStr(selectBuf[5]));
ShowMessage('selectBuf[6] ' + IntToStr(selectBuf[6]));
ShowMessage('selectBuf[7] ' + IntToStr(selectBuf[7]));
ShowMessage('selectBuf[8] ' + IntToStr(selectBuf[8]));
ShowMessage('selectBuf[9] ' + IntToStr(selectBuf[9]));
ShowMessage('selectBuf[10] ' + IntToStr(selectBuf[10]));
ShowMessage('selectBuf[11] ' + IntToStr(selectBuf[11]));


end;
end;
end.

There is another class, here is object creating. In OpenGLControl1Pain function it is an object creating and then they are moved into name stack. By mouse clicking an object under mouse cursor must be moved into selection buffer.
The question is following, by clicking I don't drag over an object but all objects. Selection buffer receives all objects in the order in which they are recorded in the stack name. Please help me anybody!