Java-Gaming.org
 Featured games (91) games approved by the League of Dukes Games in Showcase (577) games submitted by our members Games in WIP (498) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 LWJGL Tutorial Series - Moving and Rotating Triangle  (Read 2983 times) 0 Members and 1 Guest are viewing this topic.
SHC
 « Posted 2013-09-07 07:47:25 »

LWJGL Tutorial Series - Moving and Rotating Triangle

In this third part of the tutorial, we are going to render a triangle instead of the quad. Triangles are more preferred instead of quads since they can be easily made to create 3D models. However, let's first render that triangle in 2D. Create a new class called "Tutorial3" and copy everything except the render code. We're now ready to draw the triangle.

Rendering a Triangle

You can simply render a triangle with
 GL_TRIANGLES
in the same way as you've previously rendered quads. Here I'm directly rendering the vertices.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21 public void render(){    glClear(GL_COLOR_BUFFER_BIT);    // Draw a triangle.    glBegin(GL_TRIANGLES);    {        // Draw first vertex in red        glColor3f(1, 0, 0);        glVertex2f(0, 0);        // Draw second vertex in green.        glColor3f(0, 1, 0);        glVertex2f(0, 100);        // Draw third vertex in blue.        glColor3f(0, 0, 1);        glVertex2f(100, 100);    }    glEnd();}

Simple right? This renders a basic simple triangle like this.

Moving the Triangle

We move the triangle in the same way we moved the rectangle. Translate it. So now the code becomes

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 glPushMatrix();{    // Translate to the location.    glTranslatef(x, y, 0);    // Draw a triangle.    glBegin(GL_TRIANGLES);    {        // Draw first vertex in red        glColor3f(1, 0, 0);        glVertex2f(0, 0);        // Draw second vertex in green.        glColor3f(0, 1, 0);        glVertex2f(0, 100);        // Draw third vertex in blue.        glColor3f(0, 0, 1);        glVertex2f(100, 100);    }    glEnd();}glPopMatrix();

Don't forget to write the input checking code from the previous tutorial. Now you can move the triangle with your keyboard. Now we need to rotate it.

Rotating the Triangle

We rotate the coordinate system before drawing to actually rotate the triangle. For that purpose, we need to use the
 glRotatef()
function. It's syntax is

 1  2  3  4  5  6  7  8  9 /** * Rotates the coordinate system of opengl by an angle in degrees. * * @param angle The angle to be rotated in degrees. * @param x Rotate X-axis? * @param y Rotate Y-axis? * @param z Rotate Z-axis? */void glRotatef(float angle, float x, float y, float z);

This can be used to rotate the axes. For 2D, we need to rotate on the Z-Axis since X and Y axes should be displayed properly. So let's write it as

 1 glRotatef(rotation, 0, 0, 1); // Rotate 1 degree clock-wise on the z-axis

Also, we need to rotate on the center right? So we translate to the center of the triangle and after rotating, we translate back and render. So now the code becomes

 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  26  27  28  29  30  31  32 glPushMatrix();{    // Translate to the location.    glTranslatef(x, y, 0);    // Translate to the center of triangle    glTranslatef(50, 50, 0);    // Rotate on the z-axis with 1-degree.    glRotatef(rotation, 0, 0, 1);    // Translate back to the first coord.    glTranslatef(-50, -50, 0);    // Draw a triangle.    glBegin(GL_TRIANGLES);    {        // Draw first vertex in red        glColor3f(1, 0, 0);        glVertex2f(0, 0);        // Draw second vertex in green.        glColor3f(0, 1, 0);        glVertex2f(0, 100);        // Draw third vertex in blue.        glColor3f(0, 0, 1);        glVertex2f(100, 100);    }    glEnd();}glPopMatrix();

And to rotate continuously add 1 to the rotation variable every update. Now it looks like

It's the end of the third tutorial and in the next part, we're going to load textures. If there are errors or has better ways, notify me with comments.

Source Code

Tutorial3.java

quew8

JGO Coder

Medals: 23

 « Reply #1 - Posted 2013-09-07 10:06:27 »

Definitely explain what glPushMatrix and glPopMatrix do. The number of times people mess up misusing these...

Also potentially use a delta value in rotating the triangle. I don't know if you're focusing purely on teaching LWJGL/OpenGL or what, but just an idea.

Anyway, good work on these. It's about time someone did this and you're doing it well.
SHC
 « Reply #2 - Posted 2013-09-07 10:13:10 »

@quew8

Thanks for reminding me of them. I completely forgot to mention them. I'm writing the code beforehand and if it worked well, am writing the tutorials. I'll update them.

wreed12345

JGO Knight

Medals: 22
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #3 - Posted 2013-10-16 04:36:22 »

Thank you so much!  Your making learning this very fun and easy. Keep it up!

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 xsi3rr4x (24 views) 2014-04-15 18:08:23 BurntPizza (19 views) 2014-04-15 03:46:01 UprightPath (33 views) 2014-04-14 17:39:50 UprightPath (17 views) 2014-04-14 17:35:47 Porlus (33 views) 2014-04-14 15:48:38 tom_mai78101 (59 views) 2014-04-10 04:04:31 BurntPizza (117 views) 2014-04-08 23:06:04 tom_mai78101 (217 views) 2014-04-05 13:34:39 trollwarrior1 (184 views) 2014-04-04 12:06:45 CJLetsGame (191 views) 2014-04-01 02:16:10
 princec 32x Riven 29x Rayvolution 20x Gibbo3771 15x saucymeatman 12x kpars 12x Roquen 12x BurntPizza 12x ctomni231 11x Drenius 11x trollwarrior1 10x matheus23 10x theagentd 9x HeroesGraveDev 9x JFixby 8x SHC 8x
 List of Learning Resourcesby Longarmx2014-04-08 03:14:44Good Examples2014-04-05 13:51:37Good Examplesby Grunnt2014-04-03 15:48:46Good Examplesby Grunnt2014-04-03 15:48:37Good Examples2014-04-01 18:40:51Good Examples2014-04-01 18:40:34Anonymous/Local/Inner class gotchasby Roquen2014-03-11 15:22:30Anonymous/Local/Inner class gotchasby Roquen2014-03-11 15:05:20
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org