Drawing with one colour

Now we will put some of the things togehter, just to see if it works.

VTrace1.jpg

Nothing fancy here, but hopefully we will see the sphere we defined drawn with a single colour. Edit the Main.C file:

#include "Ray.H"
#include "Transform.H"
#include "Sphere.H"

#include <GL/gl.h>
#include <GL/glut.h>

#define TX     0.0f
#define TY     0.0f
#define TZ   -10.0f
#define RADIUS 1.0f
#define ZMIN   0.0f
#define ZMAX   1.0f
#define PHIMAX 360.0f

int W, H;
Shape *pShape;
Transform *pObjectToWorld;
Transform *pWorldToObject;
OSL::ShaderGlobals SG;

void render(int width, int height)
{
    Float invWidth = 1.0 / width;
    Float invHeight = 1.0 / height;
    Float x, y;
    Float zw = 10.0f;
    Float d = 10.0f;
    Float t, epsilon;

    Vec3 p(0.0f, 0.0f, zw);
    glBegin(GL_POINTS);
    glColor3f(0.0f, 1.0f, 1.0f);
    for (int r = 0; r < height; r++) {
        for(int c = 0; c < width; c++) {
            x = (2.0f * c * invWidth - 1.0f);
            y = (2.0f * r * invHeight - 1.0f);
            Vec3 vec(x, y, -d);
            Ray ray(p, vec.normalized(), M_EPSILON);
            if (pShape->Intersect(ray, &t, &epsilon, &SG)) {
                glVertex2i(c, r);
            }
        }
    }
    glEnd();
}

void resize(int width, int height)
{
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, width, 0, height);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  W = width;
  H = height;
}

void init(void)
{
  resize(glutGet(GLUT_WINDOW_WIDTH),
         glutGet(GLUT_WINDOW_HEIGHT));
}

void redraw(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    render(W, H);
    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    Transform trans = Translate(Vec3(TX, TY, TZ));
    pObjectToWorld = new Transform(trans);
    pWorldToObject = new Transform(Inverse(*pObjectToWorld));
    pShape = new Sphere(pObjectToWorld,
                        pWorldToObject,
                        false,
                        RADIUS,
                        ZMIN,
                        ZMAX,
                        PHIMAX);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("VTracer");
    glutDisplayFunc(redraw);
    glutReshapeFunc(resize);
    init();
    glutMainLoop();

    return 0;
}

We use glut to set up an Open GL capable window. In the main function we define the sphere along with the world to object and object to world transformations. The render function does the actual job of shooting rays, if a ray hits the sphere a cyan coloured pixel is drawn.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License