Sign in to follow this  
Shawn619

OpenGL Opengl - Texture problem

Recommended Posts

Shawn619    342
This problem came when i tried to add textures. I'm only only dealing with .bmp file types atm. So i started by adding the 'imageloader.cpp' and 'imageloader.h', then i added the following variable and function:
[code]
GLuint _textureId; //The id of the texture
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}
[/code]
And that function code was taken directly from a reputable website(videotutorialsrock.com) so i dont suspect that to be any problem.
Then i added code to my init() function:
[code]
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0);
// images
Image* image = loadBMP("vtr.bmp");
_textureId = loadTexture(image);
delete image;
}
[/code]
At this point i checked that everything is working fine and there is no problems loading the images.
Next i enable 2d textures to prepare and draw my texture on my quad:
[code]
void drawFloor(){
float size = 20.0;
float startX = -60.0f;
float startZ = 60.0f;
//glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
for(int x = 0;x < 6; x++){
for(int z = 0;z < 6; z++){
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size*z);//bottom-left
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size*z);//bottom-right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size + -size*z);//top-right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size + -size*z);//top-left
glEnd();
}
}
glDisable(GL_TEXTURE_2D);
//glPopMatrix();
}
[/code]
I should note that there is nothing wrong with the for-loops and structure, it works fine without textures and just color.
This produces my quads without texture, and with a color that i havent defined anywhere previously in my program (Bright Red):
[img]http://oi42.tinypic.com/xbd3mg.jpg[/img]
I tried googling this issue and found that it's the result of multiplying colors in my program that i already have and this causes this issue. And people were using "glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);" to overcome this issue, so i did as well, but it isnt working.
When i dont use "glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);", i get a darkish-red color for my quads which is a color i havent previously defined anywhere else in my program:
[img]http://oi39.tinypic.com/11vo314.jpg[/img]
Full code:
[code]
#include <iostream>
#include <stdlib.h>
#include <gl/glut.h>
#include "imageloader.h"
using namespace std;
//variables
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0;//camera rotation angles
float _angle = -70.0f;
float lastx, lasty;//mouse
GLfloat lightPos1[] = {0.0f, 0.0f, 0.0f, 1.0f};//light position
GLuint _textureId; //The id of the texture
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {

if (key=='q')
{
xrot += 1;
if (xrot >360) xrot -= 360;
}

if (key=='z')
{
xrot -= 1;
if (xrot < -360) xrot += 360;
}

if (key=='w')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad)) ;
zpos -= float(cos(yrotrad)) ;
ypos -= float(sin(xrotrad)) ;
}

if (key=='s')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad));
zpos += float(cos(yrotrad)) ;
ypos += float(sin(xrotrad));
}

if (key=='d')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos += float(cos(yrotrad)) * 0.2;
zpos += float(sin(yrotrad)) * 0.2;
}

if (key=='a')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= float(cos(yrotrad)) * 0.2;
zpos -= float(sin(yrotrad)) * 0.2;
}
switch (key) {
case 27: //Escape key
exit(0);
}
}
void mouseMovement(int x, int y) {
int diffx=x-lastx;
int diffy=y-lasty;
lastx=x; //set lastx to the current x position
lasty=y; //set lasty to the current y position
xrot += (float) diffy /1.2;
yrot += (float) diffx /1.2;
}
void camera (void) {
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glTranslated(-xpos,-ypos,-zpos);
}
void lighting (void){
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0004f);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos1);
}
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0);
// images
Image* image = loadBMP("vtr.bmp");
_textureId = loadTexture(image);
delete image;
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 2000.0);
}
void drawPlane(){
glPushMatrix();
glTranslatef(0.0f, 0.0f, -20.0f);
glColor3f(0.0f, 1.0f, 1.0f);
/* plane */
glBegin(GL_QUADS);
//glNormal3f(0.0f, 0.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-5.0f, 0.0f, 0.0f); //bottom-left
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(-5.0f, 0.0f, -20.0f);//top-left
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(5.0f, 0.0f, -20.0f);//top-right
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(5.0f, 0.0f, 0.0f);//bottom-right
glEnd();
glPopMatrix();
}
void drawBox(){
glPushMatrix();

glTranslatef(0.0f, 0.0f, -20.0f);
glRotatef(_angle, 0.0f, 6.0f, 0.5f);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);

