here is the output: http://i43.tinypic.com/9a5zyx.png
if things were working the way i wanted, the colors in the left square would match the colors in the right square. thanks for any help regarding the subject
#include <gl/glfw.h>
const char* title="test";
GLuint img;
unsigned int w=64,h=64;
int screenwidth,screenheight;
void enable2d()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glViewport(0,0,screenwidth,screenheight);
glOrtho(0,screenwidth,screenheight,0,-1,1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_LIGHTING_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
}
void drawmytex()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,img);
glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertex2i(0,0);
glTexCoord2i(1,0);
glVertex2i(w,0);
glTexCoord2i(1,1);
glVertex2i(w,h);
glTexCoord2i(0,1);
glVertex2i(0,h);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void drawquad(int x,int y)
{
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex2i(x,y);
glColor3f(1.0f,0.0f,1.0f);
glVertex2i(x+w,y);
glColor3f(0.0f,1.0f,1.0f);
glVertex2i(x+w,y+h);
glColor3f(0.0f,0.0f,1.0f);
glVertex2i(x,y+h);
glEnd();
}
void texcopy()
{
if (!glIsTexture(img))
glDeleteTextures(1,&img);
glGenTextures(1,&img);
glBindTexture(GL_TEXTURE_2D,img);
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,GL_RGBA,w,h,0,GL_RGBA,GL_UNSIGNED_BYTE,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,h,0,-1,1);
glViewport(0,0,w,h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
drawquad(0,0);
glBindTexture(GL_TEXTURE_2D,img);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,0,0,w,h,0);
glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,screenwidth,screenheight,0,-1,1);
glViewport(0,0,screenwidth,screenheight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main()
{
int running;
glfwInit();
running=glfwOpenWindow(640,480,0,0,0,0,0,0,GLFW_WINDOW);
if (!running)
{
glfwTerminate();
return 0;
}
glfwSetWindowTitle(title);
glfwEnable(GLFW_STICKY_KEYS);
glfwGetWindowSize(&screenwidth,&screenheight);
enable2d();
texcopy();
do
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
drawquad(64,0);
drawmytex();
glfwSwapBuffers();
running=!glfwGetKey(GLFW_KEY_ESC)&&glfwGetWindowParam(GLFW_OPENED);
GLenum error=glGetError();
if (error!=GL_NO_ERROR)running=error;
glfwSleep(.017);
}
while (running==1);
glDeleteTextures(1,&img);
glfwTerminate();
return running;
}
Try adding 'glColor3f(1,1,1);' in your 'drawmytex' function. I suspect that your texture is modulated (multiplied) with the current color, if so, the problem is not the texture copy but the way you display it.
Related
I have set the background texture with Ortho bounds.I have some drawings in perspective which are required to be overlaid on top of this.
Why the foreground Object in perspective cannot be seen?
void handleResize(int w, int h)
{
width=w;
height=h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}
void Draw2D()
{
//ORTHO
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 1024.0, 0.0, 512.0, 0.0, 1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
//Bottom
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(1024.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(1024.0,512.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,512.0);
glEnd();
}
void Draw3D()
{
//PERSPECTIVE
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)width / (float)height, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -6.0f);
glutWireCube(0.5);
}
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Draw3D();
Draw2D();
glutSwapBuffers();
}
#Matso..I am using below function for the depth testing
void initRendering()
{
//glEnable(GL_DEPTH_TEST);//Comment this
glDepthMask(false);//Use this
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
Image* image = loadBMP("earth.bmp");
_textureId = loadTexture(image);
delete image;
}
Introducing
glDepthMask(false);
did the trick.I am able to view my perspective drawings.
I am trying to draw text using openGL which will be displayed in a window, over a kinect camera image. The program can draw squares and other shapes fine, but when I call the method to draw the text, it crashes. It seems to be crashing on the
glutStrokeCharacter(font, c);
Everything else works and when I comment out just this line the program still runs fine. Below is a code snippet of how I try and draw the text.
void Button::DrawSquare(bool selected)
{
glEnable( GL_POINT_SMOOTH );
glLineWidth(7);
if (selected == true) glColor3f(0,1,0);
else glColor3f((123.0/255.0f),(205/255.0f),(237.0/255.0f));
glBegin(GL_LINE_LOOP);
glVertex2f(X, Y);
glVertex2f(X+length, Y);
glVertex2f(X+length, Y+width);
glVertex2f(X, Y+width);
glEnd();
glLineWidth(3);
glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(X, Y);
glVertex2f(X+length, Y);
glVertex2f(X+length, Y+width);
glVertex2f(X, Y+width);
glEnd();
glLineWidth(4);
//Start drawing text
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
if (selected == true) glColor3f(0,1,0);
else glColor3f(1,1,0);
void *font = GLUT_BITMAP_TIMES_ROMAN_10;
glRasterPos2i(this->X+15,this->Y+35);
string s(*this->trackname);
for (string::iterator i = s.begin(); i != s.end(); ++i)
{
char c = *i;
glutStrokeCharacter(font, c);// <<-- Line that gives error
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glColor3f(255,255,255);
}
The drawing of the squares just before the text works fine.
Here is a minimal working example of how to render text using glutBitmapCharacter:
static const std::string text_to_render = "testing 1 2 3 4";
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glDisable( GL_DEPTH_TEST ); // disable to render text in front of anything else
glRasterPos2f(0, 0); // center of screen
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
GLvoid * font = GLUT_BITMAP_TIMES_ROMAN_10;
glColor3f(1.0f, 0.0f, 0.0f);
for(char chr : text_to_render)
{
glutBitmapCharacter(font, chr);
}
glEnable(GL_DEPTH_TEST); // re-enable depth test
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glutSwapBuffers();
}
The only difference here is that I'm disabling OpenGL's GL_DEPTH_TEST and I'm loading the identity matrices after pushing the current matrices.
Let me know if this is of any help.
I'm currently porting a game of which the code is very obfuscated due to porting from C to Java.
My problem is that some users report a black screen and no other problems (sound is working fine e.g.), with no errors showing a problem. On my pc it runs fine, and it makes for a hell of debugging.
I was wondering if anyone can post a (list of) reason(s) this might be occurring. I've read somewhere one of the issues could be using a 32 bits Java on a 64 bits system.
My code below, also opensourced at: https://code.google.com/p/jake2t/
private void renderSideBySide() {
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, sbsFboId);
// Render side by side
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glViewport(0,0,width,height);
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
glDisable(GL_CULL_FACE);
int shaderId = sbsShader.getId();
glDisable(GL_TEXTURE_2D);
ARBShaderObjects.glUseProgramObjectARB(shaderId);
if (postFboTextureLocation[0] < 0) {
postFboTextureLocation[0] = ARBShaderObjects.glGetUniformLocationARB(shaderId, "leftTexture");
}
if (postFboTextureLocation[1] < 0) {
postFboTextureLocation[1] = ARBShaderObjects.glGetUniformLocationARB(shaderId, "rightTexture");
}
if (postFboDepthTextureLocation[0] < 0) {
postFboDepthTextureLocation[0] = ARBShaderObjects.glGetUniformLocationARB(shaderId, "leftDepthTexture");
}
if (postFboDepthTextureLocation[1] < 0) {
postFboDepthTextureLocation[1] = ARBShaderObjects.glGetUniformLocationARB(shaderId, "rightDepthTexture");
}
// Load the images with the colors and the depth values
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, postFboTextureId[0]);
ARBShaderObjects.glUniform1iARB(postFboTextureLocation[0], 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, postFboTextureId[1]);
ARBShaderObjects.glUniform1iARB(postFboTextureLocation[1], 1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, postFboDepthTexture[0]);
ARBShaderObjects.glUniform1iARB(postFboDepthTextureLocation[0], 2);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, postFboDepthTexture[1]);
ARBShaderObjects.glUniform1iARB(postFboDepthTextureLocation[1], 3);
glBegin (GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2i (0, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex2i (width, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex2i (width, height);
glTexCoord2f(0.0f, 1.0f);
glVertex2i (0, height);
glEnd();
ARBShaderObjects.glUseProgramObjectARB(0);
// Rendering with warping
glPopMatrix();
glPopAttrib();
unbindFBO();
}
public void drawPostFBOs() {
renderSideBySide();
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glViewport(0,0,width,height);
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
int shaderId = riftShader.getId();
ARBShaderObjects.glUseProgramObjectARB(shaderId);
if (sbsFboTextureLocation < 0) {
sbsFboTextureLocation = ARBShaderObjects.glGetUniformLocationARB(shaderId, "tex");
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sbsFboTextureId);
ARBShaderObjects.glUniform1iARB(sbsFboTextureLocation, 0);
glBegin (GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2i (0, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex2i (width, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex2i (width, height);
glTexCoord2f(0.0f, 1.0f);
glVertex2i (0, height);
glEnd();
ARBShaderObjects.glUseProgramObjectARB(0);
glPopMatrix();
glPopAttrib();
}
The common problems that OpenGL 3+ programmer doesn't consider when coding are:
OpenGL Extensions
use of ARB and EXT extension.
use of correct OGL extension.
use multiple implementation of different OGL extensions. Here is an example using C++:
#ifndef _MESA_INVERT_
/*** ... some codes here ... ***/
#else
/*** ... alt some codes here ... ***/
#endif
Check if the video card support the following extension using:
glxinfo - Linux and MacOS X based system.
glview - Windows based system.
How can I Create a 4 views (LEFT/TOP/PERSPECTIVE/FRONT) in Open GL ??
I Have Used this :
int main(int c,char ** argv)
{
glutInit(&c,argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH |GLUT_RGB );
glutInitWindowSize(w,h);
MainWin =glutCreateWindow("Teapot Window");
glutDisplayFunc(display);
LeftWin = glutCreateSubWindow(MainWin,0,0,s_window_w,s_window_h);
glutDisplayFunc(displayLeft);
glutReshapeFunc(reshapeLeft);
TopWin = glutCreateSubWindow(MainWin,s_window_w+3,0,s_window_w,s_window_h);
glutDisplayFunc(displayTop);
glutReshapeFunc(reshapeTop);
PerspectiveWin = glutCreateSubWindow(MainWin,0,s_window_h+3,s_window_w,s_window_h);
glutDisplayFunc(displayPerspective);
glutReshapeFunc(reshapePerspective);
FrontWin = glutCreateSubWindow(MainWin,s_window_w+3,s_window_h+3,s_window_w,s_window_h);
glutDisplayFunc(displayFront);
glutReshapeFunc(reshapeFront);
glutMainLoop();
return 0;
}
Then I have make the projection for top and left and like this :
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5f,0.5f,-0.35f,0.35f,1,500);
When I use the top lines for the front also , the output is something like this :
Where is my fault ?
thanks in advance
Why the Front view is empty and the perspective and left view are same ??
For perspective projection i had used glFrustum ....
So Am I going wrong ?
Please help on creating multiple views like 3ds max or maya ...
below is the code for display functions:
void displayLeft()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-1,0,0,0,0,1,0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f,0.3f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(teapot_x,teapot_y,teapot_z);
glRotatef(teapot_angle,0,1,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2f);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displayTop()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,-1,0,0,0,0,0,0,-1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f,0.3f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(teapot_x,teapot_y,teapot_z);
glRotatef(teapot_angle,0,1,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2f);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displayFront()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,0,0,0,0,1,0,0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f,0.3f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(teapot_x,teapot_y,teapot_z);
glRotatef(teapot_angle,0,1,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2f);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displayPerspective()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-1,0,0,0,0,1,0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f,0.3f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(teapot_x,teapot_y,teapot_z);
glRotatef(0,0,1,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2f);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
Use glViewport and glScissor to cut out panes of the window. Then for each pane perform the rendering as usual. Setting the projection matrix is actually a drawing state operation, so it belongs into the display function, not reshape.
Your display function should become something like
void ViewportScissor(int x, int y, int width, int height)
{
glViewport(x, y, width, height);
glScissor(x, y, width, height);
}
void display(void)
{
int const win_width = glutGet(GLUT_WINDOW_WIDTH);
int const win_height = glutGet(GLUT_WINDOW_HEIGHT);
glDisable(GL_SCISSOR);
glClear(…);
glEnable(GL_SCISSOR);
ViewportScissor(0, 0, win_width/2, win_height/2);
glMatrixMode(GL_PROJECTION);
setup_frontview_projection();
glMatrixMode(GL_MODELVIEW);
setup_frontview();
draw_scene();
ViewportScissor(win_width/2, 0, win_width/2, win_height/2);
glMatrixMode(GL_PROJECTION);
setup_rightview_projection();
glMatrixMode(GL_MODELVIEW);
setup_rightview();
draw_scene();
ViewportScissor(0, win_height/2, win_width/2, win_height/2);
glMatrixMode(GL_PROJECTION);
setup_topview_projection();
glMatrixMode(GL_MODELVIEW);
setup_topview();
draw_scene();
ViewportScissor(win_width/2, win_height/2, win_width/2, win_height/2);
glMatrixMode(GL_PROJECTION);
setup_freecamview_projection();
glMatrixMode(GL_MODELVIEW);
setup_freecamview();
draw_scene();
glutSwapBuffers();
}
Adding splitter lines/frames is left as an exercise for the reader.
I am trying to load and draw a 2d texture using OpenGL with GLFW and SOIL. I have this code, but I only get one solid color (which seems to come from the texture).
I have tested whether the .png loads with an example that came with SOIL, and it worked fine so there has to be some issue in my code.
This is my code:
#include <cstdio>
#include "GL/glfw.h"
#include "SOIL.h"
// function declarations
void drawscene();
void idlefunc();
void updatedisplay();
// global data
GLuint texture; // our example texture
int main(int argc, char **argv) {
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return 1;
}
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 16, 0, GLFW_WINDOW)) {
fprintf(stderr, "Failed to open GLFW window\n");
return 1;
}
// enable vsync (if available)
glfwSwapInterval(1);
// load textures
texture = SOIL_load_OGL_texture(
"tex.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT
);
// check for an error during the texture loading
if (!texture) {
printf("SOIL loading error: '%s'\n", SOIL_last_result());
}
while (glfwGetWindowParam(GLFW_OPENED)) {
idlefunc();
}
// if we get here something went wrong
return 0;
}
// this function gets called every frame
void idlefunc() {
updatedisplay();
drawscene();
}
// set up te display
void updatedisplay() {
int screen_width, screen_height;
glfwGetWindowSize(&screen_width, &screen_height);
if (screen_height <= 0) screen_height = 1;
if (screen_width <= 0) screen_width = 1;
glViewport(0, 0, screen_width, screen_height);
glClearColor(0.02f, 0.02f, 0.02f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, screen_width, screen_height, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// displacement trick for exact pixelization
glTranslatef(0.375f, 0.375f, 0.0f);
}
// draw the scene in this function
void drawscene() {
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(10.0f, 10.0f, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 128.0f);
glVertex2f(0.0f, 128.0f);
glTexCoord2f(128.0f, 128.0f);
glVertex2f(128.0f, 128.0f);
glTexCoord2f(128.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glEnd();
glPopMatrix();
glfwSwapBuffers();
}
Found the issue (thanks to user786653). No matter the vertex coords, the tex coords are between 0.0 and 1.0. This is the fixed code:
// draw the scene in this function
void drawscene() {
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(10.0f, 10.0f, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 128.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(128.0f, 128.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glEnd();
glPopMatrix();
glfwSwapBuffers();
}