8 Replies - 1268 Views - Last Post: 28 May 2013 - 06:23 PM Rate Topic: -----

#1 Jiro_   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 27-August 12

texturing not working on Linux (works on Windows) - openGL 2

Posted 27 May 2013 - 08:05 AM

I am writing a simple wrapper around openGL, mostly for educational purposes. to learn openGL I am using this website. since my computer uses Intel, I'm using the openGL 2 port.
so far, everything is going fine, but I noticed one thing I haven't been able to fix. In lesson 5 they add textures to their models, and when I run those examples under Windows, they work flawless. However when ran under Linux, the textures don't even show up in the examples, and therefor I cannot test if my library works.
this is my model class (you'll find a lot similiarities with the site tutorials I gave earlier):
#include "model.h"
#include "../Helpers/Controls.hpp"

/*
 *some basic matrix rules:
 *scale*rotation*translation
 *(in GLSL, not in C++!)
 */
obj_Model::obj_Model(string model_file,string VertexShader,string FragmentShader)
{
    this->model_file = model_file;
    this->Loaded = this->initialised = false;
    //bring the file names into the class
    this->vertexShader=VertexShader;
    this->fragmentShader=FragmentShader;
    this->Position = vec3(0,0,0);
}

int obj_Model::Load(void)
{
    std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;
    std::vector<glm::vec3> temp_Vertices;
    std::vector<glm::vec2> temp_Uvs;
    std::vector<glm::vec3> temp_Normals;
    FILE * file = fopen(this->model_file.c_str(), "r");
    if(file == NULL)
        return FILE_NOT_FOUND;
    while(1)
    {

        char lineHeader[128];
        // read the first word of the line
        int res = fscanf(file, "%s", lineHeader);
        if (res == EOF)
            break; // EOF = End Of File. Quit the loop.

        // else : parse lineHeader

        if (strcmp(lineHeader,"v") == 0)
        {
            glm::vec3 vertex;
            fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
            temp_Vertices.push_back(vertex);
        }
        else if(strcmp(lineHeader,"vt") == 0)
        {
            glm::vec2 uv;
            fscanf(file, "%f %f\n", &uv.x, &uv.y);
            uv.y = -uv.y; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
            temp_Uvs.push_back(uv);
        }
        else if (strcmp(lineHeader, "vn") == 0)
        {
            glm::vec3 normal;
            fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
            temp_Normals.push_back(normal);
        }
        else if (strcmp(lineHeader, "f") == 0)
        {
            std::string vertex1, vertex2, vertex3;
            unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
            int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
            if (matches != 9)
            {
                int matches = fscanf(file, "%d//%d//%d %d//%d//%d %d//%d//%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
                if (matches != 9)
                    return INVALID_OBJ_FILE;
            }
            vertexIndices.push_back(vertexIndex[0]);
            vertexIndices.push_back(vertexIndex[1]);
            vertexIndices.push_back(vertexIndex[2]);
            uvIndices.push_back(uvIndex[0]);
            uvIndices.push_back(uvIndex[1]);
            uvIndices.push_back(uvIndex[2]);
            normalIndices.push_back(normalIndex[0]);
            normalIndices.push_back(normalIndex[1]);
            normalIndices.push_back(normalIndex[2]);
        }
        else
        {
            // Probably a comment, eat up the rest of the line
            char stupidBuffer[1000];
            fgets(stupidBuffer, 1000, file);
        }

    }
    // For each vertex of each triangle
    for(unsigned int i=0; i<vertexIndices.size(); i++)
    {
        // Get the indices of its attributes
        unsigned int vertexIndex = vertexIndices[i];
        unsigned int uvIndex = uvIndices[i];
        unsigned int normalIndex = normalIndices[i];

        // Get the attributes thanks to the index
        glm::vec3 vertex = temp_Vertices[ vertexIndex-1 ];
        glm::vec2 uv = temp_Uvs[ uvIndex-1 ];
        glm::vec3 normal = temp_Normals[ normalIndex-1 ];

        // Put the attributes in buffers
        this->Vertices.push_back(vertex);
        this->Uvs.push_back(uv);
        this->Normals .push_back(normal);

    }
    this->Loaded = true;
    cout << "debugging log for loading:" << endl
         << "size of Vertices: " << this->Vertices.size() << endl
         << "size of UV: " << this->Uvs.size() << endl;
    return NO_ERRORS;
}
float obj_Model::getScale(void)
{
    return this->Scale;
}
void obj_Model::getScale(float &scale)
{
    scale = this->Scale;
}
vec3 obj_Model::getRotation()
{
    return this->Rotation;
}
void obj_Model::getRotation(vec3 &rotation)
{
    rotation = this->Rotation;
}
vec3 obj_Model::getPosition(void)
{
    return this->Position;
}
void obj_Model::getPosition(vec3 &Position)
{
    Position = this->Position;
}
void obj_Model::setScale(float scale)
{
    this->Scale = scale;
}
void obj_Model::setRotation(vec3 rotation)
{
    this->Rotation = rotation;
}
void obj_Model::setPosition(vec3 position)
{
    this->Position = position;
}
string obj_Model::getFragmentShader() const
{
    return fragmentShader;
}
void obj_Model::setFragmentShader(const string &value)
{
    fragmentShader = value;
}
string obj_Model::getVertexShader() const
{
    return vertexShader;
}
void obj_Model::setVertexShader(const string &value)
{
    vertexShader = value;
}

int obj_Model::Draw()
{
    if(!initialised)
        cout << "called drawing with non inited model" << endl;
    if(!Loaded)
        cout << "called drawing with non loaded model" << endl;
    //TODO: REMOVE CAMERA INIT
#pragma message "camera init still here..."
    // Projection matrix : 45 Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
    // Camera matrix
    glm::mat4 View       = glm::lookAt(
                glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
                glm::vec3(0,0,0), // and looks at the origin
                glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
                );
    // Model matrix : an identity matrix
    glm::mat4 Model      = glm::mat4(1.0f);
    //set the actual position
    Model = glm::translate(Model, this->Position);
    // Our ModelViewProjection : multiplication of our 3 matrices
    glm::mat4 MVP        = Projection * View * Model; // Remember, matrix multiplication is the other way around
    //END CAMERA INIT
    //tell GL to use the GLSL ID for this model
    glUseProgram(programID);
    //tell GL the about the current transformations
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
    glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &Model[0][0]);
    glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &View[0][0]);
    //our light
    glm::vec3 lightPos = glm::vec3(0,3,-1);
    glUniform3f(LightID, lightPos.x, lightPos.y, lightPos.z);
    // Bind our texture in Texture Unit 0
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, Texture);
    // Set our "myTextureSampler" sampler to user Texture Unit 0
    glUniform1i(TextureID, 0);
    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(
                vertexPosition_modelspaceID,  // The attribute we want to configure
                3,                            // size
                GL_FLOAT,                     // type
                GL_FALSE,                     // normalized?
                0,                            // stride
                (void*)0                      // array buffer offset
                );

    // 2nd attribute buffer : UVs
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
    glVertexAttribPointer(
                vertexUVID,                   // The attribute we want to configure
                2,                            // size : U+V => 2
                GL_FLOAT,                     // type
                GL_FALSE,                     // normalized?
                0,                            // stride
                (void*)0                      // array buffer offset
                );

    // 3rd attribute buffer : normals
    glEnableVertexAttribArray(2);
    glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
    glVertexAttribPointer(
                vertexNormal_modelspaceID,    // The attribute we want to configure
                3,                            // size
                GL_FLOAT,                     // type
                GL_FALSE,                     // normalized?
                0,                            // stride
                (void*)0                      // array buffer offset
                );
    //TODO: 4th attribute buffer : colors
    // Draw the triangles !
    glDrawArrays(GL_TRIANGLES, 0, this->Vertices.size() );

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);

    return 0x0;
}

