PDA

View Full Version : Textures on and instances of a sphere



mountainoflight
11-17-2008, 05:45 AM
Hi,

I'm feeling very bewildered - I'm using OpenGL's quadrics function to generate a sphere, and I want to texture (using procedural texture mapping) and light it using shaders. I already have the code for both the shaders (.vs and .fs files) and the sphere (obviously), but I have no idea how to actually apply the former to the object (i.e. the actual code). Can anyone help?

Also, my program will have up to around 200 spheres, and each needs to have its texture orientation and location defined by an external ascii file. What will be the most efficient way to do this, and is there a simple way to define the orientation of either the mapping or sphere after mapping of the texture??

Many thanks for your help.

Nighthawk
11-17-2008, 08:29 AM
For rendering with GLSL shaders active:
http://www.lighthouse3d.com/opengl/glsl/index.php?oglexample1
Call setShaders() before rendering your objects.

Moving the texture on the object can be accomplished by using the texture matrix:
glMatrixMode(GL_TEXTURE);
glRotate(...) etc

The latter will only work when your vertex shader is actually using the texture matrix. With no shaders active, the texture matrix should work as well.

mountainoflight
11-18-2008, 06:06 AM
Ok,I'm still having problems applying shaders. Here's my code so far (below). Could someone point out how to actually get my shaders to be applied to the sphere? At the moment I just get a white lit one, with none of my shaders applied

Thanks to the previous poster for their advice... it was a great start!


#include "../../shared/gltools.h" // OpenGL toolkit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
GLuint v, f, p;

char *textFileRead(char *fn);

////////////////////////////////////////////////////////////////////////////
// Change viewing volume and viewport. Called when window is resized
void ChangeSize(int w, int h)
{
GLfloat fAspect;

// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

fAspect = (GLfloat)w/(GLfloat)h;

// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Produce the perspective projection
gluPerspective(35.0f, fAspect, 1.0, 40.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

// Set up

void SetupRC()
{
// Light values and coordinates
GLfloat whiteLight[] = { 0.05f, 0.05f, 0.05f, 1.0f };
GLfloat sourceLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };

glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE);

// Enable lighting
glEnable(GL_LIGHTING);

// Setup and enable light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
glLightfv(GL_LIGHT0,GL_AMBIENT,sourceLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);

// Enable color tracking
glEnable(GL_COLOR_MATERIAL);

// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
}

// Respond to arrow keys
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;

if(key == GLUT_KEY_DOWN)
xRot += 5.0f;

if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;

if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;

xRot = (GLfloat)((const int)xRot % 360);
yRot = (GLfloat)((const int)yRot % 360);

// Refresh the Window
glutPostRedisplay();
}


// Called to draw scene
void RenderScene(void)
{
GLUquadricObj *pObj; // Quadric object

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Save the matrix state and do the rotations
glPushMatrix();
// Move object back and do in place rotation
glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);

// Draw something
pObj = gluNewQuadric();
gltDrawUnitAxes();
gluSphere(pObj, 0.5f, 32, 32);

// Restore the matrix state
glPopMatrix();

// Buffer swap
glutSwapBuffers();
}

void setShaders() {

char *vs = NULL,*fs = NULL;

v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);


vs = textFileRead("beachball.vs");
fs = textFileRead("beachball.fs");

const char * ff = fs;
const char * vv = vs;

glShaderSource(v, 1, &amp;vv,NULL);
glShaderSource(f, 1, &amp;ff,NULL);

free(vs);free(fs);

glCompileShader(v);
glCompileShader(f);

p = glCreateProgram();
glAttachShader(p,f);
glAttachShader(p,v);

glLinkProgram(p);
glUseProgram(p);
}

char *textFileRead(char *fn) {


FILE *fp;
char *content = NULL;

int count=0;

if (fn != NULL) {
fp = fopen(fn,"rt");

if (fp != NULL) {

fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);

if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}

int textFileWrite(char *fn, char *s) {

FILE *fp;
int status = 0;

if (fn != NULL) {
fp = fopen(fn,"w");

if (fp != NULL) {

if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s))
status = 1;
fclose(fp);
}
}
return(status);
}

int main(int argc, char *argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Beachball");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
setShaders();
glutDisplayFunc(RenderScene);
SetupRC();

glutMainLoop();

return 0;
}

_NK47
11-18-2008, 06:47 AM
because it's more important to understand then just to copy n paste, the concept: glUseProgram(p) will replace your fixed function pipeline with a vertex shader or a fragment shader or both depending on how you create a shader. in your case it will replace both and vertex/fragment shaders (glAttachShader(p,f); glAttachShader(p,v);) get executed after you call glUseProgram(p). that is, anything you draw from that point will go over your vertex/fragment shader to the final screen image. if your other sphere is not lit im assuming the that you disabled shaders and put it back to use fixed functions which is done with glUseProgram(0) or shaders are somehow wrong. hope that helps.