1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include "GL/glew.h"
#include "GL/glfw.h"
using namespace std;
GLuint loadShaders (const char* vertex_file_path, const char* fragment_file_path){
cout << "loadShaders" << endl;
string vs_instructions;
cout << "vertex shader filename: " << vertex_file_path << endl;
cout << "fragment_file_path filename: " << fragment_file_path << endl;
ifstream fm (vertex_file_path);
if (fm.is_open()) {
std::string Line = "";
while (fm.good()) {
getline (fm, Line);
vs_instructions += "\n" + Line;
}
}
else {
cout << "Could not open the file" << endl;
return 0;
}
fm.close ();
string fs_instructions;
fm.open (fragment_file_path);
if (fm.is_open()) {
std::string Line = "";
while (getline(fm, Line))
fs_instructions += "\n" + Line;
}
else {
cout << "Could not open the file" << endl;
return 0;
}
GLint result = GL_FALSE;
int log_length;
GLuint fs_id = glCreateShader(GL_FRAGMENT_SHADER);
GLuint vs_id = glCreateShader(GL_VERTEX_SHADER);
char const * vs_ptr = vs_instructions.c_str();
glShaderSource (vs_id, 1, &vs_ptr, NULL);
glCompileShader(vs_id);
glGetShaderiv (vs_id, GL_COMPILE_STATUS, &result);
glGetShaderiv (vs_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0){
vector<char> vs_er_ms (log_length+1);
glGetShaderInfoLog(vs_id, log_length, NULL, &vs_er_ms[0]);
cout << vs_er_ms[0] << endl;;
}
char const * fs_ptr = fs_instructions.c_str();
glShaderSource (fs_id, 1, &fs_ptr, NULL);
glCompileShader(fs_id);
glGetShaderiv(fs_id, GL_COMPILE_STATUS, &result);
glGetShaderiv(fs_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0){
std::vector<char> fs_er_ms (log_length+1);
glGetShaderInfoLog(fs_id, log_length, NULL, &fs_er_ms[0]);
cout << &fs_er_ms[0] << endl;
}
GLuint program_id = glCreateProgram();
glAttachShader (program_id, vs_id);
glAttachShader (program_id, fs_id);
glLinkProgram (program_id);
glGetProgramiv (program_id, GL_LINK_STATUS, &result);
glGetProgramiv (program_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0){
std::vector<char> pg_er_ms(log_length+1);
glGetProgramInfoLog(program_id, log_length, NULL, &pg_er_ms[0]);
cout << pg_er_ms[0] << endl;
}
glDetachShader(program_id, vs_id);
glDetachShader(program_id, fs_id);
glDeleteShader(vs_id);
glDeleteShader(fs_id);
return program_id;
}
void display (GLuint matrix_id, GLuint programID, GLuint vertex_pos_modelspace_id, GLuint vertex_buffer/*GLfloat * model_view_projection_mat_row1_x*/) {
glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glUseProgram (programID);
glEnableVertexAttribArray (vertex_pos_modelspace_id);
glBindBuffer (GL_ARRAY_BUFFER, vertex_buffer);
glVertexAttribPointer(
vertex_pos_modelspace_id, // The attribute we want to configure
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
glDisableVertexAttribArray(vertex_pos_modelspace_id);
glfwSwapBuffers();
}
int main (int argc, char* argv[]) {
bool running=false;
/*if (!GLEW_VERSION_2_0) {
cout << "glew not available. closing..." << endl;
exit (-1);
}*/
glfwInit ();
glEnable (GL_DEPTH_TEST);
if (!glfwOpenWindow (1024,768,8,8,8,0,8,0,GLFW_WINDOW)) {
glfwTerminate();
exit (1);
}
glewInit ();
glfwSetWindowTitle ("Test window:");
glClearColor (0.0f,0.66f,1.0f,1.0f);
GLuint programID = loadShaders ("/media/34GB/demos/Ogl/SimpleVertexShader.vs","/media/34GB/demos/Ogl/SimpleFragmentShader.fs");
GLuint vertex_pos_modelspace_id = glGetAttribLocation(programID, "vertexPosition_modelspace");
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
//static const GLushort g_element_nuffer_data [] = {0, 1, 2};
GLuint vertex_buffer;
glGenBuffers (1, &vertex_buffer);
glBindBuffer (GL_ARRAY_BUFFER, vertex_buffer);
glBufferData (GL_ARRAY_BUFFER, sizeof (g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
do {
//display (scene,program_id,matrix_id,vertex_pos_modelspace_id,vertex_buffer,&model_view_projection_mat.row1.x);
display (programID,0,vertex_pos_modelspace_id,vertex_buffer);
running = glfwGetWindowParam (GLFW_OPENED);
} while (running);
glDeleteBuffers(1, &vertex_buffer);
glDeleteProgram(programID);
glfwTerminate();
}
| |