Sed is replacing the start instead of appending to end - regex
I am trying to modify a CSV file to append to the lines the value A. My conditional regex is ,$ where $ is the last character.
My sed 's/,$/,A/' does not return any changes.
However doing sed 's/$/########/' replaces the first N characters of each line with my replacement string.
Example:
user#HOST:/loc/yearly_files$ head merged19.csv
1900-01-01 09:00, 2084, DCNN, DLY3208, 1, 310, 1011, , , , , , , , 5, , , , , , , , , , , , , , , , , , , , , 1.1, , 0, , , , , , , , , , , , , 1, , , , , , , , 1, , 1, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , D, , D, , , , , 79, A, ,
1900-01-01 09:00, 3197, DCNN, DLY3208, 1, 449, 1001, , , , , , , , 4, , , , , , , , , , , , , , , , , , , , , 4.7, , 4.3, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 93.6, A, ,
1900-01-01 09:00, 4813, DCNN, DLY3208, 1, 653, 1001, , , , , , , , 4, , , , , , , , , , , , , , , , , , , , , 2.3, , 1.7, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 89.2, A, ,
1900-01-01 09:00, 4967, DCNN, DLY3208, 1, 687, 1001, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , 3.2, , 2.8, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 93, A, ,
1900-01-01 09:00, 5399, DCNN, DLY3208, 1, 778, 1001, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , 5.8, , 5.7, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 98.5, A, ,
1900-01-01 09:00, 6950, DCNN, DLY3208, 1, 1047, 1011, , , , , , , , 6, , , , , , , , , , , , , , , , , , , , , 6.1, , 5.1, , , , , , , , 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, , , , , , , , , , H, , , , B, , B, , , , , 84.8, A, ,
1900-01-01 09:00, 7384, DCNN, DLY3208, 1, 1136, 1001, , , , , , , , 4, , , , , , , , , , , , , , , , , , , , , 2.3, , 1.7, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 89.2, A, ,
1900-01-01 21:00, 2084, DCNN, DLY3208, 1, 310, 1011, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , -.6, , -.6, , , , , , , , , , , , , 1, , , , , , , , 1, , 1, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , D, , D, , , , , 99.3, A, ,
1900-01-01 21:00, 3197, DCNN, DLY3208, 1, 449, 1001, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , 5.7, , 5.6, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 98.5, A, ,
1900-01-01 21:00, 4967, DCNN, DLY3208, 1, 687, 1001, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , 5.3, , 4.9, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 93.6, A, ,
user#HOST:/loc/yearly_files$ head merged19.csv | sed "s/$/,NULL/"
,NULL01-01 09:00, 2084, DCNN, DLY3208, 1, 310, 1011, , , , , , , , 5, , , , , , , , , , , , , , , , , , , , , 1.1, , 0, , , , , , , , , , , , , 1, , , , , , , , 1, , 1, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , D, , D, , , , , 79, A, ,
,NULL01-01 09:00, 3197, DCNN, DLY3208, 1, 449, 1001, , , , , , , , 4, , , , , , , , , , , , , , , , , , , , , 4.7, , 4.3, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 93.6, A, ,
,NULL01-01 09:00, 4813, DCNN, DLY3208, 1, 653, 1001, , , , , , , , 4, , , , , , , , , , , , , , , , , , , , , 2.3, , 1.7, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 89.2, A, ,
,NULL01-01 09:00, 4967, DCNN, DLY3208, 1, 687, 1001, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , 3.2, , 2.8, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 93, A, ,
,NULL01-01 09:00, 5399, DCNN, DLY3208, 1, 778, 1001, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , 5.8, , 5.7, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 98.5, A, ,
,NULL01-01 09:00, 6950, DCNN, DLY3208, 1, 1047, 1011, , , , , , , , 6, , , , , , , , , , , , , , , , , , , , , 6.1, , 5.1, , , , , , , , 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, , , , , , , , , , H, , , , B, , B, , , , , 84.8, A, ,
,NULL01-01 09:00, 7384, DCNN, DLY3208, 1, 1136, 1001, , , , , , , , 4, , , , , , , , , , , , , , , , , , , , , 2.3, , 1.7, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 89.2, A, ,
,NULL01-01 21:00, 2084, DCNN, DLY3208, 1, 310, 1011, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , -.6, , -.6, , , , , , , , , , , , , 1, , , , , , , , 1, , 1, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , D, , D, , , , , 99.3, A, ,
,NULL01-01 21:00, 3197, DCNN, DLY3208, 1, 449, 1001, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , 5.7, , 5.6, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 98.5, A, ,
,NULL01-01 21:00, 4967, DCNN, DLY3208, 1, 687, 1001, , , , , , , , 8, , , , , , , , , , , , , , , , , , , , , 5.3, , 4.9, , , , , , , , , , , , , 0, , , , , , , , 0, , 0, , , , , , , , , , , , , , , , , , , , , , , , , , , D, , , , B, , B, , , , , 93.6, A, ,
user#HOST:/loc/yearly_files$
The regular expression ,$ matches a comma as the last character of the line. If your delimiter is actually a comma plus a space, then you may have an invisible character there which your regex would not match.
In addition, your NULL experiment appears to indicate that you have \r\n line endings (i.e. your files may have been generated in Windows). You can verify the content of your file using od or hexdump:
$ od -c input.csv | head -18 | tail -4
0000340 , , , , D , , D ,
0000360 , , , , 7 9 , A , ,
0000400 \r \n 1 9 0 0 - 0 1 - 0 1 0 9 :
0000420 0 0 , 3 1 9 7 , D C N N ,
Note the \r \n.
You could remove these using dos2unix which may be available for your Linux distribution, or a GNU sed script like:
$ sed -i 's/\r//' input.csv
or a non-GNU sed script run in bash, like this:
$ sed -i '' -e $'s/\r//' input.csv
or by using the appropriate options in an FTP file transfer. There are some additional options for this here.
Once your file is converted, try simply matching the end of the line, if you're pretty sure you've got the right number of delimiters:
sed 's/$/A/' input.csv
Or even better, if you know that you really want field 103 to be an A:
awk -F, '{$103="A"} 1' OFS=", " input.csv
If you know the .csv file uses \r\n as the EOL character, you can get rid of the \r characters by piping the content through tr -d '\r' before sending it to sed:
head merged19.csv | tr -d '\r' | sed 's/$/,NULL/'
Warning: The tr filter removes all the \r characters present in the file and might break something else in your file structure.
However, if your file contains \r not followed by \n then either you have a file generated using an ancient version of MacOS (prior to version 10, macOS used to use \r as the EOL character) or it is a binary file and using sed to handle it is a bad idea anyway.
A PHP solution to replace the Windows end of lines (\r\n) with Unix end of lines (\n) before passing the input to sed:
head merged19.csv |\
php -r 'while($line=fgets(STDIN)){echo(str_replace("\r\n","\n",$line));}' |\
sed 's/$/,NULL/'
The filter is a small PHP program that reads lines from stdin, replaces the \r\n combination with \n and outputs them to stdout.
It doesn't remove the \r characters embedded in the text in other combinations; there should be none but this script plays safe and doesn't make this assumption.
The command is wrapped on multiple lines (the backslash, \, at the end of line tells bash that the command continues on the next line) because the PHP script is quite long for the format used by SO. You can remove the backslash characters (\) and write everything on a single line.
You might be experiencing a problem with your terminal display.
Try writing the changes to a separate file and see if it is correct then.
head merged19.csv | sed "s/$/,NULL/" > testfile
cat testfileor even try watching it in an editor like:
nano testfile
Related
BLAS tensor contractions for two indexes together
I want to calculate D[a,d] = A[a,b,c] * B[b,c,d]. Method I: reshape A[a,b,c] => C1[a,e], B[b,c,d] => C2[e,d], e = b*c Method II: directly call dgemm. This is a run-time error. " na, nb, nc, nd ? 2 3 5 7 Time for reshaping method 2.447600000000000E-002 Intel MKL ERROR: Parameter 10 was incorrect on entry to DGEMM . Time for straight method 1.838800000000000E-002 Difference between result matrices 5.46978468774136 " Question: Can we contract two indexes together by BLAS? The following only works for one index. How to speed up reshape in higher rank tensor contraction by BLAS in Fortran? Program reshape_for_blas Use, Intrinsic :: iso_fortran_env, Only : wp => real64, li => int64 Implicit None Real( wp ), Dimension( :, :, : ), Allocatable :: a Real( wp ), Dimension( :, :, : ), Allocatable :: b Real( wp ), Dimension( :, : ), Allocatable :: c1, c2 Real( wp ), Dimension( :, : ), Allocatable :: d Real( wp ), Dimension( :, : ), Allocatable :: e Integer :: na, nb, nc, nd, ne Integer( li ) :: start, finish, rate Write( *, * ) 'na, nb, nc, nd ?' Read( *, * ) na, nb, nc, nd ne = nb * nc Allocate( a ( 1:na, 1:nb, 1:nc ) ) Allocate( b ( 1:nb, 1:nc, 1:nd ) ) Allocate( c1( 1:na, 1:ne ) ) Allocate( c2( 1:ne, 1:nd ) ) Allocate( d ( 1:na, 1:nd ) ) Allocate( e ( 1:na, 1:nd ) ) ! Set up some data Call Random_number( a ) Call Random_number( b ) ! With reshapes Call System_clock( start, rate ) c1 = Reshape( a, Shape( c1 ) ) c2 = Reshape( b, Shape( c2 ) ) Call dgemm( 'N', 'N', na, nd, ne, 1.0_wp, c1, Size( c1, Dim = 1 ), & c2, Size( c2, Dim = 1 ), & 0.0_wp, e, Size( e, Dim = 1 ) ) Call System_clock( finish, rate ) Write( *, * ) 'Time for reshaping method ', Real( finish - start, wp ) / rate ! Direct Call System_clock( start, rate ) Call dgemm( 'N', 'N', na, nd, ne, 1.0_wp, a , Size( a , Dim = 1 ), & b , Size( b , Dim = 1 ), & 0.0_wp, d, Size( d, Dim = 1 ) ) Call System_clock( finish, rate ) Write( *, * ) 'Time for straight method ', Real( finish - start, wp ) / rate Write( *, * ) 'Difference between result matrices ', Maxval( Abs( d - e ) ) End Program reshape_for_blas
Write the elements of an array to file where each element is on its own line in Fortran
I wrote some code to print out the elements of a 3d-array where each element of the array is printed to a new line on screen. That worked (see output below). Can anyone help me write a format statement that can produce the same thing in a file? (i.e. using WRITE(*,1200) and 1200 FORMAT() etc)? Here is my code: PROGRAM test_3d_array IMPLICIT NONE integer :: matrix(3,3,3), i, j, k ! three dimensional real array integer :: CORE, CON, ALPHA CORE = 3 CON = 3 ALPHA = 3 !assigning some values to the array matrix do i=1,ALPHA do j = 1,CORE do k = 1,CON matrix(i, j, k) = i+j+k end do end do end do !display the values do i=1,ALPHA do j = 1, CORE do k=1,CON WRITE(*,*) "MATRIX (", i, ",", j, ",", k, ") = ", matrix(i,j,k) end do end do end do END PROGRAM test_3d_array Here is the screen output that I want to reproduce in the file: MATRIX ( 1 , 1 , 1 ) = 3 MATRIX ( 1 , 1 , 2 ) = 4 MATRIX ( 1 , 1 , 3 ) = 5 MATRIX ( 1 , 2 , 1 ) = 4 MATRIX ( 1 , 2 , 2 ) = 5 MATRIX ( 1 , 2 , 3 ) = 6 MATRIX ( 1 , 3 , 1 ) = 5 MATRIX ( 1 , 3 , 2 ) = 6 MATRIX ( 1 , 3 , 3 ) = 7 MATRIX ( 2 , 1 , 1 ) = 4 MATRIX ( 2 , 1 , 2 ) = 5 MATRIX ( 2 , 1 , 3 ) = 6 MATRIX ( 2 , 2 , 1 ) = 5 MATRIX ( 2 , 2 , 2 ) = 6 MATRIX ( 2 , 2 , 3 ) = 7 MATRIX ( 2 , 3 , 1 ) = 6 MATRIX ( 2 , 3 , 2 ) = 7 MATRIX ( 2 , 3 , 3 ) = 8 MATRIX ( 3 , 1 , 1 ) = 5 MATRIX ( 3 , 1 , 2 ) = 6 MATRIX ( 3 , 1 , 3 ) = 7 MATRIX ( 3 , 2 , 1 ) = 6 MATRIX ( 3 , 2 , 2 ) = 7 MATRIX ( 3 , 2 , 3 ) = 8 MATRIX ( 3 , 3 , 1 ) = 7 MATRIX ( 3 , 3 , 2 ) = 8 MATRIX ( 3 , 3 , 3 ) = 9
How to divide day hours from night hours between 2 time fields
I need to separate night hours and day hours between 2 times, if the time is between 6:00 am to 6:59 pm it should be day time and if the time is between 7:00 pm and 5:59 am it should be night time. Then I need to multiply night time by 1.25 and add it to day time if any. I have tried with this formula but it seems to complex and sometimes it does not work H4=if(G4 = 0,0,IF(AND(E4 > = TIME(19 , 0 , 0),F4 < = TIME( 6 , 0 , 0)), 0 ,IF(and(E4 < TIME(19 , 0 , 0),F4 < = TIME( 6 , 0 , 0)),F4-TIME(6, , ),IF(and(AND(E4 > = TIME(19, 0 , 0),F4>TIME(6, 0 ,0)),F4 < = TIME(19,0 , 0)),F4 - TIME(6 , 0 , 0 ),IF(and(and(E4 < TIME(19, 0 , 0 ),F4 > = TIME(6 , 0 , 0)),E4<TIME(6 , , )),F4 - TIME(6 , ,),IF(and(E4 < TIME(19 , , ),F4 < TIME(19 , , )), F4 - E4,IF(AND(E4 > = TIME(19 , 0 , 0 ),F4 < = TIME(23 ,59 ,0 )), 0 ,IF(E4 < TIME( 19 , 0 , 0 ),TIME( 19 , 0 , 0 ) - E4 , 0 )))))))) I have the following structure: G4=(F4-E4+(F4 < E4)) E4 is my start time ex: 11:01:00 PM F4 is my end time ex 1:35:00 AM Is there an easier way to get the division between times as to get the time I have to then do I4=G4-H4
delete everything in G4:I range paste this into G4 cell: =ARRAYFORMULA(IF(F4:F<>"", TEXT(F4:F-E4:E+(F4:F<E4:E), "[h]:mm:ss"), )) paste this into H4 cell: =ARRAYFORMULA(IF(F4:F>E4:E, TEXT((F4:F-E4:E)+ IF(E4:E-0.25>0,, E4:E-0.25)+ IF(0.7916666667-F4:F>0,, 0.7916666667-F4:F), "[h]:mm:ss"),"")) paste this into I4 cell: =ARRAYFORMULA(IF((G4:G<>"")*(G4:G<>H4:H), TEXT(G4:G-H4:H, "[h]:mm:ss"), ))
Can read/write EEPROM address however can only read/write first memory page
EDIT: I was a BIT confused about the BITS and BYTES, documention specify bits and not in bytes and read the bits as bytes. This explains my error. Why manufacturers use bits instead of bytes to specify capacity? it is a BIT confusing. ;-) I found the Atmel 24C02n 2kb EEPROM on a spare board and want to give it a try to see what kind of data is inside this chip and if I can reuse it. Never done reading/writing external memory. The wiring is easy (i2c) and works perfectly fine. Run the i2cScanner and it found 8 addresses on the bus, 0x50 .. 0x57. First thought: "strange, 8 addresses for one device." The documentation describes there are 8x256 bytes, so okay, 8 addresses for 8 pages of 256 bytes. However, when I want to access the another page, for example 0x51, I get the same data and it seems to write to the same memory page. Do I miss something here? This is my example code using the wire library: #include <Wire.h> int A24C_PAGE_COUNT = 8; uint8_t A24C_PAGE_ADDR[8] = { 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; int A24C_PAGE_SIZE = 256; uint8_t eepromReadAddress(int address, int from_addr) { Wire.beginTransmission(address); Wire.write(from_addr); Wire.requestFrom(address, 1); uint8_t iResult = (Wire.available())?Wire.read():0x32; Wire.endTransmission(); return iResult; } void eepromWriteAddress(uint8_t address, uint8_t from_addr, uint8_t* data) { Wire.beginTransmission(address); Wire.write(from_addr); while( *data ) { Wire.write( *data++ ); } Wire.endTransmission(); } void eepromRead() { int iByte = 0; int iPage = 0; while( iPage < A24C_PAGE_COUNT ) { Serial.print( "PAGE: " ); Serial.println( iPage+1 ); while( iByte < A24C_PAGE_SIZE ) { //Serial.print( "0x" ); Serial.print( (char)eepromReadAddress( A24C_PAGE_ADDR[ iPage ], iByte ) ); ++iByte; Serial.print(( iByte % 16 == 0 )?"\n":","); } iByte=0; ++iPage; } } void setup() { Wire.begin(); Serial.begin(9600); delay( 1000 ); //eepromWriteAddress( 0x50, 128, "Hello world!" ); eepromRead(); } void loop() { } Output after once write "Hello world!" to first page at position 128, i will get: PAGE: 1 G, ,?,?,, ,,k, ,, ,, ,., , ,, ,!,,,,?, ,,!,?, ,Q,?,4 ,?, ,,I,?, ,<, ,?,?,?,?,?,?,? ?, ,D, ,,<, ,,?, ,, ,,t, , ?, ,?,?,?,?,?, , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,?,?,?,?,?,?,?,?,?,? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ?,H,e,l,l,o, ,w,o,r,l,d,!,?,?,? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? PAGE: 2 G, ,?,?,, ,,k, ,, ,, ,., , ,, ,!,,,,?, ,,!,?, ,Q,?,4 ,?, ,,I,?, ,<, ,?,?,?,?,?,?,? ?, ,D, ,,<, ,,?, ,, ,,t, , ?, ,?,?,?,?,?, , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,?,?,?,?,?,?,?,?,?,? ?,?,?,?,?,?,?,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?,?,?,?,? Each address points to the same page???? Okay, another approach, changed the following variables to read one large page (just a try to see what will happen): int A24C_PAGE_COUNT = 1; int A24C_PAGE_SIZE = 2048; Same result, only first page displayed however repeated 8 times. So what do I wrong? And why are there 8 addresses for one device anyway?
Memory capacity of RAM and ROM is normally expressed in bits, not bytes as you seem to assume. So this chip you are referring to has 8 x 256 bits of EEPROM and you seem to be expecting much more memory than is actually present - You can store 256 bytes in that chip. The low-capacity chips apparently use the same address decoding mechanism than the higher-capacity chips, so what you seem to be seeing is a simple wrap-around of addresses while you are reading the same bytes over and over again (see pg. 10 of the datasheet on "roll-over"). With regards to your "answers to more than one I2C address" - How did you connect the A0, A1, A2 lines? They need to be hard-wired to create the device's address.
OpenGL is using the last texture loaded
Edit: Ive narrowed it down to this: It seems like if I do the below in my function, and then in the renderer function never call glBindTexture again it will still render... the textures... NM->data = stbi_load ( FileBuf , &NM->width , &NM->height , &NM->bit , 0 ); //glGenTextures ( 1 , &NM->texture [ 0 ] ); // ** OFF ** //glBindTexture ( GL_TEXTURE_2D, NM->texture [ 0 ] ); // ** OFF ** //glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ); // ** Enableing This Will Render White (no texture) glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ); // ** Enableing This Will Render the texture for both objects *** SUPER WEIRD *** // *** ALSO *** I dont ever call glBindTexture Again after this but still the object is being textured *** glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , NM->width , NM->width , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NM->data ); stbi_image_free ( NM->data ); I must not be understanding something, or I have a big error in my code. I am just trying to get WaveFront objects with materials loaded, and rendered. With that said, Im just trying to piece together some code to get a better understanding of how everything works so I can go back and reprogram everything. (I am on windows) I can load .objects from files and even load textures... Problem is, the last texture loaded is used for all objects.... here it glIntercept for loading the images... glGenTextures(1,05331C10) glBindTexture(GL_TEXTURE_2D,1) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST) glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,1024,1024,0,GL_RGBA,GL_UNSIGNED_BYTE,05733040) glGenTextures(1,05332780) glBindTexture(GL_TEXTURE_2D,2) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST) glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,1024,1024,0,GL_RGBA,GL_UNSIGNED_BYTE,0573D040) .. From: NM->MapUsed = true; sscanf ( coord [ i ]->c_str() , "map_Kd %s" , NM->map_Kd ); char FileBuf [ 256 ] = { 0x0 }; sprintf ( FileBuf , "C:\\OpenGL\\Debug\\%s" , NM->map_Kd ); NM->data = stbi_load ( FileBuf , &NM->width , &NM->height , &NM->bit , 0 ); glGenTextures ( 1 , &NM->texture ); glBindTexture ( GL_TEXTURE_2D, NM->texture ); glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ); glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ); glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , NM->width , NM->height , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NM->data ); stbi_image_free ( NM->data ); The above code is how I am loading the texture. NM is in a vector array in VectorMaterialLibrary, and that is in ObjectLoader vector... void Renderer() { updatecam(); // all My Objects for ( int i = 0 ; i < ObjectLoader.size() ; i++ ) { sObjectLoader *TempObject = ObjectLoader [ i ]; if ( TempObject->MTLFile ) { for ( int z = 0 ; z < TempObject->VectorMaterialLibrary.size() ; z++ ) { sMaterialLibrary *TempMaterial = TempObject->VectorMaterialLibrary [ z ]; if ( strcmp ( TempMaterial->newmtl , TempObject->usemtl ) == 0 ) { if ( TempMaterial->MapUsed ) { glBindTexture ( GL_TEXTURE_2D, TempMaterial->texture ); // *** This Dings 1/2 respectively *** } break; } } } for ( int z = 0 ; z < TempObject->_3f.size() ; z++ ) { s3f *Temp3f = TempObject->_3f [ z ]; if ( Temp3f->GL_TYPE == GL_QUADS ) { glBegin ( GL_QUADS ); if ( Temp3f->boolvn ) { glNormal3f ( Temp3f->vn [ 0 ] , Temp3f->vn [ 1 ] , Temp3f->vn [ 2 ] ); } if ( Temp3f->boolv ) { if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 0 ] , Temp3f->vt [ 1 ] ); glVertex3f ( Temp3f->v [ 0 ] , Temp3f->v [ 1 ] , Temp3f->v [ 2 ] ); if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 2 ] , Temp3f->vt [ 3 ] ); glVertex3f ( Temp3f->v [ 3 ] , Temp3f->v [ 4 ] , Temp3f->v [ 5 ] ); if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 4 ] , Temp3f->vt [ 5 ] ); glVertex3f ( Temp3f->v [ 6 ] , Temp3f->v [ 7 ] , Temp3f->v [ 8 ] ); if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 6 ] , Temp3f->vt [ 7 ] ); glVertex3f ( Temp3f->v [ 9 ] , Temp3f->v [ 10 ] , Temp3f->v [ 11 ] ); } glEnd(); } else { glBegin ( GL_TRIANGLES ); if ( Temp3f->boolvn ) { glNormal3f ( Temp3f->vn [ 0 ] , Temp3f->vn [ 1 ] , Temp3f->vn [ 2 ] ); } if ( Temp3f->boolv ) { if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 0 ] , Temp3f->vt [ 1 ] ); glVertex3f ( Temp3f->v [ 0 ] , Temp3f->v [ 1 ] , Temp3f->v [ 2 ] ); if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 2 ] , Temp3f->vt [ 3 ] ); glVertex3f ( Temp3f->v [ 3 ] , Temp3f->v [ 4 ] , Temp3f->v [ 5 ] ); if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 4 ] , Temp3f->vt [ 5 ] ); glVertex3f ( Temp3f->v [ 6 ] , Temp3f->v [ 7 ] , Temp3f->v [ 8 ] ); } glEnd(); } } } } Also, the glintercept for the render (took the last image1 call and the start of image2...) glBegin(GL_TRIANGLES) Textures[ (0,1) ] glNormal3f(0.000000,0.000000,-1.000000) glTexCoord2f(1.000000,0.000000) glVertex3f(1.892851,-0.950534,-3.331728) glTexCoord2f(1.000000,1.000000) glVertex3f(-0.107149,-0.950534,-3.331728) glTexCoord2f(0.000000,1.000000) glVertex3f(-0.107149,1.049466,-3.331728) glEnd() glBindTexture(GL_TEXTURE_2D,2) glBegin(GL_TRIANGLES) Textures[ (0,2) ] glNormal3f(0.000000,-1.000000,-0.000000) glTexCoord2f(0.000000,0.000000) glVertex3f(1.892851,-0.950534,-3.331728) glTexCoord2f(1.000000,0.000000) glVertex3f(1.892851,-0.950534,-1.331729) glTexCoord2f(0.000000,1.000000) glVertex3f(-0.107149,-0.950534,-3.331728) glEnd() Im wondering where I went wrong. I really just want this to work so I can recode the framework from scratch again. If you need further code, I can post it, but I dont want a big wall!!! // Edit, manually doing the following.. bad // I only have 2 objects with this test, and 2 materials... if ( testing ) { glGenTextures ( 2 , Gtextures ); // ********Global testing = false; NM->data = stbi_load ( FileBuf , &NM->width , &NM->height , &NM->bit , 0 ); glBindTexture ( GL_TEXTURE_2D, Gtextures[0] ); glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ); glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ); glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , NM->width , NM->height , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NM->data ); stbi_image_free ( NM->data ); } else { NM->data = stbi_load ( FileBuf , &NM->width , &NM->height , &NM->bit , 0 ); glBindTexture ( GL_TEXTURE_2D, Gtextures[1] ); glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ); glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ); glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , NM->width , NM->height , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NM->data ); stbi_image_free ( NM->data ); } ** for ( int z = 0 ; z < TempObject->VectorMaterialLibrary.size() ; z++ ) { sMaterialLibrary *TempMaterial = TempObject->VectorMaterialLibrary [ z ]; if ( strcmp ( TempMaterial->newmtl , TempObject->usemtl ) == 0 ) { if ( TempMaterial->MapUsed ) { //mbci ( "Changing Texture" , i ); //mbci ( "Gtextures [ i ]" , Gtextures [ i ] ); glBindTexture ( GL_TEXTURE_2D, Gtextures [ i ] ); // ** I only have 2 objects so this works for loading the 2 textures... //glBindTexture ( GL_TEXTURE_2D, TempMaterial->texture ); } break; } } But the object is STILL the same texture... Here is a picture of something I am rendering. Notice everything is the same color... The two cubes in the middle should be two different colors, and the other objects are also taking the same texture... here are the two textures (scaled down ofc) What I See http://i.stack.imgur.com/s1NXn.png ** No Idea ** Cube UV1 http://i.stack.imgur.com/s44Tb.png ** This is loaded first** Cube UV2 http://i.stack.imgur.com/8KXdo.png ** This is loaded last **
I hate to do this, but after a couple days debugging and going over my OpenGL booklets I have answered my own question. The problem with my code was based on the .obj format and how I am ordering the faces. In .obj format each .obj is incremented... example: OBJECT ONE f 5/1/1 6/2/1 1/3/1 f 6/1/2 7/2/2 2/3/2 f 7/1/3 8/2/3 3/3/3 f 8/1/4 5/2/4 4/3/4 f 1/1/5 2/2/5 4/3/5 f 8/1/6 7/2/6 5/3/6 f 6/2/1 2/4/1 1/3/1 f 7/2/2 3/4/2 2/3/2 f 8/2/3 4/4/3 3/3/3 f 5/2/4 1/4/4 4/3/4 f 2/2/5 3/4/5 4/3/5 f 7/2/6 6/4/6 5/3/6 OBJECT TWO f 9/5/7 10/6/7 12/7/7 f 13/5/8 16/6/8 14/7/8 f 9/5/9 13/6/9 10/7/9 f 10/5/10 14/6/10 11/7/10 f 11/5/11 15/6/11 12/7/11 f 13/5/12 9/6/12 16/7/12 f 10/6/7 11/8/7 12/7/7 f 16/6/8 15/8/8 14/7/8 f 13/6/13 14/8/13 10/7/13 f 14/6/10 15/8/10 11/7/10 f 15/6/11 16/8/11 12/7/11 f 9/6/12 12/8/12 16/7/12 How I was storing the Vertex/VertexTexture/VertexNormals, were correct, but my loop was starting from 0 on the second object, meaning I was actually recreating the first objects all over again, thus it applied the last texture... To anyone else: Please be aware of how your program is storing and reading things. A couple days of work with only a single line of code changed to make it functional... yippie. It would be nice if there were a way to make sure blender did not do this...