int obj_Model::Init()
{
    cout << "initialising " << this->name << endl;
    if(this->initialised)
        return 0X01;
    if(!this->Loaded)
        this->Load();
    // Create and compile our GLSL program from the shaders
    programID = LoadShaders(this->vertexShader.c_str(),this->fragmentShader.c_str());
    // Get a handle for our "MVP" uniform
    MatrixID = glGetUniformLocation(programID, "MVP");
    ViewMatrixID = glGetUniformLocation(programID, "V");
    ModelMatrixID = glGetUniformLocation(programID, "M");
    // Get a handle for our buffers
    vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
    vertexUVID = glGetAttribLocation(programID, "vertexUV");
    Texture = loadDDS("uvmap.DDS");
    TextureID  = glGetUniformLocation(programID, "myTextureSampler");
    vertexNormal_modelspaceID = glGetAttribLocation(programID, "vertexNormal_modelspace");
    //setup buffers
    glGenBuffers(1, &vertexbuffer); //vertices
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, this->Vertices.size()*sizeof(vec3), &this->Vertices[0], GL_STATIC_DRAW);
    glGenBuffers(1, &uvbuffer); //UV's
    glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
    glBufferData(GL_ARRAY_BUFFER, this->Uvs.size()*sizeof(vec2), &this->Uvs[0], GL_STATIC_DRAW);
    glGenBuffers(1, &normalbuffer); //normals
    glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
    glBufferData(GL_ARRAY_BUFFER, this->Normals.size() * sizeof(glm::vec3), &this->Normals[0], GL_STATIC_DRAW);
    glUseProgram(programID);
    LightID = glGetUniformLocation(programID, "LightPosition_worldspace");

    GLuint LightColorID = glGetUniformLocation(programID,"LightColor");
    vec3 lcolor = vec3(1,0.5,0.5);
    glUniform3f(LightColorID,lcolor.x,lcolor.y,lcolor.z);
    //set attribute and return no error
    this->initialised = true;
    return 0x0; //we don't need an init on a model
}




