Java-Gaming.org
 Featured games (90) games approved by the League of Dukes Games in Showcase (557) games submitted by our members Games in WIP (477) 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 2677 times) 0 Members and 4 Guests 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

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 digdugdiggy (17 views) 2014-03-14 23:54:44 Rayvolution (27 views) 2014-03-13 00:02:00 The Lion King (51 views) 2014-03-11 04:35:53 Screem (74 views) 2014-03-08 05:04:17 Screem (74 views) 2014-03-08 05:01:59 Gibbo3771 (85 views) 2014-03-05 15:13:19 Conzar (85 views) 2014-03-05 09:59:23 Conzar (80 views) 2014-03-05 09:57:20 GamerIDGoesHere (112 views) 2014-03-02 10:31:15 GamerIDGoesHere (99 views) 2014-03-02 10:30:27
 opiop65 30x kpars 23x Rayvolution 21x BurntPizza 20x HeroesGraveDev 19x Grunnt 17x jonjava 16x saucymeatman 15x theagentd 15x Riven 14x LiquidNitrogen 14x reymantha 13x revers 13x princec 11x Troubleshoots 10x Jimmt 10x
 Anonymous/Local/Inner class gotchasby Roquen2014-03-11 15:22:30Anonymous/Local/Inner class gotchasby Roquen2014-03-11 15:05:20Anonymous/Local/Inner class gotchasby Roquen2014-03-10 14:19:36HotSpot Optionsby Roquen2014-02-26 17:00:23HotSpot Optionsby Roquen2014-02-26 11:27:03HotSpot Optionsby Dxu19942014-02-18 01:05:35HotSpot Options2014-02-16 03:47:31Mainby Roquen2014-02-10 11:17:06
 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