Create our first triangle

Notes:

It is important to note that I had to change the Makefile to get this to work.
I started the homework by downloading their source code:

SB5.zip

This source gives you access to example source code for each chapter and the helper libaries
Once I unzipped the archive I created my own personal source folder for the homework (called homework) and copied the GLTools folder into it. This helped in making the paths in the Makefile more sane.
I then created a folder for the lesson and would make the Triangle.cpp and Makefile files in it.
To make the Makefile's contents I stole what I could from the Chapter 1 example Makefile. You can see I removed the X11 library references since it didn't need it.
(It may be important to note that I am using Linux since that is my preferred working enviroment.)

Makefile

NAME = Triangle
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 first exercise: Render a triangle
 */
#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

GLBatch triangleBatch;
GLShaderManager shaderManager;

// 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();

    // Load up a triangle
    GLfloat vVerts[] = {-0.5f, 0.0f, 0.0f,
                         0.5f, 0.0f, 0.0f,
                         0.0f, 0.5f, 0.0f};

    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();
}

// 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("Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);

    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