I am running openSUSE 12.3 and an Intel graphics card.
if you need more information, please ask.
thank you for your time!

Is This A Good Question/Topic? 0
  • +

Replies To: texturing not working on Linux (works on Windows) - openGL 2

#2 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: texturing not working on Linux (works on Windows) - openGL 2

Posted 27 May 2013 - 09:08 AM

It is important to remember that the OpenGL framework is different between platforms, so if you have simply taken the code that works on Windows and assumed that you didn't have to touch it in order for it to run on your Linux distro, you are likely very wrong.

I would look into getting a simple, unwrapped, OpenGL sample working on openSUSE to see what needs to be done and apply those steps to your project.
Was This Post Helpful? 1
  • +
  • -

#3 Jiro_   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 27-August 12

Re: texturing not working on Linux (works on Windows) - openGL 2

Posted 27 May 2013 - 11:38 AM

View PostButchDean, on 27 May 2013 - 09:08 AM, said:

It is important to remember that the OpenGL framework is different between platforms, so if you have simply taken the code that works on Windows and assumed that you didn't have to touch it in order for it to run on your Linux distro, you are likely very wrong.

I would look into getting a simple, unwrapped, OpenGL sample working on openSUSE to see what needs to be done and apply those steps to your project.

that's so logic, I feel like slamming my face against my desk.
I'll try to find such sample, I should have tought about that earlier!
well if I find one, I'll post the code here for the other linux users that might stumble upon this thread.

thanks!
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: texturing not working on Linux (works on Windows) - openGL 2

Posted 27 May 2013 - 01:29 PM

Awesome. Good luck. :)
Was This Post Helpful? 1
  • +
  • -

#5 Jiro_   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 27-August 12

Re: texturing not working on Linux (works on Windows) - openGL 2

Posted 28 May 2013 - 04:44 AM

