• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
    • By Jens Eckervogt
      Hello guys, 
       
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System.Collections.Generic; using System.IO; using System.Text; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> inPositions; private static List<Vector2> inTexcoords; private static List<Vector3> inNormals; private static List<float> positions; private static List<float> texcoords; private static List<int> indices; public static RawModel LoadObjModel(string filename, Loader loader) { inPositions = new List<Vector3>(); inTexcoords = new List<Vector2>(); inNormals = new List<Vector3>(); positions = new List<float>(); texcoords = new List<float>(); indices = new List<int>(); int nextIdx = 0; using (var reader = new StreamReader(File.Open("Contents/" + filename + ".obj", FileMode.Open), Encoding.UTF8)) { string line = reader.ReadLine(); int i = reader.Read(); while (true) { string[] currentLine = line.Split(); if (currentLine[0] == "v") { Vector3 pos = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inPositions.Add(pos); if (currentLine[1] == "t") { Vector2 tex = new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2])); inTexcoords.Add(tex); } if (currentLine[1] == "n") { Vector3 nom = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inNormals.Add(nom); } } if (currentLine[0] == "f") { Vector3 pos = inPositions[0]; positions.Add(pos.X); positions.Add(pos.Y); positions.Add(pos.Z); Vector2 tc = inTexcoords[0]; texcoords.Add(tc.X); texcoords.Add(tc.Y); indices.Add(nextIdx); ++nextIdx; } reader.Close(); return loader.loadToVAO(positions.ToArray(), texcoords.ToArray(), indices.ToArray()); } } } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
       
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
    • By codelyoko373
      I wasn't sure if this would be the right place for a topic like this so sorry if it isn't.
      I'm currently working on a project for Uni using FreeGLUT to make a simple solar system simulation. I've got to the point where I've implemented all the planets and have used a Scene Graph to link them all together. The issue I'm having with now though is basically the planets and moons orbit correctly at their own orbit speeds.
      I'm not really experienced with using matrices for stuff like this so It's likely why I can't figure out how exactly to get it working. This is where I'm applying the transformation matrices, as well as pushing and popping them. This is within the Render function that every planet including the sun and moons will have and run.
      if (tag != "Sun") { glRotatef(orbitAngle, orbitRotation.X, orbitRotation.Y, orbitRotation.Z); } glPushMatrix(); glTranslatef(position.X, position.Y, position.Z); glRotatef(rotationAngle, rotation.X, rotation.Y, rotation.Z); glScalef(scale.X, scale.Y, scale.Z); glDrawElements(GL_TRIANGLES, mesh->indiceCount, GL_UNSIGNED_SHORT, mesh->indices); if (tag != "Sun") { glPopMatrix(); } The "If(tag != "Sun")" parts are my attempts are getting the planets to orbit correctly though it likely isn't the way I'm meant to be doing it. So I was wondering if someone would be able to help me? As I really don't have an idea on what I would do to get it working. Using the if statement is truthfully the closest I've got to it working but there are still weird effects like the planets orbiting faster then they should depending on the number of planets actually be updated/rendered.
    • By Jens Eckervogt
      Hello everyone, 
      I have problem with texture
      using System; using OpenTK; using OpenTK.Input; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using System.Drawing; using System.Reflection; namespace Tutorial_05 { class Game : GameWindow { private static int WIDTH = 1200; private static int HEIGHT = 720; private static KeyboardState keyState; private int vaoID; private int vboID; private int iboID; private Vector3[] vertices = { new Vector3(-0.5f, 0.5f, 0.0f), // V0 new Vector3(-0.5f, -0.5f, 0.0f), // V1 new Vector3(0.5f, -0.5f, 0.0f), // V2 new Vector3(0.5f, 0.5f, 0.0f) // V3 }; private Vector2[] texcoords = { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) }; private int[] indices = { 0, 1, 3, 3, 1, 2 }; private string vertsrc = @"#version 450 core in vec3 position; in vec2 textureCoords; out vec2 pass_textureCoords; void main(void) { gl_Position = vec4(position, 1.0); pass_textureCoords = textureCoords; }"; private string fragsrc = @"#version 450 core in vec2 pass_textureCoords; out vec4 out_color; uniform sampler2D textureSampler; void main(void) { out_color = texture(textureSampler, pass_textureCoords); }"; private int programID; private int vertexShaderID; private int fragmentShaderID; private int textureID; private Bitmap texsrc; public Game() : base(WIDTH, HEIGHT, GraphicsMode.Default, "Tutorial 05 - Texturing", GameWindowFlags.Default, DisplayDevice.Default, 4, 5, GraphicsContextFlags.Default) { } protected override void OnLoad(EventArgs e) { base.OnLoad(e); CursorVisible = true; GL.GenVertexArrays(1, out vaoID); GL.BindVertexArray(vaoID); GL.GenBuffers(1, out vboID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); GL.GenBuffers(1, out iboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw); vertexShaderID = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vertexShaderID, vertsrc); GL.CompileShader(vertexShaderID); fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(fragmentShaderID, fragsrc); GL.CompileShader(fragmentShaderID); programID = GL.CreateProgram(); GL.AttachShader(programID, vertexShaderID); GL.AttachShader(programID, fragmentShaderID); GL.LinkProgram(programID); // Loading texture from embedded resource texsrc = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial_05.example.png")); textureID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texsrc.Width, texsrc.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); System.Drawing.Imaging.BitmapData bitmap_data = texsrc.LockBits(new Rectangle(0, 0, texsrc.Width, texsrc.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texsrc.Width, texsrc.Height, PixelFormat.Bgra, PixelType.UnsignedByte, bitmap_data.Scan0); texsrc.UnlockBits(bitmap_data); GL.Enable(EnableCap.Texture2D); GL.BufferData(BufferTarget.TextureBuffer, (IntPtr)(texcoords.Length * Vector2.SizeInBytes), texcoords, BufferUsageHint.StaticDraw); GL.BindAttribLocation(programID, 0, "position"); GL.BindAttribLocation(programID, 1, "textureCoords"); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(0, 0, ClientRectangle.Width, ClientRectangle.Height); } protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Key.Escape)) { Exit(); } } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); // Prepare for background GL.Clear(ClearBufferMask.ColorBufferBit); GL.ClearColor(Color4.Red); // Draw traingles GL.EnableVertexAttribArray(0); GL.EnableVertexAttribArray(1); GL.BindVertexArray(vaoID); GL.UseProgram(programID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture3D, textureID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(1); SwapBuffers(); } protected override void OnClosed(EventArgs e) { base.OnClosed(e); GL.DeleteVertexArray(vaoID); GL.DeleteBuffer(vboID); } } } I can not remember where do I add GL.Uniform2();
    • By Jens Eckervogt
      Hello everyone
      For @80bserver8 nice job - I have found Google search. How did you port from Javascript WebGL to C# OpenTK.?
      I have been searched Google but it shows f***ing Unity 3D. I really want know how do I understand I want start with OpenTK But I want know where is porting of Javascript and C#?
       
      Thanks!
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Best way to modify incorrect textures?

This topic is 4612 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

Ok, so I'm using this tutorial on using SDL_Surface's as OpenGL textures. Everything works fine and dandy except for the fact that the texture is mirrored and flipped. They suggest to simply add in this code:
//change to texture matrix and do the trick
glMatrixMode(GL_TEXTURE);
glRotatef(180.0f,0.0f,0.0f,1.0f);
glScalef(-1.0f,1.0f,1.0f);
While that works great for that, I am thinking if that is the best solution in the long run (which I don't think it is). If I were to use a regular OpenGL texture, then it would then be messed up and then all chaos will break lose. Now since I am using SDL, I'm thinking I'll pretty much only use the IMG_Load function (SDL_Image) and this technique of converting from SDL_Surface to an OpenGL texture all thoughout, but I still need to take into account the case of if I were to need to not use this technique later on. Right now I'm thinking my two main choices are either to use the OpenGL code to mirror and flip, or I can add in code to my texture loading function to mirror and flip the image then and have it all correct. Of coursethe problem then is all the extra overhead and resource comsumption if a lot of textures are used. So you can see my delima. Does anyone have any other possible suggestions/ideas as to what I can do to remedy this? Thanks for your time.

Share this post


Link to post
Share on other sites
Advertisement
FLIP IT WHEN U LOAD IT
dont know if IMG_Load function (SDL_Image) can do this or not (check the header)
else u can flip the texture coordinates

Share this post


Link to post
Share on other sites
FLIP IT WHEN U LOAD IT
dont know if IMG_Load function (SDL_Image) can do this or not (check the header)
else u can flip the texture coordinates

Share this post


Link to post
Share on other sites
Like zedzeek says, the best way would be to mirror and flip your pixel data during loading of images. It's far more efficient that altering the texture matrix each time you want to flip your textures.

Another soultion could be - simply - changing texture coordinates of a primitive you are rendering.

Hope it helps!

Share this post


Link to post
Share on other sites
Clapton has the right idea, I think. As it sounds to me - the thing that is causing your problem is that in SDL, the positive y-axis is downwards, ie 0,0 is at the top left of your screen. In OpenGL, (0,0) is the bottom left. y-axis is positive in the upward direction. So the texture coordinate (0,0) is the bottom left of the texture. Are you sure the texture coords are alright?

Share this post


Link to post
Share on other sites
Quote:
Original post by deavik
Are you sure the texture coords are alright?


Well, I was just using NeHe code from texturing a cube, but on the tutorial site, it tells that the texture will be mirrored and flipped. But just to show you guys:

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd();



Quote:
Original post by clapton
Another soultion could be - simply - changing texture coordinates of a primitive you are rendering.


Eeek! I don't think I'd want to do that since my overall goal is not to have to do any modifications outside of normal OpenGL operations. Thanks for that suggestion though.

I guess I will end up just fixing the texture data when I load it. I'll post some code of that routine when I've got it done to see what you guys can say about it after it's done. I have a feeling it may require too much processing for what it's going to do, but I guess we will see! Thanks for the advice everyone! I will post again when the other function is done.

Share this post


Link to post
Share on other sites
Well, that went a lot easier than I thought it would. It took me less than 1 minute to fix this problem. It so happens that I took a look at some old code I posted on GameDev about mirroring in SDL. I just called the MirrorSurfaceX function, and everything is working great! Thanks all for the advice, this is a lot more convient then switching the OpenGL texture rendering code.

Share this post


Link to post
Share on other sites
It would be more interesting if you looked into what is causing the problem. This could either be a borked SDL function for loading the image (some formats have some bits that specify the origin of the axes) or you have wrong texture coordinates.

Basically try loading the same image with 2 or 3 different libraries and displaying it with OpenGL. For example TGAs have a bit that specifies the orientation, maybe other formats do too, and the loader doesn't check it.

Another thing, IIRC, is that BMPs are stored upside down and usually need to be inverted. Again you could both write your own loader and use several libraries.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ilici


Oh yea, I should take a look into what was causing the problem. It seems to be a problem with any images, tga, jpg, bmp all alike, when I use the pixel data from a SDL surface into the buildmipmaps function, it causes it to be all wrong. I'll try to do some more research into how the SDL_Surface pixels are held, but for now everything is working great. I have used other texturing functions that work fine with other libraries (well not using pure SDL though) and it's worked fine, but I wanted a SDL specific solution that would not require adding in other stuff outside of the SDL realm [wink]

I've finally made a single function that loads an image into an OpenGL texture. It can easily be modified if you have a SDL_Surface and want to use it as a texture as well. I've put the snippet on ym webpage, but here it is as well:


// Function that will load an image file and create an OpenGL texture from it
bool LoadTexture( const char* fileName, GLuint *texture )
{
// Load the image file
#ifdef _SDL_IMAGE_H
// Use SDL_Image if avaliable
SDL_Surface* imgFile1 = IMG_Load(fileName);
#else
// Otherwise use the standard SDL BMP loading
SDL_Surface* imgFile1 = BMP_Load(fileName);
#endif

// If the image could not be loaded
if( !imgFile1 )
return false;

// This is the final image
SDL_Surface* imgFile = SDL_CreateRGBSurface( SDL_HWSURFACE, imgFile1->w, imgFile1->h, imgFile1->format->BitsPerPixel, imgFile1->format->Rmask, imgFile1->format->Gmask, imgFile1->format->Bmask, imgFile1->format->Amask);

// If the final image's space could not be allcoated
if( !imgFile )
return false;

// Loop though and fix the pixels
for( int y = 0; y < imgFile1->h; y++ )
{
for( int x = 0; x < imgFile1->w; x++ )
{
// Don't worry too much about trying to understand all of what follows, just know it works ;)

// This is the code that gets a pixel on a surface
Uint32 curPixel = 0;

int FinalX = x;
int FinalY = imgFile1->h - y - 1;

// Store the surface BPP
int bpp = imgFile1->format->BytesPerPixel;

// Here p is the address to the curPixel we want to retrieve
Uint8 *p = (Uint8 *)imgFile1->pixels + FinalY * imgFile1->pitch + FinalX * bpp;

switch(bpp)
{
case 1:
curPixel = (*p);

case 2:
curPixel = (*(Uint16 *)p);

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
curPixel = (p[0] << 16 | p[1] << 8 | p[2]);
else
curPixel = (p[0] | p[1] << 8 | p[2] << 16);

case 4:
curPixel = (*(Uint32 *)p);
}

// This is the code that will draw pixels
Uint8 *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color = curPixel;
char c1, c2, c3;

// Lock the imgFile, if needed
if(SDL_MUSTLOCK(imgFile))
{
if(SDL_LockSurface(imgFile) < 0)
return false;
}

// How we draw the pixel depends on the bitdepth
switch(imgFile->format->BytesPerPixel)
{
case 1:
ubuff8 = (Uint8*) imgFile->pixels;
ubuff8 += (y * imgFile->pitch) + x;
*ubuff8 = (Uint8) color;
break;

case 2:
ubuff8 = (Uint8*) imgFile->pixels;
ubuff8 += (y * imgFile->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color;
break;

case 3:
ubuff8 = (Uint8*) imgFile->pixels;
ubuff8 += (y * imgFile->pitch) + (x*3);

if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
}
else
{
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);
}

ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;

case 4:
ubuff8 = (Uint8*) imgFile->pixels;
ubuff8 += (y*imgFile->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;

default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
return false;
}

/* Unlock the imgFile if needed */
if(SDL_MUSTLOCK(imgFile))
SDL_UnlockSurface(imgFile);
}
}

// Free this temporary surface
SDL_FreeSurface( imgFile1 );

// Create the texture
glGenTextures( 1, texture );

// Bind the texture
glBindTexture( GL_TEXTURE_2D, *texture );

// Build the texture now
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, imgFile->w, imgFile->h, GL_BGR, GL_UNSIGNED_BYTE, imgFile->pixels );

// Free the final image since we are done with it now
SDL_FreeSurface( imgFile );

// Return success
return true;
}



I'm sure it's just a matter of how the data is stored in the SDL_Surface and how it should be for the mipmaps though. Thanks for the time! If I do find out what the problem was and have some links that explain better, I'll be sure to post back here.

Share this post


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

  • Advertisement