1 Replies - 7158 Views - Last Post: 29 October 2011 - 08:09 AM Rate Topic: -----

#1 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Texture mapping and glutSolidSphere

Posted 29 October 2011 - 06:05 AM

Hey all

I'm trying to map a texture (Attached with this post, although I converted it from bmp to png to reduce the size... Yes my connection is THAT crappy) on a sphere generated by glutSolidSphere, but for some reason I it's not looking right. You can see the result below.

I'm pretty sure the error is in my main.cpp and not in imageloader.cpp, but I've included it just in case.
My code is as follows:

main.cpp:

#include <iostream>
#include <stdlib.h>
#include "imageloader.h"

#include <GL/glut.h>

using namespace std;

const float SIZE = 4.0f;

float _angle = 0;
GLuint _textureId;

const GLfloat ambientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
const GLfloat sgenparams[] = { 1, 0, 0, 0 };
const GLfloat tgenparams[] = { 0, 1, 0, 0 };

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_COLOR_MATERIAL);
	glDisable(GL_CULL_FACE);
	glEnable(GL_TEXTURE_2D);


	Image* img = loadBMP("sky.bmp");
	_textureId = loadTextureFromImage(img);
	delete img;

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

void handleResize(int w, int h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}

void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBindTexture(GL_TEXTURE_2D, _textureId);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glTranslatef(0.0f, 0.0f, -20.0f);
	glRotatef(_angle, 0, 1, 0);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
	glTexGenfv( GL_S, GL_OBJECT_PLANE, sgenparams );
	glTexGenfv( GL_T, GL_OBJECT_PLANE, tgenparams );
	glutSolidSphere(5, 50, 50);

	glutSwapBuffers();
}

//Called every 25 milliseconds
void update(int value) {
	_angle += 1.5f;
	if (_angle > 360) {
		_angle -= 360;
	}

	glutPostRedisplay();
	glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400);

	glutCreateWindow("Sphery sky");
	initRendering();

	glutDisplayFunc(drawScene);
	glutReshapeFunc(handleResize);
	glutTimerFunc(25, update, 0);

	glutMainLoop();
	return 0;
}



imageloader.cpp:

#include <iostream>
#include <fstream>
#include "imageloader.h"
#include <gl/glut.h>

using namespace std;

Image::Image(char* ps, int w, int h) : pixels(ps), width(w), height(h) {

}

Image::~Image() {
	
}

namespace {
	//Converts a four-character array to an integer, using little-endian form
	int toInt(const char* bytes) {
		return (int)(((unsigned char)bytes[3] << 24) |
			((unsigned char)bytes[2] << 16) |
			((unsigned char)bytes[1] << 8) |
			(unsigned char)bytes[0]);
	}

	//Reads the next four bytes as an integer, using little-endian form
	int readInt(ifstream &input) {
		char buffer[4];
		input.read(buffer, 4);
		return toInt(buffer);
	}
}

Image* loadBMP(const char* filename) {
	ifstream infile(filename, ifstream::binary);
	infile.seekg(10, std::ios::cur);
	int dataOffset = readInt(infile);

	//Read the header
	int headerSize = readInt(infile);
	int width = readInt(infile);
	int height = readInt(infile);

	//Read the data
	int bytesPerRow = ((width * 3 + 3) / 4) * 4 - (width * 3 % 4);
	int size = bytesPerRow * height;
	char* pixels = new char[size];
	infile.seekg(dataOffset, ios_base::beg);
	infile.read(pixels, size);
	infile.close();
	//Get the data into the right format
	char* pixels2 = new char[width * height * 3];
	for(int y = 0; y < height; y++) {
		for(int x = 0; x < width; x++) {
			for(int c = 0; c < 3; c++) {
				pixels2[3*(width*y + x) + c] = pixels[bytesPerRow*y + 3*x + (2 - c)];
			}
		}
	}
	delete[] pixels;
	return new Image(pixels2, width, height);
}

GLuint loadTextureFromImage(Image* image) {
	GLuint textureId;
	glGenTextures(1, &textureId); //Make room for our texture
	glBindTexture(GL_TEXTURE_2D, textureId);
	//Map the image to the texture
	glTexImage2D(GL_TEXTURE_2D, 
		0,
		GL_RGB,
		image->width, image->height,
		0,
		GL_RGB,
		GL_UNSIGNED_BYTE,
		image->pixels);
	return textureId;
}



Thank you in advance!

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Texture mapping and glutSolidSphere

#2 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Texture mapping and glutSolidSphere

Posted 29 October 2011 - 08:09 AM

Solved it myself using gluSphere instead of glutSolidSphere... For anyone who's interrested, here's my new main.cpp:

#include <iostream>
#include <stdlib.h>
#include "imageloader.h"
#include <cmath>

#include <GL/glut.h>

using namespace std;

const float SIZE = 10.0f;
#define M_PI	(3.14159265)
GLUquadricObj *sphere = NULL;

float _angle = 0;
GLuint _textureId;

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_COLOR_MATERIAL);
	glDisable(GL_CULL_FACE);
	glEnable(GL_TEXTURE_2D);


	Image* img = loadBMP("sky.bmp");
	_textureId = loadTextureFromImage(img);
	delete img;

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	sphere = gluNewQuadric();
	gluQuadricDrawStyle(sphere, GLU_FILL);
	gluQuadricTexture(sphere, GL_TRUE);
	gluQuadricNormals(sphere, GLU_SMOOTH);
}

void handleResize(int w, int h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}

void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBindTexture(GL_TEXTURE_2D, _textureId);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glTranslatef(0.0f, 0.0f, -20.0f);
	glRotatef(_angle, 0, 1, 0);

	gluSphere(sphere, 5.0, 20, 20);

	glutSwapBuffers();
}

//Called every 25 milliseconds
void update(int value) {
	_angle += 1.5f;
	if (_angle > 360) {
		_angle -= 360;
	}

	glutPostRedisplay();
	glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400);

	glutCreateWindow("Sphery sky");
	initRendering();

	glutDisplayFunc(drawScene);
	glutReshapeFunc(handleResize);
	glutTimerFunc(25, update, 0);

	glutMainLoop();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1