Thread: [OpenGL] Add perspective projection
hi,
can please add perspective projection code below?
learned how multiply matrices, vectors, dot/cross products are, etc except how setup perspective projection matrix , use - can't it.
edit: i'll try out this , see if works.php code:
#include <gl/glew.h>
#include <gl/freeglut.h>
#include <stdio.h>
gluint app, buf;
glint locpos, loccolor;
const glfloat data[] = {
-0.5f, -0.5f,
1.0f, 0.0f, 0.0f,
0.5f, -0.5f,
0.0f, 1.0f, 0.0f,
0.0f, 0.5f,
0.0f, 0.0f, 1.0f
};
gluint createshadertype(const glenum type, const glchar *src, const char *progname) {
gluint shader = glcreateshader(type);
if(shader == 0) {
return 0;
}
glshadersource(shader, 1, &src, null);
glcompileshader(shader);
glint status;
glgetshaderiv(shader, gl_compile_status, &status);
if(status != gl_false) {
return shader;
}
glint len;
glgetshaderiv(shader, gl_info_log_length, &len);
if(len > 0) {
char *log = new char[len];
glgetshaderinfolog(shader, len, null, log);
const char *sztype;
if(type == gl_vertex_shader) {
sztype = "vertex";
} else if(type == gl_fragment_shader) {
sztype = "fragment";
} else {
sztype = "unknown";
}
printf("error compiling %s shader of %s: %s\n", sztype, progname, log);
delete[] log;
}
gldeleteshader(shader);
return 0;
}
gluint createshader(const glchar *vertsrc, const glchar *fragsrc, const char *progname) {
gluint vertshader = createshadertype(gl_vertex_shader, vertsrc, progname);
if(vertshader == 0) {
return 0;
}
gluint fragshader = createshadertype(gl_fragment_shader, fragsrc, progname);
if(fragshader == 0) {
gldeleteshader(vertshader);
return 0;
}
gluint prog = glcreateprogram();
glattachshader(prog, vertshader);
glattachshader(prog, fragshader);
gllinkprogram(prog);
gldeleteshader(vertshader);
gldeleteshader(fragshader);
glint status;
glgetprogramiv(prog, gl_link_status, &status);
if(status != gl_false) {
return prog;
}
glint len;
glgetprogramiv(prog, gl_info_log_length, &len);
if(len > 0) {
char *log = new char[len];
glgetprograminfolog(prog, len, null, log);
printf("error linking %s program: %s\n", progname, log);
delete[] log;
}
gldeleteprogram(prog);
return 0;
}
bool
appsetup() {
glclearcolor(0.0f, 0.0f, 0.0f, 1.0f);
glgenbuffers(1, &buf);
glbindbuffer(gl_array_buffer, buf);
glbufferdata(gl_array_buffer, sizeof(data), data, gl_static_draw);
glbindbuffer(gl_array_buffer, 0);
const glchar *vertsrc =
"#version 330\n"
"in vec2 vpos;\n"
"in vec3 vcolor;\n"
"out vec3 vpasscolor;\n"
"void main() {\n"
" vpasscolor = vcolor;\n"
" gl_position = vec4(vpos, 0.0f, 1.0f);\n"
"}\n";
const glchar *fragsrc =
"#version 330\n"
"in vec3 vpasscolor;\n"
"out vec4 voutcolor;\n"
"void main() {\n"
" voutcolor = vec4(vpasscolor, 1.0f);\n"
"}\n";
app = createshader(vertsrc, fragsrc, "main shader");
if(app == 0) {
return false;
}
locpos = glgetattriblocation(app, "vpos");
loccolor = glgetattriblocation(app, "vcolor");
return true;
}
void
winclose() {
if(app != 0) {
gldeleteprogram(app);
}
}
void
windraw() {
glclear(gl_color_buffer_bit);
gluseprogram(app);
glbindbuffer(gl_array_buffer, buf);
glenablevertexattribarray(locpos);
glenablevertexattribarray(loccolor);
glvertexattribpointer(locpos, 2, gl_float, gl_false, sizeof(glfloat)*5, 0);
glvertexattribpointer(loccolor, 3, gl_float, gl_false, sizeof(glfloat)*5, (void*) (sizeof(glfloat)*2));
gldrawarrays(gl_triangles, 0, 3);
gldisablevertexattribarray(locpos);
gldisablevertexattribarray(loccolor);
glbindbuffer(gl_array_buffer, 0);
glutswapbuffers();
}
void
winresize(int w, int h) {
glviewport(0, 0, w, h);
}
int main(int argc, char *argv[]) {
glutinit(&argc, argv);
glutinitdisplaymode(glut_rgba | glut_double | glut_depth);
glutinitwindowsize(800, 500);
glutcreatewindow("gl 3.3");
glutdisplayfunc(windraw);
glutreshapefunc(winresize);
glutclosefunc(winclose);
glenum status = glewinit();
if(status != glew_ok) {
printf("glew init failed %s\n", glewgeterrorstring(status));
return 0;
}
if(!appsetup()) {
return 0;
}
glutmainloop();
return 0;
}
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] [OpenGL] Add perspective projection
Ubuntu
Comments
Post a Comment