Move our first triangle

Notes:

This example is for showing off keyboard mutation of an object; I say mutation instead of movement since this is not how you move objects in OpenGL. You are actively morphing the object's coordinates (which leads to weird rotation issues). You normally won't do that but instead change where the object is rendered on the screen using a matrix.
The code in the book mentions moving a block but I saw no point in jumping to a block immediately so I am keeping the shape a triangle.

Makefile

NAME = Move
SRCPATH = ./
SHAREDPATH = ../GLTools/src/
SHAREDINCPATH = ../GLTools/include/
LIBDIRS = -L/usr/local/lib
INCDIRS = -I/usr/include -I/usr/local/include -I/usr/include/GL -I$(SHAREDINCPATH)  -I$(SHAREDINCPATH)GL

CC = g++
CFLAGS = -g $(INCDIRS)
#LIBS = -lX11 -lglut -lGL -lGLU -lm
LIBS = -lGL -lglut -lGLU -lm

all: 
	$(CC) $(CFLAGS) $(LIBS) $(SRCPATH)$(NAME).cpp $(SHAREDPATH)glew.c $(SHAREDPATH)GLTools.cpp $(SHAREDPATH)GLBatch.cpp $(SHAREDPATH)GLTriangleBatch.cpp $(SHAREDPATH)GLShaderManager.cpp $(SHAREDPATH)math3d.cpp -o $(NAME)
clean:
	rm -f *.o

Source:

/*
 * OpenGL SuperBible second exercise: Render a triangle and move with keys
 */
#include <GLTools.h>    // OpenGL toolkit
#include <GLShaderManager.h>    // Shader manager class

#ifdef __APPLE__
#include <glut/glew.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif

// an assortment of needed classes
//GLBatch squareBatch;
GLBatch triangleBatch;
GLShaderManager shaderManager;

// A global variable?
// triangle
GLfloat vVerts[] = {-0.5f, 0.0f, 0.0f,
                     0.5f, 0.0f, 0.0f,
                     0.0f, 0.5f, 0.0f};

// Resize the viewport on window creation or resize
void ChangeSize(int w, int h)
{
    glViewport(0,0, w, h);
}

// setup the rendering context
void SetupRC()
{
    // Blue background
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

    shaderManager.InitializeStockShaders();

    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
}

// render the scene
void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f};
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();

    // Perform the buffer swap to display the back buffer
    glutSwapBuffers();
}

// handle key input
void SpecialKeys(int key, int x, int y)
{
    GLfloat stepSize = 0.0025f;
    GLfloat triangleSize = 0.5f;

    GLfloat triangleX = vVerts[0] + triangleSize; //X
    GLfloat triangleY = vVerts[1]; //Y

    if (key == GLUT_KEY_UP)
    {
        triangleY += stepSize;
        printf("key up\n");
    }
    if (key == GLUT_KEY_DOWN)
    {
        triangleY -= stepSize;
        printf("key down\n");
    }
    if (key == GLUT_KEY_LEFT)
    {
        triangleX -= stepSize;
        printf("key left\n");
    }
    if (key == GLUT_KEY_RIGHT)
    {
        triangleX += stepSize;
        printf("key right\n");
    }

    // Collision detection
    /*
    if (triangleX < -1.0f) triangleX = -1.0f;
    if (triangleX > (1.0f - triangleSize * 2)) triangleX = -1.0f - triangleSize * 2;
    if (triangleY < -1.0f + triangleSize * 2) triangleY = -1.0f + triangleSize * 2;
    if (triangleY >  1.0f) triangleY = 1.0f;
    */

    // Recalculate vertex positions
    vVerts[0] = triangleX - triangleSize;       // X
    vVerts[1] = triangleY;                      // Y
                                                // Z
                                        
    vVerts[3] = triangleX;                      // X
    vVerts[4] = triangleY + triangleSize;       // Y
                                                // Z
    
    vVerts[6] = triangleX + triangleSize;       // X
    vVerts[7] = triangleY;                      // Y
                                                // Z

    //squareBatch.CopyVertexData3f(vVerts);
    triangleBatch.CopyVertexData3f(vVerts);

    glutPostRedisplay();
}

// main entry point for GLUT based games
int main(int argc, char *argv[])
{
    gltSetWorkingDirectory(argv[0]);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(480, 480);
    glutCreateWindow("Move");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    glutSpecialFunc(SpecialKeys);

    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
        return 1;
    }

    SetupRC();

    glutMainLoop();
    return 0;
}

Answer:


Back to index