Create a new project - called lab03a_triangles - by copying an existing project as before
Insert this standard skeleton code:
#include "libopengl.h"
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void setupRC(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glOrtho (-100.0f, 100.0f, -100.0f, 100.0f, -100.0f, 100.0f);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Lab 05");
glutDisplayFunc(renderScene);
setupRC();
glutMainLoop();
return 0;
}
Create a new method triangle and invoke it from renderScene.
Display two triangles using this code:
glBegin(GL_TRIANGLES);
glVertex2f(0.0f, 0.0f); // V0
glVertex2f(25.0f, 25.0f); // V1
glVertex2f(50.0f, 0.0f); // V2
glVertex2f(-50.0f, 0.0f); // V3
glVertex2f(-75.0f, 50.0f); // V4
glVertex2f(-25.0f, 0.0f); // V5
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(0.0f, 0.0f); // V0
glVertex2f(50.0f, 0.0f); // V1
glVertex2f(25.0f, 25.0f); // V2
glVertex2f(75.0f, 25.0f); // V3
glVertex2f(50.0f, 50.0f); // V4
glEnd();
Build and test
When using strips, the triangles will be drawn using solid colours, making it difficult to identify the triangles.
This mode of rendering can be changed using this api call:
In setupRC, set the mode for the front of the polygons to be line only (and not fill):
glPolygonMode(GL_FRONT,GL_LINE);
Build and test
Now try building a triangle using a fan:
glVertex2f(0.0f, 0.0f);
glVertex2f(0.0f, 50.0f);
glVertex2f(25.0f, 30.0f);
glVertex2f(40.0f, 0.0f);
glVertex2f(25.0f, -30.0f);
glPolygonMode(GL_BACK,GL_LINE);
Build and test. Can you explain why it is now no longer solid?
We can also use the fan to render a cricle:
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
float x, y, angle;
for(angle = 0.0f; angle < (2.0f*M_PI); angle += (M_PI/8.0f))
{
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
glVertex2f(x, y);
}
glEnd();
void loadAndDrawLineStrip(char * fileName)
{
fstream inStream;
inStream.open(fileName, ios ::in); // open the file
if(inStream.fail())
return;
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
GLint numpolys, numLines, x ,y;
inStream >> numpolys; // read the number of polylines
for(int j = 0; j < numpolys; j++) // read each polyline
{
inStream >> numLines;
glBegin(GL_LINE_STRIP); // draw the next polyline
for (int i = 0; i < numLines; i++)
{
inStream >> x >> y; // read the next x, y pair
glVertex2i(x, y);
}
glEnd();
}
glFlush();
inStream.close();
}
Incorporate this into your lab exercise, and verify that you can load sample vertex file (use some of the txt files from lab 04 exercises).
Refactor this code to decouple the loading of the vertex data from subsequent rendering.
Use simple C arrays
One method should load the model data and return a 2D array, one row per vertex series (line loop).
A second method should accept this 2D array and render it.
The Standard Template Library is a more effective library for managing data structures. For simple arrays, it is generally recommended that the stl class vec be used. Refactor the implementation in 2 above to use vec instead of built in arrays. This tutorial here
Now look at this tutorial here