Commit c01c4149 authored by bbguimaraes's avatar bbguimaraes
Browse files

open.gl: feedback

parent fe23c72e
CPPFLAGS = -std=c++11 -I..
LDFLAGS = -lSOIL -lglfw -lGLEW -lGLU -lGL
all: feedback geometry variable ex1
feedback: feedback.cpp ../common.o
geometry: geometry.cpp ../common.o
variable: variable.cpp ../common.o
ex1: ex1.cpp ../common.o
.PHONY: clean
clean:
rm -f feedback geometry variable ex1
#include "common.h"
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define GLSL(src) "#version 150 core\n" # src
const unsigned int WINDOW_WIDTH = 800;
const unsigned int WINDOW_HEIGHT = 600;
const GLchar * VERTEX_SHADER_SRC = GLSL(
in vec2 pos;
void main() {
gl_Position = vec4(pos.x, pos.y, 0.0, 1.0);
}
);
const char * FRAG_SHADER_SRC = GLSL(
out vec4 outColor;
void main() {
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
);
const GLchar * FEEDBACK_SHADER_SRC = GLSL(
in vec2 pos;
out vec2 outValue;
uniform vec2 g;
const float step = 0.01;
void main() {
outValue = pos + step * normalize(g - pos);
}
);
int main(int argc, char ** argv) {
GLfloat POINTS_INITIAL[] = {
// pos
-0.5f, 0.5f,
0.5f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f,
};
unsigned int POINTS_COUNT = sizeof(POINTS_INITIAL) / sizeof(GLfloat);
GLfloat points[POINTS_COUNT];
std::memcpy(points, POINTS_INITIAL, POINTS_COUNT);
GLFWwindow * window = init();
GLuint feedback_shader = create_shader_or_exit(
FEEDBACK_SHADER_SRC, GL_VERTEX_SHADER);
GLuint feedback_program = glCreateProgram();
glAttachShader(feedback_program, feedback_shader);
const GLchar * feedback_varyings[] = {"outValue"};
glTransformFeedbackVaryings(
feedback_program, 1, feedback_varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(feedback_program);
glUseProgram(feedback_program);
GLuint feedback_vao;
glGenVertexArrays(1, &feedback_vao);
glBindVertexArray(feedback_vao);
GLint pos_attr = glGetAttribLocation(feedback_program, "pos");
glEnableVertexAttribArray(pos_attr);
glVertexAttribPointer(
pos_attr, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
GLuint g_uni = glGetUniformLocation(feedback_program, "g");
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// TODO GL_STATIC_READ?
glBufferData(
GL_ARRAY_BUFFER,
sizeof(POINTS_INITIAL), POINTS_INITIAL, GL_STATIC_READ);
GLuint vertex_shader = create_shader(VERTEX_SHADER_SRC, GL_VERTEX_SHADER);
GLuint frag_shader = create_shader(FRAG_SHADER_SRC, GL_FRAGMENT_SHADER);
GLuint shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, frag_shader);
glLinkProgram(shader_program);
glUseProgram(shader_program);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
pos_attr = glGetAttribLocation(shader_program, "pos");
glEnableVertexAttribArray(pos_attr);
glVertexAttribPointer(
pos_attr, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
glEnable(GL_DEPTH_TEST);
glPointSize(5.0f);
while(!glfwWindowShouldClose(window)) {
exit_on_gl_error();
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glUseProgram(shader_program);
glBindVertexArray(vao);
glDisable(GL_RASTERIZER_DISCARD);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, 4);
glUseProgram(feedback_program);
glBindVertexArray(feedback_vao);
double x, y;
glfwGetCursorPos(window, &x, &y);
if((0 <= x && x <= 800) && (0 <= y && y <= 600)) {
x = (x - WINDOW_WIDTH / 2.0) / (WINDOW_WIDTH / 2.0);
y = (y - WINDOW_HEIGHT / 2.0) / (WINDOW_HEIGHT / 2.0);
y = -y;
glUniform2f(g_uni, x, y);
}
glEnable(GL_RASTERIZER_DISCARD);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vbo);
glBeginTransformFeedback(GL_POINTS);
glDrawArrays(GL_POINTS, 0, 5);
glEndTransformFeedback();
glFlush();
}
}
#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>
#include <GL/gl.h>
#include "common.h"
#define GLSL(src) "#version 150 core\n" # src
const GLchar * VERTEX_SHADER_SRC = GLSL(
in float inValue;
out float outValue;
void main() {
outValue = sqrt(inValue);
}
);
int main(int argc, char ** argv) {
glfwInit();
glfwWindowHint(GLFW_VISIBLE, false);
GLFWwindow * window = create_window(800, 600, "OpenGL");
glfwMakeContextCurrent(window);
init_glew_or_exit();
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shader, 1, &VERTEX_SHADER_SRC, nullptr);
glCompileShader(shader);
GLuint program = glCreateProgram();
glAttachShader(program, shader);
const GLchar * feedback_varyings[] = {"outValue"};
glTransformFeedbackVaryings(
program, 1, feedback_varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
glUseProgram(program);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLfloat data[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
GLint input_attr = glGetAttribLocation(program, "inValue");
glEnableVertexAttribArray(input_attr);
glVertexAttribPointer(input_attr, 1, GL_FLOAT, GL_FALSE, 0, 0);
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), nullptr, GL_STATIC_READ);
glEnable(GL_RASTERIZER_DISCARD);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo);
glBeginTransformFeedback(GL_POINTS);
glDrawArrays(GL_POINTS, 0, 5);
glEndTransformFeedback();
glFlush();
GLfloat feedback[5];
glGetBufferSubData(
GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(feedback), feedback);
exit_on_gl_error();
std::printf(
"%f %f %f %f %f\n",
feedback[0], feedback[1], feedback[2], feedback[3], feedback[4]);
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>
#include <GL/gl.h>
#include "common.h"
#define GLSL(src) "#version 150 core\n" # src
const GLchar * VERTEX_SHADER_SRC = GLSL(
in float inValue;
out float geoValue;
void main() {
geoValue = sqrt(inValue);
}
);
const GLchar * GEOMETRY_SHADER_SRC = GLSL(
layout(points) in;
layout(triangle_strip, max_vertices = 3) out;
in float[] geoValue;
out float outValue;
void main() {
for(int i = 0; i < 3; ++i) {
outValue = geoValue[0] + i;
EmitVertex();
}
EndPrimitive();
}
);
int main(int argc, char ** argv) {
glfwInit();
glfwWindowHint(GLFW_VISIBLE, false);
GLFWwindow * window = create_window(800, 600, "OpenGL");
glfwMakeContextCurrent(window);
init_glew_or_exit();
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &VERTEX_SHADER_SRC, nullptr);
glCompileShader(vertex_shader);
GLuint geo_shader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geo_shader, 1, &GEOMETRY_SHADER_SRC, nullptr);
glCompileShader(geo_shader);
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, geo_shader);
const GLchar * feedback_varyings[] = {"outValue"};
glTransformFeedbackVaryings(
program, 1, feedback_varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
glUseProgram(program);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLfloat data[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
GLint input_attr = glGetAttribLocation(program, "inValue");
glEnableVertexAttribArray(input_attr);
glVertexAttribPointer(input_attr, 1, GL_FLOAT, GL_FALSE, 0, 0);
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(data), nullptr, GL_STATIC_READ);
glEnable(GL_RASTERIZER_DISCARD);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo);
glBeginTransformFeedback(GL_TRIANGLES);
glDrawArrays(GL_POINTS, 0, 5);
glEndTransformFeedback();
glFlush();
GLfloat feedback[15];
glGetBufferSubData(
GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(feedback), feedback);
exit_on_gl_error();
for(unsigned int i = 0; i < 15; ++i)
std::printf("%f\n", feedback[i]);
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>
#include <GL/gl.h>
#include "common.h"
#define GLSL(src) "#version 150 core\n" # src
const GLchar * VERTEX_SHADER_SRC = GLSL(
in float inValue;
out float geoValue;
void main() {
geoValue = sqrt(inValue);
}
);
const GLchar * GEOMETRY_SHADER_SRC = GLSL(
layout(points) in;
layout(triangle_strip, max_vertices = 3) out;
in float[] geoValue;
out float outValue;
void main() {
for(int i = 0; i < 3; ++i) {
outValue = geoValue[0] + i;
EmitVertex();
}
EndPrimitive();
}
);
int main(int argc, char ** argv) {
glfwInit();
glfwWindowHint(GLFW_VISIBLE, false);
GLFWwindow * window = create_window(800, 600, "OpenGL");
glfwMakeContextCurrent(window);
init_glew_or_exit();
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &VERTEX_SHADER_SRC, nullptr);
glCompileShader(vertex_shader);
GLuint geo_shader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geo_shader, 1, &GEOMETRY_SHADER_SRC, nullptr);
glCompileShader(geo_shader);
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, geo_shader);
const GLchar * feedback_varyings[] = {"outValue"};
glTransformFeedbackVaryings(
program, 1, feedback_varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
glUseProgram(program);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLfloat data[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
GLint input_attr = glGetAttribLocation(program, "inValue");
glEnableVertexAttribArray(input_attr);
glVertexAttribPointer(input_attr, 1, GL_FLOAT, GL_FALSE, 0, 0);
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(data), nullptr, GL_STATIC_READ);
glEnable(GL_RASTERIZER_DISCARD);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo);
GLuint query;
glGenQueries(1, &query);
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, query);
glBeginTransformFeedback(GL_TRIANGLES);
glDrawArrays(GL_POINTS, 0, 5);
glEndTransformFeedback();
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
glFlush();
GLuint primitives;
glGetQueryObjectuiv(query, GL_QUERY_RESULT, &primitives);
GLfloat feedback[15];
glGetBufferSubData(
GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(feedback), feedback);
exit_on_gl_error();
std::printf("%u primitives written!\n", primitives);
for(unsigned int i = 0; i < 15; ++i)
std::printf("%f\n", feedback[i]);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment