Académique Documents
Professionnel Documents
Culture Documents
LWJGLBasics3(TheQuad)LWJGL
LWJGLBasics3(TheQuad)
FromLWJGL
Contents
1Introduction
2Structure
3DrawingaQuadwithOpenGL
4Example
5StaticImport
6Credit
Introduction
ThistutorialwillexplainhowtoaccessOpenGLwithLWJGLandisnotintendedtoteachyouOpenGL.
TheLWJGLOpenGLAPIprettymuchmaps1:1ontotheCversion(withafewexceptions).Thisallows
onetoeasilyuseoradaptanyofthemanyOpenGLtutorialsandguidesavailableonlineandelsewherefor
usewithLWJGL.
Structure
AlltheOpenGLmethodsarefoundintheorg.lwjgl.opengl.*packageandarestoredinrelevantnamed
classese.g.GL11,GL12,GL13,GL20,ARBVertexShader,ARBFragmentShader,etc.
AllmethodsfromOpenGL1.1arefoundintheclassGL11,AllmethodsintroducedinOpenGL1.2are
foundintheclassGL12,etc.Thisallowsonetoeasilyidentifyandtargetspecificversionsorextensionsof
OpenGL.
DrawingaQuadwithOpenGL
ThecodefromtheDisplaytutorialwillbeusedwithafewmodificationstodrawaquadontheDisplay.A
2dviewshouldbesufficientfordrawingaquad,thiswillbesetbyusinganorthographicmatrixofsize
800*600withaclippingdistancebetween1and1.Thisonlyneedstobesetonceandwillbecalled
outsidethemainloop.
1
2
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0,800,0,600,1,1);
http://wiki.lwjgl.org/wiki/LWJGL_Basics_3_(The_Quad)
1/4
4/11/2015
3
4
LWJGLBasics3(TheQuad)LWJGL
GL11.glOrtho(0,800,0,600,1,1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
Theremainingcodeneedstobecalledeveryframeandwillgointhemainloop.Thiswillclearthescreen,
setthecolorofthequad,anddrawtheverticiesofthequad.
1
2
3
4
5
6
7
8
9
10
11
12
13
//Clearthescreenanddepthbuffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
//setthecolorofthequad(R,G,B,A)
GL11.glColor3f(0.5f,0.5f,1.0f);
//drawquad
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(100,100);
GL11.glVertex2f(100+200,100);
GL11.glVertex2f(100+200,100+200);
GL11.glVertex2f(100,100+200);
GL11.glEnd();
Example
Afullworkingexampleisfoundbelow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
?
importorg.lwjgl.LWJGLException;
importorg.lwjgl.opengl.Display;
importorg.lwjgl.opengl.DisplayMode;
importorg.lwjgl.opengl.GL11;
publicclassQuadExample{
publicvoidstart(){
try{
Display.setDisplayMode(newDisplayMode(800,600));
Display.create();
}catch(LWJGLExceptione){
e.printStackTrace();
System.exit(0);
}
//initOpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0,800,0,600,1,1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
while(!Display.isCloseRequested()){
//Clearthescreenanddepthbuffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
26
http://wiki.lwjgl.org/wiki/LWJGL_Basics_3_(The_Quad)
2/4
4/11/2015
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
LWJGLBasics3(TheQuad)LWJGL
//setthecolorofthequad(R,G,B,A)
GL11.glColor3f(0.5f,0.5f,1.0f);
//drawquad
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(100,100);
GL11.glVertex2f(100+200,100);
GL11.glVertex2f(100+200,100+200);
GL11.glVertex2f(100,100+200);
GL11.glEnd();
Display.update();
}
Display.destroy();
}
publicstaticvoidmain(String[]argv){
QuadExamplequadExample=newQuadExample();
quadExample.start();
}
}
ThisshouldbeenoughtogetstartedwithOpenGLandLWJGL.TheLWJGLJavadoc
(http://www.lwjgl.org/javadoc/)containsafulllistofallclassesandmethodssupportedbyLWJGLand
shouldbeusedasareferencetofindspecificOpenGLfunctionality.
StaticImport
AllOpenGLmethodsinLWJGLareaccessedstatically,meaningtheclassnameispresentbeforethe
methodname.DependingonyourcodingstyleyoumayprefernottotypetheclassnameoneveryOpenGL
method.Java'sstaticimportfeaturecanbeusedheretohidetheclassnames.
1
importstaticorg.lwjgl.opengl.GL11.*;
TheabovelineimportsallthemethodsfromtheGL11classtoallowusewithoutspecifyingtheclass.The
codecannowbetypedasfollows:
1
//clearthescreenanddepthbuffer
2
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
3
4
//setthecolorofthequad(R,G,B,A)
5
glColor3f(0.5f,0.5f,1.0f);
6
7
//drawquad
8
glBegin(GL_QUADS);
9
glVertex2f(100,100);
10
glVertex2f(100+200,100);
http://wiki.lwjgl.org/wiki/LWJGL_Basics_3_(The_Quad)
3/4
4/11/2015
10
11
12
13
LWJGLBasics3(TheQuad)LWJGL
glVertex2f(100+200,100);
glVertex2f(100+200,100+200);
glVertex2f(100,100+200);
glEnd();
Credit
TutorialCreditNinjaCave(http://ninjacave.com/lwjglbasics3)
Retrievedfrom"http://wiki.lwjgl.org/index.php?title=LWJGL_Basics_3_(The_Quad)&oldid=751"
Thispagewaslastmodifiedon23January2012,at23:15.
ContentisavailableunderPublicDomainunlessotherwisenoted.
http://wiki.lwjgl.org/wiki/LWJGL_Basics_3_(The_Quad)
4/4