hi, i would like to apply a texture to a GLUQuadric sphere i found an example int he NEHE pages : http://nehe.gamedev.net/data/lessons/le-…-?lesson=18 and from opengl red book for what concerne GLUQuadric sphere http://glprogramming.com/red/chapter11.html also i have found (because i have it) from the book Programming activity the source code on line : http://examples.oreilly.com/9780596154158/ i have tried this code , no errors in the building process but i receive an ecception in this line when i debug it: text.texData.textureTarget = GL_TEXTURE_2D; i leave the code if someone may explain what is wrong… ch13sphere .cpp:
#include "ch13_sphere.h"
//int FAN_VERTICES = 360;
void testApp::setup(){
//img.setImageType(OF_IMAGE_COLOR);
img.loadImage("cone.jpg");
text.texData.textureTarget = GL_TEXTURE_2D;
text.allocate(512, 512, GL_RGBA, false);
ang = 0;
glEnable(GL_DEPTH_TEST); // enable depth testing, otherwise things will look really werid
glDepthFunc(GL_LEQUAL); // set the type of depth testing
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glEnable ( GL_TEXTURE_2D );
sphere = gluNewQuadric();
glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
gluQuadricDrawStyle( sphere, GLU_FILL);
gluQuadricNormals( sphere, GLU_SMOOTH);
gluQuadricTexture( sphere, GL_TRUE );
/********/
// new
/********/
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
/********/
// end new
/********/
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
text.loadData(img.getPixels(), 512, 512, GL_RGB);
}
void testApp::update(){
ofBackground(122,122,122);
ang+=0.2;
}
void testApp::draw() {
text.loadData(img.getPixels(), 512, 512, GL_RGB);
glTranslatef(350, 250, 0);
glRotated(ang, 1.0, 0.0, 0.0);
// enable texturing
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, text.getTextureData().textureID);
//glutWireSphere(200, 30, 30);
gluSphere(sphere, 200, 30, 30);
//ofRect(200, 200, 200, 200);
glEnable(text.texData.textureTarget);
/*
GLfloat afColors[] = { 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f };
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glColorPointer(4,GL_FLOAT,0,afColors);
//glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
FAN_VERTICES = 360;
GLfloat afVertices[FAN_VERTICES*3];
GLfloat angle = 0.0f;
afVertices[0] = 0;
afVertices[1] = 0;
afVertices[2] = 0.0f;
for(int i = 3; i<(FAN_VERTICES*3); i++){
afVertices[i++] = (200*cos(angle) + 0)*1;
afVertices[i++] = 200*sin(angle) + 0;
//afVertices[i++] = 200*sin(angle) + 0;
afVertices[i] = 0.0f;
angle+=(PI/180);
}
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,afVertices);
//pass the texture coordinates
GLfloat pfTexCoord[2*FAN_VERTICES];
angle = 0.0f;
pfTexCoord[0] = 0.5f;
pfTexCoord[1] = 0.5f;
for(int i=2; i<(2*FAN_VERTICES); i++){
pfTexCoord[i++] = 0.5f * cos(angle) + 0.5f;
pfTexCoord[i] = 0.5f * sin(angle) + 0.5f;
angle += (PI/180.0f);
}
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,GL_FLOAT,0,pfTexCoord);
glDrawArrays(GL_TRIANGLE_FAN, 0, FAN_VERTICES);
glPopMatrix();
*/
glDisable(text.texData.textureTarget);
}
void testApp::keyPressed (int key){
}
void testApp::keyReleased (int key){
}
void testApp::mouseMoved(int x, int y ){
}
void testApp::mouseDragged(int x, int y, int button){
}
void testApp::mousePressed(int x, int y, int button){
}
void testApp::mouseReleased(){
}
and the ch13 sphere.h:
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "CustomImage.h"
class testApp : public ofSimpleApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased();
ofTexture text;
CustomImage img;
GLUquadric * sphere;
int FAN_VERTICES;
float ang;
};
#endif
there also other two file CustoImage.cpp
/*
* CustomImage.cpp
* openFrameworks
*
* Created by base on 2/9/09.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/
#include "CustomImage.h"
ofTexture CustomImage::getTex() {
return tex;
}
and CustomImage.h
/*
* CustomImage.h
* openFrameworks
*
* Created by base on 2/9/09.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/
#include "ofImage.h"
class CustomImage : public ofImage {
public:
ofTexture getTex();
};