//Front
//glNormal3f(0.0f, 0.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 4.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 4.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 6.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 6.0f, 1.5f);

//Right
//glNormal3f(1.0f, 0.0f, 0.0f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 4.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 6.0f, -1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 6.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 4.0f, 1.5f);

//Back
//glNormal3f(0.0f, 0.0f, -1.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 4.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 6.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 6.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 4.0f, -1.5f);

//Left
//glNormal3f(-1.0f, 0.0f, 0.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 4.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 4.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 6.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 6.0f, -1.5f);

glEnd();
glPopMatrix();
}
void drawBuilding(){
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(2.0f, 1.0f, 0.0f);
//front - orange
glVertex3f(-60.0f, 0.0f, -60.0f);//bottom-left
glVertex3f(60.0f, 0.0f, -60.0f);//bottom-right
glVertex3f(60.0f, 50.0f, -60.0f);//top-right
glVertex3f(-60.0f, 50.0f, -60.0f);//top-left
glColor3f(0.5f, 1.0f, 0.0f);
//right - green
glVertex3f(60.0f, 0.0f, -60.0f);//bottom-left
glVertex3f(60.0f, 0.0f, 60.0f);//bottom-right
glVertex3f(60.0f, 50.0f, 60.0f);//top-right
glVertex3f(60.0f, 50.0f, -60.0f);//top-left
glColor3f(0.2f, 0.6f, 0.5f);
//back - blue
glVertex3f(60.0f, 0.0f, 60.0f);//bottom left
glVertex3f(-60.0f, 0.0f, 60.0f);//bottom-right
glVertex3f(-60.0f, 50.0f, 60.0f);//top-right
glVertex3f(60.0f, 50.0f, 60.0f);//top-left
glColor3f(0.3f, 0.3f, 0.3f);
//left - gray
glVertex3f(-60.0f, 0.0f, 60.0f);//bottom-left
glVertex3f(-60.0f, 0.0f, -60.0f);//bottom-right
glVertex3f(-60.0f, 50.0f, -60.0f);//top-right
glVertex3f(-60.0f, 50.0f, 60.0f);//top-left
glEnd();
glPopMatrix();
}
void drawFloor(){
float size = 20.0;
float startX = -60.0f;
float startZ = 60.0f;
//glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
for(int x = 0;x < 6; x++){
for(int z = 0;z < 6; z++){
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size*z);//bottom-left
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size*z);//bottom-right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size + -size*z);//top-right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size + -size*z);//top-left
glEnd();
}
}
glDisable(GL_TEXTURE_2D);
//glPopMatrix();
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//camera();//fixed
//set lighting position
lighting();
//set camera position
camera();//following
/** DRAW **/
drawPlane();
drawBox();
drawBuilding();
drawFloor();
/** END DRAW **/
glutSwapBuffers();
}
void update(int value) {
_angle += 1.5f;
if (_angle > 360) {
_angle -= 360;
}

glutPostRedisplay();
glutTimerFunc(30, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);

//Create the window
glutCreateWindow("Lighting");
initRendering();

//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutPassiveMotionFunc(mouseMovement);
glutTimerFunc(30, update, 0); //Add a timer

glutMainLoop();
return 0;
}
[/code]

Share this post


Link to post
Share on other sites
FXACE    182
You did not used [url="http://www.opengl.org/sdk/docs/man/xhtml/glTexCoord.xml"]glTexCoord[/url] right (this is important!).

Your code:
[code]
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);<< check this
glVertex3f(startX + size*x, 0.0f, startZ + -size*z);//bottom-left
glTexCoord2f(0.0f, 0.0f);<<
glVertex3f(startX + size + size*x, 0.0f, startZ + -size*z);//bottom-right
glTexCoord2f(0.0f, 0.0f);<<
glVertex3f(startX + size + size*x, 0.0f, startZ + -size + -size*z);//top-right
glTexCoord2f(0.0f, 0.0f);<<
glVertex3f(startX + size*x, 0.0f, startZ + -size + -size*z);//top-left
glEnd();
[/code]

I think it should be:

[code]
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size*z);//bottom-left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size*z);//bottom-right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size + -size*z);//top-right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size + -size*z);//top-left
glEnd();
[/code]

edit:
[code]

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[/code]
It would be much better to keep this code into texture initialization function (because you are not changing it to another values in loop),

Best wishes, FXACE.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now