after some derping with my code, I tried importing another file format then DDS, turns out that as well TGA as BMP import perfectly and texture out flawless. I guess something was wrong with my DDS compile module ^^"
the code I am using to import BMP and TGA is this (for my fellow Linux users!):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <GL/glew.h>

#include <GL/glfw.h>


GLuint loadBMP_custom(const char * imagepath){

	printf("Reading image %s\n", imagepath);

	// Data read from the header of the BMP file
	unsigned char header[54];
	unsigned int dataPos;
	unsigned int imageSize;
	unsigned int width, height;
	// Actual RGB data
	unsigned char * data;

	// Open the file
	FILE * file = fopen(imagepath,"rb");
	if (!file)							    {printf("%s could not be opened. Are you in the right directory ? Don't forget to read the FAQ !\n", imagepath); return 0;}

	// Read the header, i.e. the 54 first bytes

	// If less than 54 byes are read, problem
	if ( fread(header, 1, 54, file)!=54 ){ 
		printf("Not a correct BMP file\n");
		return 0;
	}
	// A BMP files always begins with "BM"
	if ( header[0]!='B' || header[1]!='M' ){
		printf("Not a correct BMP file\n");
		return 0;
	}
	// Make sure this is a 24bpp file
	if ( *(int*)&(header[0x1E])!=0  )         {printf("Not a correct BMP file\n");    return 0;}
	if ( *(int*)&(header[0x1C])!=24 )         {printf("Not a correct BMP file\n");    return 0;}

	// Read the information about the image
	dataPos    = *(int*)&(header[0x0A]);
	imageSize  = *(int*)&(header[0x22]);
	width      = *(int*)&(header[0x12]);
	height     = *(int*)&(header[0x16]);

	// Some BMP files are misformatted, guess missing information
	if (imageSize==0)    imageSize=width*height*3; // 3 : one byte for each Red, Green and Blue component
	if (dataPos==0)      dataPos=54; // The BMP header is done that way

	// Create a buffer
	data = new unsigned char [imageSize];

	// Read the actual data from the file into the buffer
	fread(data,1,imageSize,file);

	// Everything is in memory now, the file wan be closed
	fclose (file);

	// Create one OpenGL texture
	GLuint textureID;
	glGenTextures(1, &textureID);
	
	// "Bind" the newly created texture : all future texture functions will modify this texture
	glBindTexture(GL_TEXTURE_2D, textureID);

	// Give the image to OpenGL
	glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

	// OpenGL has now copied the data. Free our own version
	delete [] data;

	// Poor filtering, or ...
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

	// ... nice trilinear filtering.
	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); 
	glGenerateMipmap(GL_TEXTURE_2D);

	// Return the ID of the texture we just created
	return textureID;
}

GLuint loadTGA_glfw(const char * imagepath){

	// Create one OpenGL texture
	GLuint textureID;
	glGenTextures(1, &textureID);

	// "Bind" the newly created texture : all future texture functions will modify this texture
	glBindTexture(GL_TEXTURE_2D, textureID);

	// Read the file, call glTexImage2D with the right parameters
	glfwLoadTexture2D(imagepath, 0);

	// Nice trilinear filtering.
	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); 
	glGenerateMipmap(GL_TEXTURE_2D);

	// Return the ID of the texture we just created
	return textureID;
}



#define FOURCC_DXT1 0x31545844 // Equivalent to "DXT1" in ASCII
#define FOURCC_DXT3 0x33545844 // Equivalent to "DXT3" in ASCII
#define FOURCC_DXT5 0x35545844 // Equivalent to "DXT5" in ASCII

