My code should read a image using soil library and using function renderTekstura should render a part of tekstura(as an atlas). Unfortunatelly, when I am going to display 32x32image which begins in punct 0.5, 0.5 I get the same picture as in 0.0, 0.0
Maybe there is sth wrong with soil flags when I read a texture? please help
#include <stdio.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "soil.h"
#ifndef GL_CLAMP_TO_EDGE
#define GL_CLAMP_TO_EDGE 0x812F
#endif
GLuint usertex;
GLuint tekstura;
void renderPrimitive (void)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,usertex);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S , GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glBegin(GL_QUADS); // Start drawing a quad primitive
glTexCoord2f(0,0);
glVertex3f(-5.0f, -5.0f, 0.0f); // The bottom left corner
glTexCoord2f(0,10);
glVertex3f(-5.0f, 5.0f, 0.0f); // The top left corner
glTexCoord2f(10,10);
glVertex3f(5.0f, 5.0f, 0.0f); // The top right corner
glTexCoord2f(10,0);
glVertex3f(5.0f, -5.0f, 0.0f); // The bottom right corner
glEnd();
glDisable(GL_TEXTURE_2D);
}
void renderTekstura(float coord_x, float coord_y, float rect_x, float rect_y)
{
//polozenie lewego gornego rogu zadanego fragmentu
coord_x = coord_x/128.0f; //szerokość tekstury w pikselach
coord_y = coord_y/128.0f; //wysokość t w pikselach
//wielkosc fragmentu
rect_x = rect_x/128.0f; //szerokość tekstury w pikselach
rect_y = rect_y/128.0f; //wysokość t w pikselach
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tekstura);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S , GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR , GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //Filtrowanie tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //Przycinanie tekstury
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS); // Start drawing a quad primitive
glTexCoord2f(coord_x+rect_x, coord_y+rect_y);
printf("%f %f
",coord_x+rect_x, coord_y+rect_y);
glVertex3f(0.5f, 0.5f, 0.0f); // The bottom left corner
glTexCoord2f(coord_x,coord_y+rect_y);
printf("%f %f
", coord_x,coord_y+rect_y);
glVertex3f(-0.5f, 0.5f, 0.0f); // The top left corner
glTexCoord2f(coord_x, coord_y);
printf("%f %f
", coord_x, coord_y);
glVertex3f(-0.5f, -0.5f, 0.0f); // The top left corner
glTexCoord2f(coord_x+rect_x, coord_y);
printf("%f %f
", coord_x+rect_x, coord_y);
glVertex3f(0.5f, -0.5f, 0.0f); // The bottom right corner
glEnd();
glDisable(GL_TEXTURE_2D);
}
void display (void)
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
glDisable(GL_BLEND);
glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
glTranslatef(0.0f, 0.0f, -5.0f); // Push eveything 5 units back into the scene, otherwise we won't see the primitive
float coord_x =0.5, coord_y = 0.5, rect_x=64.0, rect_y=64.0;
renderTekstura(coord_x, coord_y, rect_x, rect_y);
//glTranslatef(0.0f, 0.0f, -5.0f);
//renderPrimitive(); // Render the primitive
glFlush(); // Flush the OpenGL buffers to the window
}
void reshape (int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(100, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}
int main (int argc, char **argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize (500, 500); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
glutCreateWindow ("You’re first OpenGL Window"); // Set the title for the window
glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshaping
/* load an image file directly as a new OpenGL texture */
usertex = SOIL_load_OGL_texture
(
"czter1 kopia.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
tekstura = SOIL_load_OGL_texture
(
"you1.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
printf("Hello");
/* check for an error during the load process */
if( 0 == tekstura )
printf( "SOIL loading error: '%s'
", SOIL_last_result() );
else
printf("SOIL load success for user block");
glutMainLoop(); // Enter GLUT's main loop
return 0;
}