• Advertisement
Sign in to follow this  

Loading a Texture - Visual Artifacts

This topic is 1933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

On my quest to keep learning OpenGL, I've gone as far as [url="http://nehe.gamedev.net/tutorial/texture_filters,_lighting_&_keyboard_control/15002/"]Nehe's Lesson 7[/url] with loading a Crate texture.
But now I'm stuck. Since I'm on a Mac, I've had to write my own function to load a BMP file, and following [url="http://www.gamedeception.net/archive/index.php?t-5425.html"]this[/url] advice, I had my function ready and working, at least in tutorial 6. For #7, I've added my own class, that's just a wrapper that contains the information needed for data, width and height, and it's called BMPData.cpp.
Anyway, the problem is that the texture, in the end, appears like [url="http://imgur.com/z8NEs"]this[/url].
Here's my code, I've tried doing anything with it, but have come up with not solution in the past two days, any help is really appriciated [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]
[source lang="cpp"]#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <glm-0.9.3.4/glm/glm.hpp>
#include <fstream.h>
#include "ResourcePath.hpp"
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BMPData.h"
using namespace glm;

GLfloat rtri, rquad;

GLfloat xrot, yrot, zrot, xspeed, yspeed, z = -5.0f;
std::string dataPath = "/Users/umbertosonnino/Documents/XCode/tobedeleted/tobedeleted/data/";

bool lp, fp, light;

GLfloat LightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[] = { 0, 0, 2.0f, 1.0f, 1.0f };
GLuint filter = 0, texture[3];

BMPData *image;


BMPData *loadBMP(std::string filename){
FILE* file = NULL;

if(!filename.c_str()){
fprintf(stderr, "File name has not been handed to the program\n");
return NULL;
}

file = fopen(filename.c_str(), "rb");

if(file == NULL){
fprintf(stderr, "Error opening the file %s\n", filename.c_str());
return NULL;
}

unsigned long width = 0;
unsigned long height = 0;
unsigned long size = 0;
fseek(file, 18, SEEK_SET);
fread(&width, 4, 1, file);
fread(&height, 4, 1, file);
fseek(file, 0, SEEK_END);
size = ftell(file) - 54;

unsigned char* data = (unsigned char*) malloc(size);

fseek(file, 54, SEEK_SET);
fread(data, size, 1, file);

image = new BMPData(data, width, height);

fclose(file);

return image;
}
unsigned int loadGLTextures(){

int status = false;
BMPData *bmp;

if( (bmp = loadBMP(dataPath+"Crate.bmp")) ){

status = true;

glGenTextures(3, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, (GLsizei)bmp->getWidth(), (GLsizei)bmp->getHeight(), 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, bmp->getData());

glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, (GLsizei)bmp->getWidth(), (GLsizei)bmp->getHeight(), 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, bmp->getData());

// Create MipMapped Texture
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, (GLsizei)bmp->getWidth(), (GLsizei)bmp->getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE, bmp->getData());
}

if(bmp){
if(bmp->getData()){
bmp->~BMPData();
}

free(bmp);
}

return status;
}

int draw(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,z);

glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);

glBindTexture(GL_TEXTURE_2D, texture[filter]);
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();


xrot+=xspeed;
yrot+=yspeed;

return true;
}

int initGL(){
//resizeGL function in Nehe
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)800/(GLfloat)600,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
//end of resize function


if(!loadGLTextures()){
fprintf(stderr, "Error loading the bmp during initialization\n");
return false;
}

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.3f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
glEnable(GL_LIGHT1);

return true;
}

int main (int argc, const char * argv[])
{
sf::Window window(sf::VideoMode(800, 600), "Nehe Lighting and Keyboard Tutotorial: Embracing OpenGL");

initGL();


do
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
if(event.type == sf::Event::Resized){
glViewport(0, 0, event.size.width, event.size.height);
}
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();

// Escape pressed : exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();

if(event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::L &&
!lp){
lp = true;
light = !light;
if(!light){
glDisable(GL_LIGHTING);
}
if(light){
glEnable(GL_LIGHTING);
}
}
if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::L){
lp = false;
}

if(event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::F &&
!fp){
fp = true;
filter += 1;
if(filter >2) filter = 0;
}
if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::F){
fp = false;
}
//When pressing up i'm moving the cube farther into distance
if(event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::U) z += 0.2f;
//When pressing down on the keyboard i'm moving the cuve closer to the screen
if(event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard:[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]) z-= 0.2f;
if(event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::Up) xspeed -= 0.1f;
if(event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::Down) xspeed += 0.1f;
if(event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::Left) yspeed -= 0.1f;
if(event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::Right) yspeed += 0.1f;
}
draw();


// Update the window
window.display();

}while(window.isOpen());

return EXIT_SUCCESS;
}
[/source]

Share this post


Link to post
Share on other sites
Advertisement
The problem appears to be a mismatch between the format of the bitmap and the format passed to glTexImage2D, although I note that my copy of the OpenGL Programmer's Reference says that GL_BGR_EXT is not a valid value for the second format parameter of glTexImage2D (I think you want GL_BGR instead of GL_BGR_EXT.

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1349258291' post='4986331']
The problem appears to be a mismatch between the format of the bitmap and the format passed to glTexImage2D, although I note that my copy of the OpenGL Programmer's Reference says that GL_BGR_EXT is not a valid value for the second format parameter of glTexImage2D (I think you want GL_BGR instead of GL_BGR_EXT.
[/quote]Thanks for the fast reply!
I tried changing those, but the result sadly is the same

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement