CS 455 Lecture 3 – An Interactive Renderer
Agenda
- what ?s
- goal: an interactive renderer
- vertex attributes: per vertex state
- position
- color
- texture coordinates
- ambient occlusion
- normals
- you name it
- shader programs
- drawable objects = vertex attributes + shader program
- uniforms: per draw state
- handling key events
- handling mouse events
TODO
Before next class:
- On your own: read some tutorials or watch some videos on 3-D modeling. (Neal Hirsig offers a complete course.) Build a model of your choosing in Blender or some other modeling program. Post an image of it on this blog with exactly these three categories:
cs455
,spring 2015
,postmortems
. This is worth a Blugold. Log in to the blog using the Login link in the menu at the top of the page. You should have received an email with your password last week. Check your junk mail if you can’t find it, or go through the Forgot my Password dance.
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!”
The sunY came up
Pixels, once black, shone grayscale
I gasped, “Hello, world!”