Permalink
Browse files

Add triangle render code from http://en.wikibooks.org/wiki/OpenGL_Pro…

  • Loading branch information...
dff180
dff180 committed Jun 22, 2012
1 parent b24e740 commit 2b88be6ab6e8528412cba94dc4904e6804b50507
Showing with 233 additions and 4 deletions.
  1. +2 −2 Makefile
  2. +75 −1 MyGLWindow.cpp
  3. +5 −1 MyGLWindow.h
  4. +4 −0 main.cpp
  5. +120 −0 shader_utils.cpp
  6. +16 −0 shader_utils.h
  7. +6 −0 triangle.f.glsl
  8. +5 −0 triangle.v.glsl
@@ -1,9 +1,9 @@
CC=g++
CFLAGS=-c -g -Wall -O3 -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads
LDFLAGS=-L/opt/vc/lib -lGLESv2 -lEGL -lopenmaxil -lbcm_host
SOURCES=EGLConfig.cpp EGLWindow.cpp main.cpp MyGLWindow.cpp
SOURCES=EGLConfig.cpp EGLWindow.cpp main.cpp MyGLWindow.cpp shader_utils.cpp
OBJECTS=$(SOURCES:%.cpp=%.o)
EXECUTABLE=EGLWindow
EXECUTABLE=pishadertoy
all: $(SOURCES) $(EXECUTABLE)
@@ -16,6 +16,9 @@
*/
#include "MyGLWindow.h"
#include <iostream>
#include <stdio.h>
#include "shader_utils.h"
MyGLWindow::MyGLWindow(EGLconfig *_config) : EGLWindow(_config)
{
@@ -27,12 +30,59 @@ MyGLWindow::MyGLWindow(EGLconfig *_config) : EGLWindow(_config)
}
MyGLWindow::~MyGLWindow()
{
//TODO is this ever called
}
int MyGLWindow::init_resources()
{
GLfloat triangle_vertices[] = {
0.0, 0.8,
-0.8, -0.8,
0.8, -0.8,
};
glGenBuffers(1, &vbo_triangle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
GLint link_ok = GL_FALSE;
GLuint vs, fs;
if ((vs = create_shader("triangle.v.glsl", GL_VERTEX_SHADER)) == 0) return 0;
if ((fs = create_shader("triangle.f.glsl", GL_FRAGMENT_SHADER)) == 0) return 0;
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
fprintf(stderr, "glLinkProgram:");
print_log(program);
return 0;
}
const char* attribute_name = "coord2d";
attribute_coord2d = glGetAttribLocation(program, attribute_name);
if (attribute_coord2d == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return 0;
}
return 1;
}
void MyGLWindow::initializeGL()
{
init_resources();
// Enable alpha blend
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// set the clear colour
glClearColor(1,1,1,1);
// glClearColor(1,1,1,1);
}
@@ -48,6 +98,30 @@ void MyGLWindow::paintGL()
glClearColor(r,g,b,1);
// clear screen
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// jt
glUseProgram(program);
glEnableVertexAttribArray(attribute_coord2d);
/* Describe our vertices array to OpenGL (it can't guess its format automatically) */
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
glVertexAttribPointer(
attribute_coord2d, // attribute
2, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
/* Push each element in buffer_vertices to the vertex shader */
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(attribute_coord2d);
// jt end
// to update we need to swap the buffers
swapBuffers();
}
@@ -33,9 +33,13 @@ class MyGLWindow : public EGLWindow
protected :
/// @brief one time OpenGL initialisation
virtual void initializeGL();
int init_resources();
GLuint vbo_triangle;
GLuint program;
GLint attribute_coord2d;
};
#endif
#endif
@@ -33,6 +33,8 @@ int main()
config->setDepth(16);
// now create a new window using the default config
MyGLWindow win(config);
/*
// now set the size of the screen in this case I'm going to do a
// rectangle in the middle (if you don't call this you would get a full
// screen rect by default)
@@ -42,6 +44,8 @@ int main()
win.setUpscale(false);
win.setScreen(w/4,h/4,w/2,h/2);
int x,y;
*/
while(1)
{
win.paintGL();
@@ -0,0 +1,120 @@
/**
* From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/OpenGL_Programming
* This file is in the public domain.
* Contributors: Sylvain Beucler
*/
#include <stdio.h>
#include <stdlib.h>
#include "shader_utils.h"
/**
* Store all the file's contents in memory, useful to pass shaders
* source code to OpenGL
*/
char* file_read(const char* filename)
{
FILE* in = fopen(filename, "rb");
if (in == NULL) return NULL;
int res_size = BUFSIZ;
char* res = (char*)malloc(res_size);
int nb_read_total = 0;
while (!feof(in) && !ferror(in)) {
if (nb_read_total + BUFSIZ > res_size) {
if (res_size > 10*1024*1024) break;
res_size = res_size * 2;
res = (char*)realloc(res, res_size);
}
char* p_res = res + nb_read_total;
nb_read_total += fread(p_res, 1, BUFSIZ, in);
}
fclose(in);
res = (char*)realloc(res, nb_read_total + 1);
res[nb_read_total] = '\0';
return res;
}
/**
* Display compilation errors from the OpenGL shader compiler
*/
void print_log(GLuint object)
{
GLint log_length = 0;
if (glIsShader(object))
glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
else if (glIsProgram(object))
glGetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length);
else {
fprintf(stderr, "printlog: Not a shader or a program\n");
return;
}
char* log = (char*)malloc(log_length);
if (glIsShader(object))
glGetShaderInfoLog(object, log_length, NULL, log);
else if (glIsProgram(object))
glGetProgramInfoLog(object, log_length, NULL, log);
fprintf(stderr, "%s", log);
free(log);
}
/**
* Compile the shader from file 'filename', with error handling
*/
GLuint create_shader(const char* filename, GLenum type)
{
const GLchar* source = file_read(filename);
if (source == NULL) {
fprintf(stderr, "Error opening %s: ", filename); perror("");
return 0;
}
GLuint res = glCreateShader(type);
const GLchar* sources[] = {
// Define GLSL version
#ifdef GL_ES_VERSION_2_0
"#version 100\n"
#else
"#version 120\n"
#endif
,
// GLES2 precision specifiers
#ifdef GL_ES_VERSION_2_0
// Define default float precision for fragment shaders:
(type == GL_FRAGMENT_SHADER) ?
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
"precision highp float; \n"
"#else \n"
"precision mediump float; \n"
"#endif \n"
: ""
// Note: OpenGL ES automatically defines this:
// #define GL_ES
#else
// Ignore GLES 2 precision specifiers:
"#define lowp \n"
"#define mediump\n"
"#define highp \n"
#endif
,
source };
glShaderSource(res, 3, sources, NULL);
free((void*)source);
glCompileShader(res);
GLint compile_ok = GL_FALSE;
glGetShaderiv(res, GL_COMPILE_STATUS, &compile_ok);
if (compile_ok == GL_FALSE) {
fprintf(stderr, "%s:", filename);
print_log(res);
glDeleteShader(res);
return 0;
}
return res;
}
@@ -0,0 +1,16 @@
/**
* From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/OpenGL_Programming
* This file is in the public domain.
* Contributors: Sylvain Beucler
*/
#ifndef _CREATE_SHADER_H
#define _CREATE_SHADER_H
//#include <GL/glew.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
char* file_read(const char* filename);
void print_log(GLuint object);
GLuint create_shader(const char* filename, GLenum type);
#endif
@@ -0,0 +1,6 @@
void main(void) {
gl_FragColor[0] = gl_FragCoord.x / 640.0;
gl_FragColor[1] = gl_FragCoord.y / 480.0;
gl_FragColor[2] = 0.5;
gl_FragColor[3] = 1.0;
}
@@ -0,0 +1,5 @@
attribute vec2 coord2d;
void main(void) {
gl_Position = vec4(coord2d, 0.0, 1.0);
}

0 comments on commit 2b88be6

Please sign in to comment.