PDA

View Full Version : link to working c++ code with shader texturing?



crankygoo
01-03-2010, 10:41 AM
can anybody provide working c++ sample with shader texturing?
there're many drunk tutorials without full app working code just with bits of shader and application code.

just a simple "hello, cube" app, PLEASE :(
when i compile & step over, i understand a lot more than reading those damn books&tutors.

i'm into 3.1 so that's why i need that.

marshats
01-03-2010, 03:25 PM
Will openGL 3.2 code help? I have a basecode 3.2 example for drawing a textured triangle (not a cube like you asked though). Note the example uses SDL 1.3 unstable (http://www.libsdl.org/svn.php) for getting 3.2 context and DevIL image loading library (http://openil.sourceforge.net/about.php) for reading textures from disk.

I will post the code here but understand that it is openGL 3.2 (NOT 3.1) code requiring the above libraries! If you really wanted 3.1 code then ignore the following code snippet! You will also have to supply your own image file named "Frac.png" -- I don't have a way to give you my image file but any RGB encoded png file should work fine for testing the code. Also if you are on windows you may have to add manually glee or glew to get the extensions required for GL3.2 functions (on linux using a #define GL_GLEXT_PROTOTYPES is enough to use extensions easily and linux was used to test the following code)

Also, to make this a cube example you will have to change only lines 141 and 142 where vertices[] and texcoords[] are defined and the total number of commensurate vertices (3rd Argument) used by glDrawArrays on line 172.



//image.vert
#version 150 core

in vec2 vert;
in vec2 texcoord;
out vec2 coord;

void main() {
coord=texcoord;
gl_Position=vec4(vert,0.,1.);
}



//image.frag
#version 150 core

uniform sampler2D tex;
in vec2 coord;
out vec4 color;

void main() {
color=texture(tex,coord);
}



#include <iostream>
#include <fstream>
#include <cassert>

#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <SDL.h> // actually unstable SDL1.3 version required !!
#include <IL/ilut.h> //http://openil.sourceforge.net/tuts/tut_step/index.htm

struct TextureHandle {
ILubyte *p; /* pointer to image data loaded into memory */
ILuint id; /* unique DevIL id of image */
ILint w; /* image width */
ILint h; /* image height */
ILenum DestFormat; /* see ilConvertImage */
ILenum DestType; /* see ilConvertImage */
};
TextureHandle frac;

//---+----3----+----2----+----1----+---><---+----1----+----2----+----3----+----4
//---------------------------- LoadImageDevIL ------------------------------

// use devIL, cross platform image loading library(openil.sourceforge.net/about.php)

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
//When IL_ORIGIN_SET enabled, the origin is specified at an absolute
//position, and all images loaded or saved adhere to this set origin.
ilEnable(IL_ORIGIN_SET);
//sets the origin to be IL_ORIGIN_LOWER_LEFT when loading all images, so
//that any image with a different origin will be flipped to have the set
//origin.
ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

//Now load the image file
ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0; // failure
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); // set destination image properties

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);
T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1; // success
}


//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
//--------------------------- cleanup_images -------------------------------

// Clear out the memory used by loading image files.

void cleanup_images(void) {
printf("cleanup image memory:{");

if (frac.id) {
printf(" %d",frac.id);
ilDeleteImages(1, &amp;frac.id);
}

printf(" }\n");
}


int main() {
//SDL Initialization
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,3 );
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,2 );
SDL_Init(SDL_INIT_VIDEO);

const int r_width(640),r_height(480);
SDL_WindowID window(SDL_CreateWindow("Foo",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,r_w idth,r_height,SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN)) ;
SDL_GLContext glcontext(SDL_GL_CreateContext(window));

std::cout<<"OpenGL "<<glGetString(GL_VERSION)<<" (GLSL "<<glGetString(GL_SHADING_LANGUAGE_VERSION)<<')'<<std::endl;

// Read in textures from disk (bind to openGL next though ...)
ilInit();
assert( LoadImageDevIL ("Frac.png", &amp;frac) );
atexit(cleanup_images);

//Bind texture to opengl
GLuint textureid;

glGenTextures(1,&amp;textureid);
glBindTexture(GL_TEXTURE_2D,textureid);

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexImage2D (GL_TEXTURE_2D, 0, frac.DestFormat, frac.w, frac.h, 0, frac.DestFormat, frac.DestType, frac.p);
glGenerateMipmap(GL_TEXTURE_2D); //Generate mipmaps now!!!

//Create shaders and shader program
GLuint vshader(glCreateShader(GL_VERTEX_SHADER));
GLuint fshader(glCreateShader(GL_FRAGMENT_SHADER));
GLuint program(glCreateProgram());

{
std::ifstream source_file("image.vert");

std::string data;
std::getline(source_file,data,'\0');

const GLchar *vshader_source(data.c_str());

glShaderSource(vshader,1,&amp;vshader_source,NULL);
}

{
std::ifstream source_file("image.frag");

std::string data;
std::getline(source_file,data,'\0');

const GLchar *fshader_source(data.c_str());

glShaderSource(fshader,1,&amp;fshader_source,NULL);
}

glCompileShader(vshader);
glCompileShader(fshader);

glAttachShader(program,vshader);
glAttachShader(program,fshader);
glLinkProgram(program);

//Get handles to shader uniforms
GLint texloc(glGetUniformLocation(program,"tex")); //uniform sampler2D tex;

//Create geometry vertex array
GLuint tris,coords,vao;
const GLfloat vertices[]={0.0f,0.0f, 0.0f,0.5f, 0.5f,0.5f};
const GLfloat texcoords[]={0.0f,0.0f, 0.0f,1.0f, 1.0f,1.0f};

glGenVertexArrays(1,&amp;vao);
glBindVertexArray(vao);

//in vec2 vert;
glGenBuffers(1,&amp;tris);
glBindBuffer(GL_ARRAY_BUFFER,tris);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vert ices,GL_STATIC_DRAW);
const GLint location(glGetAttribLocation(program,"vert"));
glVertexAttribPointer(location,2,GL_FLOAT,GL_TRUE, 0,NULL);
glEnableVertexAttribArray(location);

//in vec2 texcoord;
glGenBuffers(1,&amp;coords);
glBindBuffer(GL_ARRAY_BUFFER,coords);
glBufferData(GL_ARRAY_BUFFER,sizeof(texcoords),tex coords,GL_STATIC_DRAW);
const GLint coordloc(glGetAttribLocation(program,"texcoord"));
glVertexAttribPointer(coordloc,2,GL_FLOAT,GL_TRUE, 0,NULL);
glEnableVertexAttribArray(coordloc);

glUseProgram(program);

//Render Loop
SDL_Event event;
do {
glUniform1i(texloc,0); // 0 for GL_TEXTURE0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,textureid);

glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES,0,3);

SDL_GL_SwapWindow(window);

SDL_PollEvent(&amp;event); //non-blocking
} while (event.type!=SDL_MOUSEBUTTONDOWN); //exit by clicking in window

//Quit
glDeleteShader(vshader);
glDeleteShader(fshader);
glDeleteProgram(program);
glDeleteTextures(1,&amp;textureid);
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
SDL_Quit();
}

crankygoo
01-03-2010, 09:50 PM
thanx, man. will work on it today. 3.2 is fine :)