GLuint loadDDS(const char * imagepath)
{

    unsigned char header[124];
	FILE *fp; 
 
	/* try to open the file */ 
	fp = fopen(imagepath, "rb"); 
	if (fp == NULL) 
		return 0; 
   
	/* verify the type of file */ 
	char filecode[4]; 
	fread(filecode, 1, 4, fp); 
	if (strncmp(filecode, "DDS ", 4) != 0) { 
		fclose(fp); 
		return 0; 
	}
	
	/* get the surface desc */ 
	fread(&header, 124, 1, fp); 

	unsigned int height      = *(unsigned int*)&(header[8 ]);
	unsigned int width	     = *(unsigned int*)&(header[12]);
	unsigned int linearSize	 = *(unsigned int*)&(header[16]);
	unsigned int mipMapCount = *(unsigned int*)&(header[24]);
	unsigned int fourCC      = *(unsigned int*)&(header[80]);

 
	unsigned char * buffer;
	unsigned int bufsize;
	/* how big is it going to be including all mipmaps? */ 
	bufsize = mipMapCount > 1 ? linearSize * 2 : linearSize; 
	buffer = (unsigned char*)malloc(bufsize * sizeof(unsigned char)); 
	fread(buffer, 1, bufsize, fp); 
	/* close the file pointer */ 
	fclose(fp);

	unsigned int components  = (fourCC == FOURCC_DXT1) ? 3 : 4; 
	unsigned int format;
	switch(fourCC) 
	{ 
	case FOURCC_DXT1: 
		format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; 
		break; 
	case FOURCC_DXT3: 
		format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; 
		break; 
	case FOURCC_DXT5: 
		format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; 
		break; 
	default: 
		free(buffer); 
		return 0; 
	}

	// Create one OpenGL texture
	GLuint textureID;
	glGenTextures(1, &textureID);

	// "Bind" the newly created texture : all future texture functions will modify this texture
	glBindTexture(GL_TEXTURE_2D, textureID);
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);	
	
	unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16; 
	unsigned int offset = 0;

	/* load the mipmaps */ 
	for (unsigned int level = 0; level < mipMapCount && (width || height); ++level) 
	{ 
		unsigned int size = ((width+3)/4)*((height+3)/4)*blockSize; 
		glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height,  
			0, size, buffer + offset); 
	 
		offset += size; 
		width  /= 2; 
		height /= 2; 
	} 

	free(buffer); 

	return textureID;


}


do note that, however mentioned here, the DDS is not working under Linux (haven't tested it under Windows or Mac yet so PM me if you do/did)

thanks for your help, and another :^: for you sir!
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,453
  • Joined: 05-May 12

Re: texturing not working on Linux (works on Windows) - openGL 2

Posted 28 May 2013 - 06:01 AM

You have to be a bit more descriptive than "is not working". Have you stepped through the code and looked at the values you are getting versus the values you were expecting to get? If so where are you starting to get unexpected values?
Was This Post Helpful? 0
  • +
  • -

#7 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: texturing not working on Linux (works on Windows) - openGL 2

Posted 28 May 2013 - 06:22 AM

View PostJiro_, on 28 May 2013 - 08:44 AM, said:

thanks for your help

Glad you got it working! :)
Was This Post Helpful? 0
  • +
  • -

#8 Jiro_   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 27-August 12

Re: texturing not working on Linux (works on Windows) - openGL 2

Posted 28 May 2013 - 12:03 PM

View PostSkydiver, on 28 May 2013 - 06:01 AM, said:

You have to be a bit more descriptive than "is not working". Have you stepped through the code and looked at the values you are getting versus the values you were expecting to get? If so where are you starting to get unexpected values?


not working was all the details I could give, the functiont that loads the DDS is supposed to make a GLUINT of it (for as far as I know some sort of ID for openGL internal working) but it always returned 0. therefor, no texture was loaded, but I have no idea what could cause it...
all I know is that it didn't work, but I got it fixed by using bmp or tga images.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,453
  • Joined: 05-May 12

Re: texturing not working on Linux (works on Windows) - openGL 2

Posted 28 May 2013 - 06:23 PM

So in short, you just copied and pasted the code from somewhere and don't know how the code is supposed to work. Shouldn't you contact the author of the code, then to find out how it is supposed to work and help you figure out what is happening?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1