teaching machines

CS 455 Lecture 3 – An Interactive Renderer

February 3, 2015 by . Filed under cs455, lectures, spring 2015.

Agenda

TODO

Before next class:

Code

second.cpp

#include <iostream>

#include "BaseRenderer.h"
#include "glut_utilities.h"
#include "VertexArray.h"

using namespace td;

class SecondRenderer : public BaseRenderer {
  public:
    void OnDraw(float delta);
    void OnInitialize();
    void OnKey(Keys::key_t key);
    void OnLeftMouseDown(int x, int y);
    void OnLeftMouseDragged(int x, int y);

  private:
    VertexAttributes *plane_attributes;
    ShaderProgram *shader_program;
    VertexArray *plane;
    float point_size;
    int mouse_x;
    int mouse_y;
    float shift_x;
    float shift_y;
};

void SecondRenderer::OnLeftMouseDown(int x, int y) {
  mouse_x = x;
  mouse_y = y; 
}

void SecondRenderer::OnLeftMouseDragged(int x, int y) {
  shift_x = (x - mouse_x); 
}

void SecondRenderer::OnDraw(float delta) {
  /* glClear(GL_COLOR_BUFFER_BIT); */
  BaseRenderer::OnDraw(delta);

  shader_program->Bind();  
  // TODO use shift_x and shift_y
  shader_program->SetUniform("shift", rand() % 100 / 100.0f, rand() % 100 / 100.0f);
  plane->Bind();
  plane->DrawSequence(GL_POINTS);
  plane->Unbind();
  shader_program->Unbind();
}

void SecondRenderer::OnInitialize() {
  SetBackgroundColor(175.0f / 255, 238.0f / 255, 238.0f / 255, 1.0f);   

  point_size = 1.0f;

  int width = 32;
  int height = 22;

  float *positions = new float[width * height * 2];
  float *position = positions;
  for (int r = 0; r < height; ++r) {
    for (int c = 0; c < width; ++c) {
      position[0] = -1 + c * 0.1;
      position[1] = -1 + r * 0.1;
      position += 2;
    }
  }

  // Ship positions off to GPU.
  plane_attributes = new VertexAttributes();
  plane_attributes->AddAttribute("position", width * height, 2, positions);

  // CPU positions are free to go now.
  delete[] positions;

  shader_program = ShaderProgram::FromFiles(SHADERS_DIR "/passthru.v.glsl", SHADERS_DIR "/white.f.glsl");

  plane = new VertexArray(*shader_program, *plane_attributes);
}

void SecondRenderer::OnKey(Keys::key_t key) {
  switch (key) {
    case '+':
      point_size += 1.0f;
      glPointSize(point_size);
      break;
    case '-':
      point_size -= 1.0f;
      glPointSize(point_size);
      break;
    default:
      BaseRenderer::OnKey(key);
      break;  
  }
}

int main(int argc, char **argv) {
  SecondRenderer *renderer = new SecondRenderer();
  glut_render(renderer); 
  return 0;
}

passthru.v.glsl

#version 120

attribute vec2 position;

uniform vec2 shift;

void main() {
  gl_Position = vec4(position + shift, 0.0, 1.0);
}

white.f.glsl

#version 120

void main() {
  gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0);
}

CMakeLists.txt

add_definitions(-DSHADERS_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
add_executable(second second.cpp ${COMMON})
target_link_libraries(second ${GLUT_LIBRARIES} ${GLEW_LIBRARY} ${OPENGL_LIBRARIES})

if(WIN32)
  add_custom_command(
      TARGET second POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different
      "${GLUT_DLL}"
      $<TARGET_FILE_DIR:second>)
    
  add_custom_command(
      TARGET second POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different
      "${GLEW_DLL}"
      $<TARGET_FILE_DIR:second>)
endif(WIN32)

Haiku

on my first graphics program
The sunY came up
Pixels, once black, shone grayscale
I gasped, “Hello, world!”